From 209e2918217aef8d900a194309160f7119a205bc Mon Sep 17 00:00:00 2001
From: Daniel Friesel <derf@finalrewind.org>
Date: Sun, 20 Feb 2022 17:35:30 +0100
Subject: Move webhook database queries to Users model

---
 lib/Travelynx.pm                    | 89 ++++++++-----------------------------
 lib/Travelynx/Controller/Account.pm |  9 ++--
 lib/Travelynx/Model/Users.pm        | 66 +++++++++++++++++++++++++++
 templates/account.html.ep           |  2 +-
 4 files changed, 92 insertions(+), 74 deletions(-)

diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm
index 90103d7..621f3e8 100755
--- a/lib/Travelynx.pm
+++ b/lib/Travelynx.pm
@@ -891,75 +891,11 @@ sub startup {
 		}
 	);
 
-	$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};
-
-			if ( $opt{token} ) {
-				$opt{token} =~ tr{\r\n}{}d;
-			}
-
-			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 ) = @_;
-
-			if ( length($text) > 1000 ) {
-				$text = substr( $text, 0, 1000 ) . '…';
-			}
-
-			$self->pg->db->update(
-				'webhooks',
-				{
-					errored    => $success ? 0 : 1,
-					latest_run => DateTime->now( time_zone => 'Europe/Berlin' ),
-					output     => $text,
-				},
-				{
-					user_id => $uid,
-					url     => $url
-				}
-			);
-		}
-	);
-
 	$self->helper(
 		'run_hook' => sub {
 			my ( $self, $uid, $reason, $callback ) = @_;
 
-			my $hook = $self->get_webhook($uid);
+			my $hook = $self->users->get_webhook( uid => $uid );
 
 			if ( not $hook->{enabled} or not $hook->{url} =~ m{^ https?:// }x )
 			{
@@ -994,12 +930,20 @@ sub startup {
 				sub {
 					my ($tx) = @_;
 					if ( my $err = $tx->error ) {
-						$self->mark_hook_status( $uid, $hook->{url}, 0,
-							"HTTP $err->{code} $err->{message}" );
+						$self->users->update_webhook_status(
+							uid     => $uid,
+							url     => $hook->{url},
+							success => 0,
+							text    => "HTTP $err->{code} $err->{message}"
+						);
 					}
 					else {
-						$self->mark_hook_status( $uid, $hook->{url}, 1,
-							$tx->result->body );
+						$self->users->update_webhook_status(
+							uid     => $uid,
+							url     => $hook->{url},
+							success => 1,
+							text    => $tx->result->body
+						);
 					}
 					if ($callback) {
 						&$callback();
@@ -1009,7 +953,12 @@ sub startup {
 			)->catch(
 				sub {
 					my ($err) = @_;
-					$self->mark_hook_status( $uid, $hook->{url}, 0, $err );
+					$self->users->update_webhook_status(
+						uid     => $uid,
+						url     => $hook->{url},
+						success => 0,
+						text    => $err
+					);
 					if ($callback) {
 						&$callback();
 					}
diff --git a/lib/Travelynx/Controller/Account.pm b/lib/Travelynx/Controller/Account.pm
index 88df2d5..0435d51 100644
--- a/lib/Travelynx/Controller/Account.pm
+++ b/lib/Travelynx/Controller/Account.pm
@@ -378,13 +378,16 @@ sub insight {
 sub webhook {
 	my ($self) = @_;
 
-	my $hook = $self->get_webhook;
+	my $uid = $self->current_user->{id};
+
+	my $hook = $self->users->get_webhook( uid => $uid );
 
 	if ( $self->param('action') and $self->param('action') eq 'save' ) {
 		$hook->{url}     = $self->param('url');
 		$hook->{token}   = $self->param('token');
 		$hook->{enabled} = $self->param('enabled') // 0;
-		$self->set_webhook(
+		$self->users->set_webhook(
+			uid     => $uid,
 			url     => $hook->{url},
 			token   => $hook->{token},
 			enabled => $hook->{enabled}
@@ -395,7 +398,7 @@ sub webhook {
 			sub {
 				$self->render(
 					'webhooks',
-					hook     => $self->get_webhook,
+					hook     => $self->users->get_webhook( uid => $uid ),
 					new_hook => 1
 				);
 			}
diff --git a/lib/Travelynx/Model/Users.pm b/lib/Travelynx/Model/Users.pm
index 70d81c4..3d19831 100644
--- a/lib/Travelynx/Model/Users.pm
+++ b/lib/Travelynx/Model/Users.pm
@@ -478,4 +478,70 @@ sub use_history {
 	}
 }
 
+sub get_webhook {
+	my ( $self, %opt ) = @_;
+	my $db  = $opt{db} // $self->{pg}->db;
+	my $uid = $opt{uid};
+
+	my $res_h = $db->select( 'webhooks_str', '*', { user_id => $uid } )->hash;
+
+	$res_h->{latest_run} = DateTime->from_epoch(
+		epoch     => $res_h->{latest_run_ts} // 0,
+		time_zone => 'Europe/Berlin',
+		locale    => 'de-DE',
+	);
+
+	return $res_h;
+}
+
+sub set_webhook {
+	my ( $self, %opt ) = @_;
+	my $db = $opt{db} // $self->{pg}->db;
+
+	if ( $opt{token} ) {
+		$opt{token} =~ tr{\r\n}{}d;
+	}
+
+	my $res = $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'
+		}
+	);
+}
+
+sub update_webhook_status {
+	my ( $self, %opt ) = @_;
+
+	my $db      = $opt{db} // $self->{pg}->db;
+	my $uid     = $opt{uid};
+	my $url     = $opt{url};
+	my $success = $opt{success};
+	my $text    = $opt{text};
+
+	if ( length($text) > 1000 ) {
+		$text = substr( $text, 0, 1000 ) . '…';
+	}
+
+	$db->update(
+		'webhooks',
+		{
+			errored    => $success ? 0 : 1,
+			latest_run => DateTime->now( time_zone => 'Europe/Berlin' ),
+			output     => $text,
+		},
+		{
+			user_id => $uid,
+			url     => $url
+		}
+	);
+}
+
 1;
diff --git a/templates/account.html.ep b/templates/account.html.ep
index b2877f0..bce4dcb 100644
--- a/templates/account.html.ep
+++ b/templates/account.html.ep
@@ -35,7 +35,7 @@
 % }
 
 % my $acc = current_user();
-% my $hook = get_webhook();
+% my $hook = users->get_webhook(uid => $acc->{id});
 % my $traewelling = traewelling->get(uid => $acc->{id});
 % my $use_history = users->use_history(uid => $acc->{id});
 <div class="row">
-- 
cgit v1.2.3