2025-01-20 19:43:21 -08:00
|
|
|
use serde::Deserialize;
|
|
|
|
|
use time::OffsetDateTime;
|
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
2025-04-28 15:17:58 -07:00
|
|
|
use super::Identifiable;
|
|
|
|
|
|
|
|
|
|
pub type MessageID = <Message as Identifiable>::ID;
|
|
|
|
|
|
2025-01-20 19:43:21 -08:00
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
|
pub struct Message {
|
|
|
|
|
pub guid: String,
|
|
|
|
|
|
|
|
|
|
#[serde(rename = "text")]
|
|
|
|
|
pub text: String,
|
|
|
|
|
|
|
|
|
|
#[serde(rename = "sender")]
|
|
|
|
|
pub sender: Option<String>,
|
|
|
|
|
|
|
|
|
|
#[serde(with = "time::serde::iso8601")]
|
|
|
|
|
pub date: OffsetDateTime,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Message {
|
|
|
|
|
pub fn builder() -> MessageBuilder {
|
|
|
|
|
MessageBuilder::new()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-28 15:17:58 -07:00
|
|
|
impl Identifiable for Message {
|
|
|
|
|
type ID = String;
|
|
|
|
|
|
|
|
|
|
fn id(&self) -> &Self::ID {
|
|
|
|
|
&self.guid
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-20 19:43:21 -08:00
|
|
|
#[derive(Default)]
|
|
|
|
|
pub struct MessageBuilder {
|
|
|
|
|
guid: Option<String>,
|
|
|
|
|
text: Option<String>,
|
|
|
|
|
sender: Option<String>,
|
|
|
|
|
date: Option<OffsetDateTime>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MessageBuilder {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
Self::default()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn guid(mut self, guid: String) -> Self {
|
|
|
|
|
self.guid = Some(guid);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn text(mut self, text: String) -> Self {
|
|
|
|
|
self.text = Some(text);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn sender(mut self, sender: String) -> Self {
|
|
|
|
|
self.sender = Some(sender);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn date(mut self, date: OffsetDateTime) -> Self {
|
|
|
|
|
self.date = Some(date);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn build(self) -> Message {
|
|
|
|
|
Message {
|
|
|
|
|
guid: self.guid.unwrap_or(Uuid::new_v4().to_string()),
|
|
|
|
|
text: self.text.unwrap_or("".to_string()),
|
|
|
|
|
sender: self.sender,
|
|
|
|
|
date: self.date.unwrap_or(OffsetDateTime::now_utc()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|