From 8cbba0671d5c932321760d61909c51f478c4ba93 Mon Sep 17 00:00:00 2001
From: Paul Makles <paulmakles@gmail.com>
Date: Tue, 19 Jan 2021 11:46:22 +0000
Subject: [PATCH] Move new subscriptions to a unfied prehandle function.

---
 src/database/entities/channel.rs  | 12 --------
 src/notifications/events.rs       | 47 +++++++++++++++++++++++++++++--
 src/notifications/hive.rs         |  2 --
 src/routes/users/add_friend.rs    |  3 --
 src/routes/users/block_user.rs    |  3 --
 src/routes/users/remove_friend.rs |  4 ---
 src/routes/users/unblock_user.rs  |  4 ---
 7 files changed, 45 insertions(+), 30 deletions(-)

diff --git a/src/database/entities/channel.rs b/src/database/entities/channel.rs
index 23f964d..7f5c277 100644
--- a/src/database/entities/channel.rs
+++ b/src/database/entities/channel.rs
@@ -56,19 +56,7 @@ impl Channel {
                 with: "channel",
             })?;
 
-        // ! IMPORTANT FIXME: THESE SUBSCRIPTIONS SHOULD BE DONE FROM HIVE NOT HERE!!!
         let channel_id = self.id().to_string();
-        match &self {
-            Channel::SavedMessages { user, .. } => {
-                hive::subscribe_if_exists(user.clone(), channel_id.clone()).ok();
-            }
-            Channel::DirectMessage { recipients, .. } | Channel::Group { recipients, .. } => {
-                for recipient in recipients {
-                    hive::subscribe_if_exists(recipient.clone(), channel_id.clone()).ok();
-                }
-            }
-        }
-
         ClientboundNotification::ChannelCreate(self)
             .publish(channel_id)
             .await
diff --git a/src/notifications/events.rs b/src/notifications/events.rs
index b408d62..4031d4e 100644
--- a/src/notifications/events.rs
+++ b/src/notifications/events.rs
@@ -1,8 +1,9 @@
-use rauth::auth::Session;
 use serde::{Deserialize, Serialize};
+use rauth::auth::Session;
+use hive_pubsub::PubSub;
 use snafu::Snafu;
 
-use super::hive::get_hive;
+use super::hive::{get_hive, subscribe_if_exists};
 use crate::database::*;
 
 #[derive(Serialize, Deserialize, Debug, Snafu)]
@@ -65,6 +66,48 @@ pub enum ClientboundNotification {
 
 impl ClientboundNotification {
     pub async fn publish(self, topic: String) -> Result<(), String> {
+        prehandle_hook(&self); // ! TODO: this should be moved to pubsub
         hive_pubsub::backend::mongo::publish(get_hive(), &topic, self).await
     }
 }
+
+pub fn prehandle_hook(notification: &ClientboundNotification) {
+    match &notification {
+        ClientboundNotification::ChannelGroupJoin { id, user } => {
+            subscribe_if_exists(user.clone(), id.clone()).ok();
+        }
+        ClientboundNotification::ChannelCreate(channel) => {
+            let channel_id = channel.id();
+            match &channel {
+                Channel::SavedMessages { user, .. } => {
+                    subscribe_if_exists(user.clone(), channel_id.to_string()).ok();
+                }
+                Channel::DirectMessage { recipients, .. } | Channel::Group { recipients, .. } => {
+                    for recipient in recipients {
+                        subscribe_if_exists(recipient.clone(), channel_id.to_string()).ok();
+                    }
+                }
+            }
+        }
+        ClientboundNotification::ChannelGroupLeave { id, user } => {
+            get_hive()
+                .hive
+                .unsubscribe(&user.to_string(), &id.to_string())
+                .ok();
+        }
+        ClientboundNotification::UserRelationship { id, user, status } => {
+            match status {
+                RelationshipStatus::None => {
+                    get_hive()
+                        .hive
+                        .unsubscribe(&id.to_string(), &user.to_string())
+                        .ok();
+                },
+                _ => {
+                    subscribe_if_exists(id.clone(), user.clone()).ok();
+                }
+            }
+        }
+        _ => {}
+    }
+}
diff --git a/src/notifications/hive.rs b/src/notifications/hive.rs
index e3025e5..e7d79bf 100644
--- a/src/notifications/hive.rs
+++ b/src/notifications/hive.rs
@@ -36,8 +36,6 @@ pub async fn listen() {
         .fuse()
         .await
         .expect("Hive hit an error");
-
-    dbg!("a");
 }
 
 pub fn subscribe_multiple(user: String, topics: Vec<String>) -> Result<(), String> {
diff --git a/src/routes/users/add_friend.rs b/src/routes/users/add_friend.rs
index dfbae93..298dc92 100644
--- a/src/routes/users/add_friend.rs
+++ b/src/routes/users/add_friend.rs
@@ -137,9 +137,6 @@ pub async fn req(user: User, username: String) -> Result<JsonValue> {
                     )
                     .ok();
 
-                    hive::subscribe_if_exists(user.id.clone(), target_id.to_string()).ok();
-                    hive::subscribe_if_exists(target_id.to_string(), user.id.clone()).ok();
-
                     Ok(json!({ "status": "Outgoing" }))
                 }
                 Err(_) => Err(Error::DatabaseError {
diff --git a/src/routes/users/block_user.rs b/src/routes/users/block_user.rs
index cae153f..9c069e5 100644
--- a/src/routes/users/block_user.rs
+++ b/src/routes/users/block_user.rs
@@ -90,9 +90,6 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
                     )
                     .ok();
 
-                    hive::subscribe_if_exists(user.id.clone(), target.id.clone()).ok();
-                    hive::subscribe_if_exists(target.id.clone(), user.id.clone()).ok();
-
                     Ok(json!({ "status": "Blocked" }))
                 }
                 Err(_) => Err(Error::DatabaseError {
diff --git a/src/routes/users/remove_friend.rs b/src/routes/users/remove_friend.rs
index 9054c97..e68f20a 100644
--- a/src/routes/users/remove_friend.rs
+++ b/src/routes/users/remove_friend.rs
@@ -60,10 +60,6 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
                     )
                     .ok();
 
-                    let hive = hive::get_hive();
-                    hive.unsubscribe(&user.id, &target.id).ok();
-                    hive.unsubscribe(&target.id, &user.id).ok();
-
                     Ok(json!({ "status": "None" }))
                 }
                 Err(_) => Err(Error::DatabaseError {
diff --git a/src/routes/users/unblock_user.rs b/src/routes/users/unblock_user.rs
index 1c4aaba..0d23852 100644
--- a/src/routes/users/unblock_user.rs
+++ b/src/routes/users/unblock_user.rs
@@ -90,10 +90,6 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
                             )
                             .ok();
 
-                            let hive = hive::get_hive();
-                            hive.unsubscribe(&user.id, &target.id).ok();
-                            hive.unsubscribe(&target.id, &user.id).ok();
-
                             Ok(json!({ "status": "None" }))
                         }
                         Err(_) => Err(Error::DatabaseError {
-- 
GitLab