Private
Public Access
1
0

plumb all known attachments via dbus if known

This commit is contained in:
2025-06-06 16:28:29 -07:00
parent 2e55f3ac9e
commit 77e1078d6a
10 changed files with 368 additions and 54 deletions

View File

@@ -0,0 +1,217 @@
use chrono::NaiveDateTime;
use chrono::DateTime;
use std::collections::HashMap;
use uuid::Uuid;
use kordophone::model::message::AttachmentMetadata;
use kordophone::model::outgoing_message::OutgoingMessage;
use crate::daemon::models::Attachment;
use crate::daemon::attachment_store::AttachmentStore;
#[derive(Clone, Debug)]
pub enum Participant {
Me,
Remote {
id: Option<i32>,
display_name: String,
},
}
impl From<String> for Participant {
fn from(display_name: String) -> Self {
Participant::Remote {
id: None,
display_name,
}
}
}
impl From<&str> for Participant {
fn from(display_name: &str) -> Self {
Participant::Remote {
id: None,
display_name: display_name.to_string(),
}
}
}
impl From<kordophone_db::models::Participant> for Participant {
fn from(participant: kordophone_db::models::Participant) -> Self {
match participant {
kordophone_db::models::Participant::Me => Participant::Me,
kordophone_db::models::Participant::Remote { id, display_name } => {
Participant::Remote { id, display_name }
}
}
}
}
impl Participant {
pub fn display_name(&self) -> String {
match self {
Participant::Me => "(Me)".to_string(),
Participant::Remote { display_name, .. } => display_name.clone(),
}
}
}
#[derive(Clone, Debug)]
pub struct Message {
pub id: String,
pub sender: Participant,
pub text: String,
pub date: NaiveDateTime,
pub attachments: Vec<Attachment>,
}
impl Message {
pub fn builder() -> MessageBuilder {
MessageBuilder::new()
}
}
fn attachments_from(file_transfer_guids: &Vec<String>, attachment_metadata: &Option<HashMap<String, AttachmentMetadata>>) -> Vec<Attachment> {
file_transfer_guids
.iter()
.map(|guid| {
let mut attachment = AttachmentStore::get_attachment_impl(&AttachmentStore::get_default_store_path(), guid);
attachment.metadata = match attachment_metadata {
Some(attachment_metadata) => attachment_metadata.get(guid).cloned().map(|metadata| metadata.into()),
None => None,
};
attachment
})
.collect()
}
impl From<kordophone_db::models::Message> for Message {
fn from(message: kordophone_db::models::Message) -> Self {
let attachments = attachments_from(&message.file_transfer_guids, &message.attachment_metadata);
Self {
id: message.id,
sender: message.sender.into(),
text: message.text,
date: message.date,
attachments,
}
}
}
impl From<Message> for kordophone_db::models::Message {
fn from(message: Message) -> Self {
Self {
id: message.id,
sender: match message.sender {
Participant::Me => kordophone_db::models::Participant::Me,
Participant::Remote { id, display_name } => {
kordophone_db::models::Participant::Remote { id, display_name }
}
},
text: message.text,
date: message.date,
file_transfer_guids: message.attachments.iter().map(|a| a.guid.clone()).collect(),
attachment_metadata: {
let metadata_map: HashMap<String, kordophone::model::message::AttachmentMetadata> = message.attachments
.iter()
.filter_map(|a| a.metadata.as_ref().map(|m| (a.guid.clone(), m.clone().into())))
.collect();
if metadata_map.is_empty() { None } else { Some(metadata_map) }
},
}
}
}
impl From<kordophone::model::Message> for Message {
fn from(message: kordophone::model::Message) -> Self {
let attachments = attachments_from(&message.file_transfer_guids, &message.attachment_metadata);
Self {
id: message.guid,
sender: match message.sender {
Some(sender) => Participant::Remote {
id: None,
display_name: sender,
},
None => Participant::Me,
},
text: message.text,
date: DateTime::from_timestamp(
message.date.unix_timestamp(),
message.date.unix_timestamp_nanos()
.try_into()
.unwrap_or(0),
)
.unwrap()
.naive_local(),
attachments,
}
}
}
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,
attachments: Vec::new(), // Outgoing messages don't have attachments initially
}
}
}
pub struct MessageBuilder {
id: Option<String>,
sender: Option<Participant>,
text: Option<String>,
date: Option<NaiveDateTime>,
attachments: Vec<Attachment>,
}
impl Default for MessageBuilder {
fn default() -> Self {
Self::new()
}
}
impl MessageBuilder {
pub fn new() -> Self {
Self {
id: None,
sender: None,
text: None,
date: None,
attachments: Vec::new(),
}
}
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
}
pub fn attachments(mut self, attachments: Vec<Attachment>) -> Self {
self.attachments = attachments;
self
}
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()),
attachments: self.attachments,
}
}
}