From 976f22c39f02a1c699bbfc2e12f620bc81b823c5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz>
Date: Thu, 23 Dec 2021 01:41:15 +0100
Subject: [PATCH] Prechod na redis pub/sub

---
 .gitlab-ci.yml                     |  2 +-
 lib/CF/Controller/Announcements.pm |  9 +++------
 lib/CF/Controller/Config.pm        |  3 +--
 lib/CF/Controller/Posts.pm         | 15 +++++----------
 lib/CF/Controller/Program.pm       | 19 +++++--------------
 lib/CF/Controller/Users.pm         |  9 +++------
 lib/CF/Controller/Websockets.pm    |  9 +--------
 sql/8/up.sql                       | 11 -----------
 8 files changed, 19 insertions(+), 58 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 73ec42b..d203b60 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -2,7 +2,7 @@ image: docker:20.10.9
 
 variables:
   DOCKER_TLS_CERTDIR: "/certs"
-  IMAGE_VER: 2.4.1
+  IMAGE_VER: 2.4.2
 
 services:
   - docker:20.10.9-dind
diff --git a/lib/CF/Controller/Announcements.pm b/lib/CF/Controller/Announcements.pm
index b4b1668..7ae3cf4 100644
--- a/lib/CF/Controller/Announcements.pm
+++ b/lib/CF/Controller/Announcements.pm
@@ -9,7 +9,6 @@ sub create ($c) {
     $c->openapi->valid_input or return;
 
     my $args   = $c->req->json;
-    my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
 
     my $announcement = $c->schema->resultset('Announcement')->create({
         user_id  => $c->user->{id},
@@ -23,7 +22,7 @@ sub create ($c) {
         id => $announcement->id,
     });
 
-    $pubsub->json('notify')->notify( notify => {
+    $c->redis->pubsub->json('notify')->notify( notify => {
         event   => 'announcement_created',
         payload => $announcement->format(),
     });
@@ -67,12 +66,11 @@ sub update ($c) {
     return $c->error(404, 'Announcement entry not found') if ! $entry;
 
     my $update = $c->prepare_update_data( $entry, $c->req->json );
-    my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
     my $guard  = $c->schema->txn_scope_guard;
 
     $entry->update( $update );
 
-    $pubsub->json('notify')->notify( notify => {
+    $c->redis->pubsub->json('notify')->notify( notify => {
         event   => 'announcement_changed',
         payload => {
             id => $entry->id,
@@ -91,12 +89,11 @@ sub delete ($c) {
     my $entry = $c->schema->resultset('Announcement')->find( $c->stash->{id} );
     return $c->error(404, 'Announcement entry not found') if ! $entry;
 
-    my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
     my $guard  = $c->schema->txn_scope_guard;
 
     $entry->update( { deleted => \'now()'} );
 
-    $pubsub->json('notify')->notify( notify => {
+    $c->redis->pubsub->json('notify')->notify( notify => {
         event   => 'announcement_deleted',
         payload => {
             id => $entry->id,
diff --git a/lib/CF/Controller/Config.pm b/lib/CF/Controller/Config.pm
index 108da6d..8702d55 100644
--- a/lib/CF/Controller/Config.pm
+++ b/lib/CF/Controller/Config.pm
@@ -30,12 +30,11 @@ sub update ($c) {
     return $c->error(403, 'Config entry protected') if $entry->is_readonly;
 
     my $update = $c->prepare_update_data( $entry, $c->req->json );
-    my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
     my $guard  = $c->schema->txn_scope_guard;
 
     $entry->update( $update );
 
-    $pubsub->json('notify')->notify( notify => {
+    $c->redis->pubsub->json('notify')->notify( notify => {
         event   => 'config_entry_changed',
         payload => {
             id => $entry->id,
diff --git a/lib/CF/Controller/Posts.pm b/lib/CF/Controller/Posts.pm
index 6a890ee..8a7130b 100644
--- a/lib/CF/Controller/Posts.pm
+++ b/lib/CF/Controller/Posts.pm
@@ -58,9 +58,7 @@ sub create ($c) {
         content          => $args->{content},
     });
 
-    my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
-
-    $pubsub->json('notify')->notify( notify => {
+    $c->redis->pubsub->json('notify')->notify( notify => {
         event   => 'post_created',
         payload => $post->view->format(),
     });
@@ -202,7 +200,6 @@ sub update ($c) {
     }
 
     my $update = $c->prepare_update_data( $post, $c->req->json );
-    my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
     my $guard  = $c->schema->txn_scope_guard;
 
     if ( $update->{state} && $post->state != $update->{state} ) {
@@ -227,7 +224,7 @@ sub update ($c) {
                 id => $announcement->id
             });
 
-            $pubsub->json('notify')->notify( notify => {
+            $c->redis->pubsub->json('notify')->notify( notify => {
                 event   => 'announcement_created',
                 payload => $announcement->format(),
             });
@@ -245,7 +242,7 @@ sub update ($c) {
         changed => \'now()',
     });
 
-    $pubsub->json('notify')->notify( notify => {
+    $c->redis->pubsub->json('notify')->notify( notify => {
         event   => 'post_changed',
         payload => {
             id => $post->id,
@@ -263,12 +260,11 @@ sub delete ($c) {
     my $post = $c->schema->resultset('Post')->find($c->stash->{id});
     return $c->error(404, 'Post not found') if ! $post;
 
-    my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
     my $guard  = $c->schema->txn_scope_guard;
 
     $post->update({ deleted => \'now()', });
 
-    $pubsub->json('notify')->notify( notify => {
+    $c->redis->pubsub->json('notify')->notify( notify => {
         event   => 'post_deleted',
         payload => {
             id => $post->id,
@@ -322,7 +318,6 @@ sub ranking ($c) {
         $update->{ranking_dislikes}++ if $args->{ranking} == -1;
     }
 
-    my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
     my $guard  = $c->schema->txn_scope_guard;
 
     $post->update( $update );
@@ -337,7 +332,7 @@ sub ranking ($c) {
         });
     }
 
-    $pubsub->json('notify')->notify(notify => {
+    $c->redis->pubsub->json('notify')->notify(notify => {
         event   => 'post_ranked',
         payload => {
             id => $post->id,
diff --git a/lib/CF/Controller/Program.pm b/lib/CF/Controller/Program.pm
index e0ff9e4..5b8744b 100644
--- a/lib/CF/Controller/Program.pm
+++ b/lib/CF/Controller/Program.pm
@@ -34,8 +34,7 @@ sub update ($c) {
     return $c->error(404, 'Program entry not found') if ! $entry;
 
     my $update = $c->prepare_update_data( $entry, $c->req->json );
-# PG_PUBSUB
-#    my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
+
     my $guard  = $c->schema->txn_scope_guard;
 
     # muze byt jen jeden aktualni bod programu
@@ -47,21 +46,13 @@ sub update ($c) {
 
     $entry->update( $update );
 
-    $c->redis->pubsub->notify(
-        program_entry_changed => {
+    $c->redis->pubsub->json("notify")->notify(notify => {
+        event   => 'program_entry_changed',
+        payload => {
             id => $entry->id,
             %{ $update },
         }
-    );
-
-# PG_PUBSUB
-#    $pubsub->json('notify')->notify( notify => {
-#        event   => 'program_entry_changed',
-#        payload => {
-#            id => $entry->id,
-#            %{ $update },
-#        }
-#    });
+    });
 
     $guard->commit;
 
diff --git a/lib/CF/Controller/Users.pm b/lib/CF/Controller/Users.pm
index d181eda..914eb0b 100644
--- a/lib/CF/Controller/Users.pm
+++ b/lib/CF/Controller/Users.pm
@@ -29,7 +29,6 @@ sub me ($c){
 
 sub ban ($c){
     my $user   = $c->_get( $c->stash->{id} ) // return;
-    my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
     my $guard  = $c->schema->txn_scope_guard;
 
     $user->update({ banned_until => \"now()+'8 hour'", });
@@ -50,12 +49,12 @@ sub ban ($c){
         id => $announcement->id,
     });
 
-    $pubsub->json('notify')->notify( notify => {
+    $c->redis->pubsub->json('notify')->notify( notify => {
         event   => 'announcement_created',
         payload => $announcement->format(),
     });
 
-    $pubsub->json('notify')->notify( notify => {
+    $c->redis->pubsub->json('notify')->notify( notify => {
         event   => 'user_banned',
         payload => $c->spec_filter($user->formatted, 'User'),
     });
@@ -66,14 +65,12 @@ sub ban ($c){
 
 sub unban ($c){
     my $user   = $c->_get( $c->stash->{id} ) // return;
-    my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
     my $guard  = $c->schema->txn_scope_guard;
 
-
     $user->update({ banned_until => undef });
     #TODO: ANN
 
-    $pubsub->json('notify')->notify( notify => {
+    $c->redis->pubsub->json('notify')->notify( notify => {
         event   => 'user_unbanned',
         payload => $c->spec_filter($user->formatted, 'User'),
     });
diff --git a/lib/CF/Controller/Websockets.pm b/lib/CF/Controller/Websockets.pm
index 41746d2..1a5d4b1 100644
--- a/lib/CF/Controller/Websockets.pm
+++ b/lib/CF/Controller/Websockets.pm
@@ -22,16 +22,11 @@ sub main {
         $c->res->headers->add('Sec-WebSocket-Extensions' => 'permessage-deflate');
     }
 
-    $c->redis->pubsub->json('notify')->listen(
+    $c->redis->pubsub->listen(
         notify => sub($pubsub, $payload) {
             $c->send($payload);
         }
     );
-# PG_PUBSUB
-#    my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
-#    $pubsub->listen(notify => sub($pubsub, $payload) {
-#        $c->send($payload);
-#    });
 
     $c->on(json => sub( $c, $message ) {
         if ( $message->{event} eq 'KEEPALIVE' ) {
@@ -103,8 +98,6 @@ sub main {
     });
 
     $c->on(finish => sub ($c, $code, $reason = undef) {
-# PG_PUBSUB
-#        $pubsub->unlisten('notify');
         $c->redis->pubsub->unlisten('notify');
         $c->app->log->debug("WebSocket closed with status $code");
     });
diff --git a/sql/8/up.sql b/sql/8/up.sql
index a7c438c..1e28781 100644
--- a/sql/8/up.sql
+++ b/sql/8/up.sql
@@ -18,15 +18,4 @@ create table "events" (
     foreign key ("owner_id") references "users" ("id") on update cascade on delete restrict
 );
 
-create table "events_acl" (
-    "id" integer not null default nextval('uid_seq'),
-    "event_id" integer not null,
-    "subject_class" varchar(8) not null,
-    "subject" text not null,
-    "role" text,
-    primary key("id"),
-    unique("event_id", "subject_class", "subject"),
-    foreign key ("event_id") references "events" ("id") on update cascade on delete cascade
-);
-
 alter table "program" add column "event_id" integer;
-- 
GitLab