Unify last_message_id

This commit is contained in:
Yannick Funk
2021-08-26 16:56:13 +02:00
parent b15e07c707
commit 6c6a1508dc

View File

@@ -8,7 +8,7 @@ use crate::{
use futures::StreamExt; use futures::StreamExt;
use mongodb::options::UpdateOptions; use mongodb::options::UpdateOptions;
use mongodb::{ use mongodb::{
bson::{doc, to_bson, DateTime}, bson::{doc, to_bson, DateTime, Document},
options::FindOptions, options::FindOptions,
}; };
use rocket::serde::json::Value; use rocket::serde::json::Value;
@@ -99,7 +99,6 @@ impl Message {
nonce: None, nonce: None,
channel, channel,
author, author,
content, content,
attachments: None, attachments: None,
edited: None, edited: None,
@@ -124,76 +123,19 @@ impl Message {
let ss = self.clone(); let ss = self.clone();
let c_clone = channel.clone(); let c_clone = channel.clone();
async_std::task::spawn(async move { async_std::task::spawn(async move {
let mut set = if let Content::Text(text) = &ss.content {
doc! {
"last_message": {
"_id": ss.id.clone(),
"author": ss.author.clone(),
"short": text.chars().take(128).collect::<String>()
}
}
} else {
doc! {}
};
// ! MARK AS ACTIVE let last_message_id = ss.id.clone();
// ! FIXME: temp code let mut set = doc! { "last_message_id": last_message_id };
let channels = get_collection("channels"); let channels = get_collection("channels");
match &c_clone { match &c_clone {
Channel::DirectMessage { id, .. } => { Channel::DirectMessage { id, .. } => {
// ! MARK AS ACTIVE
set.insert("active", true); set.insert("active", true);
channels update_channels_last_message(&channels, id, &set).await;
.update_one( },
doc! { "_id": id }, Channel::Group { id, .. } | Channel::TextChannel { id, .. } => {
doc! { update_channels_last_message(&channels, id, &set).await;
"$set": set
},
None,
)
.await
/*.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channel",
})?;*/
.unwrap();
}
Channel::Group { id, .. } => {
if let Content::Text(_) = &ss.content {
channels
.update_one(
doc! { "_id": id },
doc! {
"$set": set
},
None,
)
.await
/*.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channel",
})?;*/
.unwrap();
}
}
Channel::TextChannel { id, .. } => {
if let Content::Text(_) = &ss.content {
channels
.update_one(
doc! { "_id": id },
doc! {
"$set": {
"last_message": &ss.id
}
},
None,
)
.await
/*.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "channel",
})?;*/
.unwrap();
}
} }
_ => {} _ => {}
} }
@@ -431,3 +373,15 @@ impl Message {
Ok(()) Ok(())
} }
} }
async fn update_channels_last_message(channels: &Collection, channel_id: &String, set: &Document) {
channels
.update_one(
doc! { "_id": channel_id },
doc! { "$set": set },
None,
)
.await
.expect("Server should not run with no, or a corrupted db");
}