47f76da4f8
Squashed commit of the following: commit 92518024ba295456358618c0e8180bd8e996fdf1 Author: Birte Kristina Friesel <birte.friesel@uos.de> Date: Fri Jul 26 18:39:46 2024 +0200 add_or_update station: remove superfluos 'new backend id := old backend id' commit df21c20c6e4c86454f8a9ac69121404415217f2a Author: Birte Kristina Friesel <birte.friesel@uos.de> Date: Fri Jul 26 18:35:51 2024 +0200 revert connection targets min_count to 3 commit be335cef07d0b42874f5fc1de4a1d13396e8e807 Author: Birte Kristina Friesel <birte.friesel@uos.de> Date: Fri Jul 26 18:20:05 2024 +0200 mention backend selection in API documentation commit 9f41828fb4f18fd707e0087def3032e8d4c8d7d8 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:19:23 2024 +0200 use_history: not all backends provide route data in departure monitor commit 09714b4d89684b8331d0e96f564a4c7432318f70 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:11:44 2024 +0200 disambiguation: pass correct hafas parameter commit 8cdf1120fc32155dc6525be64601b7c10a9c7f52 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:11:28 2024 +0200 _checked_in: hide Zuglauf link for non-db checkins commit 7455653f541198e0e0a6d11aed421487ffdb6285 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 20:01:47 2024 +0200 debug output commit b9cda07f85601a58ea32dbdacdd5399f302db52b Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Thu Jul 25 19:09:07 2024 +0200 fix remaining get_connection_targets / get_connecting_trains_p invocations commit 2759d7258c37c7498905cfe19f6b4c4f6d16bd21 Author: Birte Kristina Friesel <derf@finalrewind.org> Date: Wed Jul 24 20:50:12 2024 +0200 support non-DB HAFAS backends (WiP)
244 lines
5.3 KiB
Perl
244 lines
5.3 KiB
Perl
package Travelynx::Model::Traewelling;
|
|
|
|
# Copyright (C) 2020-2023 Birte Kristina Friesel
|
|
#
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
use strict;
|
|
use warnings;
|
|
use 5.020;
|
|
|
|
use DateTime;
|
|
|
|
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',
|
|
locale => 'de-DE',
|
|
);
|
|
|
|
}
|
|
|
|
sub new {
|
|
my ( $class, %opt ) = @_;
|
|
|
|
return bless( \%opt, $class );
|
|
}
|
|
|
|
sub now {
|
|
return DateTime->now( time_zone => 'Europe/Berlin' );
|
|
}
|
|
|
|
sub link {
|
|
my ( $self, %opt ) = @_;
|
|
|
|
my $log = [ [ $self->now->epoch, "Erfolgreich mittels OAuth2 verbunden" ] ];
|
|
|
|
my $data = { log => $log };
|
|
|
|
my $user_entry = {
|
|
user_id => $opt{uid},
|
|
push_sync => 0,
|
|
pull_sync => 0,
|
|
token => $opt{token},
|
|
refresh_token => $opt{refresh_token},
|
|
expiry => epoch_to_dt( $self->now->epoch + $opt{expires_in} ),
|
|
data => JSON->new->encode($data),
|
|
};
|
|
|
|
$self->{pg}->db->insert(
|
|
'traewelling',
|
|
$user_entry,
|
|
{
|
|
on_conflict => \
|
|
'(user_id) do update set token = EXCLUDED.token, refresh_token = EXCLUDED.refresh_token, expiry = EXCLUDED.expiry, push_sync = false, pull_sync = false, data = null, errored = false, latest_run = null'
|
|
}
|
|
);
|
|
|
|
return $user_entry;
|
|
}
|
|
|
|
sub set_user {
|
|
my ( $self, %opt ) = @_;
|
|
|
|
my $res_h
|
|
= $self->{pg}
|
|
->db->select( 'traewelling', 'data', { user_id => $opt{uid} } )
|
|
->expand->hash;
|
|
|
|
$res_h->{data}{user_id} = $opt{trwl_id};
|
|
$res_h->{data}{screen_name} = $opt{screen_name};
|
|
$res_h->{data}{user_name} = $opt{user_name};
|
|
|
|
$self->{pg}->db->update(
|
|
'traewelling',
|
|
{ data => JSON->new->encode( $res_h->{data} ) },
|
|
{ user_id => $opt{uid} }
|
|
);
|
|
}
|
|
|
|
sub unlink {
|
|
my ( $self, %opt ) = @_;
|
|
|
|
my $uid = $opt{uid};
|
|
|
|
$self->{pg}->db->delete( 'traewelling', { user_id => $uid } );
|
|
}
|
|
|
|
sub get {
|
|
my ( $self, %opt ) = @_;
|
|
|
|
my $uid = $opt{uid};
|
|
my $db = $opt{db} // $self->{pg}->db;
|
|
|
|
my $res_h
|
|
= $db->select( 'traewelling_str', '*', { user_id => $uid } )
|
|
->expand->hash;
|
|
|
|
$res_h->{latest_run} = epoch_to_dt( $res_h->{latest_run_ts} );
|
|
for my $log_entry ( @{ $res_h->{data}{log} // [] } ) {
|
|
$log_entry->[0] = epoch_to_dt( $log_entry->[0] );
|
|
}
|
|
$res_h->{expires_on}
|
|
= epoch_to_dt( $res_h->{expiry_ts} // $res_h->{data}{expires} );
|
|
|
|
my $expires_in = ( $res_h->{expiry_ts} // $res_h->{data}{expires} // 0 )
|
|
- $self->now->epoch;
|
|
|
|
if ( $expires_in < 0 ) {
|
|
$res_h->{expired} = 1;
|
|
}
|
|
elsif ( $expires_in < 14 * 24 * 3600 ) {
|
|
$res_h->{expiring} = 1;
|
|
}
|
|
|
|
return $res_h;
|
|
}
|
|
|
|
sub log {
|
|
my ( $self, %opt ) = @_;
|
|
my $uid = $opt{uid};
|
|
my $message = $opt{message};
|
|
my $is_error = $opt{is_error};
|
|
my $db = $opt{db} // $self->{pg}->db;
|
|
my $res_h
|
|
= $db->select( 'traewelling', 'data', { user_id => $uid } )->expand->hash;
|
|
splice( @{ $res_h->{data}{log} // [] }, 9 );
|
|
unshift(
|
|
@{ $res_h->{data}{log} },
|
|
[ $self->now->epoch, $message, $opt{status_id} ]
|
|
);
|
|
|
|
if ($is_error) {
|
|
$res_h->{data}{error} = $message;
|
|
}
|
|
$db->update(
|
|
'traewelling',
|
|
{
|
|
errored => $is_error ? 1 : 0,
|
|
latest_run => $self->now,
|
|
data => JSON->new->encode( $res_h->{data} )
|
|
},
|
|
{ user_id => $uid }
|
|
);
|
|
}
|
|
|
|
sub set_latest_pull_status_id {
|
|
my ( $self, %opt ) = @_;
|
|
my $uid = $opt{uid};
|
|
my $status_id = $opt{status_id};
|
|
my $db = $opt{db} // $self->{pg}->db;
|
|
|
|
my $res_h
|
|
= $db->select( 'traewelling', 'data', { user_id => $uid } )->expand->hash;
|
|
|
|
$res_h->{data}{latest_pull_status_id} = $status_id;
|
|
|
|
$db->update(
|
|
'traewelling',
|
|
{ data => JSON->new->encode( $res_h->{data} ) },
|
|
{ user_id => $uid }
|
|
);
|
|
}
|
|
|
|
sub set_latest_push_ts {
|
|
my ( $self, %opt ) = @_;
|
|
my $uid = $opt{uid};
|
|
my $ts = $opt{ts};
|
|
my $db = $opt{db} // $self->{pg}->db;
|
|
|
|
my $res_h
|
|
= $db->select( 'traewelling', 'data', { user_id => $uid } )->expand->hash;
|
|
|
|
$res_h->{data}{latest_push_ts} = $ts;
|
|
|
|
$db->update(
|
|
'traewelling',
|
|
{ data => JSON->new->encode( $res_h->{data} ) },
|
|
{ user_id => $uid }
|
|
);
|
|
}
|
|
|
|
sub set_sync {
|
|
my ( $self, %opt ) = @_;
|
|
|
|
my $uid = $opt{uid};
|
|
my $db = $opt{db} // $self->{pg}->db;
|
|
|
|
my $res_h
|
|
= $db->select( 'traewelling', 'data', { user_id => $uid } )->expand->hash;
|
|
|
|
$res_h->{data}{toot} = $opt{toot};
|
|
$res_h->{data}{tweet} = $opt{tweet};
|
|
|
|
$db->update(
|
|
'traewelling',
|
|
{
|
|
push_sync => $opt{push_sync},
|
|
pull_sync => $opt{pull_sync},
|
|
data => JSON->new->encode( $res_h->{data} ),
|
|
},
|
|
{ user_id => $uid }
|
|
);
|
|
}
|
|
|
|
sub get_pushable_accounts {
|
|
my ($self) = @_;
|
|
my $res = $self->{pg}->db->query(
|
|
qq{select t.user_id as uid, t.token as token, t.data as data,
|
|
i.user_data as user_data,
|
|
i.dep_eva as dep_eva, i.arr_eva as arr_eva,
|
|
i.data as journey_data, i.train_type as train_type,
|
|
i.train_line as train_line, i.train_no as train_no,
|
|
i.checkin_ts as checkin_ts,
|
|
i.sched_dep_ts as dep_ts,
|
|
i.sched_arr_ts as arr_ts,
|
|
i.effective_visibility as visibility
|
|
from traewelling as t
|
|
join in_transit_str as i on t.user_id = i.user_id
|
|
where t.push_sync = True
|
|
and i.arr_eva is not null
|
|
and i.backend_id <= 1
|
|
and i.cancelled = False
|
|
}
|
|
);
|
|
return $res->expand->hashes->each;
|
|
}
|
|
|
|
sub get_pull_accounts {
|
|
my ($self) = @_;
|
|
my $res = $self->{pg}->db->select(
|
|
'traewelling',
|
|
[ 'user_id', 'token', 'data' ],
|
|
{ pull_sync => 1 }
|
|
);
|
|
return $res->expand->hashes->each;
|
|
}
|
|
|
|
1;
|