mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 05:26:59 +00:00
82 lines
1.9 KiB
Rust
82 lines
1.9 KiB
Rust
use crate::{
|
|
database::*,
|
|
notifications::events::ClientboundNotification,
|
|
util::result::{Error, Result},
|
|
};
|
|
use mongodb::bson::{to_bson, DateTime};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
/*#[derive(Serialize, Deserialize, Debug)]
|
|
pub struct PreviousEntry {
|
|
pub content: String,
|
|
pub time: DateTime,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
pub struct Message {
|
|
#[serde(rename = "_id")]
|
|
pub id: String,
|
|
pub nonce: Option<String>,
|
|
pub channel: String,
|
|
pub author: String,
|
|
|
|
pub content: String,
|
|
pub edited: Option<DateTime>,
|
|
|
|
pub previous_content: Vec<PreviousEntry>,
|
|
}*/
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
pub struct Message {
|
|
#[serde(rename = "_id")]
|
|
pub id: String,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub nonce: Option<String>,
|
|
pub channel: String,
|
|
pub author: String,
|
|
|
|
pub content: String,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub edited: Option<DateTime>,
|
|
}
|
|
|
|
impl Message {
|
|
pub async fn send(self) -> 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",
|
|
})?;
|
|
|
|
let channel = self.channel.clone();
|
|
ClientboundNotification::Message(self)
|
|
.publish(channel)
|
|
.await
|
|
.ok();
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn publish_edit(self) -> Result<()> {
|
|
let channel = self.channel.clone();
|
|
ClientboundNotification::MessageEdit(self)
|
|
.publish(channel)
|
|
.await
|
|
.ok();
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn publish_delete(self) -> Result<()> {
|
|
let channel = self.channel.clone();
|
|
ClientboundNotification::MessageDelete(self.id)
|
|
.publish(channel)
|
|
.await
|
|
.ok();
|
|
|
|
Ok(())
|
|
}
|
|
}
|