Skip to content
Snippets Groups Projects
Verified Commit d0c8358b authored by insert's avatar insert
Browse files

Add last_message for DMs.

parent 7fb2a1a8
No related merge requests found
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LastMessage {
id: String,
user_id: String,
short_content: String,
}
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct Channel { pub struct Channel {
#[serde(rename = "_id")] #[serde(rename = "_id")]
...@@ -7,12 +14,11 @@ pub struct Channel { ...@@ -7,12 +14,11 @@ pub struct Channel {
#[serde(rename = "type")] #[serde(rename = "type")]
pub channel_type: u8, pub channel_type: u8,
pub last_message: Option<String>,
// for Direct Messages // for Direct Messages
pub active: Option<bool>, pub active: Option<bool>,
// for DMs / GDMs // for DMs / GDMs
pub last_message: Option<LastMessage>,
pub recipients: Option<Vec<String>>, pub recipients: Option<Vec<String>>,
// for GDMs // for GDMs
......
...@@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize}; ...@@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize};
use crate::database; use crate::database;
use database::message::Message; use database::message::Message;
use database::channel::LastMessage;
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ChannelRef { pub struct ChannelRef {
...@@ -15,6 +16,8 @@ pub struct ChannelRef { ...@@ -15,6 +16,8 @@ pub struct ChannelRef {
#[serde(rename = "type")] #[serde(rename = "type")]
pub channel_type: u8, pub channel_type: u8,
pub last_message: Option<LastMessage>,
// information required for permission calculations // information required for permission calculations
pub recipients: Option<Vec<String>>, pub recipients: Option<Vec<String>>,
pub guild: Option<String>, pub guild: Option<String>,
...@@ -44,6 +47,7 @@ impl<'r> FromParam<'r> for ChannelRef { ...@@ -44,6 +47,7 @@ impl<'r> FromParam<'r> for ChannelRef {
.projection(doc! { .projection(doc! {
"_id": 1, "_id": 1,
"type": 1, "type": 1,
"last_message": 1,
"recipients": 1, "recipients": 1,
"guild": 1, "guild": 1,
"owner": 1, "owner": 1,
......
...@@ -131,6 +131,7 @@ pub fn channel(user: UserRef, target: ChannelRef) -> Option<Response> { ...@@ -131,6 +131,7 @@ pub fn channel(user: UserRef, target: ChannelRef) -> Option<Response> {
0 => Some(Response::Success(json!({ 0 => Some(Response::Success(json!({
"id": target.id, "id": target.id,
"type": target.channel_type, "type": target.channel_type,
"last_message": target.last_message,
"recipients": target.recipients, "recipients": target.recipients,
}))), }))),
1 => { 1 => {
...@@ -142,6 +143,7 @@ pub fn channel(user: UserRef, target: ChannelRef) -> Option<Response> { ...@@ -142,6 +143,7 @@ pub fn channel(user: UserRef, target: ChannelRef) -> Option<Response> {
Some(Response::Success(json!({ Some(Response::Success(json!({
"id": target.id, "id": target.id,
"type": target.channel_type, "type": target.channel_type,
"last_message": target.last_message,
"recipients": target.recipients, "recipients": target.recipients,
"name": info.get_str("name").unwrap(), "name": info.get_str("name").unwrap(),
"owner": info.get_str("owner").unwrap(), "owner": info.get_str("owner").unwrap(),
...@@ -429,24 +431,47 @@ pub fn send_message( ...@@ -429,24 +431,47 @@ pub fn send_message(
if col if col
.insert_one( .insert_one(
doc! { doc! {
"_id": id.clone(), "_id": &id,
"nonce": nonce, "nonce": nonce,
"channel": target.id.clone(), "channel": &target.id,
"author": user.id, "author": &user.id,
"content": content, "content": &content,
}, },
None, None,
) )
.is_ok() .is_ok()
{ {
if target.channel_type == ChannelType::DM as u8 { let short_content: String = content.chars().take(24).collect();
let col = database::get_collection("channels"); let col = database::get_collection("channels");
col.update_one(
// !! 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! { "_id": &target.id },
doc! { "$set": { "active": true } }, update,
None, None,
) ).is_ok() {
.unwrap(); Response::Success(json!({ "id": id }))
} else {
Response::InternalServerError(json!({ "error": "Failed to update channel." }))
}
} else {
Response::Success(json!({ "id": id }))
} }
/*websocket::queue_message( /*websocket::queue_message(
...@@ -463,8 +488,6 @@ pub fn send_message( ...@@ -463,8 +488,6 @@ pub fn send_message(
}) })
.to_string(), .to_string(),
);*/ );*/
Response::Success(json!({ "id": id }))
} else { } else {
Response::InternalServerError(json!({ Response::InternalServerError(json!({
"error": "Failed database query." "error": "Failed database query."
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment