2020-09-30 17:12:29 +00:00
|
|
|
package Travelynx::Model::Traewelling;
|
2021-07-01 17:44:09 +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
|
2020-09-30 17:12:29 +00:00
|
|
|
|
|
|
|
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 ) = @_;
|
|
|
|
|
2023-08-07 19:17:10 +00:00
|
|
|
my $log = [ [ $self->now->epoch, "Erfolgreich mittels OAuth2 verbunden" ] ];
|
2020-09-30 17:12:29 +00:00
|
|
|
|
2023-08-09 17:28:36 +00:00
|
|
|
my $data = { log => $log };
|
2020-10-04 08:38:09 +00:00
|
|
|
|
|
|
|
my $user_entry = {
|
2023-08-09 17:28:36 +00:00
|
|
|
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),
|
2020-09-30 17:12:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$self->{pg}->db->insert(
|
|
|
|
'traewelling',
|
2020-10-04 08:38:09 +00:00
|
|
|
$user_entry,
|
2020-09-30 17:12:29 +00:00
|
|
|
{
|
|
|
|
on_conflict => \
|
2023-08-09 17:28:36 +00:00
|
|
|
'(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'
|
2020-09-30 17:12:29 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-10-04 08:38:09 +00:00
|
|
|
return $user_entry;
|
2020-09-30 17:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-02-16 19:39:49 +00:00
|
|
|
my ( $self, %opt ) = @_;
|
|
|
|
|
|
|
|
my $uid = $opt{uid};
|
|
|
|
my $db = $opt{db} // $self->{pg}->db;
|
2020-09-30 17:12:29 +00:00
|
|
|
|
|
|
|
my $res_h
|
2022-02-16 19:39:49 +00:00
|
|
|
= $db->select( 'traewelling_str', '*', { user_id => $uid } )
|
2020-09-30 17:12:29 +00:00
|
|
|
->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] );
|
|
|
|
}
|
2023-08-09 17:28:36 +00:00
|
|
|
$res_h->{expires_on}
|
|
|
|
= epoch_to_dt( $res_h->{expiry_ts} // $res_h->{data}{expires} );
|
2020-10-04 08:38:09 +00:00
|
|
|
|
2023-08-09 17:28:36 +00:00
|
|
|
my $expires_in = ( $res_h->{expiry_ts} // $res_h->{data}{expires} // 0 )
|
|
|
|
- $self->now->epoch;
|
2020-10-04 08:38:09 +00:00
|
|
|
|
|
|
|
if ( $expires_in < 0 ) {
|
|
|
|
$res_h->{expired} = 1;
|
|
|
|
}
|
|
|
|
elsif ( $expires_in < 14 * 24 * 3600 ) {
|
|
|
|
$res_h->{expiring} = 1;
|
|
|
|
}
|
2020-09-30 17:12:29 +00:00
|
|
|
|
|
|
|
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 }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-01 17:36:35 +00:00
|
|
|
sub set_latest_push_ts {
|
2020-09-30 17:12:29 +00:00
|
|
|
my ( $self, %opt ) = @_;
|
2020-10-01 17:36:35 +00:00
|
|
|
my $uid = $opt{uid};
|
|
|
|
my $ts = $opt{ts};
|
|
|
|
my $db = $opt{db} // $self->{pg}->db;
|
2020-09-30 17:12:29 +00:00
|
|
|
|
|
|
|
my $res_h
|
|
|
|
= $db->select( 'traewelling', 'data', { user_id => $uid } )->expand->hash;
|
|
|
|
|
2020-10-01 17:36:35 +00:00
|
|
|
$res_h->{data}{latest_push_ts} = $ts;
|
2020-09-30 17:12:29 +00:00
|
|
|
|
|
|
|
$db->update(
|
|
|
|
'traewelling',
|
|
|
|
{ data => JSON->new->encode( $res_h->{data} ) },
|
|
|
|
{ user_id => $uid }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub set_sync {
|
|
|
|
my ( $self, %opt ) = @_;
|
|
|
|
|
2020-10-04 10:27:20 +00:00
|
|
|
my $uid = $opt{uid};
|
|
|
|
my $db = $opt{db} // $self->{pg}->db;
|
2020-09-30 17:12:29 +00:00
|
|
|
|
2020-10-04 10:27:20 +00:00
|
|
|
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(
|
2020-09-30 17:12:29 +00:00
|
|
|
'traewelling',
|
|
|
|
{
|
2020-10-04 10:27:20 +00:00
|
|
|
push_sync => $opt{push_sync},
|
|
|
|
pull_sync => $opt{pull_sync},
|
|
|
|
data => JSON->new->encode( $res_h->{data} ),
|
2020-09-30 17:12:29 +00:00
|
|
|
},
|
|
|
|
{ user_id => $uid }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-01 17:36:35 +00:00
|
|
|
sub get_pushable_accounts {
|
2020-09-30 17:12:29 +00:00
|
|
|
my ($self) = @_;
|
2020-10-01 17:48:18 +00:00
|
|
|
my $res = $self->{pg}->db->query(
|
2020-10-01 17:36:35 +00:00
|
|
|
qq{select t.user_id as uid, t.token as token, t.data as data,
|
2020-10-03 12:33:56 +00:00
|
|
|
i.user_data as user_data,
|
2023-07-13 19:08:12 +00:00
|
|
|
i.dep_eva as dep_eva, i.arr_eva as arr_eva,
|
2020-10-01 17:36:35 +00:00
|
|
|
i.data as journey_data, i.train_type as train_type,
|
|
|
|
i.train_line as train_line, i.train_no as train_no,
|
2023-07-13 19:08:12 +00:00
|
|
|
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
|
2020-10-01 17:36:35 +00:00
|
|
|
from traewelling as t
|
2023-07-13 19:08:12 +00:00
|
|
|
join in_transit_str as i on t.user_id = i.user_id
|
2020-10-01 17:36:35 +00:00
|
|
|
where t.push_sync = True
|
2023-07-13 19:08:12 +00:00
|
|
|
and i.arr_eva is not null
|
2020-10-01 17:36:35 +00:00
|
|
|
and i.cancelled = False
|
2020-10-01 17:48:18 +00:00
|
|
|
}
|
2020-09-30 17:12:29 +00:00
|
|
|
);
|
|
|
|
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;
|