Move new subscriptions to a unfied prehandle function.
This commit is contained in:
@@ -56,19 +56,7 @@ impl Channel {
|
|||||||
with: "channel",
|
with: "channel",
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
// ! IMPORTANT FIXME: THESE SUBSCRIPTIONS SHOULD BE DONE FROM HIVE NOT HERE!!!
|
|
||||||
let channel_id = self.id().to_string();
|
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)
|
ClientboundNotification::ChannelCreate(self)
|
||||||
.publish(channel_id)
|
.publish(channel_id)
|
||||||
.await
|
.await
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
use rauth::auth::Session;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use rauth::auth::Session;
|
||||||
|
use hive_pubsub::PubSub;
|
||||||
use snafu::Snafu;
|
use snafu::Snafu;
|
||||||
|
|
||||||
use super::hive::get_hive;
|
use super::hive::{get_hive, subscribe_if_exists};
|
||||||
use crate::database::*;
|
use crate::database::*;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Snafu)]
|
#[derive(Serialize, Deserialize, Debug, Snafu)]
|
||||||
@@ -65,6 +66,48 @@ pub enum ClientboundNotification {
|
|||||||
|
|
||||||
impl ClientboundNotification {
|
impl ClientboundNotification {
|
||||||
pub async fn publish(self, topic: String) -> Result<(), String> {
|
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
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -36,8 +36,6 @@ pub async fn listen() {
|
|||||||
.fuse()
|
.fuse()
|
||||||
.await
|
.await
|
||||||
.expect("Hive hit an error");
|
.expect("Hive hit an error");
|
||||||
|
|
||||||
dbg!("a");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn subscribe_multiple(user: String, topics: Vec<String>) -> Result<(), String> {
|
pub fn subscribe_multiple(user: String, topics: Vec<String>) -> Result<(), String> {
|
||||||
|
|||||||
@@ -137,9 +137,6 @@ pub async fn req(user: User, username: String) -> Result<JsonValue> {
|
|||||||
)
|
)
|
||||||
.ok();
|
.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" }))
|
Ok(json!({ "status": "Outgoing" }))
|
||||||
}
|
}
|
||||||
Err(_) => Err(Error::DatabaseError {
|
Err(_) => Err(Error::DatabaseError {
|
||||||
|
|||||||
@@ -90,9 +90,6 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
|||||||
)
|
)
|
||||||
.ok();
|
.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" }))
|
Ok(json!({ "status": "Blocked" }))
|
||||||
}
|
}
|
||||||
Err(_) => Err(Error::DatabaseError {
|
Err(_) => Err(Error::DatabaseError {
|
||||||
|
|||||||
@@ -60,10 +60,6 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
|||||||
)
|
)
|
||||||
.ok();
|
.ok();
|
||||||
|
|
||||||
let hive = hive::get_hive();
|
|
||||||
hive.unsubscribe(&user.id, &target.id).ok();
|
|
||||||
hive.unsubscribe(&target.id, &user.id).ok();
|
|
||||||
|
|
||||||
Ok(json!({ "status": "None" }))
|
Ok(json!({ "status": "None" }))
|
||||||
}
|
}
|
||||||
Err(_) => Err(Error::DatabaseError {
|
Err(_) => Err(Error::DatabaseError {
|
||||||
|
|||||||
@@ -90,10 +90,6 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
|||||||
)
|
)
|
||||||
.ok();
|
.ok();
|
||||||
|
|
||||||
let hive = hive::get_hive();
|
|
||||||
hive.unsubscribe(&user.id, &target.id).ok();
|
|
||||||
hive.unsubscribe(&target.id, &user.id).ok();
|
|
||||||
|
|
||||||
Ok(json!({ "status": "None" }))
|
Ok(json!({ "status": "None" }))
|
||||||
}
|
}
|
||||||
Err(_) => Err(Error::DatabaseError {
|
Err(_) => Err(Error::DatabaseError {
|
||||||
|
|||||||
Reference in New Issue
Block a user