2019-03-21 21:12:45 +00:00
|
|
|
package Travelynx::Controller::Api;
|
2020-11-28 20:03:51 +00:00
|
|
|
|
2023-07-03 15:59:25 +00:00
|
|
|
# Copyright (C) 2020-2023 Birte Kristina Friesel
|
2020-11-27 21:12:56 +00:00
|
|
|
#
|
2021-01-29 17:32:13 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2019-03-21 21:12:45 +00:00
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
|
|
|
2019-12-14 19:46:02 +00:00
|
|
|
use DateTime;
|
2019-12-14 20:53:23 +00:00
|
|
|
use List::Util;
|
2019-03-21 21:12:45 +00:00
|
|
|
use UUID::Tiny qw(:std);
|
|
|
|
|
2022-07-30 08:42:21 +00:00
|
|
|
# Internal Helpers
|
|
|
|
|
2019-03-21 21:12:45 +00:00
|
|
|
sub make_token {
|
|
|
|
return create_uuid_as_string(UUID_V4);
|
|
|
|
}
|
|
|
|
|
2019-12-14 19:46:02 +00:00
|
|
|
sub sanitize {
|
|
|
|
my ( $type, $value ) = @_;
|
|
|
|
if ( not defined $value ) {
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
if ( $type eq '' ) {
|
|
|
|
return '' . $value;
|
|
|
|
}
|
2020-01-26 09:48:41 +00:00
|
|
|
if ( $value =~ m{ ^ [0-9.e]+ $ }x ) {
|
|
|
|
return 0 + $value;
|
|
|
|
}
|
|
|
|
return 0;
|
2019-12-14 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
2022-07-30 08:42:21 +00:00
|
|
|
# Contollers
|
|
|
|
|
2019-05-02 09:29:43 +00:00
|
|
|
sub documentation {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
2023-01-21 07:36:25 +00:00
|
|
|
if ( $self->is_user_authenticated ) {
|
2023-01-22 10:34:53 +00:00
|
|
|
my $uid = $self->current_user->{id};
|
2023-01-21 07:36:25 +00:00
|
|
|
$self->render(
|
|
|
|
'api_documentation',
|
2023-01-22 10:34:53 +00:00
|
|
|
uid => $uid,
|
|
|
|
api_token => $self->users->get_api_token( uid => $uid ),
|
2023-01-21 07:36:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->render('api_documentation');
|
|
|
|
}
|
2019-05-02 09:29:43 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 05:34:41 +00:00
|
|
|
sub get_v1 {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
my $api_action = $self->stash('user_action');
|
|
|
|
my $api_token = $self->stash('token');
|
|
|
|
if ( $api_action !~ qr{ ^ (?: status | history | action ) $ }x ) {
|
|
|
|
$self->render(
|
|
|
|
json => {
|
|
|
|
error => 'Invalid action',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( $api_token !~ qr{ ^ (?<id> \d+ ) - (?<token> .* ) $ }x ) {
|
|
|
|
$self->render(
|
|
|
|
json => {
|
|
|
|
error => 'Malformed token',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
my $uid = $+{id};
|
|
|
|
$api_token = $+{token};
|
2019-05-02 09:34:52 +00:00
|
|
|
|
|
|
|
if ( $uid > 2147483647 ) {
|
|
|
|
$self->render(
|
|
|
|
json => {
|
|
|
|
error => 'Malformed token',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-22 10:34:53 +00:00
|
|
|
my $token = $self->users->get_api_token( uid => $uid );
|
2023-01-03 09:12:53 +00:00
|
|
|
if ( not $api_token
|
|
|
|
or not $token->{$api_action}
|
|
|
|
or $api_token ne $token->{$api_action} )
|
|
|
|
{
|
2019-04-24 05:34:41 +00:00
|
|
|
$self->render(
|
|
|
|
json => {
|
|
|
|
error => 'Invalid token',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( $api_action eq 'status' ) {
|
2023-05-21 17:44:43 +00:00
|
|
|
$self->render( json => $self->get_user_status_json_v1( uid => $uid ) );
|
2019-04-24 05:34:41 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->render(
|
|
|
|
json => {
|
|
|
|
error => 'not implemented',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 20:53:23 +00:00
|
|
|
sub travel_v1 {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
2019-12-17 19:41:36 +00:00
|
|
|
my $payload = $self->req->json;
|
|
|
|
|
|
|
|
if ( not $payload or ref($payload) ne 'HASH' ) {
|
|
|
|
$self->render(
|
|
|
|
json => {
|
2019-12-20 15:23:41 +00:00
|
|
|
success => \0,
|
|
|
|
deprecated => \0,
|
|
|
|
error => 'Malformed JSON',
|
2019-12-17 19:41:36 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-14 20:53:23 +00:00
|
|
|
my $api_token = $payload->{token} // '';
|
|
|
|
|
|
|
|
if ( $api_token !~ qr{ ^ (?<id> \d+ ) - (?<token> .* ) $ }x ) {
|
|
|
|
$self->render(
|
|
|
|
json => {
|
2019-12-20 15:23:41 +00:00
|
|
|
success => \0,
|
|
|
|
deprecated => \0,
|
|
|
|
error => 'Malformed token',
|
2019-12-14 20:53:23 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
my $uid = $+{id};
|
|
|
|
$api_token = $+{token};
|
|
|
|
|
|
|
|
if ( $uid > 2147483647 ) {
|
|
|
|
$self->render(
|
|
|
|
json => {
|
2019-12-20 15:23:41 +00:00
|
|
|
success => \0,
|
|
|
|
deprecated => \0,
|
|
|
|
error => 'Malformed token',
|
2019-12-14 20:53:23 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-22 10:34:53 +00:00
|
|
|
my $token = $self->users->get_api_token( uid => $uid );
|
2019-12-20 15:39:15 +00:00
|
|
|
if ( not $token->{'travel'} or $api_token ne $token->{'travel'} ) {
|
2019-12-14 20:53:23 +00:00
|
|
|
$self->render(
|
|
|
|
json => {
|
2019-12-20 15:23:41 +00:00
|
|
|
success => \0,
|
|
|
|
deprecated => \0,
|
|
|
|
error => 'Invalid token',
|
2019-12-14 20:53:23 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( not exists $payload->{action}
|
|
|
|
or $payload->{action} !~ m{^(checkin|checkout|undo)$} )
|
|
|
|
{
|
|
|
|
$self->render(
|
|
|
|
json => {
|
2019-12-20 15:23:41 +00:00
|
|
|
success => \0,
|
|
|
|
deprecated => \0,
|
|
|
|
error => 'Missing or invalid action',
|
2023-05-21 17:44:43 +00:00
|
|
|
status => $self->get_user_status_json_v1( uid => $uid )
|
2019-12-14 20:53:23 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $payload->{action} eq 'checkin' ) {
|
|
|
|
my $from_station = sanitize( q{}, $payload->{fromStation} );
|
|
|
|
my $to_station = sanitize( q{}, $payload->{toStation} );
|
|
|
|
my $train_id;
|
2023-08-19 10:43:43 +00:00
|
|
|
my $hafas = exists $payload->{train}{journeyID} ? 1 : 0;
|
2019-12-14 20:53:23 +00:00
|
|
|
|
2019-12-20 15:39:15 +00:00
|
|
|
if (
|
|
|
|
not(
|
|
|
|
$from_station
|
2023-08-19 10:43:43 +00:00
|
|
|
and ( ( $payload->{train}{type} and $payload->{train}{no} )
|
|
|
|
or $payload->{train}{id}
|
|
|
|
or $payload->{train}{journeyID} )
|
2019-12-20 15:39:15 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
$self->render(
|
|
|
|
json => {
|
|
|
|
success => \0,
|
|
|
|
deprecated => \0,
|
|
|
|
error => 'Missing fromStation or train data',
|
2023-05-21 17:44:43 +00:00
|
|
|
status => $self->get_user_status_json_v1( uid => $uid )
|
2019-12-20 15:39:15 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-19 10:43:43 +00:00
|
|
|
if ( not $hafas and not $self->stations->search($from_station) ) {
|
2019-12-30 00:31:26 +00:00
|
|
|
$self->render(
|
|
|
|
json => {
|
|
|
|
success => \0,
|
|
|
|
deprecated => \0,
|
2022-12-07 18:42:48 +00:00
|
|
|
error => 'Unknown fromStation',
|
2023-05-21 17:44:43 +00:00
|
|
|
status => $self->get_user_status_json_v1( uid => $uid )
|
2019-12-30 00:31:26 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-19 10:43:43 +00:00
|
|
|
if ( $to_station
|
|
|
|
and not $hafas
|
|
|
|
and not $self->stations->search($to_station) )
|
|
|
|
{
|
2019-12-30 00:31:26 +00:00
|
|
|
$self->render(
|
|
|
|
json => {
|
|
|
|
success => \0,
|
|
|
|
deprecated => \0,
|
2022-12-07 18:42:48 +00:00
|
|
|
error => 'Unknown toStation',
|
2023-05-21 17:44:43 +00:00
|
|
|
status => $self->get_user_status_json_v1( uid => $uid )
|
2019-12-30 00:31:26 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-09 17:23:11 +00:00
|
|
|
my $train_p;
|
|
|
|
|
2023-08-19 10:43:43 +00:00
|
|
|
if ( exists $payload->{train}{journeyID} ) {
|
|
|
|
$train_p = Mojo::Promise->resolve(
|
|
|
|
sanitize( q{}, $payload->{train}{journeyID} ) );
|
|
|
|
}
|
|
|
|
elsif ( exists $payload->{train}{id} ) {
|
2023-07-09 17:23:11 +00:00
|
|
|
$train_p
|
|
|
|
= Mojo::Promise->resolve( sanitize( 0, $payload->{train}{id} ) );
|
2019-12-14 20:53:23 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
my $train_type = sanitize( q{}, $payload->{train}{type} );
|
|
|
|
my $train_no = sanitize( q{}, $payload->{train}{no} );
|
2023-07-09 17:23:11 +00:00
|
|
|
|
|
|
|
$train_p = $self->iris->get_departures_p(
|
2020-09-05 19:51:52 +00:00
|
|
|
station => $from_station,
|
|
|
|
lookbehind => 140,
|
|
|
|
lookahead => 40
|
2023-07-09 17:23:11 +00:00
|
|
|
)->then(
|
|
|
|
sub {
|
|
|
|
my ($status) = @_;
|
|
|
|
if ( $status->{errstr} ) {
|
|
|
|
return Mojo::Promise->reject(
|
|
|
|
'Error requesting departures from fromStation: '
|
|
|
|
. $status->{errstr} );
|
2019-12-14 20:53:23 +00:00
|
|
|
}
|
2023-07-09 17:23:11 +00:00
|
|
|
my ($train) = List::Util::first {
|
|
|
|
$_->type eq $train_type and $_->train_no eq $train_no
|
2019-12-14 20:53:23 +00:00
|
|
|
}
|
2023-07-09 17:23:11 +00:00
|
|
|
@{ $status->{results} };
|
|
|
|
if ( not defined $train ) {
|
|
|
|
return Mojo::Promise->reject(
|
|
|
|
'Train not found at fromStation');
|
|
|
|
}
|
|
|
|
return Mojo::Promise->resolve( $train->train_id );
|
|
|
|
}
|
|
|
|
);
|
2019-12-14 20:53:23 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 12:46:28 +00:00
|
|
|
$self->render_later;
|
|
|
|
|
2023-07-09 17:23:11 +00:00
|
|
|
$train_p->then(
|
|
|
|
sub {
|
|
|
|
my ($train_id) = @_;
|
|
|
|
return $self->checkin_p(
|
|
|
|
station => $from_station,
|
|
|
|
train_id => $train_id,
|
|
|
|
uid => $uid
|
|
|
|
);
|
|
|
|
}
|
2023-07-09 12:46:28 +00:00
|
|
|
)->then(
|
|
|
|
sub {
|
|
|
|
my ($train) = @_;
|
|
|
|
if ( $payload->{comment} ) {
|
|
|
|
$self->in_transit->update_user_data(
|
|
|
|
uid => $uid,
|
|
|
|
user_data =>
|
|
|
|
{ comment => sanitize( q{}, $payload->{comment} ) }
|
|
|
|
);
|
2019-12-14 20:53:23 +00:00
|
|
|
}
|
2023-07-09 12:46:28 +00:00
|
|
|
if ($to_station) {
|
2023-07-23 10:21:37 +00:00
|
|
|
|
|
|
|
# the user may not have provided the correct to_station, so
|
|
|
|
# request related stations for checkout.
|
2023-08-19 08:06:15 +00:00
|
|
|
return $self->checkout_p(
|
2023-07-23 10:21:37 +00:00
|
|
|
station => $to_station,
|
|
|
|
force => 0,
|
|
|
|
uid => $uid,
|
|
|
|
with_related => 1,
|
2023-07-09 12:46:28 +00:00
|
|
|
);
|
2023-08-19 08:06:15 +00:00
|
|
|
}
|
|
|
|
return Mojo::Promise->resolve;
|
|
|
|
}
|
|
|
|
)->then(
|
|
|
|
sub {
|
|
|
|
my ( undef, $error ) = @_;
|
|
|
|
if ($error) {
|
|
|
|
return Mojo::Promise->reject($error);
|
2019-12-14 20:53:23 +00:00
|
|
|
}
|
2023-07-09 12:46:28 +00:00
|
|
|
$self->render(
|
|
|
|
json => {
|
|
|
|
success => \1,
|
|
|
|
deprecated => \0,
|
|
|
|
status => $self->get_user_status_json_v1( uid => $uid )
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
)->catch(
|
|
|
|
sub {
|
|
|
|
my ($error) = @_;
|
|
|
|
$self->render(
|
|
|
|
json => {
|
|
|
|
success => \0,
|
|
|
|
deprecated => \0,
|
|
|
|
error => 'Checkin/Checkout error: ' . $error,
|
|
|
|
status => $self->get_user_status_json_v1( uid => $uid )
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
)->wait;
|
2019-12-14 20:53:23 +00:00
|
|
|
}
|
|
|
|
elsif ( $payload->{action} eq 'checkout' ) {
|
|
|
|
my $to_station = sanitize( q{}, $payload->{toStation} );
|
|
|
|
|
2019-12-20 15:39:15 +00:00
|
|
|
if ( not $to_station ) {
|
|
|
|
$self->render(
|
|
|
|
json => {
|
|
|
|
success => \0,
|
|
|
|
deprecated => \0,
|
|
|
|
error => 'Missing toStation',
|
2023-05-21 17:44:43 +00:00
|
|
|
status => $self->get_user_status_json_v1( uid => $uid )
|
2019-12-20 15:39:15 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-14 22:46:36 +00:00
|
|
|
if ( $payload->{comment} ) {
|
2020-10-11 17:38:01 +00:00
|
|
|
$self->in_transit->update_user_data(
|
|
|
|
uid => $uid,
|
|
|
|
user_data => { comment => sanitize( q{}, $payload->{comment} ) }
|
|
|
|
);
|
2019-12-14 22:46:36 +00:00
|
|
|
}
|
|
|
|
|
2023-08-19 08:06:15 +00:00
|
|
|
$self->render_later;
|
|
|
|
|
2023-07-23 10:21:37 +00:00
|
|
|
# the user may not have provided the correct to_station, so
|
|
|
|
# request related stations for checkout.
|
2023-08-19 08:06:15 +00:00
|
|
|
$self->checkout_p(
|
2023-07-23 10:21:37 +00:00
|
|
|
station => $to_station,
|
|
|
|
force => $payload->{force} ? 1 : 0,
|
|
|
|
uid => $uid,
|
|
|
|
with_related => 1,
|
2023-08-19 08:06:15 +00:00
|
|
|
)->then(
|
|
|
|
sub {
|
|
|
|
my ( $train, $error ) = @_;
|
|
|
|
if ($error) {
|
|
|
|
return Mojo::Promise->reject($error);
|
2019-12-14 20:53:23 +00:00
|
|
|
}
|
2023-08-19 08:06:15 +00:00
|
|
|
$self->render(
|
|
|
|
json => {
|
|
|
|
success => \1,
|
|
|
|
deprecated => \0,
|
|
|
|
status => $self->get_user_status_json_v1( uid => $uid )
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
)->catch(
|
|
|
|
sub {
|
|
|
|
my ($err) = @_;
|
|
|
|
$self->render(
|
|
|
|
json => {
|
|
|
|
success => \0,
|
|
|
|
deprecated => \0,
|
|
|
|
error => 'Checkout error: ' . $err,
|
|
|
|
status => $self->get_user_status_json_v1( uid => $uid )
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
)->wait;
|
2019-12-14 20:53:23 +00:00
|
|
|
}
|
|
|
|
elsif ( $payload->{action} eq 'undo' ) {
|
|
|
|
my $error = $self->undo( 'in_transit', $uid );
|
|
|
|
if ($error) {
|
|
|
|
$self->render(
|
|
|
|
json => {
|
2019-12-20 15:23:41 +00:00
|
|
|
success => \0,
|
|
|
|
deprecated => \0,
|
|
|
|
error => $error,
|
2023-05-21 17:44:43 +00:00
|
|
|
status => $self->get_user_status_json_v1( uid => $uid )
|
2019-12-14 20:53:23 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->render(
|
|
|
|
json => {
|
2019-12-20 15:23:41 +00:00
|
|
|
success => \1,
|
|
|
|
deprecated => \0,
|
2023-05-21 17:44:43 +00:00
|
|
|
status => $self->get_user_status_json_v1( uid => $uid )
|
2019-12-14 20:53:23 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 19:46:02 +00:00
|
|
|
sub import_v1 {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
2019-12-17 19:41:36 +00:00
|
|
|
my $payload = $self->req->json;
|
|
|
|
|
|
|
|
if ( not $payload or ref($payload) ne 'HASH' ) {
|
|
|
|
$self->render(
|
|
|
|
json => {
|
2020-01-26 09:47:51 +00:00
|
|
|
success => \0,
|
|
|
|
deprecated => \0,
|
|
|
|
error => 'Malformed JSON',
|
2019-12-17 19:46:17 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-14 19:46:02 +00:00
|
|
|
my $api_token = $payload->{token} // '';
|
|
|
|
|
|
|
|
if ( $api_token !~ qr{ ^ (?<id> \d+ ) - (?<token> .* ) $ }x ) {
|
|
|
|
$self->render(
|
|
|
|
json => {
|
2020-01-26 09:47:51 +00:00
|
|
|
success => \0,
|
|
|
|
deprecated => \0,
|
|
|
|
error => 'Malformed token',
|
2019-12-14 19:46:02 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
my $uid = $+{id};
|
|
|
|
$api_token = $+{token};
|
|
|
|
|
|
|
|
if ( $uid > 2147483647 ) {
|
|
|
|
$self->render(
|
|
|
|
json => {
|
2020-01-26 09:47:51 +00:00
|
|
|
success => \0,
|
|
|
|
deprecated => \0,
|
|
|
|
error => 'Malformed token',
|
2019-12-14 19:46:02 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-26 13:55:33 +00:00
|
|
|
my $token = $self->users->get_api_token( uid => $uid );
|
2020-01-26 09:47:51 +00:00
|
|
|
if ( not $token->{'import'} or $api_token ne $token->{'import'} ) {
|
2019-12-14 19:46:02 +00:00
|
|
|
$self->render(
|
|
|
|
json => {
|
2020-01-26 09:47:51 +00:00
|
|
|
success => \0,
|
|
|
|
deprecated => \0,
|
|
|
|
error => 'Invalid token',
|
2019-12-14 19:46:02 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( not exists $payload->{fromStation}
|
|
|
|
or not exists $payload->{toStation} )
|
|
|
|
{
|
|
|
|
$self->render(
|
|
|
|
json => {
|
2020-01-26 09:47:51 +00:00
|
|
|
success => \0,
|
|
|
|
deprecated => \0,
|
|
|
|
error => 'missing fromStation or toStation',
|
2019-12-14 19:46:02 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
my %opt;
|
|
|
|
|
|
|
|
eval {
|
2019-12-17 19:09:02 +00:00
|
|
|
|
|
|
|
if ( not $payload->{fromStation}{name}
|
|
|
|
or not $payload->{toStation}{name} )
|
|
|
|
{
|
|
|
|
die("Missing fromStation/toStation name\n");
|
|
|
|
}
|
|
|
|
if ( not $payload->{train}{type} or not $payload->{train}{no} ) {
|
|
|
|
die("Missing train data\n");
|
|
|
|
}
|
|
|
|
if ( not $payload->{fromStation}{scheduledTime}
|
|
|
|
or not $payload->{toStation}{scheduledTime} )
|
|
|
|
{
|
|
|
|
die("Missing fromStation/toStation scheduledTime\n");
|
|
|
|
}
|
|
|
|
|
2019-12-14 19:46:02 +00:00
|
|
|
%opt = (
|
2022-07-30 08:42:21 +00:00
|
|
|
uid => $uid,
|
|
|
|
train_type => sanitize( q{}, $payload->{train}{type} ),
|
|
|
|
train_no => sanitize( q{}, $payload->{train}{no} ),
|
|
|
|
train_line => sanitize( q{}, $payload->{train}{line} ),
|
|
|
|
cancelled => $payload->{cancelled} ? 1 : 0,
|
|
|
|
dep_station => sanitize( q{}, $payload->{fromStation}{name} ),
|
|
|
|
arr_station => sanitize( q{}, $payload->{toStation}{name} ),
|
2019-12-14 19:46:02 +00:00
|
|
|
sched_departure =>
|
|
|
|
sanitize( 0, $payload->{fromStation}{scheduledTime} ),
|
|
|
|
rt_departure => sanitize(
|
|
|
|
0,
|
|
|
|
$payload->{fromStation}{realTime}
|
|
|
|
// $payload->{fromStation}{scheduledTime}
|
|
|
|
),
|
|
|
|
sched_arrival =>
|
|
|
|
sanitize( 0, $payload->{toStation}{scheduledTime} ),
|
|
|
|
rt_arrival => sanitize(
|
|
|
|
0,
|
|
|
|
$payload->{toStation}{realTime}
|
|
|
|
// $payload->{toStation}{scheduledTime}
|
|
|
|
),
|
|
|
|
comment => sanitize( q{}, $payload->{comment} ),
|
2019-12-17 19:09:02 +00:00
|
|
|
lax => $payload->{lax} ? 1 : 0,
|
2019-12-14 19:46:02 +00:00
|
|
|
);
|
|
|
|
|
2020-01-26 09:48:41 +00:00
|
|
|
if ( $payload->{intermediateStops}
|
|
|
|
and ref( $payload->{intermediateStops} ) eq 'ARRAY' )
|
|
|
|
{
|
2020-01-26 15:04:08 +00:00
|
|
|
$opt{route}
|
2020-01-26 09:48:41 +00:00
|
|
|
= [ map { sanitize( q{}, $_ ) }
|
|
|
|
@{ $payload->{intermediateStops} } ];
|
2019-12-14 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for my $key (qw(sched_departure rt_departure sched_arrival rt_arrival))
|
|
|
|
{
|
|
|
|
$opt{$key} = DateTime->from_epoch(
|
|
|
|
time_zone => 'Europe/Berlin',
|
|
|
|
epoch => $opt{$key}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
my ($first_line) = split( qr{\n}, $@ );
|
|
|
|
$self->render(
|
|
|
|
json => {
|
2020-01-26 09:47:51 +00:00
|
|
|
success => \0,
|
|
|
|
deprecated => \0,
|
|
|
|
error => $first_line
|
2019-12-14 19:46:02 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $db = $self->pg->db;
|
|
|
|
my $tx = $db->begin;
|
|
|
|
|
|
|
|
$opt{db} = $db;
|
2020-08-16 19:20:23 +00:00
|
|
|
my ( $journey_id, $error ) = $self->journeys->add(%opt);
|
2019-12-14 19:46:02 +00:00
|
|
|
my $journey;
|
|
|
|
|
|
|
|
if ( not $error ) {
|
2020-08-16 19:20:23 +00:00
|
|
|
$journey = $self->journeys->get_single(
|
2019-12-14 19:46:02 +00:00
|
|
|
uid => $uid,
|
|
|
|
db => $db,
|
|
|
|
journey_id => $journey_id,
|
|
|
|
verbose => 1
|
|
|
|
);
|
2019-12-17 19:01:39 +00:00
|
|
|
$error
|
2020-08-16 19:20:23 +00:00
|
|
|
= $self->journeys->sanity_check( $journey, $payload->{lax} ? 1 : 0 );
|
2019-12-14 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($error) {
|
|
|
|
$self->render(
|
|
|
|
json => {
|
2020-01-26 09:47:51 +00:00
|
|
|
success => \0,
|
|
|
|
deprecated => \0,
|
|
|
|
error => $error
|
2019-12-14 19:46:02 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
elsif ( $payload->{dryRun} ) {
|
|
|
|
$self->render(
|
|
|
|
json => {
|
2020-01-26 09:47:51 +00:00
|
|
|
success => \1,
|
|
|
|
deprecated => \0,
|
|
|
|
id => $journey_id,
|
|
|
|
result => $journey
|
2019-12-14 19:46:02 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
2020-11-28 20:03:51 +00:00
|
|
|
$self->journey_stats_cache->invalidate(
|
2020-08-16 19:20:23 +00:00
|
|
|
ts => $opt{rt_departure},
|
|
|
|
db => $db,
|
|
|
|
uid => $uid
|
|
|
|
);
|
2019-12-14 19:46:02 +00:00
|
|
|
$tx->commit;
|
|
|
|
$self->render(
|
|
|
|
json => {
|
2020-01-26 09:47:51 +00:00
|
|
|
success => \1,
|
|
|
|
deprecated => \0,
|
|
|
|
id => $journey_id,
|
|
|
|
result => $journey
|
2019-12-14 19:46:02 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 21:12:45 +00:00
|
|
|
sub set_token {
|
|
|
|
my ($self) = @_;
|
|
|
|
if ( $self->validation->csrf_protect->has_error('csrf_token') ) {
|
2023-06-04 17:25:24 +00:00
|
|
|
$self->render(
|
|
|
|
'bad_request',
|
|
|
|
csrf => 1,
|
|
|
|
status => 400
|
|
|
|
);
|
2019-03-21 21:12:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
my $token = make_token();
|
2023-01-22 11:33:02 +00:00
|
|
|
my $token_id = $self->users->get_token_id( $self->param('token') );
|
2019-03-21 21:12:45 +00:00
|
|
|
|
|
|
|
if ( not $token_id ) {
|
|
|
|
$self->redirect_to('account');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $self->param('action') eq 'delete' ) {
|
2019-04-22 10:11:22 +00:00
|
|
|
$self->pg->db->delete(
|
|
|
|
'tokens',
|
|
|
|
{
|
|
|
|
user_id => $self->current_user->{id},
|
|
|
|
type => $token_id
|
|
|
|
}
|
|
|
|
);
|
2019-03-21 21:12:45 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-04-22 10:11:22 +00:00
|
|
|
$self->pg->db->insert(
|
|
|
|
'tokens',
|
|
|
|
{
|
|
|
|
user_id => $self->current_user->{id},
|
|
|
|
type => $token_id,
|
|
|
|
token => $token
|
|
|
|
},
|
|
|
|
{
|
|
|
|
on_conflict => \
|
|
|
|
'(user_id, type) do update set token = EXCLUDED.token'
|
|
|
|
},
|
|
|
|
);
|
2019-03-21 21:12:45 +00:00
|
|
|
}
|
|
|
|
$self->redirect_to('account');
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|