summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Model/Traewelling.pm
blob: 7f08b0de3e1990eb4287b7c5175865ee7bc14d60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package Travelynx::Model::Traewelling;

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 angemeldet" ] ];

	my $data = {
		user_id   => $opt{uid},
		email     => $opt{email},
		push_sync => 0,
		pull_sync => 0,
		token     => $opt{token},
		data      => JSON->new->encode( { log => $log } ),
	};

	$self->{pg}->db->insert(
		'traewelling',
		$data,
		{
			on_conflict => \
'(user_id) do update set email = EXCLUDED.email, token = EXCLUDED.token, push_sync = false, pull_sync = false, data = null, errored = false, latest_run = null'
		}
	);

	return $data;
}

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, $uid ) = @_;
	$uid //= $self->current_user->{id};

	my $res_h
	  = $self->{pg}->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] );
	}

	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_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_push_status_id} = $status_id;

	$db->update(
		'traewelling',
		{ data    => JSON->new->encode( $res_h->{data} ) },
		{ user_id => $uid }
	);
}

sub set_sync {
	my ( $self, %opt ) = @_;

	my $uid       = $opt{uid};
	my $push_sync = $opt{push_sync};
	my $pull_sync = $opt{pull_sync};

	$self->{pg}->db->update(
		'traewelling',
		{
			push_sync => $push_sync,
			pull_sync => $pull_sync
		},
		{ user_id => $uid }
	);
}

sub get_push_accounts {
	my ($self) = @_;
	my $res = $self->{pg}->db->select(
		'traewelling',
		[ 'user_id', 'token', 'data' ],
		{ push_sync => 1 }
	);
	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;