From d0c8358bc8132f9c86ef6bc470dcb206edd2a878 Mon Sep 17 00:00:00 2001 From: Paul Makles <paulmakles@gmail.com> Date: Fri, 10 Apr 2020 13:45:19 +0100 Subject: [PATCH] Add last_message for DMs. --- src/database/channel.rs | 10 +++++++-- src/guards/channel.rs | 4 ++++ src/routes/channel.rs | 47 ++++++++++++++++++++++++++++++----------- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/database/channel.rs b/src/database/channel.rs index 309b598..d74b477 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 7b64e2b..97200d0 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 e2a5e52..f3d5906 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." -- GitLab