2019-03-21 21:12:45 +00:00
|
|
|
package Travelynx;
|
|
|
|
use Mojo::Base 'Mojolicious';
|
|
|
|
|
2019-04-17 11:10:49 +00:00
|
|
|
use Mojo::Pg;
|
2019-03-21 21:12:45 +00:00
|
|
|
use Mojolicious::Plugin::Authentication;
|
|
|
|
use Cache::File;
|
|
|
|
use Crypt::Eksblowfish::Bcrypt qw(bcrypt en_base64);
|
|
|
|
use DateTime;
|
|
|
|
use Encode qw(decode encode);
|
|
|
|
use Geo::Distance;
|
2019-04-07 14:55:35 +00:00
|
|
|
use JSON;
|
2019-03-21 21:12:45 +00:00
|
|
|
use List::Util qw(first);
|
|
|
|
use List::MoreUtils qw(after_incl before_incl);
|
|
|
|
use Travel::Status::DE::IRIS;
|
|
|
|
use Travel::Status::DE::IRIS::Stations;
|
2019-04-02 18:10:48 +00:00
|
|
|
use Travelynx::Helper::Sendmail;
|
2019-03-21 21:12:45 +00:00
|
|
|
|
|
|
|
sub check_password {
|
|
|
|
my ( $password, $hash ) = @_;
|
|
|
|
|
|
|
|
if ( bcrypt( $password, $hash ) eq $hash ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub epoch_to_dt {
|
|
|
|
my ($epoch) = @_;
|
|
|
|
|
|
|
|
# Bugs (and user errors) may lead to undefined timestamps. Set them to
|
|
|
|
# 1970-01-01 to avoid crashing and show obviously wrong data instead.
|
|
|
|
$epoch //= 0;
|
|
|
|
|
|
|
|
return DateTime->from_epoch(
|
|
|
|
epoch => $epoch,
|
|
|
|
time_zone => 'Europe/Berlin'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_station {
|
|
|
|
my ($station_name) = @_;
|
|
|
|
|
|
|
|
my @candidates
|
|
|
|
= Travel::Status::DE::IRIS::Stations::get_station($station_name);
|
|
|
|
|
|
|
|
if ( @candidates == 1 ) {
|
|
|
|
return $candidates[0];
|
|
|
|
}
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub startup {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
2019-03-22 20:18:03 +00:00
|
|
|
push( @{ $self->commands->namespaces }, 'Travelynx::Command' );
|
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
$self->defaults( layout => 'default' );
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
$self->types->type( json => 'application/json; charset=utf-8' );
|
|
|
|
|
2019-04-13 10:17:19 +00:00
|
|
|
$self->plugin('Config');
|
|
|
|
|
|
|
|
if ( $self->config->{secrets} ) {
|
|
|
|
$self->secrets( $self->config->{secrets} );
|
|
|
|
}
|
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
$self->plugin(
|
|
|
|
authentication => {
|
|
|
|
autoload_user => 1,
|
|
|
|
fail_render => { template => 'login' },
|
|
|
|
load_user => sub {
|
|
|
|
my ( $self, $uid ) = @_;
|
|
|
|
return $self->get_user_data($uid);
|
|
|
|
},
|
|
|
|
validate_user => sub {
|
|
|
|
my ( $self, $username, $password, $extradata ) = @_;
|
|
|
|
my $user_info = $self->get_user_password($username);
|
|
|
|
if ( not $user_info ) {
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
if ( $user_info->{status} != 1 ) {
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
if ( check_password( $password, $user_info->{password_hash} ) )
|
|
|
|
{
|
|
|
|
return $user_info->{id};
|
|
|
|
}
|
2019-03-21 21:12:45 +00:00
|
|
|
return undef;
|
2019-03-22 15:57:24 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$self->sessions->default_expiration( 60 * 60 * 24 * 180 );
|
|
|
|
|
|
|
|
$self->defaults( layout => 'default' );
|
|
|
|
|
2019-05-18 06:23:59 +00:00
|
|
|
$self->hook(
|
|
|
|
before_dispatch => sub {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
# The "theme" cookie is set client-side if the theme we delivered was
|
|
|
|
# changed by dark mode detection or by using the theme switcher). It's
|
|
|
|
# not part of Mojolicious' session data (and can't be, due to
|
|
|
|
# signing and HTTPOnly), so we need to add it here.
|
|
|
|
for my $cookie ( @{ $self->req->cookies } ) {
|
|
|
|
if ( $cookie->name eq 'theme' ) {
|
|
|
|
$self->session( theme => $cookie->value );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-04-13 10:17:19 +00:00
|
|
|
$self->attr(
|
|
|
|
cache_iris_main => sub {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
return Cache::File->new(
|
|
|
|
cache_root => $self->app->config->{cache}->{schedule},
|
|
|
|
default_expires => '6 hours',
|
|
|
|
lock_level => Cache::File::LOCK_LOCAL(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$self->attr(
|
|
|
|
cache_iris_rt => sub {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
return Cache::File->new(
|
|
|
|
cache_root => $self->app->config->{cache}->{realtime},
|
|
|
|
default_expires => '70 seconds',
|
|
|
|
lock_level => Cache::File::LOCK_LOCAL(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
$self->attr(
|
|
|
|
token_type => sub {
|
|
|
|
return {
|
|
|
|
status => 1,
|
|
|
|
history => 2,
|
|
|
|
action => 3,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$self->attr(
|
|
|
|
token_types => sub {
|
|
|
|
return [qw(status history action)];
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-04-17 11:10:49 +00:00
|
|
|
$self->helper(
|
|
|
|
sendmail => sub {
|
|
|
|
state $sendmail = Travelynx::Helper::Sendmail->new(
|
|
|
|
config => ( $self->config->{mail} // {} ),
|
|
|
|
log => $self->log
|
2019-03-22 15:57:24 +00:00
|
|
|
);
|
2019-04-17 11:10:49 +00:00
|
|
|
}
|
2019-03-22 15:57:24 +00:00
|
|
|
);
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-04 16:26:53 +00:00
|
|
|
$self->helper(
|
2019-04-17 11:10:49 +00:00
|
|
|
pg => sub {
|
|
|
|
my ($self) = @_;
|
|
|
|
my $config = $self->app->config;
|
|
|
|
|
|
|
|
my $dbname = $config->{db}->{database};
|
|
|
|
my $host = $config->{db}->{host} // 'localhost';
|
|
|
|
my $port = $config->{db}->{port} // 5432;
|
|
|
|
my $user = $config->{db}->{user};
|
|
|
|
my $pw = $config->{db}->{password};
|
|
|
|
|
|
|
|
state $pg
|
|
|
|
= Mojo::Pg->new("postgresql://${user}\@${host}:${port}/${dbname}")
|
|
|
|
->password($pw);
|
2019-04-13 15:09:10 +00:00
|
|
|
}
|
2019-04-04 16:26:53 +00:00
|
|
|
);
|
2019-04-02 18:10:48 +00:00
|
|
|
|
2019-04-21 13:46:19 +00:00
|
|
|
$self->helper(
|
|
|
|
'numify_skipped_stations' => sub {
|
|
|
|
my ( $self, $count ) = @_;
|
|
|
|
|
|
|
|
if ( $count == 0 ) {
|
|
|
|
return 'INTERNAL ERROR';
|
|
|
|
}
|
|
|
|
if ( $count == 1 ) {
|
|
|
|
return
|
|
|
|
'Eine Station ohne Geokoordinaten wurde nicht berücksichtigt.';
|
|
|
|
}
|
|
|
|
return
|
|
|
|
"${count} Stationen ohne Geookordinaten wurden nicht berücksichtigt.";
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
$self->helper(
|
|
|
|
'get_departures' => sub {
|
2019-04-23 16:08:07 +00:00
|
|
|
my ( $self, $station, $lookbehind, $lookahead ) = @_;
|
2019-03-22 15:57:24 +00:00
|
|
|
|
|
|
|
$lookbehind //= 180;
|
2019-04-23 16:08:07 +00:00
|
|
|
$lookahead //= 30;
|
2019-03-22 15:57:24 +00:00
|
|
|
|
|
|
|
my @station_matches
|
|
|
|
= Travel::Status::DE::IRIS::Stations::get_station($station);
|
|
|
|
|
|
|
|
if ( @station_matches == 1 ) {
|
|
|
|
$station = $station_matches[0][0];
|
|
|
|
my $status = Travel::Status::DE::IRIS->new(
|
|
|
|
station => $station,
|
2019-04-13 10:17:19 +00:00
|
|
|
main_cache => $self->app->cache_iris_main,
|
|
|
|
realtime_cache => $self->app->cache_iris_rt,
|
2019-03-22 15:57:24 +00:00
|
|
|
lookbehind => 20,
|
|
|
|
datetime => DateTime->now( time_zone => 'Europe/Berlin' )
|
|
|
|
->subtract( minutes => $lookbehind ),
|
2019-05-09 15:58:23 +00:00
|
|
|
lookahead => $lookbehind + $lookahead,
|
|
|
|
lwp_options => {
|
|
|
|
timeout => 10,
|
|
|
|
agent => 'travelynx/' . $self->app->config->{version},
|
|
|
|
},
|
2019-03-22 15:57:24 +00:00
|
|
|
);
|
|
|
|
return {
|
|
|
|
results => [ $status->results ],
|
|
|
|
errstr => $status->errstr,
|
|
|
|
station_ds100 => (
|
|
|
|
$status->station ? $status->station->{ds100} : 'undef'
|
|
|
|
),
|
|
|
|
station_name =>
|
|
|
|
( $status->station ? $status->station->{name} : 'undef' ),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
elsif ( @station_matches > 1 ) {
|
|
|
|
return {
|
|
|
|
results => [],
|
|
|
|
errstr => 'Ambiguous station name',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return {
|
|
|
|
results => [],
|
|
|
|
errstr => 'Unknown station name',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-26 17:53:01 +00:00
|
|
|
# Returns (journey id, error)
|
2019-04-13 08:43:05 +00:00
|
|
|
# Must be called during a transaction.
|
|
|
|
# Must perform a rollback on error.
|
|
|
|
$self->helper(
|
|
|
|
'add_journey' => sub {
|
|
|
|
my ( $self, %opt ) = @_;
|
|
|
|
|
2019-04-26 17:53:01 +00:00
|
|
|
my $db = $opt{db};
|
2019-04-13 08:43:05 +00:00
|
|
|
my $uid = $self->current_user->{id};
|
2019-04-26 17:53:01 +00:00
|
|
|
my $now = DateTime->now( time_zone => 'Europe/Berlin' );
|
2019-04-13 08:43:05 +00:00
|
|
|
my $dep_station = get_station( $opt{dep_station} );
|
|
|
|
my $arr_station = get_station( $opt{arr_station} );
|
|
|
|
|
|
|
|
if ( not $dep_station ) {
|
2019-04-26 17:56:03 +00:00
|
|
|
return ( undef, 'Unbekannter Startbahnhof' );
|
2019-04-13 08:43:05 +00:00
|
|
|
}
|
|
|
|
if ( not $arr_station ) {
|
2019-04-26 17:56:03 +00:00
|
|
|
return ( undef, 'Unbekannter Zielbahnhof' );
|
2019-04-13 08:43:05 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 17:53:01 +00:00
|
|
|
my $entry = {
|
|
|
|
user_id => $uid,
|
|
|
|
train_type => $opt{train_type},
|
|
|
|
train_line => $opt{train_line},
|
|
|
|
train_no => $opt{train_no},
|
|
|
|
train_id => 'manual',
|
|
|
|
checkin_station_id => $self->get_station_id(
|
|
|
|
ds100 => $dep_station->[0],
|
|
|
|
name => $dep_station->[1],
|
|
|
|
),
|
|
|
|
checkin_time => $now,
|
|
|
|
sched_departure => $opt{sched_departure},
|
|
|
|
real_departure => $opt{rt_departure},
|
|
|
|
checkout_station_id => $self->get_station_id(
|
|
|
|
ds100 => $arr_station->[0],
|
|
|
|
name => $arr_station->[1],
|
|
|
|
),
|
|
|
|
sched_arrival => $opt{sched_arrival},
|
|
|
|
real_arrival => $opt{rt_arrival},
|
|
|
|
checkout_time => $now,
|
|
|
|
edited => 0x3fff,
|
|
|
|
cancelled => $opt{cancelled} ? 1 : 0,
|
|
|
|
route => $dep_station->[1] . '|' . $arr_station->[1],
|
2019-04-17 17:35:38 +00:00
|
|
|
};
|
|
|
|
|
2019-04-26 17:53:01 +00:00
|
|
|
if ( $opt{comment} ) {
|
|
|
|
$entry->{messages} = '0:' . $opt{comment};
|
2019-04-13 08:43:05 +00:00
|
|
|
}
|
2019-04-17 17:35:38 +00:00
|
|
|
|
2019-04-26 17:53:01 +00:00
|
|
|
my $journey_id = undef;
|
2019-04-17 17:35:38 +00:00
|
|
|
eval {
|
2019-04-26 17:53:01 +00:00
|
|
|
$journey_id
|
|
|
|
= $db->insert( 'journeys', $entry, { returning => 'id' } )
|
|
|
|
->hash->{id};
|
2019-04-26 18:39:44 +00:00
|
|
|
$self->invalidate_stats_cache( $opt{rt_departure}, $db );
|
2019-04-17 17:35:38 +00:00
|
|
|
};
|
2019-04-26 17:53:01 +00:00
|
|
|
|
2019-04-17 17:35:38 +00:00
|
|
|
if ($@) {
|
2019-04-26 17:53:01 +00:00
|
|
|
$self->app->log->error("add_journey($uid): $@");
|
|
|
|
return ( undef, 'add_journey failed: ' . $@ );
|
2019-04-13 08:43:05 +00:00
|
|
|
}
|
2019-04-26 17:53:01 +00:00
|
|
|
|
|
|
|
return ( $journey_id, undef );
|
2019-04-13 08:43:05 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
$self->helper(
|
|
|
|
'checkin' => sub {
|
2019-04-23 16:08:07 +00:00
|
|
|
my ( $self, $station, $train_id ) = @_;
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
my $status = $self->get_departures( $station, 140, 30 );
|
2019-03-22 15:57:24 +00:00
|
|
|
if ( $status->{errstr} ) {
|
|
|
|
return ( undef, $status->{errstr} );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
my ($train)
|
|
|
|
= first { $_->train_id eq $train_id } @{ $status->{results} };
|
|
|
|
if ( not defined $train ) {
|
|
|
|
return ( undef, "Train ${train_id} not found" );
|
|
|
|
}
|
|
|
|
else {
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
my $user = $self->get_user_status;
|
2019-04-23 16:08:07 +00:00
|
|
|
if ( $user->{checked_in} or $user->{cancelled} ) {
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-03-25 20:37:11 +00:00
|
|
|
# If a user is already checked in, we assume that they forgot to
|
|
|
|
# check out and do it for them.
|
2019-03-22 15:57:24 +00:00
|
|
|
$self->checkout( $station, 1 );
|
|
|
|
}
|
|
|
|
|
2019-04-17 17:35:38 +00:00
|
|
|
eval {
|
|
|
|
$self->pg->db->insert(
|
2019-04-23 16:08:07 +00:00
|
|
|
'in_transit',
|
2019-04-17 17:35:38 +00:00
|
|
|
{
|
2019-04-23 16:08:07 +00:00
|
|
|
user_id => $self->current_user->{id},
|
|
|
|
cancelled => $train->departure_is_cancelled
|
|
|
|
? 1
|
|
|
|
: 0,
|
|
|
|
checkin_station_id => $self->get_station_id(
|
2019-04-17 17:35:38 +00:00
|
|
|
ds100 => $status->{station_ds100},
|
|
|
|
name => $status->{station_name}
|
|
|
|
),
|
2019-04-23 16:08:07 +00:00
|
|
|
checkin_time =>
|
2019-04-17 17:35:38 +00:00
|
|
|
DateTime->now( time_zone => 'Europe/Berlin' ),
|
2019-05-18 15:10:53 +00:00
|
|
|
dep_platform => $train->platform,
|
2019-04-23 16:08:07 +00:00
|
|
|
train_type => $train->type,
|
|
|
|
train_line => $train->line_no,
|
|
|
|
train_no => $train->train_no,
|
|
|
|
train_id => $train->train_id,
|
|
|
|
sched_departure => $train->sched_departure,
|
|
|
|
real_departure => $train->departure,
|
|
|
|
route => join( '|', $train->route ),
|
|
|
|
messages => join(
|
2019-04-17 17:35:38 +00:00
|
|
|
'|',
|
|
|
|
map {
|
|
|
|
( $_->[0] ? $_->[0]->epoch : q{} ) . ':'
|
|
|
|
. $_->[1]
|
|
|
|
} $train->messages
|
|
|
|
)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
if ($@) {
|
2019-03-25 17:12:01 +00:00
|
|
|
my $uid = $self->current_user->{id};
|
|
|
|
$self->app->log->error(
|
2019-04-23 16:08:07 +00:00
|
|
|
"Checkin($uid): INSERT failed: $@");
|
2019-04-17 17:35:38 +00:00
|
|
|
return ( undef, 'INSERT failed: ' . $@ );
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
2019-05-05 16:09:11 +00:00
|
|
|
$self->run_hook( $self->current_user->{id}, 'checkin' );
|
2019-04-17 17:35:38 +00:00
|
|
|
return ( $train, undef );
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$self->helper(
|
|
|
|
'undo' => sub {
|
2019-04-23 16:08:07 +00:00
|
|
|
my ( $self, $journey_id ) = @_;
|
|
|
|
my $uid = $self->current_user->{id};
|
2019-03-22 15:57:24 +00:00
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
if ( $journey_id eq 'in_transit' ) {
|
|
|
|
eval {
|
|
|
|
$self->pg->db->delete( 'in_transit', { user_id => $uid } );
|
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
$self->app->log->error("Undo($uid, $journey_id): $@");
|
|
|
|
return "Undo($journey_id): $@";
|
|
|
|
}
|
2019-05-05 16:09:11 +00:00
|
|
|
$self->run_hook( $uid, 'undo' );
|
2019-04-23 16:08:07 +00:00
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
if ( $journey_id !~ m{ ^ \d+ $ }x ) {
|
|
|
|
return 'Invalid Journey ID';
|
2019-03-21 21:12:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 11:10:49 +00:00
|
|
|
eval {
|
2019-04-23 16:08:07 +00:00
|
|
|
my $db = $self->pg->db;
|
|
|
|
my $tx = $db->begin;
|
|
|
|
|
|
|
|
my $journey = $db->select(
|
|
|
|
'journeys',
|
|
|
|
'*',
|
|
|
|
{
|
|
|
|
user_id => $uid,
|
|
|
|
id => $journey_id
|
|
|
|
}
|
|
|
|
)->hash;
|
|
|
|
$db->delete(
|
|
|
|
'journeys',
|
|
|
|
{
|
|
|
|
user_id => $uid,
|
|
|
|
id => $journey_id
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $journey->{edited} ) {
|
|
|
|
die(
|
|
|
|
"Cannot undo a journey which has already been edited. Please delete manually.\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete $journey->{edited};
|
|
|
|
delete $journey->{id};
|
|
|
|
|
|
|
|
$db->insert( 'in_transit', $journey );
|
|
|
|
|
|
|
|
my $cache_ts = DateTime->now( time_zone => 'Europe/Berlin' );
|
|
|
|
if ( $journey->{real_departure}
|
|
|
|
=~ m{ ^ (?<year> \d{4} ) - (?<month> \d{2} ) }x )
|
|
|
|
{
|
|
|
|
$cache_ts->set(
|
|
|
|
year => $+{year},
|
|
|
|
month => $+{month}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->invalidate_stats_cache( $cache_ts, $db );
|
|
|
|
|
|
|
|
$tx->commit;
|
2019-04-17 11:10:49 +00:00
|
|
|
};
|
|
|
|
if ($@) {
|
2019-04-23 16:08:07 +00:00
|
|
|
$self->app->log->error("Undo($uid, $journey_id): $@");
|
|
|
|
return "Undo($journey_id): $@";
|
2019-03-21 21:12:45 +00:00
|
|
|
}
|
2019-05-05 16:09:11 +00:00
|
|
|
$self->run_hook( $uid, 'undo' );
|
2019-04-23 16:08:07 +00:00
|
|
|
return undef;
|
2019-03-21 21:12:45 +00:00
|
|
|
}
|
2019-03-22 15:57:24 +00:00
|
|
|
);
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
# Statistics are partitioned by real_departure, which must be provided
|
|
|
|
# when calling this function e.g. after journey deletion or editing.
|
|
|
|
# If a joureny's real_departure has been edited, this function must be
|
|
|
|
# called twice: once with the old and once with the new value.
|
2019-04-07 15:16:06 +00:00
|
|
|
$self->helper(
|
|
|
|
'invalidate_stats_cache' => sub {
|
2019-04-23 19:30:31 +00:00
|
|
|
my ( $self, $ts, $db, $uid ) = @_;
|
2019-04-07 15:16:06 +00:00
|
|
|
|
2019-04-23 19:30:31 +00:00
|
|
|
$uid //= $self->current_user->{id};
|
|
|
|
$db //= $self->pg->db;
|
2019-04-07 15:16:06 +00:00
|
|
|
|
2019-04-17 11:10:49 +00:00
|
|
|
$self->pg->db->delete(
|
|
|
|
'journey_stats',
|
|
|
|
{
|
|
|
|
user_id => $uid,
|
|
|
|
year => $ts->year,
|
|
|
|
month => $ts->month,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$self->pg->db->delete(
|
|
|
|
'journey_stats',
|
|
|
|
{
|
|
|
|
user_id => $uid,
|
|
|
|
year => $ts->year,
|
|
|
|
month => 0,
|
|
|
|
}
|
|
|
|
);
|
2019-04-07 15:16:06 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
$self->helper(
|
|
|
|
'checkout' => sub {
|
2019-04-23 19:30:31 +00:00
|
|
|
my ( $self, $station, $force, $uid ) = @_;
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-23 19:30:31 +00:00
|
|
|
my $db = $self->pg->db;
|
|
|
|
my $status = $self->get_departures( $station, 120, 120 );
|
|
|
|
$uid //= $self->current_user->{id};
|
|
|
|
my $user = $self->get_user_status($uid);
|
2019-03-22 15:57:24 +00:00
|
|
|
my $train_id = $user->{train_id};
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
if ( not $user->{checked_in} and not $user->{cancelled} ) {
|
2019-04-23 16:08:07 +00:00
|
|
|
return ( 0, 'You are not checked into any train' );
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
|
|
|
if ( $status->{errstr} and not $force ) {
|
2019-04-23 16:08:07 +00:00
|
|
|
return ( 1, $status->{errstr} );
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-07 15:16:06 +00:00
|
|
|
my $now = DateTime->now( time_zone => 'Europe/Berlin' );
|
2019-04-23 16:08:07 +00:00
|
|
|
my $journey
|
|
|
|
= $db->select( 'in_transit', '*', { user_id => $uid } )->hash;
|
2019-03-22 15:57:24 +00:00
|
|
|
my ($train)
|
|
|
|
= first { $_->train_id eq $train_id } @{ $status->{results} };
|
2019-04-23 16:08:07 +00:00
|
|
|
|
|
|
|
# Store the intended checkout station regardless of this operation's
|
|
|
|
# success.
|
|
|
|
my $new_checkout_station_id = $self->get_station_id(
|
|
|
|
ds100 => $status->{station_ds100},
|
|
|
|
name => $status->{station_name}
|
|
|
|
);
|
|
|
|
$db->update(
|
|
|
|
'in_transit',
|
|
|
|
{
|
|
|
|
checkout_station_id => $new_checkout_station_id,
|
|
|
|
},
|
|
|
|
{ user_id => $uid }
|
|
|
|
);
|
|
|
|
|
|
|
|
# If in_transit already contains arrival data for another estimated
|
|
|
|
# destination, we must invalidate it.
|
|
|
|
if ( defined $journey->{checkout_station_id}
|
|
|
|
and $journey->{checkout_station_id}
|
|
|
|
!= $new_checkout_station_id )
|
|
|
|
{
|
|
|
|
$db->update(
|
|
|
|
'in_transit',
|
|
|
|
{
|
|
|
|
checkout_time => undef,
|
2019-05-18 15:10:53 +00:00
|
|
|
arr_platform => undef,
|
2019-04-23 16:08:07 +00:00
|
|
|
sched_arrival => undef,
|
|
|
|
real_arrival => undef,
|
|
|
|
},
|
|
|
|
{ user_id => $uid }
|
|
|
|
);
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
2019-04-23 16:08:07 +00:00
|
|
|
|
|
|
|
if ( not( defined $train or $force ) ) {
|
|
|
|
return ( 1, undef );
|
|
|
|
}
|
|
|
|
|
|
|
|
my $has_arrived = 0;
|
|
|
|
|
|
|
|
eval {
|
|
|
|
|
|
|
|
my $tx = $db->begin;
|
|
|
|
|
|
|
|
if ( defined $train ) {
|
|
|
|
$has_arrived = $train->arrival->epoch < $now->epoch ? 1 : 0;
|
|
|
|
$db->update(
|
|
|
|
'in_transit',
|
2019-04-17 17:35:38 +00:00
|
|
|
{
|
2019-04-23 16:08:07 +00:00
|
|
|
checkout_time => $now,
|
2019-05-18 15:10:53 +00:00
|
|
|
arr_platform => $train->platform,
|
2019-04-23 16:08:07 +00:00
|
|
|
sched_arrival => $train->sched_arrival,
|
|
|
|
real_arrival => $train->arrival,
|
|
|
|
cancelled => $train->arrival_is_cancelled ? 1 : 0,
|
|
|
|
route => join( '|', $train->route ),
|
|
|
|
messages => join(
|
2019-04-17 17:35:38 +00:00
|
|
|
'|',
|
|
|
|
map {
|
|
|
|
( $_->[0] ? $_->[0]->epoch : q{} ) . ':'
|
|
|
|
. $_->[1]
|
|
|
|
} $train->messages
|
2019-04-23 16:08:07 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
{ user_id => $uid }
|
2019-04-17 17:35:38 +00:00
|
|
|
);
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
2019-04-23 16:08:07 +00:00
|
|
|
|
|
|
|
$journey
|
|
|
|
= $db->select( 'in_transit', '*', { user_id => $uid } )->hash;
|
|
|
|
|
|
|
|
if ( $has_arrived or $force ) {
|
|
|
|
$journey->{edited} = 0;
|
|
|
|
$journey->{checkout_time} = $now;
|
|
|
|
$db->insert( 'journeys', $journey );
|
|
|
|
$db->delete( 'in_transit', { user_id => $uid } );
|
|
|
|
|
|
|
|
my $cache_ts = $now->clone;
|
|
|
|
if ( $journey->{real_departure}
|
|
|
|
=~ m{ ^ (?<year> \d{4} ) - (?<month> \d{2} ) }x )
|
|
|
|
{
|
|
|
|
$cache_ts->set(
|
|
|
|
year => $+{year},
|
|
|
|
month => $+{month}
|
|
|
|
);
|
|
|
|
}
|
2019-04-23 19:30:31 +00:00
|
|
|
$self->invalidate_stats_cache( $cache_ts, $db, $uid );
|
2019-04-23 16:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$tx->commit;
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
|
|
|
$self->app->log->error("Checkout($uid): $@");
|
|
|
|
return ( 1, 'Checkout error: ' . $@ );
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
2019-04-23 16:08:07 +00:00
|
|
|
|
|
|
|
if ( $has_arrived or $force ) {
|
2019-05-05 16:09:11 +00:00
|
|
|
$self->run_hook( $uid, 'checkout' );
|
2019-05-06 14:59:48 +00:00
|
|
|
return ( 0, undef );
|
2019-04-23 16:08:07 +00:00
|
|
|
}
|
2019-05-06 14:59:48 +00:00
|
|
|
$self->run_hook( $uid, 'update' );
|
2019-04-23 16:08:07 +00:00
|
|
|
return ( 1, undef );
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
2019-03-21 21:12:45 +00:00
|
|
|
);
|
|
|
|
|
2019-04-30 10:47:32 +00:00
|
|
|
$self->helper(
|
|
|
|
'mark_seen' => sub {
|
|
|
|
my ( $self, $uid ) = @_;
|
|
|
|
|
|
|
|
$self->pg->db->update(
|
|
|
|
'users',
|
|
|
|
{ last_seen => DateTime->now( time_zone => 'Europe/Berlin' ) },
|
|
|
|
{ id => $uid }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-04-08 19:20:04 +00:00
|
|
|
$self->helper(
|
|
|
|
'update_journey_part' => sub {
|
2019-04-23 16:08:07 +00:00
|
|
|
my ( $self, $db, $journey_id, $key, $value ) = @_;
|
2019-04-17 17:35:38 +00:00
|
|
|
my $rows;
|
2019-04-08 19:20:04 +00:00
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
my $journey = $self->get_journey(
|
|
|
|
db => $db,
|
|
|
|
journey_id => $journey_id,
|
|
|
|
);
|
|
|
|
|
2019-04-17 17:35:38 +00:00
|
|
|
eval {
|
|
|
|
if ( $key eq 'sched_departure' ) {
|
|
|
|
$rows = $db->update(
|
2019-04-23 16:08:07 +00:00
|
|
|
'journeys',
|
2019-04-17 17:35:38 +00:00
|
|
|
{
|
2019-04-23 16:08:07 +00:00
|
|
|
sched_departure => $value,
|
|
|
|
edited => $journey->{edited} | 0x0001,
|
2019-04-17 17:35:38 +00:00
|
|
|
},
|
|
|
|
{
|
2019-04-23 16:08:07 +00:00
|
|
|
id => $journey_id,
|
2019-04-17 17:35:38 +00:00
|
|
|
}
|
|
|
|
)->rows;
|
|
|
|
}
|
|
|
|
elsif ( $key eq 'rt_departure' ) {
|
|
|
|
$rows = $db->update(
|
2019-04-23 16:08:07 +00:00
|
|
|
'journeys',
|
2019-04-17 17:35:38 +00:00
|
|
|
{
|
2019-04-23 16:08:07 +00:00
|
|
|
real_departure => $value,
|
|
|
|
edited => $journey->{edited} | 0x0002,
|
2019-04-17 17:35:38 +00:00
|
|
|
},
|
|
|
|
{
|
2019-04-23 16:08:07 +00:00
|
|
|
id => $journey_id,
|
2019-04-17 17:35:38 +00:00
|
|
|
}
|
|
|
|
)->rows;
|
2019-04-23 16:08:07 +00:00
|
|
|
|
|
|
|
# stats are partitioned by rt_departure -> both the cache for
|
|
|
|
# the old value (see bottom of this function) and the new value
|
|
|
|
# (here) must be invalidated.
|
|
|
|
$self->invalidate_stats_cache( $value, $db );
|
2019-04-17 17:35:38 +00:00
|
|
|
}
|
|
|
|
elsif ( $key eq 'sched_arrival' ) {
|
|
|
|
$rows = $db->update(
|
2019-04-23 16:08:07 +00:00
|
|
|
'journeys',
|
2019-04-17 17:35:38 +00:00
|
|
|
{
|
2019-04-23 16:08:07 +00:00
|
|
|
sched_arrival => $value,
|
|
|
|
edited => $journey->{edited} | 0x0100,
|
2019-04-17 17:35:38 +00:00
|
|
|
},
|
|
|
|
{
|
2019-04-23 16:08:07 +00:00
|
|
|
id => $journey_id,
|
2019-04-17 17:35:38 +00:00
|
|
|
}
|
|
|
|
)->rows;
|
|
|
|
}
|
|
|
|
elsif ( $key eq 'rt_arrival' ) {
|
|
|
|
$rows = $db->update(
|
2019-04-23 16:08:07 +00:00
|
|
|
'journeys',
|
2019-04-17 17:35:38 +00:00
|
|
|
{
|
2019-04-23 16:08:07 +00:00
|
|
|
real_arrival => $value,
|
|
|
|
edited => $journey->{edited} | 0x0200,
|
2019-04-17 17:35:38 +00:00
|
|
|
},
|
|
|
|
{
|
2019-04-23 16:08:07 +00:00
|
|
|
id => $journey_id,
|
2019-04-17 17:35:38 +00:00
|
|
|
}
|
|
|
|
)->rows;
|
|
|
|
}
|
|
|
|
else {
|
2019-04-23 16:08:07 +00:00
|
|
|
die("Invalid key $key\n");
|
2019-04-17 17:35:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
2019-04-08 19:20:04 +00:00
|
|
|
$self->app->log->error(
|
2019-04-23 16:08:07 +00:00
|
|
|
"update_journey_part($journey_id, $key): $@");
|
|
|
|
return "update_journey_part($key): $@";
|
2019-04-08 19:20:04 +00:00
|
|
|
}
|
2019-04-17 17:35:38 +00:00
|
|
|
if ( $rows == 1 ) {
|
2019-04-23 16:08:07 +00:00
|
|
|
$self->invalidate_stats_cache( $journey->{rt_departure}, $db );
|
2019-04-17 17:35:38 +00:00
|
|
|
return undef;
|
2019-04-08 19:20:04 +00:00
|
|
|
}
|
2019-04-17 17:35:38 +00:00
|
|
|
return 'UPDATE failed: did not match any journey part';
|
2019-04-08 19:20:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$self->helper(
|
|
|
|
'journey_sanity_check' => sub {
|
|
|
|
my ( $self, $journey ) = @_;
|
|
|
|
|
|
|
|
if ( $journey->{sched_duration} and $journey->{sched_duration} < 0 )
|
|
|
|
{
|
2019-04-09 16:11:55 +00:00
|
|
|
return
|
|
|
|
'Die geplante Dauer dieser Zugfahrt ist negativ. Zeitreisen werden aktuell nicht unterstützt.';
|
2019-04-08 19:20:04 +00:00
|
|
|
}
|
|
|
|
if ( $journey->{rt_duration} and $journey->{rt_duration} < 0 ) {
|
2019-04-09 16:11:55 +00:00
|
|
|
return
|
|
|
|
'Die Dauer dieser Zugfahrt ist negativ. Zeitreisen werden aktuell nicht unterstützt.';
|
2019-04-08 19:20:04 +00:00
|
|
|
}
|
|
|
|
if ( $journey->{sched_duration}
|
|
|
|
and $journey->{sched_duration} > 60 * 60 * 24 )
|
|
|
|
{
|
2019-04-09 16:11:55 +00:00
|
|
|
return 'Die Zugfahrt ist länger als 24 Stunden.';
|
2019-04-08 19:20:04 +00:00
|
|
|
}
|
|
|
|
if ( $journey->{rt_duration}
|
|
|
|
and $journey->{rt_duration} > 60 * 60 * 24 )
|
|
|
|
{
|
2019-04-09 16:11:55 +00:00
|
|
|
return 'Die Zugfahrt ist länger als 24 Stunden.';
|
2019-04-08 19:20:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
$self->helper(
|
|
|
|
'get_station_id' => sub {
|
|
|
|
my ( $self, %opt ) = @_;
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-22 07:58:39 +00:00
|
|
|
my $res = $self->pg->db->select( 'stations', ['id'],
|
|
|
|
{ ds100 => $opt{ds100} } );
|
|
|
|
my $res_h = $res->hash;
|
|
|
|
|
|
|
|
if ($res_h) {
|
|
|
|
$res->finish;
|
|
|
|
return $res_h->{id};
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
2019-04-22 07:58:39 +00:00
|
|
|
|
|
|
|
$self->pg->db->insert(
|
|
|
|
'stations',
|
|
|
|
{
|
|
|
|
ds100 => $opt{ds100},
|
|
|
|
name => $opt{name},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$res = $self->pg->db->select( 'stations', ['id'],
|
|
|
|
{ ds100 => $opt{ds100} } );
|
|
|
|
my $id = $res->hash->{id};
|
|
|
|
$res->finish;
|
|
|
|
return $id;
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
|
|
|
);
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
$self->helper(
|
2019-04-30 16:05:07 +00:00
|
|
|
'verify_registration_token' => sub {
|
|
|
|
my ( $self, $uid, $token ) = @_;
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-30 16:05:07 +00:00
|
|
|
my $db = $self->pg->db;
|
|
|
|
my $tx = $db->begin;
|
|
|
|
|
|
|
|
my $res = $db->select(
|
|
|
|
'pending_registrations',
|
|
|
|
'count(*) as count',
|
|
|
|
{
|
|
|
|
user_id => $uid,
|
|
|
|
token => $token
|
|
|
|
}
|
2019-04-19 19:56:54 +00:00
|
|
|
);
|
|
|
|
|
2019-04-30 16:05:07 +00:00
|
|
|
if ( $res->hash->{count} ) {
|
|
|
|
$db->update( 'users', { status => 1 }, { id => $uid } );
|
|
|
|
$db->delete( 'pending_registrations', { user_id => $uid } );
|
|
|
|
$tx->commit;
|
|
|
|
return 1;
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
);
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-29 18:12:59 +00:00
|
|
|
$self->helper(
|
|
|
|
'get_uid_by_name_and_mail' => sub {
|
|
|
|
my ( $self, $name, $email ) = @_;
|
|
|
|
|
|
|
|
my $res = $self->pg->db->select(
|
|
|
|
'users',
|
|
|
|
['id'],
|
|
|
|
{
|
|
|
|
name => $name,
|
|
|
|
email => $email,
|
|
|
|
status => 1
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( my $user = $res->hash ) {
|
|
|
|
return $user->{id};
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-04-30 21:23:49 +00:00
|
|
|
$self->helper(
|
|
|
|
'get_privacy_by_name' => sub {
|
|
|
|
my ( $self, $name ) = @_;
|
|
|
|
|
|
|
|
my $res = $self->pg->db->select(
|
|
|
|
'users',
|
|
|
|
[ 'id', 'public_level' ],
|
|
|
|
{
|
|
|
|
name => $name,
|
|
|
|
status => 1
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( my $user = $res->hash ) {
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$self->helper(
|
|
|
|
'set_privacy' => sub {
|
|
|
|
my ( $self, $uid, $public_level ) = @_;
|
|
|
|
|
|
|
|
$self->pg->db->update(
|
|
|
|
'users',
|
|
|
|
{ public_level => $public_level },
|
|
|
|
{ id => $uid }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-04-29 18:12:59 +00:00
|
|
|
$self->helper(
|
|
|
|
'mark_for_password_reset' => sub {
|
|
|
|
my ( $self, $db, $uid, $token ) = @_;
|
|
|
|
|
|
|
|
my $res = $db->select(
|
|
|
|
'pending_passwords',
|
|
|
|
'count(*) as count',
|
|
|
|
{ user_id => $uid }
|
|
|
|
);
|
|
|
|
if ( $res->hash->{count} ) {
|
|
|
|
return 'in progress';
|
|
|
|
}
|
|
|
|
|
|
|
|
$db->insert(
|
|
|
|
'pending_passwords',
|
|
|
|
{
|
|
|
|
user_id => $uid,
|
|
|
|
token => $token,
|
|
|
|
requested_at =>
|
|
|
|
DateTime->now( time_zone => 'Europe/Berlin' )
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$self->helper(
|
|
|
|
'verify_password_token' => sub {
|
|
|
|
my ( $self, $uid, $token ) = @_;
|
|
|
|
|
|
|
|
my $res = $self->pg->db->select(
|
|
|
|
'pending_passwords',
|
|
|
|
'count(*) as count',
|
|
|
|
{
|
|
|
|
user_id => $uid,
|
|
|
|
token => $token
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $res->hash->{count} ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-04-30 10:08:51 +00:00
|
|
|
$self->helper(
|
|
|
|
'mark_for_mail_change' => sub {
|
|
|
|
my ( $self, $db, $uid, $email, $token ) = @_;
|
|
|
|
|
|
|
|
$db->insert(
|
|
|
|
'pending_mails',
|
|
|
|
{
|
|
|
|
user_id => $uid,
|
|
|
|
email => $email,
|
|
|
|
token => $token,
|
|
|
|
requested_at =>
|
|
|
|
DateTime->now( time_zone => 'Europe/Berlin' )
|
|
|
|
},
|
|
|
|
{
|
|
|
|
on_conflict => \
|
|
|
|
'(user_id) do update set email = EXCLUDED.email, token = EXCLUDED.token, requested_at = EXCLUDED.requested_at'
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$self->helper(
|
|
|
|
'change_mail_with_token' => sub {
|
|
|
|
my ( $self, $uid, $token ) = @_;
|
|
|
|
|
|
|
|
my $db = $self->pg->db;
|
|
|
|
my $tx = $db->begin;
|
|
|
|
|
|
|
|
my $res_h = $db->select(
|
|
|
|
'pending_mails',
|
|
|
|
['email'],
|
|
|
|
{
|
|
|
|
user_id => $uid,
|
|
|
|
token => $token
|
|
|
|
}
|
|
|
|
)->hash;
|
|
|
|
|
|
|
|
if ($res_h) {
|
|
|
|
$db->update(
|
|
|
|
'users',
|
|
|
|
{ email => $res_h->{email} },
|
|
|
|
{ id => $uid }
|
|
|
|
);
|
|
|
|
$db->delete( 'pending_mails', { user_id => $uid } );
|
|
|
|
$tx->commit;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-04-29 18:12:59 +00:00
|
|
|
$self->helper(
|
|
|
|
'remove_password_token' => sub {
|
|
|
|
my ( $self, $uid, $token ) = @_;
|
|
|
|
|
|
|
|
$self->pg->db->delete(
|
|
|
|
'pending_passwords',
|
|
|
|
{
|
|
|
|
user_id => $uid,
|
|
|
|
token => $token
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
# 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 ) = @_;
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
$uid //= $self->current_user->{id};
|
2019-04-22 07:58:39 +00:00
|
|
|
|
|
|
|
my $user_data = $self->pg->db->select(
|
|
|
|
'users',
|
|
|
|
'id, name, status, public_level, email, '
|
|
|
|
. 'extract(epoch from registered_at) as registered_at_ts, '
|
2019-04-30 10:47:32 +00:00
|
|
|
. 'extract(epoch from last_seen) as last_seen_ts, '
|
2019-04-22 07:58:39 +00:00
|
|
|
. 'extract(epoch from deletion_requested) as deletion_requested_ts',
|
|
|
|
{ id => $uid }
|
|
|
|
)->hash;
|
|
|
|
|
|
|
|
if ($user_data) {
|
2019-03-22 15:57:24 +00:00
|
|
|
return {
|
2019-04-22 07:58:39 +00:00
|
|
|
id => $user_data->{id},
|
|
|
|
name => $user_data->{name},
|
|
|
|
status => $user_data->{status},
|
|
|
|
is_public => $user_data->{public_level},
|
|
|
|
email => $user_data->{email},
|
2019-03-22 15:57:24 +00:00
|
|
|
registered_at => DateTime->from_epoch(
|
2019-04-22 07:58:39 +00:00
|
|
|
epoch => $user_data->{registered_at_ts},
|
2019-03-22 15:57:24 +00:00
|
|
|
time_zone => 'Europe/Berlin'
|
|
|
|
),
|
|
|
|
last_seen => DateTime->from_epoch(
|
2019-04-30 10:47:32 +00:00
|
|
|
epoch => $user_data->{last_seen_ts},
|
2019-03-22 15:57:24 +00:00
|
|
|
time_zone => 'Europe/Berlin'
|
|
|
|
),
|
2019-04-22 07:58:39 +00:00
|
|
|
deletion_requested => $user_data->{deletion_requested_ts}
|
2019-03-22 15:57:24 +00:00
|
|
|
? DateTime->from_epoch(
|
2019-04-22 07:58:39 +00:00
|
|
|
epoch => $user_data->{deletion_requested_ts},
|
2019-03-22 15:57:24 +00:00
|
|
|
time_zone => 'Europe/Berlin'
|
|
|
|
)
|
|
|
|
: undef,
|
|
|
|
};
|
2019-03-21 21:12:45 +00:00
|
|
|
}
|
2019-03-22 15:57:24 +00:00
|
|
|
return undef;
|
2019-03-21 21:12:45 +00:00
|
|
|
}
|
2019-03-22 15:57:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$self->helper(
|
|
|
|
'get_api_token' => sub {
|
|
|
|
my ( $self, $uid ) = @_;
|
|
|
|
$uid //= $self->current_user->{id};
|
2019-04-22 10:11:22 +00:00
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
my $token = {};
|
2019-04-22 10:11:22 +00:00
|
|
|
my $res = $self->pg->db->select(
|
|
|
|
'tokens',
|
|
|
|
[ 'type', 'token' ],
|
|
|
|
{ user_id => $uid }
|
|
|
|
);
|
|
|
|
|
|
|
|
for my $entry ( $res->hashes->each ) {
|
|
|
|
$token->{ $self->app->token_types->[ $entry->{type} - 1 ] }
|
|
|
|
= $entry->{token};
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
2019-04-22 10:11:22 +00:00
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
return $token;
|
2019-03-21 21:12:45 +00:00
|
|
|
}
|
2019-03-22 15:57:24 +00:00
|
|
|
);
|
|
|
|
|
2019-05-05 16:09:11 +00:00
|
|
|
$self->helper(
|
|
|
|
'get_webhook' => sub {
|
|
|
|
my ( $self, $uid ) = @_;
|
|
|
|
$uid //= $self->current_user->{id};
|
|
|
|
|
|
|
|
my $res_h
|
|
|
|
= $self->pg->db->select( 'webhooks_str', '*',
|
|
|
|
{ user_id => $uid } )->hash;
|
|
|
|
|
|
|
|
$res_h->{latest_run} = epoch_to_dt( $res_h->{latest_run_ts} );
|
|
|
|
|
|
|
|
return $res_h;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$self->helper(
|
|
|
|
'set_webhook' => sub {
|
|
|
|
my ( $self, %opt ) = @_;
|
|
|
|
|
|
|
|
$opt{uid} //= $self->current_user->{id};
|
|
|
|
|
2019-05-06 14:59:48 +00:00
|
|
|
if ( $opt{token} ) {
|
|
|
|
$opt{token} =~ tr{\r\n}{}d;
|
|
|
|
}
|
|
|
|
|
2019-05-05 16:09:11 +00:00
|
|
|
my $res = $self->pg->db->insert(
|
|
|
|
'webhooks',
|
|
|
|
{
|
|
|
|
user_id => $opt{uid},
|
|
|
|
enabled => $opt{enabled},
|
|
|
|
url => $opt{url},
|
|
|
|
token => $opt{token}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
on_conflict => \
|
|
|
|
'(user_id) do update set enabled = EXCLUDED.enabled, url = EXCLUDED.url, token = EXCLUDED.token, errored = null, latest_run = null, output = null'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$self->helper(
|
|
|
|
'mark_hook_status' => sub {
|
|
|
|
my ( $self, $uid, $url, $success, $text ) = @_;
|
|
|
|
|
2019-05-06 14:59:48 +00:00
|
|
|
if ( length($text) > 1000 ) {
|
|
|
|
$text = substr( $text, 0, 1000 ) . '…';
|
2019-05-05 16:09:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$self->pg->db->update(
|
|
|
|
'webhooks',
|
|
|
|
{
|
2019-05-10 17:03:18 +00:00
|
|
|
errored => $success ? 0 : 1,
|
2019-05-05 16:09:11 +00:00
|
|
|
latest_run => DateTime->now( time_zone => 'Europe/Berlin' ),
|
|
|
|
output => $text,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
user_id => $uid,
|
|
|
|
url => $url
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$self->helper(
|
|
|
|
'run_hook' => sub {
|
2019-05-14 18:05:45 +00:00
|
|
|
my ( $self, $uid, $reason, $callback ) = @_;
|
2019-05-05 16:09:11 +00:00
|
|
|
|
|
|
|
my $hook = $self->get_webhook($uid);
|
|
|
|
|
|
|
|
if ( not $hook->{enabled} or not $hook->{url} =~ m{^ https?:// }x )
|
|
|
|
{
|
2019-05-14 18:05:45 +00:00
|
|
|
if ($callback) {
|
|
|
|
&$callback();
|
|
|
|
}
|
2019-05-05 16:09:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-06 14:59:48 +00:00
|
|
|
my $status = $self->get_user_status_json_v1($uid);
|
2019-05-05 16:09:11 +00:00
|
|
|
my $header = {};
|
|
|
|
my $hook_body = {
|
|
|
|
reason => $reason,
|
|
|
|
status => $status,
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( $hook->{token} ) {
|
|
|
|
$header->{Authorization} = "Bearer $hook->{token}";
|
|
|
|
}
|
|
|
|
|
|
|
|
my $ua = $self->ua;
|
2019-05-14 18:05:45 +00:00
|
|
|
if ($callback) {
|
|
|
|
$ua->request_timeout(4);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$ua->request_timeout(10);
|
|
|
|
}
|
2019-05-05 16:09:11 +00:00
|
|
|
|
|
|
|
$ua->post_p( $hook->{url} => $header => json => $hook_body )->then(
|
|
|
|
sub {
|
|
|
|
my ($tx) = @_;
|
|
|
|
if ( my $err = $tx->error ) {
|
|
|
|
$self->mark_hook_status( $uid, $hook->{url}, 0,
|
|
|
|
"HTTP $err->{code} $err->{message}" );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->mark_hook_status( $uid, $hook->{url}, 1,
|
|
|
|
$tx->result->body );
|
|
|
|
}
|
2019-05-14 18:05:45 +00:00
|
|
|
if ($callback) {
|
|
|
|
&$callback();
|
|
|
|
}
|
2019-05-05 16:09:11 +00:00
|
|
|
}
|
|
|
|
)->catch(
|
|
|
|
sub {
|
|
|
|
my ($err) = @_;
|
|
|
|
$self->mark_hook_status( $uid, $hook->{url}, 0, $err );
|
2019-05-14 18:05:45 +00:00
|
|
|
if ($callback) {
|
|
|
|
&$callback();
|
|
|
|
}
|
2019-05-05 16:09:11 +00:00
|
|
|
}
|
|
|
|
)->wait;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
$self->helper(
|
|
|
|
'get_user_password' => sub {
|
|
|
|
my ( $self, $name ) = @_;
|
2019-04-22 07:58:39 +00:00
|
|
|
|
|
|
|
my $res_h = $self->pg->db->select(
|
|
|
|
'users',
|
|
|
|
'id, name, status, password as password_hash',
|
|
|
|
{ name => $name }
|
|
|
|
)->hash;
|
|
|
|
|
|
|
|
return $res_h;
|
2019-03-21 21:12:45 +00:00
|
|
|
}
|
2019-03-22 15:57:24 +00:00
|
|
|
);
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
$self->helper(
|
|
|
|
'add_user' => sub {
|
2019-04-22 10:11:22 +00:00
|
|
|
my ( $self, $db, $user_name, $email, $token, $password ) = @_;
|
|
|
|
|
|
|
|
# This helper must be called during a transaction, as user creation
|
|
|
|
# may fail even after the database entry has been generated, e.g. if
|
|
|
|
# the registration mail cannot be sent. We therefore use $db (the
|
|
|
|
# database handle performing the transaction) instead of $self->pg->db
|
|
|
|
# (which may be a new handle not belonging to the transaction).
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-22 07:58:39 +00:00
|
|
|
my $now = DateTime->now( time_zone => 'Europe/Berlin' );
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-22 10:11:22 +00:00
|
|
|
my $res = $db->insert(
|
2019-04-22 07:58:39 +00:00
|
|
|
'users',
|
|
|
|
{
|
|
|
|
name => $user_name,
|
|
|
|
status => 0,
|
|
|
|
public_level => 0,
|
|
|
|
email => $email,
|
|
|
|
password => $password,
|
|
|
|
registered_at => $now,
|
2019-04-30 10:47:32 +00:00
|
|
|
last_seen => $now,
|
2019-04-22 07:58:39 +00:00
|
|
|
},
|
|
|
|
{ returning => 'id' }
|
|
|
|
);
|
2019-04-30 16:05:07 +00:00
|
|
|
my $uid = $res->hash->{id};
|
|
|
|
|
|
|
|
$db->insert(
|
|
|
|
'pending_registrations',
|
|
|
|
{
|
|
|
|
user_id => $uid,
|
|
|
|
token => $token
|
|
|
|
}
|
|
|
|
);
|
2019-04-22 07:58:39 +00:00
|
|
|
|
2019-04-30 16:05:07 +00:00
|
|
|
return $uid;
|
2019-04-22 07:58:39 +00:00
|
|
|
}
|
|
|
|
);
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-22 07:58:39 +00:00
|
|
|
$self->helper(
|
|
|
|
'flag_user_deletion' => sub {
|
|
|
|
my ( $self, $uid ) = @_;
|
|
|
|
|
|
|
|
my $now = DateTime->now( time_zone => 'Europe/Berlin' );
|
|
|
|
|
|
|
|
$self->pg->db->update(
|
|
|
|
'users',
|
|
|
|
{ deletion_requested => $now },
|
|
|
|
{
|
|
|
|
id => $uid,
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
2019-04-22 07:58:39 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$self->helper(
|
|
|
|
'unflag_user_deletion' => sub {
|
|
|
|
my ( $self, $uid ) = @_;
|
|
|
|
|
|
|
|
$self->pg->db->update(
|
|
|
|
'users',
|
|
|
|
{
|
|
|
|
deletion_requested => undef,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id => $uid,
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
2019-04-22 07:58:39 +00:00
|
|
|
);
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
|
|
|
);
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-06 09:20:10 +00:00
|
|
|
$self->helper(
|
|
|
|
'set_user_password' => sub {
|
|
|
|
my ( $self, $uid, $password ) = @_;
|
2019-04-22 07:58:39 +00:00
|
|
|
|
|
|
|
$self->pg->db->update(
|
|
|
|
'users',
|
|
|
|
{ password => $password },
|
|
|
|
{ id => $uid }
|
|
|
|
);
|
2019-04-06 09:20:10 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
$self->helper(
|
|
|
|
'check_if_user_name_exists' => sub {
|
|
|
|
my ( $self, $user_name ) = @_;
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-22 07:58:39 +00:00
|
|
|
my $count = $self->pg->db->select(
|
|
|
|
'users',
|
|
|
|
'count(*) as count',
|
|
|
|
{ name => $user_name }
|
|
|
|
)->hash->{count};
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-22 07:58:39 +00:00
|
|
|
if ($count) {
|
2019-03-22 15:57:24 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2019-03-21 21:12:45 +00:00
|
|
|
}
|
2019-03-22 15:57:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$self->helper(
|
|
|
|
'check_if_mail_is_blacklisted' => sub {
|
|
|
|
my ( $self, $mail ) = @_;
|
|
|
|
|
2019-04-22 07:58:39 +00:00
|
|
|
my $count = $self->pg->db->select(
|
|
|
|
'users',
|
|
|
|
'count(*) as count',
|
|
|
|
{
|
|
|
|
email => $mail,
|
|
|
|
status => 0,
|
|
|
|
}
|
|
|
|
)->hash->{count};
|
|
|
|
|
|
|
|
if ($count) {
|
2019-03-22 15:57:24 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2019-04-22 07:58:39 +00:00
|
|
|
|
|
|
|
$count = $self->pg->db->select(
|
2019-04-30 10:08:51 +00:00
|
|
|
'mail_blacklist',
|
2019-04-22 07:58:39 +00:00
|
|
|
'count(*) as count',
|
|
|
|
{
|
|
|
|
email => $mail,
|
|
|
|
num_tries => { '>', 1 },
|
|
|
|
}
|
|
|
|
)->hash->{count};
|
|
|
|
|
|
|
|
if ($count) {
|
2019-03-22 15:57:24 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2019-03-21 21:12:45 +00:00
|
|
|
}
|
2019-03-22 15:57:24 +00:00
|
|
|
);
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-04 16:26:53 +00:00
|
|
|
$self->helper(
|
|
|
|
'delete_journey' => sub {
|
2019-04-23 16:08:07 +00:00
|
|
|
my ( $self, $journey_id, $checkin_epoch, $checkout_epoch ) = @_;
|
2019-04-04 16:26:53 +00:00
|
|
|
my $uid = $self->current_user->{id};
|
|
|
|
|
|
|
|
my @journeys = $self->get_user_travels(
|
2019-04-23 16:08:07 +00:00
|
|
|
uid => $uid,
|
|
|
|
journey_id => $journey_id
|
2019-04-04 16:26:53 +00:00
|
|
|
);
|
|
|
|
if ( @journeys == 0 ) {
|
|
|
|
return 'Journey not found';
|
|
|
|
}
|
|
|
|
my $journey = $journeys[0];
|
|
|
|
|
|
|
|
# Double-check (comparing both ID and action epoch) to make sure we
|
|
|
|
# are really deleting the right journey and the user isn't just
|
|
|
|
# playing around with POST requests.
|
2019-04-23 16:08:07 +00:00
|
|
|
if ( $journey->{id} != $journey_id
|
2019-04-04 16:26:53 +00:00
|
|
|
or $journey->{checkin}->epoch != $checkin_epoch
|
|
|
|
or $journey->{checkout}->epoch != $checkout_epoch )
|
|
|
|
{
|
|
|
|
return 'Invalid journey data';
|
|
|
|
}
|
2019-04-17 17:35:38 +00:00
|
|
|
|
|
|
|
my $rows;
|
|
|
|
eval {
|
|
|
|
$rows = $self->pg->db->delete(
|
2019-04-23 16:08:07 +00:00
|
|
|
'journeys',
|
2019-04-17 17:35:38 +00:00
|
|
|
{
|
|
|
|
user_id => $uid,
|
2019-04-23 16:08:07 +00:00
|
|
|
id => $journey_id,
|
2019-04-17 17:35:38 +00:00
|
|
|
}
|
|
|
|
)->rows;
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
2019-04-23 16:08:07 +00:00
|
|
|
$self->app->log->error("Delete($uid, $journey_id): $@");
|
2019-04-17 17:35:38 +00:00
|
|
|
return 'DELETE failed: ' . $@;
|
|
|
|
}
|
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
if ( $rows == 1 ) {
|
|
|
|
$self->invalidate_stats_cache( $journey->{rt_departure} );
|
2019-04-17 17:35:38 +00:00
|
|
|
return undef;
|
2019-04-04 16:26:53 +00:00
|
|
|
}
|
2019-04-23 16:08:07 +00:00
|
|
|
return sprintf( 'Deleted %d rows, expected 1', $rows );
|
2019-04-04 16:26:53 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-04-07 14:55:35 +00:00
|
|
|
$self->helper(
|
|
|
|
'get_journey_stats' => sub {
|
|
|
|
my ( $self, %opt ) = @_;
|
|
|
|
|
|
|
|
if ( $opt{cancelled} ) {
|
|
|
|
$self->app->log->warning(
|
|
|
|
'get_journey_stats called with illegal option cancelled => 1'
|
|
|
|
);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-05-11 13:23:29 +00:00
|
|
|
my $uid = $opt{uid} // $self->current_user->{id};
|
2019-04-07 14:55:35 +00:00
|
|
|
my $year = $opt{year} // 0;
|
|
|
|
my $month = $opt{month} // 0;
|
|
|
|
|
|
|
|
# Assumption: If the stats cache contains an entry it is up-to-date.
|
|
|
|
# -> Cache entries must be explicitly invalidated whenever the user
|
|
|
|
# checks out of a train or manually edits/adds a journey.
|
|
|
|
|
2019-04-22 07:58:39 +00:00
|
|
|
my $res = $self->pg->db->select(
|
|
|
|
'journey_stats',
|
|
|
|
['data'],
|
|
|
|
{
|
|
|
|
user_id => $uid,
|
|
|
|
year => $year,
|
|
|
|
month => $month
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
my $res_h = $res->expand->hash;
|
2019-04-07 14:55:35 +00:00
|
|
|
|
2019-04-22 07:58:39 +00:00
|
|
|
if ($res_h) {
|
|
|
|
$res->finish;
|
|
|
|
return $res_h->{data};
|
2019-04-07 14:55:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
my $interval_start = DateTime->new(
|
|
|
|
time_zone => 'Europe/Berlin',
|
|
|
|
year => 2000,
|
|
|
|
month => 1,
|
|
|
|
day => 1,
|
|
|
|
hour => 0,
|
|
|
|
minute => 0,
|
|
|
|
second => 0,
|
|
|
|
);
|
|
|
|
|
|
|
|
# I wonder if people will still be traveling by train in the year 3000
|
|
|
|
my $interval_end = $interval_start->clone->add( years => 1000 );
|
|
|
|
|
|
|
|
if ( $opt{year} and $opt{month} ) {
|
|
|
|
$interval_start->set(
|
|
|
|
year => $opt{year},
|
|
|
|
month => $opt{month}
|
|
|
|
);
|
|
|
|
$interval_end = $interval_start->clone->add( months => 1 );
|
|
|
|
}
|
|
|
|
elsif ( $opt{year} ) {
|
|
|
|
$interval_start->set( year => $opt{year} );
|
|
|
|
$interval_end = $interval_start->clone->add( years => 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
my @journeys = $self->get_user_travels(
|
2019-05-11 13:23:29 +00:00
|
|
|
uid => $uid,
|
2019-04-07 14:55:35 +00:00
|
|
|
cancelled => $opt{cancelled} ? 1 : 0,
|
|
|
|
verbose => 1,
|
|
|
|
after => $interval_start,
|
|
|
|
before => $interval_end
|
|
|
|
);
|
|
|
|
my $stats = $self->compute_journey_stats(@journeys);
|
|
|
|
|
2019-04-17 11:10:49 +00:00
|
|
|
$self->pg->db->insert(
|
|
|
|
'journey_stats',
|
|
|
|
{
|
|
|
|
user_id => $uid,
|
|
|
|
year => $year,
|
|
|
|
month => $month,
|
|
|
|
data => JSON->new->encode($stats),
|
|
|
|
}
|
|
|
|
);
|
2019-04-07 14:55:35 +00:00
|
|
|
|
|
|
|
return $stats;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-04-27 07:11:42 +00:00
|
|
|
$self->helper(
|
|
|
|
'history_years' => sub {
|
|
|
|
my ( $self, $uid ) = @_;
|
|
|
|
$uid //= $self->current_user->{id},
|
|
|
|
|
|
|
|
my $res = $self->pg->db->select(
|
|
|
|
'journeys',
|
|
|
|
'distinct extract(year from real_departure) as year',
|
|
|
|
{ user_id => $uid },
|
|
|
|
{ order_by => { -asc => 'year' } }
|
|
|
|
);
|
|
|
|
|
|
|
|
my @ret;
|
|
|
|
for my $row ( $res->hashes->each ) {
|
|
|
|
push( @ret, [ $row->{year}, $row->{year} ] );
|
|
|
|
}
|
|
|
|
return @ret;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$self->helper(
|
|
|
|
'history_months' => sub {
|
|
|
|
my ( $self, $uid ) = @_;
|
|
|
|
$uid //= $self->current_user->{id},
|
|
|
|
|
|
|
|
my $res = $self->pg->db->select(
|
|
|
|
'journeys',
|
|
|
|
"distinct to_char(real_departure, 'YYYY.MM') as yearmonth",
|
|
|
|
{ user_id => $uid },
|
|
|
|
{ order_by => { -asc => 'yearmonth' } }
|
|
|
|
);
|
|
|
|
|
|
|
|
my @ret;
|
|
|
|
for my $row ( $res->hashes->each ) {
|
|
|
|
my ( $year, $month ) = split( qr{[.]}, $row->{yearmonth} );
|
|
|
|
push( @ret, [ "${year}/${month}", "${month}.${year}" ] );
|
|
|
|
}
|
|
|
|
return @ret;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-04-26 17:53:01 +00:00
|
|
|
$self->helper(
|
|
|
|
'get_oldest_journey_ts' => sub {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
my $res_h = $self->pg->db->select(
|
|
|
|
'journeys_str',
|
|
|
|
['sched_dep_ts'],
|
|
|
|
{
|
|
|
|
user_id => $self->current_user->{id},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
limit => 1,
|
|
|
|
order_by => {
|
|
|
|
-asc => 'real_dep_ts',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)->hash;
|
|
|
|
|
|
|
|
if ($res_h) {
|
|
|
|
return epoch_to_dt( $res_h->{sched_dep_ts} );
|
|
|
|
}
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-05-19 06:55:23 +00:00
|
|
|
$self->helper(
|
|
|
|
'get_connection_targets' => sub {
|
|
|
|
my ( $self, %opt ) = @_;
|
|
|
|
|
|
|
|
my $uid = $opt{uid} // $self->current_user->{id};
|
|
|
|
my $threshold = $opt{threshold}
|
|
|
|
// DateTime->now( time_zone => 'Europe/Berlin' )
|
|
|
|
->subtract( weeks => 60 );
|
|
|
|
my $db = $opt{db} // $self->pg->db;
|
|
|
|
|
|
|
|
my $journey = $db->select( 'in_transit', ['checkout_station_id'],
|
|
|
|
{ user_id => $uid } )->hash;
|
|
|
|
if ( not $journey ) {
|
|
|
|
$journey = $db->select(
|
|
|
|
'journeys',
|
|
|
|
['checkout_station_id'],
|
|
|
|
{
|
|
|
|
user_id => $uid,
|
|
|
|
cancelled => 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
limit => 1,
|
|
|
|
order_by => { -desc => 'real_dep_ts' }
|
|
|
|
}
|
|
|
|
)->hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( not $journey ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $res = $db->query(
|
|
|
|
qq{
|
|
|
|
select
|
|
|
|
count(stations.name) as count,
|
|
|
|
stations.name as dest
|
|
|
|
from journeys
|
|
|
|
left outer join stations on checkout_station_id = stations.id
|
|
|
|
where user_id = ?
|
|
|
|
and checkin_station_id = ?
|
|
|
|
and real_departure > ?
|
|
|
|
group by stations.name
|
|
|
|
order by count desc;
|
|
|
|
},
|
|
|
|
$uid,
|
|
|
|
$journey->{checkout_station_id},
|
|
|
|
$threshold
|
|
|
|
);
|
|
|
|
my @destinations = $res->hashes->grep( sub { shift->{count} > 2 } )
|
|
|
|
->map( sub { shift->{dest} } )->each;
|
|
|
|
return @destinations;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$self->helper(
|
|
|
|
'get_connecting_trains' => sub {
|
|
|
|
my ( $self, %opt ) = @_;
|
|
|
|
|
|
|
|
my $status = $self->get_user_status;
|
|
|
|
|
|
|
|
if ( not $status->{arr_ds100} ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
my @destinations = $self->get_connection_targets(%opt);
|
|
|
|
my $stationboard
|
|
|
|
= $self->get_departures( $status->{arr_ds100}, 0, 60 );
|
|
|
|
|
|
|
|
@destinations = grep { $_ ne $status->{dep_name} } @destinations;
|
|
|
|
|
|
|
|
if ( $stationboard->{errstr} ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
my @results;
|
|
|
|
my %via_count = map { $_ => 0 } @destinations;
|
|
|
|
for my $train ( @{ $stationboard->{results} } ) {
|
|
|
|
if ( not $train->departure ) {
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
if ( $train->departure->epoch < $status->{real_arrival}->epoch )
|
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
if ( $train->train_id eq $status->{train_id} ) {
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
my @via = ( $train->route_post, $train->route_end );
|
|
|
|
for my $dest (@destinations) {
|
|
|
|
if ( $via_count{$dest} < 3
|
|
|
|
and List::Util::any { $_ eq $dest } @via )
|
|
|
|
{
|
|
|
|
push( @results, [ $train, $dest ] );
|
|
|
|
$via_count{$dest}++;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@results = map { $_->[0] }
|
|
|
|
sort { $a->[1] <=> $b->[1] }
|
|
|
|
map {
|
|
|
|
[
|
|
|
|
$_,
|
|
|
|
$_->[0]->departure->epoch // $_->[0]->sched_departur->epoch
|
|
|
|
]
|
|
|
|
} @results;
|
|
|
|
|
|
|
|
return @results;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
$self->helper(
|
|
|
|
'get_user_travels' => sub {
|
|
|
|
my ( $self, %opt ) = @_;
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-03-27 20:20:59 +00:00
|
|
|
my $uid = $opt{uid} || $self->current_user->{id};
|
2019-04-22 09:11:14 +00:00
|
|
|
|
2019-04-22 10:11:22 +00:00
|
|
|
# If get_user_travels is called from inside a transaction, db
|
|
|
|
# specifies the database handle performing the transaction.
|
|
|
|
# Otherwise, we grab a fresh one.
|
|
|
|
my $db = $opt{db} // $self->pg->db;
|
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
my %where = (
|
|
|
|
user_id => $uid,
|
|
|
|
cancelled => 0
|
|
|
|
);
|
2019-04-22 09:11:14 +00:00
|
|
|
my %order = (
|
|
|
|
order_by => {
|
2019-04-23 16:08:07 +00:00
|
|
|
-desc => 'real_dep_ts',
|
2019-04-22 09:11:14 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
if ( $opt{cancelled} ) {
|
|
|
|
$where{cancelled} = 1;
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
if ( $opt{limit} ) {
|
|
|
|
$order{limit} = $opt{limit};
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
2019-03-27 20:20:59 +00:00
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
if ( $opt{journey_id} ) {
|
|
|
|
$where{journey_id} = $opt{journey_id};
|
|
|
|
delete $where{cancelled};
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
2019-04-23 16:08:07 +00:00
|
|
|
elsif ( $opt{after} and $opt{before} ) {
|
|
|
|
$where{real_dep_ts} = {
|
|
|
|
-between => [ $opt{after}->epoch, $opt{before}->epoch, ] };
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
my @travels;
|
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
my $res = $db->select( 'journeys_str', '*', \%where, \%order );
|
2019-04-22 09:11:14 +00:00
|
|
|
|
|
|
|
for my $entry ( $res->hashes->each ) {
|
2019-03-22 15:57:24 +00:00
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
my $ref = {
|
|
|
|
id => $entry->{journey_id},
|
|
|
|
type => $entry->{train_type},
|
|
|
|
line => $entry->{train_line},
|
|
|
|
no => $entry->{train_no},
|
|
|
|
from_name => $entry->{dep_name},
|
|
|
|
checkin => epoch_to_dt( $entry->{checkin_ts} ),
|
|
|
|
sched_departure => epoch_to_dt( $entry->{sched_dep_ts} ),
|
|
|
|
rt_departure => epoch_to_dt( $entry->{real_dep_ts} ),
|
|
|
|
to_name => $entry->{arr_name},
|
|
|
|
checkout => epoch_to_dt( $entry->{checkout_ts} ),
|
|
|
|
sched_arrival => epoch_to_dt( $entry->{sched_arr_ts} ),
|
|
|
|
rt_arrival => epoch_to_dt( $entry->{real_arr_ts} ),
|
|
|
|
messages => $entry->{messages}
|
|
|
|
? [ split( qr{[|]}, $entry->{messages} ) ]
|
|
|
|
: undef,
|
|
|
|
route => $entry->{route}
|
|
|
|
? [ split( qr{[|]}, $entry->{route} ) ]
|
|
|
|
: undef,
|
|
|
|
edited => $entry->{edited},
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( $opt{verbose} ) {
|
|
|
|
$ref->{cancelled} = $entry->{cancelled};
|
|
|
|
my @parsed_messages;
|
|
|
|
for my $message ( @{ $ref->{messages} // [] } ) {
|
|
|
|
my ( $ts, $msg ) = split( qr{:}, $message );
|
|
|
|
push( @parsed_messages, [ epoch_to_dt($ts), $msg ] );
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
2019-04-23 16:08:07 +00:00
|
|
|
$ref->{messages} = [ reverse @parsed_messages ];
|
|
|
|
$ref->{sched_duration}
|
2019-04-26 17:53:01 +00:00
|
|
|
= $ref->{sched_arrival}->epoch
|
2019-04-23 16:08:07 +00:00
|
|
|
? $ref->{sched_arrival}->epoch
|
|
|
|
- $ref->{sched_departure}->epoch
|
|
|
|
: undef;
|
|
|
|
$ref->{rt_duration}
|
2019-04-26 17:53:01 +00:00
|
|
|
= $ref->{rt_arrival}->epoch
|
2019-04-23 16:08:07 +00:00
|
|
|
? $ref->{rt_arrival}->epoch - $ref->{rt_departure}->epoch
|
|
|
|
: undef;
|
|
|
|
my ( $km, $skip )
|
|
|
|
= $self->get_travel_distance( $ref->{from_name},
|
|
|
|
$ref->{to_name}, $ref->{route} );
|
|
|
|
$ref->{km_route} = $km;
|
|
|
|
$ref->{skip_route} = $skip;
|
|
|
|
( $km, $skip )
|
|
|
|
= $self->get_travel_distance( $ref->{from_name},
|
|
|
|
$ref->{to_name},
|
|
|
|
[ $ref->{from_name}, $ref->{to_name} ] );
|
|
|
|
$ref->{km_beeline} = $km;
|
|
|
|
$ref->{skip_beeline} = $skip;
|
|
|
|
my $kmh_divisor
|
|
|
|
= ( $ref->{rt_duration} // $ref->{sched_duration}
|
|
|
|
// 999999 ) / 3600;
|
|
|
|
$ref->{kmh_route}
|
|
|
|
= $kmh_divisor ? $ref->{km_route} / $kmh_divisor : -1;
|
|
|
|
$ref->{kmh_beeline}
|
|
|
|
= $kmh_divisor
|
|
|
|
? $ref->{km_beeline} / $kmh_divisor
|
|
|
|
: -1;
|
2019-03-22 15:57:24 +00:00
|
|
|
}
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
push( @travels, $ref );
|
2019-03-27 20:20:59 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
return @travels;
|
|
|
|
}
|
|
|
|
);
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-08 19:20:04 +00:00
|
|
|
$self->helper(
|
|
|
|
'get_journey' => sub {
|
|
|
|
my ( $self, %opt ) = @_;
|
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
$opt{cancelled} = 'any';
|
2019-04-08 19:20:04 +00:00
|
|
|
my @journeys = $self->get_user_travels(%opt);
|
2019-04-23 16:08:07 +00:00
|
|
|
if ( @journeys == 0 ) {
|
2019-04-08 19:20:04 +00:00
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $journeys[0];
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
$self->helper(
|
|
|
|
'get_user_status' => sub {
|
|
|
|
my ( $self, $uid ) = @_;
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
$uid //= $self->current_user->{id};
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
my $db = $self->pg->db;
|
|
|
|
my $now = DateTime->now( time_zone => 'Europe/Berlin' );
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
my $in_transit
|
|
|
|
= $db->select( 'in_transit_str', '*', { user_id => $uid } )->hash;
|
2019-03-22 15:57:24 +00:00
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
if ($in_transit) {
|
2019-04-22 09:11:14 +00:00
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
my @route = split( qr{[|]}, $in_transit->{route} // q{} );
|
2019-03-22 15:57:24 +00:00
|
|
|
my @route_after;
|
|
|
|
my $is_after = 0;
|
|
|
|
for my $station (@route) {
|
|
|
|
|
|
|
|
if ($is_after) {
|
|
|
|
push( @route_after, $station );
|
|
|
|
}
|
2019-05-03 08:52:30 +00:00
|
|
|
if ( $station eq $in_transit->{dep_name} ) {
|
|
|
|
$is_after = 1;
|
|
|
|
}
|
2019-03-21 21:12:45 +00:00
|
|
|
}
|
2019-04-23 16:08:07 +00:00
|
|
|
|
|
|
|
my $ts = $in_transit->{checkout_ts}
|
|
|
|
// $in_transit->{checkin_ts};
|
|
|
|
my $action_time = epoch_to_dt($ts);
|
|
|
|
|
2019-04-23 19:30:31 +00:00
|
|
|
my $ret = {
|
2019-04-23 16:08:07 +00:00
|
|
|
checked_in => !$in_transit->{cancelled},
|
|
|
|
cancelled => $in_transit->{cancelled},
|
|
|
|
timestamp => $action_time,
|
|
|
|
timestamp_delta => $now->epoch - $action_time->epoch,
|
|
|
|
train_type => $in_transit->{train_type},
|
|
|
|
train_line => $in_transit->{train_line},
|
|
|
|
train_no => $in_transit->{train_no},
|
|
|
|
train_id => $in_transit->{train_id},
|
|
|
|
sched_departure =>
|
|
|
|
epoch_to_dt( $in_transit->{sched_dep_ts} ),
|
|
|
|
real_departure => epoch_to_dt( $in_transit->{real_dep_ts} ),
|
|
|
|
dep_ds100 => $in_transit->{dep_ds100},
|
|
|
|
dep_name => $in_transit->{dep_name},
|
2019-05-18 15:10:53 +00:00
|
|
|
dep_platform => $in_transit->{dep_platform},
|
2019-04-23 16:08:07 +00:00
|
|
|
sched_arrival => epoch_to_dt( $in_transit->{sched_arr_ts} ),
|
|
|
|
real_arrival => epoch_to_dt( $in_transit->{real_arr_ts} ),
|
|
|
|
arr_ds100 => $in_transit->{arr_ds100},
|
|
|
|
arr_name => $in_transit->{arr_name},
|
2019-05-18 15:10:53 +00:00
|
|
|
arr_platform => $in_transit->{arr_platform},
|
2019-04-23 16:08:07 +00:00
|
|
|
route_after => \@route_after,
|
2019-04-24 12:58:08 +00:00
|
|
|
messages => $in_transit->{messages}
|
|
|
|
? [ split( qr{[|]}, $in_transit->{messages} ) ]
|
|
|
|
: undef,
|
2019-03-22 15:57:24 +00:00
|
|
|
};
|
2019-04-23 19:30:31 +00:00
|
|
|
|
2019-04-24 12:58:08 +00:00
|
|
|
my @parsed_messages;
|
|
|
|
for my $message ( @{ $ret->{messages} // [] } ) {
|
|
|
|
my ( $ts, $msg ) = split( qr{:}, $message );
|
|
|
|
push( @parsed_messages, [ epoch_to_dt($ts), $msg ] );
|
|
|
|
}
|
|
|
|
$ret->{messages} = [ reverse @parsed_messages ];
|
|
|
|
|
2019-04-23 19:30:31 +00:00
|
|
|
$ret->{departure_countdown}
|
|
|
|
= $ret->{real_departure}->epoch - $now->epoch;
|
|
|
|
if ( $in_transit->{real_arr_ts} ) {
|
|
|
|
$ret->{arrival_countdown}
|
|
|
|
= $ret->{real_arrival}->epoch - $now->epoch;
|
|
|
|
$ret->{journey_duration} = $ret->{real_arrival}->epoch
|
|
|
|
- $ret->{real_departure}->epoch;
|
2019-05-08 05:12:54 +00:00
|
|
|
$ret->{journey_completion}
|
|
|
|
= $ret->{journey_duration}
|
|
|
|
? 1
|
|
|
|
- ( $ret->{arrival_countdown} / $ret->{journey_duration} )
|
|
|
|
: 1;
|
2019-04-23 19:30:31 +00:00
|
|
|
if ( $ret->{journey_completion} > 1 ) {
|
|
|
|
$ret->{journey_completion} = 1;
|
|
|
|
}
|
|
|
|
elsif ( $ret->{journey_completion} < 0 ) {
|
|
|
|
$ret->{journey_completion} = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$ret->{arrival_countdown} = undef;
|
|
|
|
$ret->{journey_duration} = undef;
|
|
|
|
$ret->{journey_completion} = undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ret;
|
2019-03-21 21:12:45 +00:00
|
|
|
}
|
2019-04-23 16:08:07 +00:00
|
|
|
|
|
|
|
my $latest = $db->select(
|
|
|
|
'journeys_str',
|
|
|
|
'*',
|
|
|
|
{
|
|
|
|
user_id => $uid,
|
|
|
|
cancelled => 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
order_by => { -desc => 'journey_id' },
|
|
|
|
limit => 1
|
|
|
|
}
|
|
|
|
)->hash;
|
|
|
|
|
|
|
|
if ($latest) {
|
|
|
|
my $ts = $latest->{checkout_ts};
|
|
|
|
my $action_time = epoch_to_dt($ts);
|
|
|
|
return {
|
|
|
|
checked_in => 0,
|
|
|
|
cancelled => 0,
|
|
|
|
journey_id => $latest->{journey_id},
|
|
|
|
timestamp => $action_time,
|
|
|
|
timestamp_delta => $now->epoch - $action_time->epoch,
|
|
|
|
train_type => $latest->{train_type},
|
|
|
|
train_line => $latest->{train_line},
|
|
|
|
train_no => $latest->{train_no},
|
|
|
|
train_id => $latest->{train_id},
|
|
|
|
sched_departure => epoch_to_dt( $latest->{sched_dep_ts} ),
|
|
|
|
real_departure => epoch_to_dt( $latest->{real_dep_ts} ),
|
|
|
|
dep_ds100 => $latest->{dep_ds100},
|
|
|
|
dep_name => $latest->{dep_name},
|
2019-05-18 15:10:53 +00:00
|
|
|
dep_platform => $latest->{dep_platform},
|
2019-04-23 16:08:07 +00:00
|
|
|
sched_arrival => epoch_to_dt( $latest->{sched_arr_ts} ),
|
|
|
|
real_arrival => epoch_to_dt( $latest->{real_arr_ts} ),
|
|
|
|
arr_ds100 => $latest->{arr_ds100},
|
|
|
|
arr_name => $latest->{arr_name},
|
2019-05-18 15:10:53 +00:00
|
|
|
arr_platform => $latest->{arr_platform},
|
2019-04-23 16:08:07 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
return {
|
2019-04-23 16:08:07 +00:00
|
|
|
checked_in => 0,
|
|
|
|
cancelled => 0,
|
|
|
|
no_journeys_yet => 1,
|
|
|
|
timestamp => epoch_to_dt(0),
|
|
|
|
timestamp_delta => $now->epoch,
|
2019-03-22 15:57:24 +00:00
|
|
|
};
|
2019-03-21 21:12:45 +00:00
|
|
|
}
|
2019-03-22 15:57:24 +00:00
|
|
|
);
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-05-06 14:59:48 +00:00
|
|
|
$self->helper(
|
|
|
|
'get_user_status_json_v1' => sub {
|
|
|
|
my ( $self, $uid ) = @_;
|
|
|
|
my $status = $self->get_user_status($uid);
|
|
|
|
|
|
|
|
my $ret = {
|
|
|
|
deprecated => \0,
|
|
|
|
checkedIn => (
|
|
|
|
$status->{checked_in}
|
|
|
|
or $status->{cancelled}
|
|
|
|
) ? \1 : \0,
|
|
|
|
fromStation => {
|
|
|
|
ds100 => $status->{dep_ds100},
|
|
|
|
name => $status->{dep_name},
|
|
|
|
uic => undef,
|
|
|
|
longitude => undef,
|
|
|
|
latitude => undef,
|
|
|
|
scheduledTime => $status->{sched_departure}->epoch || undef,
|
|
|
|
realTime => $status->{real_departure}->epoch || undef,
|
|
|
|
},
|
|
|
|
toStation => {
|
|
|
|
ds100 => $status->{arr_ds100},
|
|
|
|
name => $status->{arr_name},
|
|
|
|
uic => undef,
|
|
|
|
longitude => undef,
|
|
|
|
latitude => undef,
|
|
|
|
scheduledTime => $status->{sched_arrival}->epoch || undef,
|
|
|
|
realTime => $status->{real_arrival}->epoch || undef,
|
|
|
|
},
|
|
|
|
train => {
|
|
|
|
type => $status->{train_type},
|
|
|
|
line => $status->{train_line},
|
|
|
|
no => $status->{train_no},
|
|
|
|
id => $status->{train_id},
|
|
|
|
},
|
|
|
|
actionTime => $status->{timestamp}->epoch,
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( $status->{dep_ds100} ) {
|
|
|
|
my @station_descriptions
|
|
|
|
= Travel::Status::DE::IRIS::Stations::get_station(
|
|
|
|
$status->{dep_ds100} );
|
|
|
|
if ( @station_descriptions == 1 ) {
|
|
|
|
(
|
|
|
|
undef, undef,
|
|
|
|
$ret->{fromStation}{uic},
|
|
|
|
$ret->{fromStation}{longitude},
|
|
|
|
$ret->{fromStation}{latitude}
|
|
|
|
) = @{ $station_descriptions[0] };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $status->{arr_ds100} ) {
|
|
|
|
my @station_descriptions
|
|
|
|
= Travel::Status::DE::IRIS::Stations::get_station(
|
|
|
|
$status->{arr_ds100} );
|
|
|
|
if ( @station_descriptions == 1 ) {
|
|
|
|
(
|
|
|
|
undef, undef,
|
|
|
|
$ret->{toStation}{uic},
|
|
|
|
$ret->{toStation}{longitude},
|
|
|
|
$ret->{toStation}{latitude}
|
|
|
|
) = @{ $station_descriptions[0] };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
$self->helper(
|
|
|
|
'get_travel_distance' => sub {
|
|
|
|
my ( $self, $from, $to, $route_ref ) = @_;
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
my $distance = 0;
|
2019-04-21 13:46:19 +00:00
|
|
|
my $skipped = 0;
|
2019-03-22 15:57:24 +00:00
|
|
|
my $geo = Geo::Distance->new();
|
|
|
|
my @route = after_incl { $_ eq $from } @{$route_ref};
|
|
|
|
@route = before_incl { $_ eq $to } @route;
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
if ( @route < 2 ) {
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
# I AM ERROR
|
|
|
|
return 0;
|
|
|
|
}
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
my $prev_station = get_station( shift @route );
|
|
|
|
if ( not $prev_station ) {
|
|
|
|
return 0;
|
2019-03-21 21:12:45 +00:00
|
|
|
}
|
2019-03-22 15:57:24 +00:00
|
|
|
|
|
|
|
for my $station_name (@route) {
|
|
|
|
if ( my $station = get_station($station_name) ) {
|
2019-04-21 13:46:19 +00:00
|
|
|
if ( $#{$prev_station} >= 4 and $#{$station} >= 4 ) {
|
|
|
|
$distance
|
|
|
|
+= $geo->distance( 'kilometer', $prev_station->[3],
|
|
|
|
$prev_station->[4], $station->[3], $station->[4] );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$skipped++;
|
|
|
|
}
|
2019-03-22 15:57:24 +00:00
|
|
|
$prev_station = $station;
|
|
|
|
}
|
2019-03-21 21:12:45 +00:00
|
|
|
}
|
2019-03-22 15:57:24 +00:00
|
|
|
|
2019-04-21 13:46:19 +00:00
|
|
|
return ( $distance, $skipped );
|
2019-03-21 21:12:45 +00:00
|
|
|
}
|
2019-03-22 15:57:24 +00:00
|
|
|
);
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-03-27 20:20:59 +00:00
|
|
|
$self->helper(
|
|
|
|
'compute_journey_stats' => sub {
|
|
|
|
my ( $self, @journeys ) = @_;
|
|
|
|
my $km_route = 0;
|
|
|
|
my $km_beeline = 0;
|
|
|
|
my $min_travel_sched = 0;
|
|
|
|
my $min_travel_real = 0;
|
|
|
|
my $delay_dep = 0;
|
|
|
|
my $delay_arr = 0;
|
2019-04-01 18:28:06 +00:00
|
|
|
my $interchange_real = 0;
|
2019-03-27 20:20:59 +00:00
|
|
|
my $num_trains = 0;
|
|
|
|
my $num_journeys = 0;
|
2019-04-28 20:33:09 +00:00
|
|
|
my @inconsistencies;
|
2019-03-27 20:20:59 +00:00
|
|
|
|
2019-04-28 20:33:09 +00:00
|
|
|
my $next_departure = epoch_to_dt(0);
|
2019-04-01 18:28:06 +00:00
|
|
|
|
2019-03-27 20:20:59 +00:00
|
|
|
for my $journey (@journeys) {
|
|
|
|
$num_trains++;
|
|
|
|
$km_route += $journey->{km_route};
|
|
|
|
$km_beeline += $journey->{km_beeline};
|
2019-05-11 13:23:29 +00:00
|
|
|
if ( $journey->{sched_duration}
|
|
|
|
and $journey->{sched_duration} > 0 )
|
|
|
|
{
|
2019-03-27 20:20:59 +00:00
|
|
|
$min_travel_sched += $journey->{sched_duration} / 60;
|
|
|
|
}
|
2019-05-11 13:23:29 +00:00
|
|
|
if ( $journey->{rt_duration} and $journey->{rt_duration} > 0 ) {
|
2019-03-27 20:20:59 +00:00
|
|
|
$min_travel_real += $journey->{rt_duration} / 60;
|
|
|
|
}
|
|
|
|
if ( $journey->{sched_departure} and $journey->{rt_departure} )
|
|
|
|
{
|
|
|
|
$delay_dep
|
|
|
|
+= ( $journey->{rt_departure}->epoch
|
|
|
|
- $journey->{sched_departure}->epoch ) / 60;
|
|
|
|
}
|
|
|
|
if ( $journey->{sched_arrival} and $journey->{rt_arrival} ) {
|
|
|
|
$delay_arr
|
|
|
|
+= ( $journey->{rt_arrival}->epoch
|
|
|
|
- $journey->{sched_arrival}->epoch ) / 60;
|
|
|
|
}
|
2019-04-01 18:28:06 +00:00
|
|
|
|
|
|
|
# Note that journeys are sorted from recent to older entries
|
|
|
|
if ( $journey->{rt_arrival}
|
2019-04-28 20:33:09 +00:00
|
|
|
and $next_departure->epoch
|
|
|
|
and $next_departure->epoch - $journey->{rt_arrival}->epoch
|
2019-04-01 18:28:06 +00:00
|
|
|
< ( 60 * 60 ) )
|
|
|
|
{
|
2019-04-28 20:33:09 +00:00
|
|
|
if ( $next_departure->epoch - $journey->{rt_arrival}->epoch
|
|
|
|
< 0 )
|
|
|
|
{
|
|
|
|
push( @inconsistencies,
|
|
|
|
$next_departure->strftime('%d.%m.%Y %H:%M') );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$interchange_real
|
|
|
|
+= ( $next_departure->epoch
|
|
|
|
- $journey->{rt_arrival}->epoch )
|
|
|
|
/ 60;
|
|
|
|
}
|
2019-04-01 18:28:06 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$num_journeys++;
|
|
|
|
}
|
2019-04-28 20:33:09 +00:00
|
|
|
$next_departure = $journey->{rt_departure};
|
2019-03-27 20:20:59 +00:00
|
|
|
}
|
|
|
|
return {
|
2019-04-01 18:28:06 +00:00
|
|
|
km_route => $km_route,
|
|
|
|
km_beeline => $km_beeline,
|
|
|
|
num_trains => $num_trains,
|
|
|
|
num_journeys => $num_journeys,
|
|
|
|
min_travel_sched => $min_travel_sched,
|
|
|
|
min_travel_real => $min_travel_real,
|
|
|
|
min_interchange_real => $interchange_real,
|
|
|
|
delay_dep => $delay_dep,
|
|
|
|
delay_arr => $delay_arr,
|
2019-04-28 20:33:09 +00:00
|
|
|
inconsistencies => \@inconsistencies,
|
2019-03-27 20:20:59 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
$self->helper(
|
|
|
|
'navbar_class' => sub {
|
|
|
|
my ( $self, $path ) = @_;
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
if ( $self->req->url eq $self->url_for($path) ) {
|
|
|
|
return 'active';
|
|
|
|
}
|
|
|
|
return q{};
|
2019-03-21 21:12:45 +00:00
|
|
|
}
|
2019-03-22 15:57:24 +00:00
|
|
|
);
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
my $r = $self->routes;
|
|
|
|
|
|
|
|
$r->get('/')->to('traveling#homepage');
|
|
|
|
$r->get('/about')->to('static#about');
|
2019-05-02 09:29:43 +00:00
|
|
|
$r->get('/api')->to('api#documentation');
|
2019-05-03 08:32:58 +00:00
|
|
|
$r->get('/changelog')->to('static#changelog');
|
2019-03-22 15:57:24 +00:00
|
|
|
$r->get('/impressum')->to('static#imprint');
|
|
|
|
$r->get('/imprint')->to('static#imprint');
|
2019-05-03 18:43:46 +00:00
|
|
|
$r->get('/offline')->to('static#offline');
|
2019-03-22 15:57:24 +00:00
|
|
|
$r->get('/api/v0/:user_action/:token')->to('api#get_v0');
|
2019-04-24 05:34:41 +00:00
|
|
|
$r->get('/api/v1/:user_action/:token')->to('api#get_v1');
|
2019-03-22 15:57:24 +00:00
|
|
|
$r->get('/login')->to('account#login_form');
|
2019-04-29 18:12:59 +00:00
|
|
|
$r->get('/recover')->to('account#request_password_reset');
|
|
|
|
$r->get('/recover/:id/:token')->to('account#recover_password');
|
2019-03-22 15:57:24 +00:00
|
|
|
$r->get('/register')->to('account#registration_form');
|
|
|
|
$r->get('/reg/:id/:token')->to('account#verify');
|
2019-04-30 21:23:49 +00:00
|
|
|
$r->get('/status/:name')->to('traveling#user_status');
|
2019-05-02 08:05:15 +00:00
|
|
|
$r->get('/ajax/status/:name')->to('traveling#public_status_card');
|
2019-03-22 15:57:24 +00:00
|
|
|
$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');
|
2019-04-29 18:12:59 +00:00
|
|
|
$r->post('/recover')->to('account#request_password_reset');
|
2019-03-22 15:57:24 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
);
|
2019-03-21 21:12:45 +00:00
|
|
|
|
2019-03-22 15:57:24 +00:00
|
|
|
$authed_r->get('/account')->to('account#account');
|
2019-04-30 21:23:49 +00:00
|
|
|
$authed_r->get('/account/privacy')->to('account#privacy');
|
2019-05-05 16:09:11 +00:00
|
|
|
$authed_r->get('/account/hooks')->to('account#webhook');
|
2019-04-23 20:27:13 +00:00
|
|
|
$authed_r->get('/ajax/status_card.html')->to('traveling#status_card');
|
2019-04-07 16:44:33 +00:00
|
|
|
$authed_r->get('/cancelled')->to('traveling#cancelled');
|
2019-05-03 16:57:24 +00:00
|
|
|
$authed_r->get('/account/password')->to('account#password_form');
|
|
|
|
$authed_r->get('/account/mail')->to('account#change_mail');
|
2019-03-22 15:57:24 +00:00
|
|
|
$authed_r->get('/export.json')->to('account#json_export');
|
2019-04-07 16:44:33 +00:00
|
|
|
$authed_r->get('/history.json')->to('traveling#json_history');
|
2019-03-22 15:57:24 +00:00
|
|
|
$authed_r->get('/history')->to('traveling#history');
|
2019-04-07 16:44:33 +00:00
|
|
|
$authed_r->get('/history/:year')->to('traveling#yearly_history');
|
2019-03-27 20:20:59 +00:00
|
|
|
$authed_r->get('/history/:year/:month')->to('traveling#monthly_history');
|
2019-04-07 18:20:37 +00:00
|
|
|
$authed_r->get('/journey/add')->to('traveling#add_journey_form');
|
2019-03-22 15:57:24 +00:00
|
|
|
$authed_r->get('/journey/:id')->to('traveling#journey_details');
|
|
|
|
$authed_r->get('/s/*station')->to('traveling#station');
|
2019-04-30 10:08:51 +00:00
|
|
|
$authed_r->get('/confirm_mail/:token')->to('account#confirm_mail');
|
2019-04-30 21:23:49 +00:00
|
|
|
$authed_r->post('/account/privacy')->to('account#privacy');
|
2019-05-05 16:09:11 +00:00
|
|
|
$authed_r->post('/account/hooks')->to('account#webhook');
|
2019-04-13 08:43:05 +00:00
|
|
|
$authed_r->post('/journey/add')->to('traveling#add_journey_form');
|
2019-04-07 18:20:37 +00:00
|
|
|
$authed_r->post('/journey/edit')->to('traveling#edit_journey');
|
2019-05-03 16:57:24 +00:00
|
|
|
$authed_r->post('/account/password')->to('account#change_password');
|
|
|
|
$authed_r->post('/account/mail')->to('account#change_mail');
|
2019-03-22 15:57:24 +00:00
|
|
|
$authed_r->post('/delete')->to('account#delete');
|
|
|
|
$authed_r->post('/logout')->to('account#do_logout');
|
|
|
|
$authed_r->post('/set_token')->to('api#set_token');
|
2019-03-21 21:12:45 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|