diff --git a/src/routes/users/block_user.rs b/src/routes/users/block_user.rs index e7f12663c24e794fdd391e881949e12dceec1ecf..4f86bf9c914c68f1c411c6e75e16cf7656621ea4 100644 --- a/src/routes/users/block_user.rs +++ b/src/routes/users/block_user.rs @@ -1,4 +1,4 @@ -use crate::util::result::Result; +use crate::{notifications::{events::ClientboundNotification, hive}, util::result::Result}; use crate::{ database::entities::RelationshipStatus, database::entities::User, database::get_collection, database::guards::reference::Ref, database::permissions::get_relationship, util::result::Error, @@ -32,6 +32,12 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { with: "user", })?; + ClientboundNotification::UserRelationship { + id: user.id.clone(), + user: target.id.clone(), + status: RelationshipStatus::Blocked + }.publish(user.id.clone()).await.ok(); + Ok(json!({ "status": "Blocked" })) } RelationshipStatus::None => { @@ -65,7 +71,25 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { None ) ) { - Ok(_) => Ok(json!({ "status": "Blocked" })), + Ok(_) => { + try_join!( + ClientboundNotification::UserRelationship { + id: user.id.clone(), + user: target.id.clone(), + status: RelationshipStatus::Blocked + }.publish(user.id.clone()), + ClientboundNotification::UserRelationship { + id: target.id.clone(), + user: user.id.clone(), + status: RelationshipStatus::BlockedOther + }.publish(target.id.clone()) + ).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 { operation: "update_one", with: "user", @@ -101,7 +125,22 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { None ) ) { - Ok(_) => Ok(json!({ "status": "Blocked" })), + Ok(_) => { + try_join!( + ClientboundNotification::UserRelationship { + id: user.id.clone(), + user: target.id.clone(), + status: RelationshipStatus::Blocked + }.publish(user.id.clone()), + ClientboundNotification::UserRelationship { + id: target.id.clone(), + user: user.id.clone(), + status: RelationshipStatus::BlockedOther + }.publish(target.id.clone()) + ).ok(); + + Ok(json!({ "status": "Blocked" })) + }, Err(_) => Err(Error::DatabaseError { operation: "update_one", with: "user", diff --git a/src/routes/users/remove_friend.rs b/src/routes/users/remove_friend.rs index 468a6be47416a6c30d40c68ed90d11a5e295d3b6..97f6f20fffc985e04147f4200146ee0fdc3e215c 100644 --- a/src/routes/users/remove_friend.rs +++ b/src/routes/users/remove_friend.rs @@ -1,4 +1,4 @@ -use crate::util::result::Result; +use crate::{notifications::{hive, events::ClientboundNotification}, util::result::Result}; use crate::{ database::entities::RelationshipStatus, database::entities::User, database::get_collection, database::guards::reference::Ref, database::permissions::get_relationship, util::result::Error, @@ -6,6 +6,7 @@ use crate::{ use futures::try_join; use mongodb::bson::doc; use rocket_contrib::json::JsonValue; +use hive_pubsub::PubSub; #[delete("/<target>/friend")] pub async fn req(user: User, target: Ref) -> Result<JsonValue> { @@ -47,7 +48,26 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { None ) ) { - Ok(_) => Ok(json!({ "status": "None" })), + Ok(_) => { + try_join!( + ClientboundNotification::UserRelationship { + id: user.id.clone(), + user: target.id.clone(), + status: RelationshipStatus::None + }.publish(user.id.clone()), + ClientboundNotification::UserRelationship { + id: target.id.clone(), + user: user.id.clone(), + status: RelationshipStatus::None + }.publish(target.id.clone()) + ).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 { operation: "update_one", with: "user", diff --git a/src/routes/users/unblock_user.rs b/src/routes/users/unblock_user.rs index 585170f03f5e2fe9d894e85a43a021818a804aa5..68ebec22c466bb9a6fd4c29f978091f12121f3ad 100644 --- a/src/routes/users/unblock_user.rs +++ b/src/routes/users/unblock_user.rs @@ -1,4 +1,4 @@ -use crate::util::result::Result; +use crate::{notifications::{events::ClientboundNotification, hive}, util::result::Result}; use crate::{ database::entities::RelationshipStatus, database::entities::User, database::get_collection, database::guards::reference::Ref, database::permissions::get_relationship, util::result::Error, @@ -39,6 +39,12 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { with: "user", })?; + ClientboundNotification::UserRelationship { + id: user.id.clone(), + user: target.id.clone(), + status: RelationshipStatus::BlockedOther + }.publish(user.id.clone()).await.ok(); + Ok(json!({ "status": "BlockedOther" })) } RelationshipStatus::BlockedOther => { @@ -70,7 +76,26 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { None ) ) { - Ok(_) => Ok(json!({ "status": "None" })), + Ok(_) => { + try_join!( + ClientboundNotification::UserRelationship { + id: user.id.clone(), + user: target.id.clone(), + status: RelationshipStatus::None + }.publish(user.id.clone()), + ClientboundNotification::UserRelationship { + id: target.id.clone(), + user: user.id.clone(), + status: RelationshipStatus::None + }.publish(target.id.clone()) + ).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 { operation: "update_one", with: "user",