Add group creation. Sync channels to clients on creation.

This commit is contained in:
Paul Makles
2021-01-18 22:02:32 +00:00
parent c562d33c8f
commit 34ac8f54ef
10 changed files with 139 additions and 25 deletions

View File

@@ -1,9 +1,9 @@
use crate::database::*;
use crate::{database::*, notifications::{events::ClientboundNotification, hive}};
use crate::util::result::{Error, Result};
use mongodb::bson::to_document;
use serde::{Deserialize, Serialize};
use mongodb::bson::to_document;
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type")]
pub enum Channel {
SavedMessages {
@@ -20,6 +20,8 @@ pub enum Channel {
Group {
#[serde(rename = "_id")]
id: String,
#[serde(skip_serializing_if = "Option::is_none")]
nonce: Option<String>,
name: String,
owner: String,
description: String,
@@ -36,7 +38,7 @@ impl Channel {
}
}
pub async fn save(&self) -> Result<()> {
pub async fn publish(self) -> Result<()> {
get_collection("channels")
.insert_one(
to_document(&self).map_err(|_| Error::DatabaseError {
@@ -50,6 +52,25 @@ impl Channel {
operation: "insert_one",
with: "channel",
})?;
// ! IMPORTANT FIXME: THESE SUBSCRIPTIONS SHOULD BE DONE FROM HIVE NOT HERE!!!
let channel_id = self.id().to_string();
match &self {
Channel::SavedMessages { user, .. } => {
hive::subscribe_if_exists(user.clone(), channel_id.clone()).ok();
}
Channel::DirectMessage { recipients, .. } |
Channel::Group { recipients, .. } => {
for recipient in recipients {
hive::subscribe_if_exists(recipient.clone(), channel_id.clone()).ok();
}
}
}
ClientboundNotification::ChannelCreate(self)
.publish(channel_id)
.await
.ok();
Ok(())
}

View File

@@ -26,7 +26,7 @@ pub struct Message {
pub previous_content: Vec<PreviousEntry>,
}*/
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Message {
#[serde(rename = "_id")]
pub id: String,
@@ -41,7 +41,7 @@ pub struct Message {
}
impl Message {
pub async fn send(self) -> Result<()> {
pub async fn publish(self) -> Result<()> {
get_collection("messages")
.insert_one(to_bson(&self).unwrap().as_document().unwrap().clone(), None)
.await

View File

@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum RelationshipStatus {
None,
User,