diff --git a/src/database/channel.rs b/src/database/channel.rs index 309b59886f61a194a10af3fc6fc582d68b6d766d..d74b477cc2119a10b6288921283cc24b03a9e32b 100644 --- a/src/database/channel.rs +++ b/src/database/channel.rs @@ -1,5 +1,12 @@ use serde::{Deserialize, Serialize}; +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct LastMessage { + id: String, + user_id: String, + short_content: String, +} + #[derive(Serialize, Deserialize, Debug)] pub struct Channel { #[serde(rename = "_id")] @@ -7,12 +14,11 @@ pub struct Channel { #[serde(rename = "type")] pub channel_type: u8, - pub last_message: Option<String>, - // for Direct Messages pub active: Option<bool>, // for DMs / GDMs + pub last_message: Option<LastMessage>, pub recipients: Option<Vec<String>>, // for GDMs diff --git a/src/guards/channel.rs b/src/guards/channel.rs index 7b64e2b5777aae1df1d9dc6a6ffaf9d923ea0dec..97200d00cf160a6aa3770e9e14fad9beb12ee9e0 100644 --- a/src/guards/channel.rs +++ b/src/guards/channel.rs @@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize}; use crate::database; use database::message::Message; +use database::channel::LastMessage; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct ChannelRef { @@ -15,6 +16,8 @@ pub struct ChannelRef { #[serde(rename = "type")] pub channel_type: u8, + pub last_message: Option<LastMessage>, + // information required for permission calculations pub recipients: Option<Vec<String>>, pub guild: Option<String>, @@ -44,6 +47,7 @@ impl<'r> FromParam<'r> for ChannelRef { .projection(doc! { "_id": 1, "type": 1, + "last_message": 1, "recipients": 1, "guild": 1, "owner": 1, diff --git a/src/routes/channel.rs b/src/routes/channel.rs index e2a5e5282b8f88bfbd9b5a7d1eba090be4a2e39d..f3d5906bfadbcd32c015c4fab41579faa785e6ca 100644 --- a/src/routes/channel.rs +++ b/src/routes/channel.rs @@ -131,6 +131,7 @@ pub fn channel(user: UserRef, target: ChannelRef) -> Option<Response> { 0 => Some(Response::Success(json!({ "id": target.id, "type": target.channel_type, + "last_message": target.last_message, "recipients": target.recipients, }))), 1 => { @@ -142,6 +143,7 @@ pub fn channel(user: UserRef, target: ChannelRef) -> Option<Response> { Some(Response::Success(json!({ "id": target.id, "type": target.channel_type, + "last_message": target.last_message, "recipients": target.recipients, "name": info.get_str("name").unwrap(), "owner": info.get_str("owner").unwrap(), @@ -429,24 +431,47 @@ pub fn send_message( if col .insert_one( doc! { - "_id": id.clone(), + "_id": &id, "nonce": nonce, - "channel": target.id.clone(), - "author": user.id, - "content": content, + "channel": &target.id, + "author": &user.id, + "content": &content, }, None, ) .is_ok() { - if target.channel_type == ChannelType::DM as u8 { - let col = database::get_collection("channels"); - col.update_one( + let short_content: String = content.chars().take(24).collect(); + let col = database::get_collection("channels"); + + // !! 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": &id, + "user_id": &user.id, + "short_content": short_content, + } + } + }; + + if target.channel_type == ChannelType::DM as u8 { + update.get_document_mut("$set").unwrap().insert("active", true); + } + + if col.update_one( doc! { "_id": &target.id }, - doc! { "$set": { "active": true } }, + update, None, - ) - .unwrap(); + ).is_ok() { + Response::Success(json!({ "id": id })) + } else { + Response::InternalServerError(json!({ "error": "Failed to update channel." })) + } + } else { + Response::Success(json!({ "id": id })) } /*websocket::queue_message( @@ -463,8 +488,6 @@ pub fn send_message( }) .to_string(), );*/ - - Response::Success(json!({ "id": id })) } else { Response::InternalServerError(json!({ "error": "Failed database query."