Channel subscription, message sending, channel delete.

This commit is contained in:
Paul Makles
2021-01-18 14:50:17 +00:00
parent 15357008d6
commit 3d3db80e61
7 changed files with 139 additions and 4 deletions

View File

@@ -28,6 +28,14 @@ pub enum Channel {
}
impl Channel {
pub fn id(&self) -> &str {
match self {
Channel::SavedMessages { id, .. } => id,
Channel::DirectMessage { id, .. } => id,
Channel::Group { id, .. } => id,
}
}
pub async fn save(&self) -> Result<()> {
get_collection("channels")
.insert_one(

View File

@@ -1,5 +1,6 @@
// use mongodb::bson::DateTime;
// use serde::{Deserialize, Serialize};
use crate::{database::*, notifications::events::ClientboundNotification, util::result::Result};
use mongodb::bson::{DateTime, to_bson};
use serde::{Deserialize, Serialize};
/*#[derive(Serialize, Deserialize, Debug)]
pub struct PreviousEntry {
@@ -20,3 +21,36 @@ pub struct Message {
pub previous_content: Vec<PreviousEntry>,
}*/
#[derive(Serialize, Deserialize, Debug)]
pub struct Message {
#[serde(rename = "_id")]
pub id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub nonce: Option<String>,
pub channel: String,
pub author: String,
pub content: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub edited: Option<DateTime>,
}
impl Message {
pub async fn send(self) -> Result<()> {
get_collection("messages")
.insert_one(
to_bson(&self).unwrap().as_document().unwrap().clone(),
None
)
.await;
let channel = self.channel.clone();
ClientboundNotification::Message(self)
.publish(channel)
.await
.ok();
Ok(())
}
}

View File

@@ -28,6 +28,23 @@ pub async fn calculate(user: &User, target: &Channel) -> ChannelPermissions<[u32
ChannelPermissions([ 0 ])
}
}
Channel::DirectMessage { recipients, .. } => {
if recipients.iter().find(|x| *x == &user.id).is_some() {
if let Some(recipient) = recipients
.iter()
.find(|x| *x != &user.id) {
let perms = super::user::calculate(&user, recipient).await;
if perms.get_send_message() {
return ChannelPermissions([ ChannelPermission::View + ChannelPermission::SendMessage ]);
}
return ChannelPermissions([ ChannelPermission::View as u32 ]);
}
}
ChannelPermissions([ 0 ])
}
_ => unreachable!()
}
}