Add last_message to channels, mark DMs as active.

This commit is contained in:
Paul Makles
2021-01-24 10:24:44 +00:00
parent cb882ce1b2
commit 11f7092fcd
8 changed files with 69 additions and 13 deletions

View File

@@ -5,6 +5,14 @@ use mongodb::bson::{doc, to_document};
use rocket_contrib::json::JsonValue;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LastMessage {
#[serde(rename = "_id")]
id: String,
author: String,
short: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "channel_type")]
pub enum Channel {
@@ -18,6 +26,8 @@ pub enum Channel {
id: String,
active: bool,
recipients: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
last_message: Option<LastMessage>,
},
Group {
#[serde(rename = "_id")]
@@ -28,6 +38,8 @@ pub enum Channel {
owner: String,
description: String,
recipients: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
last_message: Option<LastMessage>,
},
}

View File

@@ -35,18 +35,60 @@ impl Message {
}
}
pub async fn publish(self) -> Result<()> {
pub async fn publish(self, channel: &Channel) -> Result<()> {
get_collection("messages")
.insert_one(to_bson(&self).unwrap().as_document().unwrap().clone(), None)
.await
.map_err(|_| Error::DatabaseError {
operation: "insert_one",
with: "messages",
with: "message",
})?;
let channel = self.channel.clone();
// ! temp code
let channels = get_collection("channels");
match channel {
Channel::DirectMessage { id, .. } => {
channels.update_one(
doc! { "_id": id },
doc! {
"active": true,
"last_message": {
"_id": self.id.clone(),
"author": self.author.clone(),
"short": self.content.chars().take(24).collect::<String>()
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channel",
})?;
},
Channel::Group { id, .. } => {
channels.update_one(
doc! { "_id": id },
doc! {
"last_message": {
"_id": self.id.clone(),
"author": self.author.clone(),
"short": self.content.chars().take(24).collect::<String>()
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channel",
})?;
},
_ => {}
}
ClientboundNotification::Message(self)
.publish(channel)
.publish(channel.id().to_string())
.await
.ok();