Only push out status; mark attachments as deleted.

This commit is contained in:
Paul
2021-04-28 17:12:14 +01:00
parent f163cb65de
commit 81002c75d2
4 changed files with 85 additions and 8 deletions

View File

@@ -20,6 +20,9 @@ pub struct File {
content_type: String, content_type: String,
size: isize, size: isize,
#[serde(skip_serializing_if = "Option::is_none")]
deleted: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
message_id: Option<String>, message_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]

View File

@@ -1,9 +1,10 @@
use crate::database::*; use crate::database::*;
use crate::notifications::events::ClientboundNotification; use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result};
use mongodb::bson::{doc, from_document, to_document}; use mongodb::{bson::{Document, doc, from_document, to_document}, options::FindOptions};
use rocket_contrib::json::JsonValue; use rocket_contrib::json::JsonValue;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use futures::StreamExt;
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LastMessage { pub struct LastMessage {
@@ -107,7 +108,56 @@ impl Channel {
pub async fn delete(&self) -> Result<()> { pub async fn delete(&self) -> Result<()> {
let id = self.id(); let id = self.id();
get_collection("messages") let messages = get_collection("messages");
// Check if there are any attachments we need to delete.
let message_ids = messages
.find(
doc! {
"channel": id,
"attachment": {
"$exists": 1
}
},
FindOptions::builder().projection(doc! { "_id": 1 }).build(),
)
.await
.map_err(|_| Error::DatabaseError {
operation: "fetch_many",
with: "messages",
})?
.filter_map(async move |s| s.ok())
.collect::<Vec<Document>>()
.await
.into_iter()
.filter_map(|x| x.get_str("_id").ok().map(|x| x.to_string()))
.collect::<Vec<String>>();
// If we found any, mark them as deleted.
if message_ids.len() > 0 {
get_collection("attachments")
.update_many(
doc! {
"message_id": {
"$in": message_ids
}
},
doc! {
"$set": {
"deleted": true
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_many",
with: "attachments",
})?;
}
// And then delete said messages.
messages
.delete_many( .delete_many(
doc! { doc! {
"channel": id "channel": id

View File

@@ -276,6 +276,26 @@ impl Message {
.await .await
.ok(); .ok();
if let Some(attachment) = &self.attachment {
get_collection("attachments")
.update_one(
doc! {
"_id": &attachment.id
},
doc! {
"$set": {
"deleted": true
}
},
None
)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "attachment",
})?;
}
Ok(()) Ok(())
} }
} }

View File

@@ -10,8 +10,10 @@ use validator::Validate;
#[derive(Validate, Serialize, Deserialize)] #[derive(Validate, Serialize, Deserialize)]
pub struct Data { pub struct Data {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
#[validate]
status: Option<UserStatus>, status: Option<UserStatus>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
#[validate]
profile: Option<UserProfile>, profile: Option<UserProfile>,
} }
@@ -29,13 +31,15 @@ pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()>
.await .await
.map_err(|_| Error::DatabaseError { operation: "update_one", with: "user" })?; .map_err(|_| Error::DatabaseError { operation: "update_one", with: "user" })?;
if let Some(status) = data.0.status {
ClientboundNotification::UserUpdate { ClientboundNotification::UserUpdate {
id: user.id.clone(), id: user.id.clone(),
data: json!(data.0), data: json!({ "status": status }),
} }
.publish(user.id.clone()) .publish(user.id.clone())
.await .await
.ok(); .ok();
}
Ok(()) Ok(())
} }