2025-06-06 16:39:31 -07:00
|
|
|
use crate::models::participant::Participant;
|
2025-01-20 22:05:34 -08:00
|
|
|
use chrono::{DateTime, NaiveDateTime};
|
2025-06-06 16:39:31 -07:00
|
|
|
use kordophone::model::message::AttachmentMetadata;
|
|
|
|
|
use kordophone::model::outgoing_message::OutgoingMessage;
|
2025-05-26 16:52:38 -07:00
|
|
|
use std::collections::HashMap;
|
2025-01-20 22:05:34 -08:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
2025-06-06 16:39:31 -07:00
|
|
|
#[derive(Clone, Debug)]
|
2025-01-20 22:05:34 -08:00
|
|
|
pub struct Message {
|
|
|
|
|
pub id: String,
|
|
|
|
|
pub sender: Participant,
|
|
|
|
|
pub text: String,
|
|
|
|
|
pub date: NaiveDateTime,
|
2025-05-26 16:52:38 -07:00
|
|
|
pub file_transfer_guids: Vec<String>,
|
|
|
|
|
pub attachment_metadata: Option<HashMap<String, AttachmentMetadata>>,
|
2025-01-20 22:05:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Message {
|
|
|
|
|
pub fn builder() -> MessageBuilder {
|
|
|
|
|
MessageBuilder::new()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<kordophone::model::Message> for Message {
|
|
|
|
|
fn from(value: kordophone::model::Message) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
id: value.guid,
|
|
|
|
|
sender: match value.sender {
|
|
|
|
|
Some(sender) => Participant::Remote {
|
2025-06-26 18:23:15 -07:00
|
|
|
handle: sender,
|
2025-06-26 16:23:53 -07:00
|
|
|
contact_id: None,
|
2025-01-20 22:05:34 -08:00
|
|
|
},
|
|
|
|
|
None => Participant::Me,
|
|
|
|
|
},
|
|
|
|
|
text: value.text,
|
|
|
|
|
date: DateTime::from_timestamp(
|
|
|
|
|
value.date.unix_timestamp(),
|
2025-06-06 16:39:31 -07:00
|
|
|
value.date.unix_timestamp_nanos().try_into().unwrap_or(0),
|
|
|
|
|
)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.naive_local(),
|
2025-05-26 16:52:38 -07:00
|
|
|
file_transfer_guids: value.file_transfer_guids,
|
|
|
|
|
attachment_metadata: value.attachment_metadata,
|
2025-01-20 22:05:34 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-03 21:45:53 -07:00
|
|
|
impl From<&OutgoingMessage> for Message {
|
|
|
|
|
fn from(value: &OutgoingMessage) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
id: value.guid.to_string(),
|
|
|
|
|
sender: Participant::Me,
|
|
|
|
|
text: value.text.clone(),
|
|
|
|
|
date: value.date,
|
2025-05-26 16:52:38 -07:00
|
|
|
file_transfer_guids: Vec::new(), // Outgoing messages don't have file transfer GUIDs initially
|
|
|
|
|
attachment_metadata: None, // Outgoing messages don't have attachment metadata initially
|
2025-05-03 21:45:53 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-20 22:05:34 -08:00
|
|
|
pub struct MessageBuilder {
|
|
|
|
|
id: Option<String>,
|
|
|
|
|
sender: Option<Participant>,
|
|
|
|
|
text: Option<String>,
|
|
|
|
|
date: Option<NaiveDateTime>,
|
2025-05-26 16:52:38 -07:00
|
|
|
file_transfer_guids: Option<Vec<String>>,
|
|
|
|
|
attachment_metadata: Option<HashMap<String, AttachmentMetadata>>,
|
2025-01-20 22:05:34 -08:00
|
|
|
}
|
|
|
|
|
|
2025-04-28 16:06:51 -07:00
|
|
|
impl Default for MessageBuilder {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self::new()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-20 22:05:34 -08:00
|
|
|
impl MessageBuilder {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
id: None,
|
|
|
|
|
sender: None,
|
|
|
|
|
text: None,
|
|
|
|
|
date: None,
|
2025-05-26 16:52:38 -07:00
|
|
|
file_transfer_guids: None,
|
|
|
|
|
attachment_metadata: None,
|
2025-01-20 22:05:34 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn sender(mut self, sender: Participant) -> Self {
|
|
|
|
|
self.sender = Some(sender);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn text(mut self, text: String) -> Self {
|
|
|
|
|
self.text = Some(text);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn date(mut self, date: NaiveDateTime) -> Self {
|
|
|
|
|
self.date = Some(date);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-26 16:52:38 -07:00
|
|
|
pub fn file_transfer_guids(mut self, file_transfer_guids: Vec<String>) -> Self {
|
|
|
|
|
self.file_transfer_guids = Some(file_transfer_guids);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-06 16:39:31 -07:00
|
|
|
pub fn attachment_metadata(
|
|
|
|
|
mut self,
|
|
|
|
|
attachment_metadata: HashMap<String, AttachmentMetadata>,
|
|
|
|
|
) -> Self {
|
2025-05-26 16:52:38 -07:00
|
|
|
self.attachment_metadata = Some(attachment_metadata);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-20 22:05:34 -08:00
|
|
|
pub fn build(self) -> Message {
|
|
|
|
|
Message {
|
|
|
|
|
id: self.id.unwrap_or_else(|| Uuid::new_v4().to_string()),
|
|
|
|
|
sender: self.sender.unwrap_or(Participant::Me),
|
|
|
|
|
text: self.text.unwrap_or_default(),
|
|
|
|
|
date: self.date.unwrap_or_else(|| chrono::Utc::now().naive_utc()),
|
2025-05-26 16:52:38 -07:00
|
|
|
file_transfer_guids: self.file_transfer_guids.unwrap_or_default(),
|
|
|
|
|
attachment_metadata: self.attachment_metadata,
|
2025-01-20 22:05:34 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|