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 rocket_contrib::json::JsonValue;
use serde::{Deserialize, Serialize}; 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)] #[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "channel_type")] #[serde(tag = "channel_type")]
pub enum Channel { pub enum Channel {
@@ -18,6 +26,8 @@ pub enum Channel {
id: String, id: String,
active: bool, active: bool,
recipients: Vec<String>, recipients: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
last_message: Option<LastMessage>,
}, },
Group { Group {
#[serde(rename = "_id")] #[serde(rename = "_id")]
@@ -28,6 +38,8 @@ pub enum Channel {
owner: String, owner: String,
description: String, description: String,
recipients: Vec<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") get_collection("messages")
.insert_one(to_bson(&self).unwrap().as_document().unwrap().clone(), None) .insert_one(to_bson(&self).unwrap().as_document().unwrap().clone(), None)
.await .await
.map_err(|_| Error::DatabaseError { .map_err(|_| Error::DatabaseError {
operation: "insert_one", 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) ClientboundNotification::Message(self)
.publish(channel) .publish(channel.id().to_string())
.await .await
.ok(); .ok();

View File

@@ -101,7 +101,7 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
id.clone(), id.clone(),
format!("<@{}> left the group.", user.id), format!("<@{}> left the group.", user.id),
) )
.publish() .publish(&target)
.await .await
.ok(); .ok();

View File

@@ -17,7 +17,7 @@ pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> {
Err(Error::LabelMe)? Err(Error::LabelMe)?
} }
if let Channel::Group { id, recipients, .. } = channel { if let Channel::Group { id, recipients, .. } = &channel {
if recipients.len() >= *MAX_GROUP_SIZE { if recipients.len() >= *MAX_GROUP_SIZE {
Err(Error::GroupTooLarge { Err(Error::GroupTooLarge {
max: *MAX_GROUP_SIZE, max: *MAX_GROUP_SIZE,
@@ -56,10 +56,10 @@ pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> {
Message::create( Message::create(
"00000000000000000000000000".to_string(), "00000000000000000000000000".to_string(),
id, id.clone(),
format!("<@{}> added <@{}> to the group.", user.id, member.id), format!("<@{}> added <@{}> to the group.", user.id, member.id),
) )
.publish() .publish(&channel)
.await .await
.ok(); .ok();

View File

@@ -70,6 +70,7 @@ pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
.unwrap_or_else(|| "A group.".to_string()), .unwrap_or_else(|| "A group.".to_string()),
owner: user.id, owner: user.id,
recipients: set.into_iter().collect::<Vec<String>>(), recipients: set.into_iter().collect::<Vec<String>>(),
last_message: None
}; };
channel.clone().publish().await?; channel.clone().publish().await?;

View File

@@ -16,9 +16,9 @@ pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> {
owner, owner,
recipients, recipients,
.. ..
} = channel } = &channel
{ {
if &user.id != &owner { if &user.id != owner {
// figure out if we want to use perm system here // figure out if we want to use perm system here
Err(Error::LabelMe)? Err(Error::LabelMe)?
} }
@@ -55,10 +55,10 @@ pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> {
Message::create( Message::create(
"00000000000000000000000000".to_string(), "00000000000000000000000000".to_string(),
id, id.clone(),
format!("<@{}> removed <@{}> from the group.", user.id, member.id), format!("<@{}> removed <@{}> from the group.", user.id, member.id),
) )
.publish() .publish(&channel)
.await .await
.ok(); .ok();

View File

@@ -56,7 +56,7 @@ pub async fn req(user: User, target: Ref, message: Json<Data>) -> Result<JsonVal
edited: None, edited: None,
}; };
msg.clone().publish().await?; msg.clone().publish(&target).await?;
Ok(json!(msg)) Ok(json!(msg))
} }

View File

@@ -40,6 +40,7 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
id, id,
active: false, active: false,
recipients: vec![user.id, target.id], recipients: vec![user.id, target.id],
last_message: None
} }
}; };