From eb404523bfd77c62c82f69935aeddaf9cfba27e9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz>
Date: Wed, 23 Dec 2020 20:17:24 +0100
Subject: [PATCH] Novy format keealive

---
 Dockerfile                      |  5 +++--
 lib/CF/Controller/Websockets.pm | 30 +++++++++++++-----------------
 2 files changed, 16 insertions(+), 19 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index 7502e3c..5b2c33a 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,11 +1,12 @@
-FROM debian:buster
-#FROM debian:buster-slim
+FROM debian:buster-slim
 
 RUN apt-get update && apt-get install -y \
     wget \
     cpanminus \
     build-essential \
     libcrypt-openssl-rsa-perl \
+    libdata-random-perl \
+    libdigest-sha-perl \
     libdbd-pg-perl \
     libdbi-perl \
     libdbix-class-perl \
diff --git a/lib/CF/Controller/Websockets.pm b/lib/CF/Controller/Websockets.pm
index e3cf643..9acd023 100644
--- a/lib/CF/Controller/Websockets.pm
+++ b/lib/CF/Controller/Websockets.pm
@@ -3,6 +3,8 @@ package CF::Controller::Websockets;
 use Mojo::Base 'Mojolicious::Controller';
 use Mojo::Pg::PubSub;
 use JSON;
+use Digest::SHA qw(hmac_sha1_hex);
+
 use feature 'signatures';
 no warnings qw{ experimental::signatures };
 
@@ -14,21 +16,6 @@ sub main {
 
     $c->inactivity_timeout(SOCKET_INACTIVITY_TIMEOUT);
 
-    my $user;
-
-    if ( my $key = $c->req->headers->authorization ) {
-       if ( $key =~ s/Bearer\s+//i ) {
-            $c->oauth_token($key);
-
-            if ( $c->user ) {
-                $user = $c->schema->resultset('User')->find_or_create(
-                    $c->user, { key => 'uuid'}
-                );
-            }
-        }
-    }
-
-
     my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
 
     $pubsub->listen(notify => sub($pubsub, $payload) {
@@ -36,7 +23,12 @@ sub main {
     });
 
     $c->on(message => sub( $c, $message ) {
-        $user->update({keepalive => \'now()'}) if $user;
+        my $data = from_json($message);
+
+        if ($data->{payload} =~ /^\d+$/) {
+            my $user = $c->schama->resultset('User')->find({ id => $1 });
+            $user->update({keepalive => \'now()'}) if $user;
+        }
 
         my $min_alive_time = "now() - '" . USER_ALIVE_TIMEOUT. " s'::interval";
 
@@ -45,7 +37,11 @@ sub main {
                 keepalive => { '>' => \$min_alive_time },
             }
         );
-        $c->send(to_json({ event => 'online_users_updated', payload => $alive }));
+
+        $c->send(to_json({ event => 'online_users_updated', payload => {
+            all     => $alive,
+            members => $alive,
+        }}));
 
     });
 
-- 
GitLab