From 46bf5fcdd19a2e1a4f482c3815ab17a62146830a Mon Sep 17 00:00:00 2001
From: Paul <paulmakles@gmail.com>
Date: Tue, 1 Jun 2021 20:38:35 +0100
Subject: [PATCH] Servers: Add TextChannel.

---
 src/database/entities/channel.rs      | 15 ++++++++++++++-
 src/database/entities/server.rs       |  3 ++-
 src/database/permissions/channel.rs   |  3 +++
 src/notifications/events.rs           | 13 +++++++++++++
 src/routes/channels/delete_channel.rs |  1 +
 src/routes/servers/server_create.rs   | 13 ++++++++++++-
 6 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/src/database/entities/channel.rs b/src/database/entities/channel.rs
index 704d743..3919dc1 100644
--- a/src/database/entities/channel.rs
+++ b/src/database/entities/channel.rs
@@ -50,6 +50,18 @@ pub enum Channel {
         #[serde(skip_serializing_if = "Option::is_none")]
         last_message: Option<LastMessage>,
     },
+    TextChannel {
+        #[serde(rename = "_id")]
+        id: String,
+        server: String,
+        #[serde(skip_serializing_if = "Option::is_none")]
+        nonce: Option<String>,
+
+        name: String,
+        description: Option<String>,
+        #[serde(skip_serializing_if = "Option::is_none")]
+        icon: Option<File>,
+    }
 }
 
 impl Channel {
@@ -57,7 +69,8 @@ impl Channel {
         match self {
             Channel::SavedMessages { id, .. }
             | Channel::DirectMessage { id, .. }
-            | Channel::Group { id, .. } => id,
+            | Channel::Group { id, .. }
+            | Channel::TextChannel { id, .. } => id,
         }
     }
 
diff --git a/src/database/entities/server.rs b/src/database/entities/server.rs
index 236bb5d..b0fc2a1 100644
--- a/src/database/entities/server.rs
+++ b/src/database/entities/server.rs
@@ -43,9 +43,10 @@ pub struct Server {
 
     pub name: String,
     // pub default_permissions: u32,
-    pub channels: Vec<String>,
     // pub invites: Vec<Invite>,
     // pub bans: Vec<Ban>,
+    pub channels: Vec<String>,
+
     #[serde(skip_serializing_if = "Option::is_none")]
     pub icon: Option<File>,
     #[serde(skip_serializing_if = "Option::is_none")]
diff --git a/src/database/permissions/channel.rs b/src/database/permissions/channel.rs
index 2009bb4..e4f27d5 100644
--- a/src/database/permissions/channel.rs
+++ b/src/database/permissions/channel.rs
@@ -81,6 +81,9 @@ impl<'a> PermissionCalculator<'a> {
                     Ok(0)
                 }
             }
+            Channel::TextChannel { .. } => {
+                Ok(ChannelPermission::View + ChannelPermission::SendMessage)
+            }
         }
     }
 
diff --git a/src/notifications/events.rs b/src/notifications/events.rs
index 4e401cf..3a118be 100644
--- a/src/notifications/events.rs
+++ b/src/notifications/events.rs
@@ -143,6 +143,18 @@ impl ClientboundNotification {
                 .ok();
         });
     }
+
+    pub fn publish_to(self, channel: &Channel) {
+        // ! FIXME: update all for channel
+        // ! FIXME: temporary solution for pushing to guilds
+        self.publish(
+            if let Channel::TextChannel { server, .. } = channel {
+                server.clone()
+            } else {
+                channel.id().to_string()
+            }
+        )
+    }
 }
 
 pub fn prehandle_hook(notification: &ClientboundNotification) {
@@ -161,6 +173,7 @@ pub fn prehandle_hook(notification: &ClientboundNotification) {
                         subscribe_if_exists(recipient.clone(), channel_id.to_string()).ok();
                     }
                 }
+                _ => {}
             }
         }
         ClientboundNotification::ServerMemberJoin { id, user } => {
diff --git a/src/routes/channels/delete_channel.rs b/src/routes/channels/delete_channel.rs
index 9575ae6..1885048 100644
--- a/src/routes/channels/delete_channel.rs
+++ b/src/routes/channels/delete_channel.rs
@@ -108,5 +108,6 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
 
             Ok(())
         }
+        Channel::TextChannel { .. } => unimplemented!()
     }
 }
diff --git a/src/routes/servers/server_create.rs b/src/routes/servers/server_create.rs
index 96a2f8c..b299ae4 100644
--- a/src/routes/servers/server_create.rs
+++ b/src/routes/servers/server_create.rs
@@ -39,13 +39,24 @@ pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
     }
 
     let id = Ulid::new().to_string();
+    let cid = Ulid::new().to_string();
+    
+    Channel::TextChannel {
+        id: cid.clone(),
+        server: id.clone(),
+        nonce: Some(info.nonce.clone()),
+        name: "general".to_string(),
+        description: None,
+        icon: None,
+    }.publish().await?;
+
     let server = Server {
         id: id.clone(),
         nonce: Some(info.nonce.clone()),
         owner: user.id.clone(),
 
         name: info.name.clone(),
-        channels: vec![],
+        channels: vec![ cid ],
 
         icon: None,
         banner: None,
-- 
GitLab