Move new subscriptions to a unfied prehandle function.

This commit is contained in:
Paul Makles
2021-01-19 11:46:22 +00:00
parent c21d7c4620
commit 8cbba0671d
7 changed files with 45 additions and 30 deletions

View File

@@ -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

View File

@@ -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();
}
}
}
_ => {}
}
}

View File

@@ -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> {

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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 {