diff --git a/src/database/channel.rs b/src/database/channel.rs index 2ec7822bfb844d6c7128e33801cc442e6e383b12..6bad1b817e1dd38bfa56dff8d79273d32e10d569 100644 --- a/src/database/channel.rs +++ b/src/database/channel.rs @@ -145,25 +145,17 @@ pub fn process_event(event: &Notification) { match event { Notification::group_user_join(ev) => { let mut cache = CACHE.lock().unwrap(); - let entry = cache.pop(&ev.id); - - if entry.is_some() { - let mut channel = entry.unwrap(); + if let Some(channel) = cache.peek_mut(&ev.id) { channel.recipients.as_mut().unwrap().push(ev.user.clone()); - cache.put(ev.id.clone(), channel); } } Notification::group_user_leave(ev) => { let mut cache = CACHE.lock().unwrap(); - let entry = cache.pop(&ev.id); - - if entry.is_some() { - let mut channel = entry.unwrap(); + if let Some(channel) = cache.peek_mut(&ev.id) { let recipients = channel.recipients.as_mut().unwrap(); if let Some(pos) = recipients.iter().position(|x| *x == ev.user) { recipients.remove(pos); } - cache.put(ev.id.clone(), channel); } } Notification::guild_channel_create(ev) => { diff --git a/src/database/guild.rs b/src/database/guild.rs index d236056b1622b729823bac927e0002bc900e0c15..68cc5c5ddb0363f6fa00760815583887fcfd1d05 100644 --- a/src/database/guild.rs +++ b/src/database/guild.rs @@ -206,8 +206,8 @@ use crate::notifications::events::Notification; pub fn process_event(event: &Notification) { match event { - Notification::guild_channel_create(ev) => {} // ? for later use - Notification::guild_channel_delete(ev) => {} // ? for later use + Notification::guild_channel_create(_ev) => {} // ? for later use + Notification::guild_channel_delete(_ev) => {} // ? for later use Notification::guild_delete(ev) => { let mut cache = CACHE.lock().unwrap(); cache.pop(&ev.id); diff --git a/src/database/user.rs b/src/database/user.rs index 96e82f97d5f838e0af9a45d14a0cf266e656e157..286613cf0b5bbde9a8da415ef2a4b23a0d363e93 100644 --- a/src/database/user.rs +++ b/src/database/user.rs @@ -84,7 +84,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for User { type Error = AuthError; fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> { - let u = request.headers().get("x-user").next(); + let u = request.headers().get("x-user").next(); let t = request.headers().get("x-auth-token").next(); if let Some(uid) = u { @@ -130,3 +130,34 @@ impl<'r> FromParam<'r> for User { } } } + +use crate::notifications::events::Notification; + +pub fn process_event(event: &Notification) { + match event { + Notification::user_friend_status(ev) => { + let mut cache = CACHE.lock().unwrap(); + if let Some(user) = cache.peek_mut(&ev.id) { + if let Some(relations) = user.relations.as_mut() { + if ev.status == 0 { + if let Some(pos) = relations.iter().position(|x| x.id == ev.user) { + relations.remove(pos); + } + } else { + if let Some(entry) = relations.iter_mut().find(|x| x.id == ev.user) { + entry.status = ev.status as u8; + } else { + relations.push( + UserRelationship { + id: ev.id.clone(), + status: ev.status as u8 + } + ); + } + } + } + } + } + _ => {} + } +} diff --git a/src/notifications/events/mod.rs b/src/notifications/events/mod.rs index 4e9ba6b7f2c409396f4d15e27694c6405837ea1f..d05111d203391f42ca7f99f85ec154df97d48d3f 100644 --- a/src/notifications/events/mod.rs +++ b/src/notifications/events/mod.rs @@ -41,5 +41,7 @@ impl Notification { pub fn push_to_cache(&self) { crate::database::channel::process_event(&self); + crate::database::guild::process_event(&self); + crate::database::user::process_event(&self); } } diff --git a/src/notifications/events/users.rs b/src/notifications/events/users.rs index 88617ebced6490c301fb21bbc937c28a304bc91f..7a23446e295c3e76534c9a69a1a1d31b9436ae67 100644 --- a/src/notifications/events/users.rs +++ b/src/notifications/events/users.rs @@ -3,5 +3,6 @@ use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct FriendStatus { pub id: String, + pub user: String, pub status: i32, } diff --git a/src/routes/user.rs b/src/routes/user.rs index 2c4cdb6ca36fd56fe9e846681ed922272c0f1bd2..ebe8dcf4cbc1c5232bc1a52de42a2adda6704106 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -268,7 +268,8 @@ pub fn add_friend(user: User, target: User) -> Response { vec![target.id.clone()], None, Notification::user_friend_status(FriendStatus { - id: user.id.clone(), + id: target.id.clone(), + user: user.id.clone(), status: Relationship::Friend as i32, }), ); @@ -277,7 +278,8 @@ pub fn add_friend(user: User, target: User) -> Response { vec![user.id.clone()], None, Notification::user_friend_status(FriendStatus { - id: target.id.clone(), + id: user.id.clone(), + user: target.id.clone(), status: Relationship::Friend as i32, }), ); @@ -339,7 +341,8 @@ pub fn add_friend(user: User, target: User) -> Response { vec![user.id.clone()], None, Notification::user_friend_status(FriendStatus { - id: target.id.clone(), + id: user.id.clone(), + user: target.id.clone(), status: Relationship::Outgoing as i32, }), ); @@ -348,7 +351,8 @@ pub fn add_friend(user: User, target: User) -> Response { vec![target.id.clone()], None, Notification::user_friend_status(FriendStatus { - id: user.id.clone(), + id: target.id.clone(), + user: user.id.clone(), status: Relationship::Incoming as i32, }), ); @@ -414,7 +418,8 @@ pub fn remove_friend(user: User, target: User) -> Response { vec![user.id.clone()], None, Notification::user_friend_status(FriendStatus { - id: target.id.clone(), + id: user.id.clone(), + user: target.id.clone(), status: Relationship::NONE as i32, }), ); @@ -423,7 +428,8 @@ pub fn remove_friend(user: User, target: User) -> Response { vec![target.id.clone()], None, Notification::user_friend_status(FriendStatus { - id: user.id.clone(), + id: target.id.clone(), + user: user.id.clone(), status: Relationship::NONE as i32, }), ); @@ -488,7 +494,8 @@ pub fn block_user(user: User, target: User) -> Response { vec![user.id.clone()], None, Notification::user_friend_status(FriendStatus { - id: target.id.clone(), + id: user.id.clone(), + user: target.id.clone(), status: Relationship::Blocked as i32, }), ); @@ -497,7 +504,8 @@ pub fn block_user(user: User, target: User) -> Response { vec![target.id.clone()], None, Notification::user_friend_status(FriendStatus { - id: user.id.clone(), + id: target.id.clone(), + user: user.id.clone(), status: Relationship::BlockedOther as i32, }), ); @@ -554,7 +562,8 @@ pub fn block_user(user: User, target: User) -> Response { vec![user.id.clone()], None, Notification::user_friend_status(FriendStatus { - id: target.id.clone(), + id: user.id.clone(), + user: target.id.clone(), status: Relationship::Blocked as i32, }), ); @@ -563,7 +572,8 @@ pub fn block_user(user: User, target: User) -> Response { vec![target.id.clone()], None, Notification::user_friend_status(FriendStatus { - id: user.id.clone(), + id: target.id.clone(), + user: user.id.clone(), status: Relationship::BlockedOther as i32, }), ); @@ -603,7 +613,8 @@ pub fn block_user(user: User, target: User) -> Response { vec![user.id.clone()], None, Notification::user_friend_status(FriendStatus { - id: target.id.clone(), + id: user.id.clone(), + user: target.id.clone(), status: Relationship::Blocked as i32, }), ); @@ -646,7 +657,8 @@ pub fn unblock_user(user: User, target: User) -> Response { vec![user.id.clone()], None, Notification::user_friend_status(FriendStatus { - id: target.id.clone(), + id: user.id.clone(), + user: target.id.clone(), status: Relationship::BlockedOther as i32, }), ); @@ -695,7 +707,8 @@ pub fn unblock_user(user: User, target: User) -> Response { vec![user.id.clone()], None, Notification::user_friend_status(FriendStatus { - id: target.id.clone(), + id: user.id.clone(), + user: target.id.clone(), status: Relationship::NONE as i32, }), ); @@ -704,7 +717,8 @@ pub fn unblock_user(user: User, target: User) -> Response { vec![target.id.clone()], None, Notification::user_friend_status(FriendStatus { - id: user.id.clone(), + id: target.id.clone(), + user: user.id.clone(), status: Relationship::NONE as i32, }), );