diff --git a/src/database/entities/channel.rs b/src/database/entities/channel.rs index 23f964db27a1d6222116487af6017c51728a43c8..7f5c277183f174dedd62ae768753dbd8c4baa4c5 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 b408d62eea7703c3fd0f94eba08304691c790bb8..4031d4e7063ea619bbb915b51e16f16239003d39 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 ¬ification { + 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 e3025e5eb3c3eddf081aae863862ff97c3e00d5d..e7d79bfad092e9ee4a5d40f803d15f3fec5b8d94 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 dfbae9316f401d32e0ada1741b4fda4e533c2a5c..298dc9228ce87e69263dcbf5620a93e1bbf54538 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 cae153f1aa6182c3e7eea7dd524a69eba1354ba6..9c069e5e90fbe8beb3d4b1d2960017a13009b91f 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 9054c97c0d8d1a10c3ea140f8e3549a5fed56188..e68f20a44f1dda3ee83a3bbc2abbff398599d15a 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 1c4aabac6f08fba098f15b0fb7e585ca51a1acd5..0d23852130e0ea6b2aca24c52fea1cdfaf0dd861 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 {