Repository: revoltchat/backend (language: rust)

Issue:
bug: ServerCreate event missing emoji data for new server, only on Ready event for old servers
### What happened?

The websocket event for ServerCreate is missing a list of emojis which causes some inconsistency when using/caching/checking emojis which means that servers your bot joins has no existing emojis and there is no API to fetch a list of emojis for a server either.
![image](https://user-images.githubusercontent.com/17956143/236652920-2bfe0c2b-a1b5-4483-9302-2491723ff0c2.png)

A fix for this would be adding emoji data to the ServerCreate event.


Discussion / hints:
This also causes issues in the frontend - when you initially join a server, you can't access its emojis in the emoji picker without reloading 

Fix patch:
diff --git a/crates/core/database/src/events/client.rs b/crates/core/database/src/events/client.rs
index 59fb7d3e7..de63a9d4a 100644
--- a/crates/core/database/src/events/client.rs
+++ b/crates/core/database/src/events/client.rs
@@ -110,6 +110,7 @@ pub enum EventV1 {
         id: String,
         server: Server,
         channels: Vec<Channel>,
+        emojis: Vec<Emoji>,
     },
 
     /// Update existing server
diff --git a/crates/core/database/src/models/emojis/ops/mongodb.rs b/crates/core/database/src/models/emojis/ops/mongodb.rs
index 882cc8eca..168d3db76 100644
--- a/crates/core/database/src/models/emojis/ops/mongodb.rs
+++ b/crates/core/database/src/models/emojis/ops/mongodb.rs
@@ -24,13 +24,12 @@ impl AbstractEmojis for MongoDb {
     async fn fetch_emoji_by_parent_id(&self, parent_id: &str) -> Result<Vec<Emoji>> {
         query!(
             self,
-            find_one,
+            find,
             COL,
             doc! {
                 "parent.id": parent_id
             }
-        )?
-        .ok_or_else(|| create_error!(NotFound))
+        )
     }
 
     /// Fetch emoji by their parent ids
diff --git a/crates/core/database/src/models/server_members/model.rs b/crates/core/database/src/models/server_members/model.rs
index 035b408f0..07c349fb1 100644
--- a/crates/core/database/src/models/server_members/model.rs
+++ b/crates/core/database/src/models/server_members/model.rs
@@ -119,6 +119,8 @@ impl Member {
             }
         }
 
+        let emojis = db.fetch_emoji_by_parent_id(&server.id).await?;
+
         EventV1::ServerMemberJoin {
             id: server.id.clone(),
             user: user.id.clone(),
@@ -134,6 +136,7 @@ impl Member {
                 .into_iter()
                 .map(|channel| channel.into())
                 .collect(),
+            emojis: emojis.into_iter().map(|emoji| emoji.into()).collect(),
         }
         .private(user.id.clone())
         .await;
diff --git a/crates/quark/src/events/client.rs b/crates/quark/src/events/client.rs
index 219a46411..e9a08eb7e 100644
--- a/crates/quark/src/events/client.rs
+++ b/crates/quark/src/events/client.rs
@@ -143,6 +143,7 @@ pub enum EventV1 {
         id: String,
         server: Server,
         channels: Vec<Channel>,
+        emojis: Vec<Emoji>,
     },
 
     /// Update existing server
diff --git a/crates/quark/src/events/impl.rs b/crates/quark/src/events/impl.rs
index e38fa9697..c157fe115 100644
--- a/crates/quark/src/events/impl.rs
+++ b/crates/quark/src/events/impl.rs
@@ -401,6 +401,7 @@ impl State {
                 id,
                 server,
                 channels,
+                emojis: _,
             } => {
                 self.insert_subscription(id.clone());
                 self.cache.servers.insert(id.clone(), server.clone());
diff --git a/crates/quark/src/impl/generic/servers/server.rs b/crates/quark/src/impl/generic/servers/server.rs
index 8131eecd2..1a4916f0c 100644
--- a/crates/quark/src/impl/generic/servers/server.rs
+++ b/crates/quark/src/impl/generic/servers/server.rs
@@ -206,6 +206,8 @@ impl Server {
             }
         }
 
+        let emojis = db.fetch_emoji_by_parent_id(&self.id).await?;
+
         EventV1::ServerMemberJoin {
             id: self.id.clone(),
             user: user.id.clone(),
@@ -217,6 +219,7 @@ impl Server {
             id: self.id.clone(),
             server: self.clone(),
             channels: channels.clone(),
+            emojis,
         }
         .private(user.id.clone())
         .await;


Test patch: