Unverified Commit e54130ad authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

optionally show local transit connections as well

parent 1c7779e9
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -1083,6 +1083,29 @@ my @migrations = (
			}
		);
	},

	# v25 -> v26
	# travelynx 1.24 adds local transit connections and needs to know targets
	# for that to work, as local transit does not support checkins yet.
	sub {
		my ($db) = @_;
		$db->query(
			qq{
				create table localtransit (
					user_id integer not null references users (id) primary key,
					data jsonb
				);
				create view user_transit as select
					id,
					use_history,
					localtransit.data as data
					from users
					left join localtransit on localtransit.user_id = id
				;
				update schema_version set version = 26;
			}
		);
	},
);

sub setup_db {
+1 −0
Original line number Diff line number Diff line
@@ -149,6 +149,7 @@ sub run {
		my $transit_res  = $db->delete( 'in_transit',    { user_id => $uid } );
		my $hooks_res    = $db->delete( 'webhooks',      { user_id => $uid } );
		my $trwl_res     = $db->delete( 'traewelling',   { user_id => $uid } );
		my $lt_res       = $db->delete( 'localtransit',  { user_id => $uid } );
		my $password_res
		  = $db->delete( 'pending_passwords', { user_id => $uid } );
		my $user_res = $db->delete( 'users', { id => $uid } );
+25 −7
Original line number Diff line number Diff line
@@ -499,7 +499,10 @@ sub insight {
	my ($self) = @_;

	my $user = $self->current_user;
	my $use_history = $self->users->use_history( uid => $user->{id} );
	my ( $use_history, $destinations ) = $self->users->use_history(
		uid                => $user->{id},
		with_local_transit => 1
	);

	if ( $self->param('action') and $self->param('action') eq 'save' ) {
		if ( $self->param('on_departure') ) {
@@ -516,9 +519,22 @@ sub insight {
			$use_history &= ~0x02;
		}

		if ( $self->param('local_transit') ) {
			$use_history |= 0x04;
		}
		else {
			$use_history &= ~0x04;
		}

		if ( $self->param('destinations') ) {
			$destinations
			  = [ split( qr{\r?\n\r?}, $self->param('destinations') ) ];
		}

		$self->users->use_history(
			uid          => $user->{id},
			set => $use_history
			set          => $use_history,
			destinations => $destinations
		);
		$self->flash( success => 'use_history' );
		$self->redirect_to('account');
@@ -526,6 +542,8 @@ sub insight {

	$self->param( on_departure  => $use_history & 0x01 ? 1 : 0 );
	$self->param( on_arrival    => $use_history & 0x02 ? 1 : 0 );
	$self->param( local_transit => $use_history & 0x04 ? 1 : 0 );
	$self->param( destinations  => join( "\n", @{$destinations} ) );
	$self->render('use_history');

}
+168 −117
Original line number Diff line number Diff line
@@ -29,7 +29,10 @@ sub get_connecting_trains_p {
	my ( $self, %opt ) = @_;

	my $uid = $opt{uid} //= $self->current_user->{id};
	my $use_history = $self->users->use_history( uid => $uid );
	my ( $use_history, $lt_stops ) = $self->users->use_history(
		uid                => $uid,
		with_local_transit => 1
	);

	my ( $eva, $exclude_via, $exclude_train_id, $exclude_before );
	my $now = $self->now->epoch;
@@ -72,7 +75,7 @@ sub get_connecting_trains_p {
		@destinations = grep { $_ ne $exclude_via } @destinations;
	}

	if ( not @destinations ) {
	if ( not( @destinations or $use_history & 0x04 and @{$lt_stops} ) ) {
		return $promise->reject;
	}

@@ -82,6 +85,7 @@ sub get_connecting_trains_p {

	my $iris_promise = Mojo::Promise->new;

	if (@destinations) {
		$self->iris->get_departures_p(
			station      => $eva,
			lookbehind   => 10,
@@ -137,8 +141,9 @@ sub get_connecting_trains_p {
             # properly by the cancellation logic etc.

					if ( $train->departure_is_cancelled ) {
					my @via
					  = ( $train->sched_route_post, $train->sched_route_end );
						my @via = (
							$train->sched_route_post, $train->sched_route_end
						);
						for my $dest (@destinations) {
							if ( has_str_in_list( $dest, @via ) ) {
								push( @cancellations, [ $train, $dest ] );
@@ -177,11 +182,13 @@ sub get_connecting_trains_p {
				  } @results;
				@cancellations = map { $_->[0] }
				  sort { $a->[1] <=> $b->[1] }
			  map { [ $_, $_->[0]->sched_departure->epoch ] } @cancellations;
				  map  { [ $_, $_->[0]->sched_departure->epoch ] }
				  @cancellations;

				# remove trains whose route matches the excluded one's
				if ($excluded_train) {
				my $route_pre = join( '|', reverse $excluded_train->route_pre );
					my $route_pre
					  = join( '|', reverse $excluded_train->route_pre );
					@results
					  = grep { join( '|', $_->[0]->route_post ) ne $route_pre }
					  @results;
@@ -200,7 +207,8 @@ sub get_connecting_trains_p {
					my $interchange_duration;
					if ( exists $stationinfo->{i} ) {
						$interchange_duration
					  = $stationinfo->{i}{$arr_platform}{ $train->platform };
						  = $stationinfo->{i}{$arr_platform}
						  { $train->platform };
						$interchange_duration //= $stationinfo->{i}{"*"};
					}
					if ( defined $interchange_duration ) {
@@ -227,6 +235,10 @@ sub get_connecting_trains_p {
				return;
			}
		)->wait;
	}
	else {
		$iris_promise->resolve( [] );
	}

	my $hafas_promise = Mojo::Promise->new;
	my $rest_api      = $self->config->{backend}{hafas_rest_api};
@@ -254,6 +266,7 @@ sub get_connecting_trains_p {
			my ( $iris, $hafas ) = @_;
			my @iris_trains  = @{ $iris->[0] };
			my @hafas_trains = @{ $hafas->[0] };
			my @transit_fyi;

			my $strp = DateTime::Format::Strptime->new(
				pattern   => '%Y-%m-%dT%H:%M:%S%z',
@@ -296,6 +309,44 @@ sub get_connecting_trains_p {
						}
					}
				}
				if ( $use_history & 0x04 and @{$lt_stops} ) {
					my %via_count = map { $_ => 0 } @{$lt_stops};
					for my $hafas_train (@hafas_trains) {
						for
						  my $stop ( @{ $hafas_train->{nextStopovers} // [] } )
						{
							for my $dest ( @{$lt_stops} ) {
								if (    $stop->{stop}{name}
									and $stop->{stop}{name} eq $dest
									and $via_count{$dest} < 2
									and $hafas_train->{when} )
								{
									my $departure = $strp->parse_datetime(
										$hafas_train->{when} );
									my $arrival
									  = $strp->parse_datetime(
										$stop->{arrival} );
									if ( $departure->epoch >= $exclude_before )
									{
										$via_count{$dest}++;
										push(
											@transit_fyi,
											[
												{
													line =>
													  $hafas_train->{line}
													  {name},
													departure => $departure,
												},
												$dest, $arrival
											]
										);
									}
								}
							}
						}
					}
				}
			};
			if ($@) {
				$self->app->log->error(
@@ -303,7 +354,7 @@ sub get_connecting_trains_p {
				);
			}

			$promise->resolve( \@iris_trains );
			$promise->resolve( \@iris_trains, \@transit_fyi );
			return;
		}
	)->catch(
+25 −2
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ use warnings;
use 5.020;

use DateTime;
use JSON;

my @sb_templates = (
	undef,
@@ -483,14 +484,36 @@ sub use_history {
	my $uid   = $opt{uid};
	my $value = $opt{set};

	if ( $opt{destinations} ) {
		$db->insert(
			'localtransit',
			{
				user_id => $uid,
				data    =>
				  JSON->new->encode( { destinations => $opt{destinations} } )
			},
			{ on_conflict => \'(user_id) do update set data = EXCLUDED.data' }
		);
	}

	if ($value) {
		$db->update( 'users', { use_history => $value }, { id => $uid } );
	}
	else {
		if ( $opt{with_local_transit} ) {
			my $res = $db->select(
				'user_transit',
				[ 'use_history', 'data' ],
				{ id => $uid }
			)->expand->hash;
			return ( $res->{use_history}, $res->{data}{destinations} // [] );
		}
		else {
			return $db->select( 'users', ['use_history'], { id => $uid } )
			  ->hash->{use_history};
		}
	}
}

sub use_external_services {
	my ( $self, %opt ) = @_;
Loading