Add join / leave messages for groups.

This commit is contained in:
Paul Makles
2020-04-12 16:42:13 +01:00
parent 87e7df02f2
commit 4fbd6c816d
5 changed files with 149 additions and 85 deletions

View File

@@ -1,4 +1,8 @@
use bson::UtcDateTime;
use super::get_collection;
use crate::guards::channel::ChannelRef;
use crate::routes::channel::ChannelType;
use bson::{doc, to_bson, UtcDateTime};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
@@ -11,7 +15,7 @@ pub struct PreviousEntry {
pub struct Message {
#[serde(rename = "_id")]
pub id: String,
// pub nonce: String, used internally
pub nonce: Option<String>,
pub channel: String,
pub author: String,
@@ -24,3 +28,63 @@ pub struct Message {
// ? TODO: write global send message
// ? pub fn send_message();
// ? handle websockets?
impl Message {
pub fn send(&self, target: &ChannelRef) -> bool {
if get_collection("messages")
.insert_one(to_bson(&self).unwrap().as_document().unwrap().clone(), None)
.is_ok()
{
let short_content: String = self.content.chars().take(24).collect();
// !! this stuff can be async
if target.channel_type == ChannelType::DM as u8
|| target.channel_type == ChannelType::GROUPDM as u8
{
let mut update = doc! {
"$set": {
"last_message": {
"id": &self.id,
"user_id": &self.author,
"short_content": short_content,
}
}
};
if target.channel_type == ChannelType::DM as u8 {
update
.get_document_mut("$set")
.unwrap()
.insert("active", true);
}
if get_collection("channels")
.update_one(doc! { "_id": &target.id }, update, None)
.is_ok()
{
true
} else {
false
}
} else {
true
}
/*websocket::queue_message(
get_recipients(&target),
json!({
"type": "message",
"data": {
"id": id.clone(),
"nonce": nonce,
"channel": target.id,
"author": user.id,
"content": content,
},
})
.to_string(),
);*/
} else {
false
}
}
}

View File

@@ -111,7 +111,7 @@ pub fn has_mutual_connection(user_id: &str, target_id: &str, with_permission: bo
let permissions = guild.get_i32("default_permissions").unwrap() as u32;
if MemberPermissions([ permissions ]).get_send_direct_messages() {
if MemberPermissions([permissions]).get_send_direct_messages() {
return true;
}
}