From 0b90145b310ccdfe2b3a63657f0452d95dcacd37 Mon Sep 17 00:00:00 2001
From: Paul Makles <paulmakles@gmail.com>
Date: Mon, 10 Aug 2020 22:23:58 +0200
Subject: [PATCH] Breaking change to FriendStatus, user cache sync.

---
 src/database/channel.rs           | 12 ++-------
 src/database/guild.rs             |  4 +--
 src/database/user.rs              | 33 +++++++++++++++++++++++-
 src/notifications/events/mod.rs   |  2 ++
 src/notifications/events/users.rs |  1 +
 src/routes/user.rs                | 42 ++++++++++++++++++++-----------
 6 files changed, 67 insertions(+), 27 deletions(-)

diff --git a/src/database/channel.rs b/src/database/channel.rs
index 2ec7822..6bad1b8 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 d236056..68cc5c5 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 96e82f9..286613c 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 4e9ba6b..d05111d 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 88617eb..7a23446 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 2c4cdb6..ebe8dcf 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,
                             }),
                         );
-- 
GitLab