This commit is contained in:
Daniel Friesel 2019-03-22 16:57:24 +01:00
parent 32cc2f0f81
commit 98822bdbf7

View file

@ -57,7 +57,6 @@ sub epoch_to_dt {
);
}
sub get_station {
my ($station_name) = @_;
@ -73,24 +72,24 @@ sub get_station {
sub startup {
my ($self) = @_;
if ( $ENV{TRAVELYNX_SECRETS} ) {
if ( $ENV{TRAVELYNX_SECRETS} ) {
$self->secrets( [ split( qr{:}, $ENV{TRAVELYNX_SECRETS} ) ] );
}
}
$self->defaults( layout => 'default' );
$self->defaults( layout => 'default' );
$self->config(
$self->config(
hypnotoad => {
accepts => 40,
listen => [ $ENV{TRAVELYNX_LISTEN} // 'http://*:8093' ],
pid_file => '/tmp/travelynx.pid',
workers => $ENV{TRAVELYNX_WORKERS} // 2,
},
);
);
$self->types->type( json => 'application/json; charset=utf-8' );
$self->types->type( json => 'application/json; charset=utf-8' );
$self->plugin(
$self->plugin(
authentication => {
autoload_user => 1,
fail_render => { template => 'login' },
@ -107,41 +106,50 @@ $self->plugin(
if ( $user_info->{status} != 1 ) {
return undef;
}
if ( check_password( $password, $user_info->{password_hash} ) ) {
if ( check_password( $password, $user_info->{password_hash} ) )
{
return $user_info->{id};
}
return undef;
},
}
);
$self->sessions->default_expiration( 60 * 60 * 24 * 180 );
);
$self->sessions->default_expiration( 60 * 60 * 24 * 180 );
$self->defaults( layout => 'default' );
$self->defaults( layout => 'default' );
$self->attr(action_type => sub {
$self->attr(
action_type => sub {
return {
checkin => 1,
checkout => 2,
undo => 3,
cancelled_from => 4,
cancelled_to => 5,
}});
$self->attr(action_types => sub {
return [
qw(checkin checkout undo cancelled_from cancelled_to)
]});
$self->attr(token_type => sub {
};
}
);
$self->attr(
action_types => sub {
return [qw(checkin checkout undo cancelled_from cancelled_to)];
}
);
$self->attr(
token_type => sub {
return {
status => 1,
history => 2,
action => 3,
}});
$self->attr(token_types => sub {
return [
qw(status history action)
]});
};
}
);
$self->attr(
token_types => sub {
return [qw(status history action)];
}
);
$self->attr(
$self->attr(
add_station_query => sub {
my ($self) = @_;
@ -151,8 +159,8 @@ $self->attr(
}
);
}
);
$self->attr(
);
$self->attr(
add_user_query => sub {
my ($self) = @_;
@ -165,8 +173,8 @@ $self->attr(
}
);
}
);
$self->attr(
);
$self->attr(
set_email_query => sub {
my ($self) = @_;
@ -176,8 +184,8 @@ $self->attr(
}
);
}
);
$self->attr(
);
$self->attr(
set_password_query => sub {
my ($self) = @_;
@ -187,8 +195,8 @@ $self->attr(
}
);
}
);
$self->attr(
);
$self->attr(
add_mail_query => sub {
my ($self) = @_;
@ -200,8 +208,8 @@ $self->attr(
}
);
}
);
$self->attr(
);
$self->attr(
set_status_query => sub {
my ($self) = @_;
@ -211,8 +219,8 @@ $self->attr(
}
);
}
);
$self->attr(
);
$self->attr(
mark_for_deletion_query => sub {
my ($self) = @_;
@ -222,8 +230,8 @@ $self->attr(
}
);
}
);
$self->attr(
);
$self->attr(
action_query => sub {
my ($self) = @_;
@ -243,8 +251,8 @@ $self->attr(
}
);
},
);
$self->attr(
);
$self->attr(
dbh => sub {
my ($self) = @_;
@ -252,8 +260,8 @@ $self->attr(
return DBI->connect( "dbi:SQLite:dbname=${dbname}", q{}, q{} );
}
);
$self->attr(
);
$self->attr(
get_all_actions_query => sub {
my ($self) = @_;
@ -270,8 +278,8 @@ $self->attr(
}
);
}
);
$self->attr(
);
$self->attr(
get_last_actions_query => sub {
my ($self) = @_;
@ -289,8 +297,8 @@ $self->attr(
}
);
}
);
$self->attr(
);
$self->attr(
get_journey_actions_query => sub {
my ($self) = @_;
@ -309,32 +317,33 @@ $self->attr(
}
);
}
);
$self->attr(
);
$self->attr(
get_userid_query => sub {
my ($self) = @_;
return $self->app->dbh->prepare(
qq{select id from users where name = ?});
}
);
$self->attr(
);
$self->attr(
get_pending_mails_query => sub {
my ($self) = @_;
return $self->app->dbh->prepare(
qq{select id from users where email = ? and status = 0;});
}
);
$self->attr(
);
$self->attr(
get_listed_mails_query => sub {
my ($self) = @_;
return $self->app->dbh->prepare(
qq{select * from pending_mails where email = ? and num_tries > 1;});
qq{select * from pending_mails where email = ? and num_tries > 1;}
);
}
);
$self->attr(
);
$self->attr(
get_user_query => sub {
my ($self) = @_;
@ -347,8 +356,8 @@ $self->attr(
}
);
}
);
$self->attr(
);
$self->attr(
get_api_tokens_query => sub {
my ($self) = @_;
@ -360,8 +369,8 @@ $self->attr(
}
);
}
);
$self->attr(
);
$self->attr(
get_api_token_query => sub {
my ($self) = @_;
@ -373,8 +382,8 @@ $self->attr(
}
);
}
);
$self->attr(
);
$self->attr(
drop_api_token_query => sub {
my ($self) = @_;
@ -384,8 +393,8 @@ $self->attr(
}
);
}
);
$self->attr(
);
$self->attr(
set_api_token_query => sub {
my ($self) = @_;
@ -398,8 +407,8 @@ $self->attr(
}
);
}
);
$self->attr(
);
$self->attr(
get_password_query => sub {
my ($self) = @_;
@ -411,8 +420,8 @@ $self->attr(
}
);
}
);
$self->attr(
);
$self->attr(
get_token_query => sub {
my ($self) = @_;
@ -424,24 +433,24 @@ $self->attr(
}
);
}
);
$self->attr(
);
$self->attr(
get_stationid_by_ds100_query => sub {
my ($self) = @_;
return $self->app->dbh->prepare(
qq{select id from stations where ds100 = ?});
}
);
$self->attr(
);
$self->attr(
get_stationid_by_name_query => sub {
my ($self) = @_;
return $self->app->dbh->prepare(
qq{select id from stations where name = ?});
}
);
$self->attr(
);
$self->attr(
undo_query => sub {
my ($self) = @_;
@ -455,9 +464,10 @@ $self->attr(
}
);
},
);
);
$self->helper('get_departures' => sub {
$self->helper(
'get_departures' => sub {
my ( $self, $station, $lookbehind ) = @_;
$lookbehind //= 180;
@ -479,8 +489,9 @@ $self->helper('get_departures' => sub {
return {
results => [ $status->results ],
errstr => $status->errstr,
station_ds100 =>
( $status->station ? $status->station->{ds100} : 'undef' ),
station_ds100 => (
$status->station ? $status->station->{ds100} : 'undef'
),
station_name =>
( $status->station ? $status->station->{name} : 'undef' ),
};
@ -497,9 +508,11 @@ $self->helper('get_departures' => sub {
errstr => 'Unknown station name',
};
}
});
}
);
$self->helper('checkin' => sub {
$self->helper(
'checkin' => sub {
my ( $self, $station, $train_id, $action_id ) = @_;
$action_id //= $self->app->action_type->{checkin};
@ -553,9 +566,13 @@ $self->helper('checkin' => sub {
$train->sched_departure->epoch,
$train->departure->epoch,
join( '|', $train->route ),
join( '|',
map { ( $_->[0] ? $_->[0]->epoch : q{} ) . ':' . $_->[1] }
$train->messages )
join(
'|',
map {
( $_->[0] ? $_->[0]->epoch : q{} ) . ':'
. $_->[1]
} $train->messages
)
);
if ( defined $success ) {
return ( $train, undef );
@ -565,20 +582,25 @@ $self->helper('checkin' => sub {
}
}
}
});
}
);
$self->helper('undo' => sub {
$self->helper(
'undo' => sub {
my ($self) = @_;
my $uid = $self->current_user->{id};
$self->app->get_last_actions_query->execute($uid);
my $rows = $self->app->get_last_actions_query->fetchall_arrayref;
if ( @{$rows} and $rows->[0][0] == $self->app->action_type->{undo} ) {
if ( @{$rows} and $rows->[0][0] == $self->app->action_type->{undo} )
{
return 'Nested undo (undoing an undo) is not supported';
}
if ( @{$rows} > 1 and $rows->[1][0] == $self->app->action_type->{undo} ) {
if ( @{$rows} > 1
and $rows->[1][0] == $self->app->action_type->{undo} )
{
return 'Repeated undo is not supported';
}
@ -593,9 +615,11 @@ $self->helper('undo' => sub {
else {
return 'INSERT failed';
}
});
}
);
$self->helper('checkout' => sub {
$self->helper(
'checkout' => sub {
my ( $self, $station, $force, $action_id ) = @_;
$action_id //= $self->app->action_type->{checkout};
@ -623,8 +647,8 @@ $self->helper('checkout' => sub {
name => $status->{station_name}
),
DateTime->now( time_zone => 'Europe/Berlin' )->epoch,
undef, undef, undef, undef, undef, undef,
undef, undef
undef, undef, undef, undef, undef,
undef, undef, undef
);
if ( defined $success ) {
return;
@ -650,12 +674,18 @@ $self->helper('checkout' => sub {
$train->line_no,
$train->train_no,
$train->train_id,
$train->sched_arrival ? $train->sched_arrival->epoch : undef,
$train->sched_arrival
? $train->sched_arrival->epoch
: undef,
$train->arrival ? $train->arrival->epoch : undef,
join( '|', $train->route ),
join( '|',
map { ( $_->[0] ? $_->[0]->epoch : q{} ) . ':' . $_->[1] }
$train->messages )
join(
'|',
map {
( $_->[0] ? $_->[0]->epoch : q{} ) . ':'
. $_->[1]
} $train->messages
)
);
if ( defined $success ) {
return;
@ -664,25 +694,33 @@ $self->helper('checkout' => sub {
return 'INSERT failed';
}
}
});
}
);
$self->helper('get_station_id' => sub {
$self->helper(
'get_station_id' => sub {
my ( $self, %opt ) = @_;
$self->app->get_stationid_by_ds100_query->execute( $opt{ds100} );
my $rows = $self->app->get_stationid_by_ds100_query->fetchall_arrayref;
my $rows
= $self->app->get_stationid_by_ds100_query->fetchall_arrayref;
if ( @{$rows} ) {
return $rows->[0][0];
}
else {
$self->app->add_station_query->execute( $opt{ds100}, $opt{name} );
$self->app->get_stationid_by_ds100_query->execute( $opt{ds100} );
my $rows = $self->app->get_stationid_by_ds100_query->fetchall_arrayref;
$self->app->add_station_query->execute( $opt{ds100},
$opt{name} );
$self->app->get_stationid_by_ds100_query->execute(
$opt{ds100} );
my $rows
= $self->app->get_stationid_by_ds100_query->fetchall_arrayref;
return $rows->[0][0];
}
});
}
);
$self->helper('get_user_token' => sub {
$self->helper(
'get_user_token' => sub {
my ( $self, $uid ) = @_;
my $query = $self->app->get_token_query;
@ -692,12 +730,14 @@ $self->helper('get_user_token' => sub {
return @{ $rows->[0] };
}
return;
});
}
);
# This helper should only be called directly when also providing a user ID.
# If you don't have one, use current_user() instead (get_user_data will
# delegate to it anyways).
$self->helper('get_user_data' => sub {
# This helper should only be called directly when also providing a user ID.
# If you don't have one, use current_user() instead (get_user_data will
# delegate to it anyways).
$self->helper(
'get_user_data' => sub {
my ( $self, $uid ) = @_;
$uid //= $self->current_user->{id};
@ -729,21 +769,26 @@ $self->helper('get_user_data' => sub {
};
}
return undef;
});
}
);
$self->helper('get_api_token' => sub {
$self->helper(
'get_api_token' => sub {
my ( $self, $uid ) = @_;
$uid //= $self->current_user->{id};
$self->app->get_api_tokens_query->execute($uid);
my $rows = $self->app->get_api_tokens_query->fetchall_arrayref;
my $token = {};
for my $row ( @{$rows} ) {
$token->{ $self->app->token_types->[ $row->[0] - 1 ] } = $row->[1];
$token->{ $self->app->token_types->[ $row->[0] - 1 ] }
= $row->[1];
}
return $token;
});
}
);
$self->helper('get_user_password' => sub {
$self->helper(
'get_user_password' => sub {
my ( $self, $name ) = @_;
my $query = $self->app->get_password_query;
$query->execute($name);
@ -758,9 +803,11 @@ $self->helper('get_user_password' => sub {
};
}
return;
});
}
);
$self->helper('add_user' => sub {
$self->helper(
'add_user' => sub {
my ( $self, $user_name, $email, $token, $password ) = @_;
$self->app->get_userid_query->execute($user_name);
@ -786,9 +833,11 @@ $self->helper('add_user' => sub {
$rows = $self->app->get_userid_query->fetchall_arrayref;
return $rows->[0][0];
}
});
}
);
$self->helper('check_if_user_name_exists' => sub {
$self->helper(
'check_if_user_name_exists' => sub {
my ( $self, $user_name ) = @_;
$self->app->get_userid_query->execute($user_name);
@ -798,9 +847,11 @@ $self->helper('check_if_user_name_exists' => sub {
return 1;
}
return 0;
});
}
);
$self->helper('check_if_mail_is_blacklisted' => sub {
$self->helper(
'check_if_mail_is_blacklisted' => sub {
my ( $self, $mail ) = @_;
$self->app->get_pending_mails_query->execute($mail);
@ -812,9 +863,11 @@ $self->helper('check_if_mail_is_blacklisted' => sub {
return 1;
}
return 0;
});
}
);
$self->helper('get_user_travels' => sub {
$self->helper(
'get_user_travels' => sub {
my ( $self, %opt ) = @_;
my $uid = $self->current_user->{id};
@ -825,15 +878,21 @@ $self->helper('get_user_travels' => sub {
if ( $opt{uid} and $opt{checkin_epoch} and $opt{checkout_epoch} ) {
$query = $self->app->get_journey_actions_query;
$query->execute( $opt{uid}, $opt{checkin_epoch}, $opt{checkout_epoch} );
$query->execute( $opt{uid}, $opt{checkin_epoch},
$opt{checkout_epoch} );
}
else {
$query->execute($uid);
}
my @match_actions = ( $self->app->action_type->{checkout}, $self->app->action_type->{checkin} );
my @match_actions = (
$self->app->action_type->{checkout},
$self->app->action_type->{checkin}
);
if ( $opt{cancelled} ) {
@match_actions
= ( $self->app->action_type->{cancelled_to}, $self->app->action_type->{cancelled_from} );
@match_actions = (
$self->app->action_type->{cancelled_to},
$self->app->action_type->{cancelled_from}
);
}
my @travels;
@ -841,17 +900,21 @@ $self->helper('get_user_travels' => sub {
while ( my @row = $query->fetchrow_array ) {
my (
$action, $raw_ts, $ds100, $name,
$train_type, $train_line, $train_no, $train_id,
$raw_sched_ts, $raw_real_ts, $raw_route, $raw_messages
$action, $raw_ts, $ds100,
$name, $train_type, $train_line,
$train_no, $train_id, $raw_sched_ts,
$raw_real_ts, $raw_route, $raw_messages
) = @row;
$name = decode( 'UTF-8', $name );
$raw_route = decode( 'UTF-8', $raw_route );
$raw_messages = decode( 'UTF-8', $raw_messages );
if ( $action == $match_actions[0]
or ( $opt{checkout_epoch} and $raw_ts == $opt{checkout_epoch} ) )
if (
$action == $match_actions[0]
or
( $opt{checkout_epoch} and $raw_ts == $opt{checkout_epoch} )
)
{
push(
@travels,
@ -866,7 +929,8 @@ $self->helper('get_user_travels' => sub {
messages => $raw_messages
? [ split( qr{[|]}, $raw_messages ) ]
: undef,
route => $raw_route ? [ split( qr{[|]}, $raw_route ) ]
route => $raw_route
? [ split( qr{[|]}, $raw_route ) ]
: undef,
completed => 0,
}
@ -877,7 +941,8 @@ $self->helper('get_user_travels' => sub {
$action == $match_actions[1]
and $prev_action == $match_actions[0]
)
or ( $opt{checkin_epoch} and $raw_ts == $opt{checkin_epoch} )
or
( $opt{checkin_epoch} and $raw_ts == $opt{checkin_epoch} )
)
{
my $ref = $travels[-1];
@ -896,7 +961,8 @@ $self->helper('get_user_travels' => sub {
my @parsed_messages;
for my $message ( @{ $ref->{messages} // [] } ) {
my ( $ts, $msg ) = split( qr{:}, $message );
push( @parsed_messages, [ epoch_to_dt($ts), $msg ] );
push( @parsed_messages,
[ epoch_to_dt($ts), $msg ] );
}
$ref->{messages} = [ reverse @parsed_messages ];
$ref->{sched_duration}
@ -906,27 +972,32 @@ $self->helper('get_user_travels' => sub {
: undef;
$ref->{rt_duration}
= $ref->{rt_arrival}
? $ref->{rt_arrival}->epoch - $ref->{rt_departure}->epoch
? $ref->{rt_arrival}->epoch
- $ref->{rt_departure}->epoch
: undef;
$ref->{km_route}
= $self->get_travel_distance( $ref->{from_name},
$ref->{to_name}, $ref->{route} );
$ref->{km_beeline}
= $self->get_travel_distance( $ref->{from_name},
$ref->{to_name}, [ $ref->{from_name}, $ref->{to_name} ] );
$ref->{kmh_route}
= $ref->{km_route}
/ (
( $ref->{rt_duration} // $ref->{sched_duration} // 999999 )
/ 3600 );
$ref->{kmh_beeline}
= $ref->{km_beeline}
/ (
( $ref->{rt_duration} // $ref->{sched_duration} // 999999 )
/ 3600 );
$ref->{to_name},
[ $ref->{from_name}, $ref->{to_name} ] );
$ref->{kmh_route} = $ref->{km_route} / (
(
$ref->{rt_duration} // $ref->{sched_duration}
// 999999
) / 3600
);
$ref->{kmh_beeline} = $ref->{km_beeline} / (
(
$ref->{rt_duration} // $ref->{sched_duration}
// 999999
) / 3600
);
}
if ( $opt{checkin_epoch}
and $action == $self->app->action_type->{cancelled_from} )
and $action
== $self->app->action_type->{cancelled_from} )
{
$ref->{cancelled} = 1;
}
@ -935,9 +1006,11 @@ $self->helper('get_user_travels' => sub {
}
return @travels;
});
}
);
$self->helper('get_user_status' => sub {
$self->helper(
'get_user_status' => sub {
my ( $self, $uid ) = @_;
$uid //= $self->current_user->{id};
@ -948,7 +1021,9 @@ $self->helper('get_user_status' => sub {
my $now = DateTime->now( time_zone => 'Europe/Berlin' );
my @cols = @{ $rows->[0] };
if ( @{$rows} > 2 and $rows->[0][0] == $self->app->action_type->{undo} ) {
if ( @{$rows} > 2
and $rows->[0][0] == $self->app->action_type->{undo} )
{
@cols = @{ $rows->[2] };
}
@ -956,10 +1031,12 @@ $self->helper('get_user_status' => sub {
my $sched_ts = epoch_to_dt( $cols[8] );
my $real_ts = epoch_to_dt( $cols[9] );
my $checkin_station_name = decode( 'UTF-8', $cols[3] );
my @route = split( qr{[|]}, decode( 'UTF-8', $cols[10] // q{} ) );
my @route
= split( qr{[|]}, decode( 'UTF-8', $cols[10] // q{} ) );
my @route_after;
my $is_after = 0;
for my $station (@route) {
if ( $station eq $checkin_station_name ) {
$is_after = 1;
}
@ -968,8 +1045,10 @@ $self->helper('get_user_status' => sub {
}
}
return {
checked_in => ( $cols[0] == $self->app->action_type->{checkin} ),
cancelled => ( $cols[0] == $self->app->action_type->{cancelled_from} ),
checked_in =>
( $cols[0] == $self->app->action_type->{checkin} ),
cancelled =>
( $cols[0] == $self->app->action_type->{cancelled_from} ),
timestamp => $action_ts,
timestamp_delta => $now->epoch - $action_ts->epoch,
sched_ts => $sched_ts,
@ -990,9 +1069,11 @@ $self->helper('get_user_status' => sub {
sched_ts => epoch_to_dt(0),
real_ts => epoch_to_dt(0),
};
});
}
);
$self->helper('get_travel_distance' => sub {
$self->helper(
'get_travel_distance' => sub {
my ( $self, $from, $to, $route_ref ) = @_;
my $distance = 0;
@ -1013,59 +1094,64 @@ $self->helper('get_travel_distance' => sub {
for my $station_name (@route) {
if ( my $station = get_station($station_name) ) {
$distance += $geo->distance( 'kilometer', $prev_station->[3],
$distance
+= $geo->distance( 'kilometer', $prev_station->[3],
$prev_station->[4], $station->[3], $station->[4] );
$prev_station = $station;
}
}
return $distance;
});
}
);
$self->helper('navbar_class' => sub {
$self->helper(
'navbar_class' => sub {
my ( $self, $path ) = @_;
if ( $self->req->url eq $self->url_for($path) ) {
return 'active';
}
return q{};
});
}
);
my $r = $self->routes;
my $r = $self->routes;
$r->get('/')->to('traveling#homepage');
$r->get('/about')->to('static#about');
$r->get('/impressum')->to('static#imprint');
$r->get('/imprint')->to('static#imprint');
$r->get('/api/v0/:user_action/:token')->to('api#get_v0');
$r->get('/login')->to('account#login_form');
$r->get('/register')->to('account#registration_form');
$r->get('/reg/:id/:token')->to('account#verify');
$r->post('/action')->to('traveling#log_action');
$r->post('/geolocation')->to('traveling#geolocation');
$r->post('/list_departures')->to('traveling#redirect_to_station');
$r->post('/login')->to('account#do_login');
$r->post('/register')->to('account#register');
$r->get('/')->to('traveling#homepage');
$r->get('/about')->to('static#about');
$r->get('/impressum')->to('static#imprint');
$r->get('/imprint')->to('static#imprint');
$r->get('/api/v0/:user_action/:token')->to('api#get_v0');
$r->get('/login')->to('account#login_form');
$r->get('/register')->to('account#registration_form');
$r->get('/reg/:id/:token')->to('account#verify');
$r->post('/action')->to('traveling#log_action');
$r->post('/geolocation')->to('traveling#geolocation');
$r->post('/list_departures')->to('traveling#redirect_to_station');
$r->post('/login')->to('account#do_login');
$r->post('/register')->to('account#register');
my $authed_r = $r->under(sub {
my $authed_r = $r->under(
sub {
my ($self) = @_;
if ( $self->is_user_authenticated ) {
return 1;
}
$self->render( 'login', redirect_to => $self->req->url );
return undef;
});
}
);
$authed_r->get('/account')->to('account#account');
$authed_r->get('/export.json')->to('account#json_export');
$authed_r->get('/history')->to('traveling#history');
$authed_r->get('/history.json')->to('traveling#json_history');
$authed_r->get('/journey/:id')->to('traveling#journey_details');
$authed_r->get('/s/*station')->to('traveling#station');
$authed_r->post('/delete')->to('account#delete');
$authed_r->post('/logout')->to('account#do_logout');
$authed_r->post('/set_token')->to('api#set_token');
$authed_r->get('/account')->to('account#account');
$authed_r->get('/export.json')->to('account#json_export');
$authed_r->get('/history')->to('traveling#history');
$authed_r->get('/history.json')->to('traveling#json_history');
$authed_r->get('/journey/:id')->to('traveling#journey_details');
$authed_r->get('/s/*station')->to('traveling#station');
$authed_r->post('/delete')->to('account#delete');
$authed_r->post('/logout')->to('account#do_logout');
$authed_r->post('/set_token')->to('api#set_token');
}