From 2a64702d20e1e9bbb54d7af02f80eac2a48dfd47 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz>
Date: Mon, 3 Jan 2022 19:29:23 +0100
Subject: [PATCH] Presun notifikace do helperu

---
 lib/CF/Controller/Announcements.pm | 23 ++++++++-----------
 lib/CF/Controller/Config.pm        |  7 +++---
 lib/CF/Controller/Posts.pm         | 37 ++++++++++++------------------
 lib/CF/Controller/Program.pm       |  7 +++---
 lib/CF/Controller/Users.pm         | 19 +++++++--------
 lib/CF/Helpers/Core.pm             | 11 +++++++++
 openapi.yaml                       | 11 +++++----
 7 files changed, 56 insertions(+), 59 deletions(-)

diff --git a/lib/CF/Controller/Announcements.pm b/lib/CF/Controller/Announcements.pm
index 7ae3cf4..f63227d 100644
--- a/lib/CF/Controller/Announcements.pm
+++ b/lib/CF/Controller/Announcements.pm
@@ -22,10 +22,9 @@ sub create ($c) {
         id => $announcement->id,
     });
 
-    $c->redis->pubsub->json('notify')->notify( notify => {
-        event   => 'announcement_created',
-        payload => $announcement->format(),
-    });
+    $c->notify('announcement_created',
+        $announcement->format(),
+    );
 
     $c->render(
         status  => 201,
@@ -70,13 +69,12 @@ sub update ($c) {
 
     $entry->update( $update );
 
-    $c->redis->pubsub->json('notify')->notify( notify => {
-        event   => 'announcement_changed',
-        payload => {
+    $c->notify('announcement_changed',
+        {
             id => $entry->id,
             %{ $update },
         }
-    });
+    );
 
     $guard->commit;
 
@@ -93,12 +91,9 @@ sub delete ($c) {
 
     $entry->update( { deleted => \'now()'} );
 
-    $c->redis->pubsub->json('notify')->notify( notify => {
-        event   => 'announcement_deleted',
-        payload => {
-            id => $entry->id,
-        }
-    });
+    $c->notify('announcement_deleted',
+        { id => $entry->id }
+    );
 
     $guard->commit;
 
diff --git a/lib/CF/Controller/Config.pm b/lib/CF/Controller/Config.pm
index 8702d55..3117c78 100644
--- a/lib/CF/Controller/Config.pm
+++ b/lib/CF/Controller/Config.pm
@@ -34,13 +34,12 @@ sub update ($c) {
 
     $entry->update( $update );
 
-    $c->redis->pubsub->json('notify')->notify( notify => {
-        event   => 'config_entry_changed',
-        payload => {
+    $c->notify('config_entry_changed',
+        {
             id => $entry->id,
             %{ $update },
         }
-    });
+    );
 
     $guard->commit;
 
diff --git a/lib/CF/Controller/Posts.pm b/lib/CF/Controller/Posts.pm
index 8a7130b..4e41fdf 100644
--- a/lib/CF/Controller/Posts.pm
+++ b/lib/CF/Controller/Posts.pm
@@ -58,10 +58,9 @@ sub create ($c) {
         content          => $args->{content},
     });
 
-    $c->redis->pubsub->json('notify')->notify( notify => {
-        event   => 'post_created',
-        payload => $post->view->format(),
-    });
+    $c->notify('post_created',
+        $post->view->format(),
+    );
 
     $c->render(
         status  => 201,
@@ -224,10 +223,9 @@ sub update ($c) {
                 id => $announcement->id
             });
 
-            $c->redis->pubsub->json('notify')->notify( notify => {
-                event   => 'announcement_created',
-                payload => $announcement->format(),
-            });
+            $c->>notify('announcement_created',
+                $announcement->format(),
+            );
         }
     }
 
@@ -242,13 +240,12 @@ sub update ($c) {
         changed => \'now()',
     });
 
-    $c->redis->pubsub->json('notify')->notify( notify => {
-        event   => 'post_changed',
-        payload => {
+    $c->notify('post_changed',
+        {
             id => $post->id,
             %{ $update },
         }
-    });
+    );
 
     $guard->commit;
     $c->render(status => 204, text => '');
@@ -264,12 +261,9 @@ sub delete ($c) {
 
     $post->update({ deleted => \'now()', });
 
-    $c->redis->pubsub->json('notify')->notify( notify => {
-        event   => 'post_deleted',
-        payload => {
-            id => $post->id,
-        }
-    });
+    $c->notify('post_deleted',
+        { id => $post->id }
+    );
 
     $guard->commit;
     $c->render(status => 204, text => '');
@@ -332,13 +326,12 @@ sub ranking ($c) {
         });
     }
 
-    $c->redis->pubsub->json('notify')->notify(notify => {
-        event   => 'post_ranked',
-        payload => {
+    $c->notify('post_ranked',
+        {
             id => $post->id,
             %{ $update },
         }
-    });
+    );
 
     $guard->commit;
     $c->render(status => 204, text => '');
diff --git a/lib/CF/Controller/Program.pm b/lib/CF/Controller/Program.pm
index 5b8744b..d1b0e66 100644
--- a/lib/CF/Controller/Program.pm
+++ b/lib/CF/Controller/Program.pm
@@ -46,13 +46,12 @@ sub update ($c) {
 
     $entry->update( $update );
 
-    $c->redis->pubsub->json("notify")->notify(notify => {
-        event   => 'program_entry_changed',
-        payload => {
+    $c->notify('program_entry_changed',
+        {
             id => $entry->id,
             %{ $update },
         }
-    });
+    );
 
     $guard->commit;
 
diff --git a/lib/CF/Controller/Users.pm b/lib/CF/Controller/Users.pm
index 914eb0b..3bf89f9 100644
--- a/lib/CF/Controller/Users.pm
+++ b/lib/CF/Controller/Users.pm
@@ -49,15 +49,13 @@ sub ban ($c){
         id => $announcement->id,
     });
 
-    $c->redis->pubsub->json('notify')->notify( notify => {
-        event   => 'announcement_created',
-        payload => $announcement->format(),
+    $c->notify('announcement_created',
+        $announcement->format(),
     });
 
-    $c->redis->pubsub->json('notify')->notify( notify => {
-        event   => 'user_banned',
-        payload => $c->spec_filter($user->formatted, 'User'),
-    });
+    $c->notify('user_banned',
+        $c->spec_filter($user->formatted, 'User'),
+    );
 
     $guard->commit;
     $c->render(status => 204, text => '');
@@ -70,10 +68,9 @@ sub unban ($c){
     $user->update({ banned_until => undef });
     #TODO: ANN
 
-    $c->redis->pubsub->json('notify')->notify( notify => {
-        event   => 'user_unbanned',
-        payload => $c->spec_filter($user->formatted, 'User'),
-    });
+    $c->notify('user_unbanned',
+        $c->spec_filter($user->formatted, 'User'),
+    );
 
     $guard->commit;
     $c->render(status => 204, text => '');
diff --git a/lib/CF/Helpers/Core.pm b/lib/CF/Helpers/Core.pm
index 1251a2b..6a7dc73 100644
--- a/lib/CF/Helpers/Core.pm
+++ b/lib/CF/Helpers/Core.pm
@@ -6,6 +6,7 @@ no warnings qw{ experimental::signatures } ;
 
 use YAML;
 use Mojo::JWT;
+use Mojo::JSON qw(encode_json);
 
 sub register ($class, $self, $conf) {
 
@@ -199,6 +200,16 @@ sub register ($class, $self, $conf) {
         );
 
     });
+
+    $self->helper( notify => sub ( $c, $event, $payload ) {
+
+        my $content = encode_json ({
+            event   => $event,
+            payload => $payload,
+        });
+
+        $self->redis->pubsub->notify( notify => $content );
+    }
 }
 
 1;
diff --git a/openapi.yaml b/openapi.yaml
index 420a451..abc2f27 100644
--- a/openapi.yaml
+++ b/openapi.yaml
@@ -540,7 +540,7 @@ paths:
     post:
       x-mojo-to: events#role_add
       security:
-        - Bearer: ['*']
+        - Bearer: ['organizer']
       tags:
         - events
       summary: "Priradit roli pro udalost"
@@ -563,20 +563,23 @@ paths:
                   type: string
                   enum: ['user', 'group']
                   example: 'user'
-                subject:
+                subject_value:
                   type: string
                   example: 'cen:f'
+                subject_label:
+                  type: string
+                  example: 'Celostátní fórum'
                 role:
                   type: string
                   enum: ['chairman', 'member', 'guest', 'jitsi']
                   example: 'member'
               required:
                 - subject_class
-                - subject
+                - subject_value
                 - role
       responses:
         201:
-          description: Role set
+          description: ACL record created
           content:
             application/json:
               schema:
-- 
GitLab