From 15357008d6dbe5a510ac29e411c6368b29670b25 Mon Sep 17 00:00:00 2001
From: Paul Makles <paulmakles@gmail.com>
Date: Mon, 18 Jan 2021 14:14:56 +0000
Subject: [PATCH] Push relevant channels in Ready payload.

---
 src/database/guards/mod.rs   |  1 -
 src/notifications/events.rs  |  1 +
 src/notifications/payload.rs | 49 +++++++++++++++++++++++++++++++-----
 3 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/src/database/guards/mod.rs b/src/database/guards/mod.rs
index be4a89d..cf046ce 100644
--- a/src/database/guards/mod.rs
+++ b/src/database/guards/mod.rs
@@ -2,4 +2,3 @@ pub mod reference;
 pub mod user;
 
 pub use reference::Ref;
-// pub use user::*;
diff --git a/src/notifications/events.rs b/src/notifications/events.rs
index 96c57d4..0e3be5d 100644
--- a/src/notifications/events.rs
+++ b/src/notifications/events.rs
@@ -33,6 +33,7 @@ pub enum ClientboundNotification {
     Authenticated,
     Ready {
         users: Vec<User>,
+        channels: Vec<Channel>
     },
 
     /*MessageCreate {
diff --git a/src/notifications/payload.rs b/src/notifications/payload.rs
index c63a730..100a521 100644
--- a/src/notifications/payload.rs
+++ b/src/notifications/payload.rs
@@ -4,10 +4,7 @@ use crate::{
     util::result::{Error, Result},
 };
 use futures::StreamExt;
-use mongodb::{
-    bson::{doc, from_bson, Bson},
-    options::FindOptions,
-};
+use mongodb::{bson::{Bson, doc, from_bson}, options::FindOptions};
 
 use super::websocket::is_online;
 
@@ -61,8 +58,48 @@ pub async fn generate_ready(mut user: User) -> Result<ClientboundNotification> {
         }
     }
 
-    user.online = Some(is_online(&user.id));
+    let mut cursor = get_collection("channels")
+        .find(
+            doc! {
+                "$or": [
+                    {
+                        "type": "SavedMessages",
+                        "user": &user.id
+                    },
+                    {
+                        "type": "DirectMessage",
+                        "recipients": &user.id,
+                        "active": true
+                    },
+                    {
+                        "type": "Group",
+                        "recipients": &user.id
+                    }
+                ]
+            },
+            None
+        )
+        .await
+        .map_err(|_| Error::DatabaseError {
+            operation: "find",
+            with: "channels",
+        })?;
+    
+    let mut channels = vec![];
+    while let Some(result) = cursor.next().await {
+        if let Ok(doc) = result {
+            channels.push(
+                from_bson(Bson::Document(doc))
+                    .map_err(|_| Error::DatabaseError {
+                        operation: "from_bson",
+                        with: "channel",
+                    })?
+            );
+        }
+    }
+
+    user.online = Some(true);
     users.push(user);
 
-    Ok(ClientboundNotification::Ready { users })
+    Ok(ClientboundNotification::Ready { users, channels })
 }
-- 
GitLab