Add last_message for DMs.

This commit is contained in:
Paul Makles
2020-04-10 13:45:19 +01:00
parent 7fb2a1a8c1
commit d0c8358bc8
3 changed files with 47 additions and 14 deletions

View File

@@ -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

View File

@@ -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,

View File

@@ -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."