Private
Public Access
1
0

Started working on attachment store

This commit is contained in:
2025-05-15 20:11:10 -07:00
parent 77177e07aa
commit 0d4c2e5104
10 changed files with 721 additions and 307 deletions

View File

@@ -1,16 +1,17 @@
use dbus::arg;
use dbus_tree::MethodErr;
use tokio::sync::mpsc;
use std::future::Future;
use std::thread;
use tokio::sync::oneshot;
use tokio::sync::mpsc;
use tokio::sync::oneshot;
use crate::daemon::{
DaemonResult,
events::{Event, Reply},
settings::Settings,
Attachment, DaemonResult,
};
use crate::dbus::interface::NetBuzzertKordophoneAttachment as DbusAttachment;
use crate::dbus::interface::NetBuzzertKordophoneRepository as DbusRepository;
use crate::dbus::interface::NetBuzzertKordophoneSettings as DbusSettings;
@@ -29,10 +30,11 @@ impl ServerImpl {
make_event: impl FnOnce(Reply<T>) -> Event,
) -> DaemonResult<T> {
let (reply_tx, reply_rx) = oneshot::channel();
self.event_sink.send(make_event(reply_tx))
self.event_sink
.send(make_event(reply_tx))
.await
.map_err(|_| "Failed to send event")?;
reply_rx.await.map_err(|_| "Failed to receive reply".into())
}
@@ -49,21 +51,48 @@ impl ServerImpl {
impl DbusRepository for ServerImpl {
fn get_version(&mut self) -> Result<String, MethodErr> {
self.send_event_sync(Event::GetVersion)
}
}
fn get_conversations(&mut self, limit: i32, offset: i32) -> Result<Vec<arg::PropMap>, dbus::MethodErr> {
fn get_conversations(
&mut self,
limit: i32,
offset: i32,
) -> Result<Vec<arg::PropMap>, dbus::MethodErr> {
self.send_event_sync(|r| Event::GetAllConversations(limit, offset, r))
.map(|conversations| {
conversations.into_iter().map(|conv| {
let mut map = arg::PropMap::new();
map.insert("guid".into(), arg::Variant(Box::new(conv.guid)));
map.insert("display_name".into(), arg::Variant(Box::new(conv.display_name.unwrap_or_default())));
map.insert("unread_count".into(), arg::Variant(Box::new(conv.unread_count as i32)));
map.insert("last_message_preview".into(), arg::Variant(Box::new(conv.last_message_preview.unwrap_or_default())));
map.insert("participants".into(), arg::Variant(Box::new(conv.participants.into_iter().map(|p| p.display_name()).collect::<Vec<String>>())));
map.insert("date".into(), arg::Variant(Box::new(conv.date.and_utc().timestamp())));
map
}).collect()
conversations
.into_iter()
.map(|conv| {
let mut map = arg::PropMap::new();
map.insert("guid".into(), arg::Variant(Box::new(conv.guid)));
map.insert(
"display_name".into(),
arg::Variant(Box::new(conv.display_name.unwrap_or_default())),
);
map.insert(
"unread_count".into(),
arg::Variant(Box::new(conv.unread_count as i32)),
);
map.insert(
"last_message_preview".into(),
arg::Variant(Box::new(conv.last_message_preview.unwrap_or_default())),
);
map.insert(
"participants".into(),
arg::Variant(Box::new(
conv.participants
.into_iter()
.map(|p| p.display_name())
.collect::<Vec<String>>(),
)),
);
map.insert(
"date".into(),
arg::Variant(Box::new(conv.date.and_utc().timestamp())),
);
map
})
.collect()
})
}
@@ -79,7 +108,11 @@ impl DbusRepository for ServerImpl {
self.send_event_sync(|r| Event::SyncConversation(conversation_id, r))
}
fn get_messages(&mut self, conversation_id: String, last_message_id: String) -> Result<Vec<arg::PropMap>, dbus::MethodErr> {
fn get_messages(
&mut self,
conversation_id: String,
last_message_id: String,
) -> Result<Vec<arg::PropMap>, dbus::MethodErr> {
let last_message_id_opt = if last_message_id.is_empty() {
None
} else {
@@ -88,14 +121,23 @@ impl DbusRepository for ServerImpl {
self.send_event_sync(|r| Event::GetMessages(conversation_id, last_message_id_opt, r))
.map(|messages| {
messages.into_iter().map(|msg| {
let mut map = arg::PropMap::new();
map.insert("id".into(), arg::Variant(Box::new(msg.id)));
map.insert("text".into(), arg::Variant(Box::new(msg.text)));
map.insert("date".into(), arg::Variant(Box::new(msg.date.and_utc().timestamp())));
map.insert("sender".into(), arg::Variant(Box::new(msg.sender.display_name())));
map
}).collect()
messages
.into_iter()
.map(|msg| {
let mut map = arg::PropMap::new();
map.insert("id".into(), arg::Variant(Box::new(msg.id)));
map.insert("text".into(), arg::Variant(Box::new(msg.text)));
map.insert(
"date".into(),
arg::Variant(Box::new(msg.date.and_utc().timestamp())),
);
map.insert(
"sender".into(),
arg::Variant(Box::new(msg.sender.display_name())),
);
map
})
.collect()
})
}
@@ -103,21 +145,35 @@ impl DbusRepository for ServerImpl {
self.send_event_sync(Event::DeleteAllConversations)
}
fn send_message(&mut self, conversation_id: String, text: String) -> Result<String, dbus::MethodErr> {
fn send_message(
&mut self,
conversation_id: String,
text: String,
) -> Result<String, dbus::MethodErr> {
self.send_event_sync(|r| Event::SendMessage(conversation_id, text, r))
.map(|uuid| uuid.to_string())
}
}
fn get_attachment(
&mut self,
attachment_id: String,
) -> Result<dbus::Path<'static>, dbus::MethodErr> {
todo!()
}
}
impl DbusSettings for ServerImpl {
fn set_server(&mut self, url: String, user: String) -> Result<(), dbus::MethodErr> {
self.send_event_sync(|r|
Event::UpdateSettings(Settings {
server_url: Some(url),
username: Some(user),
token: None,
}, r)
)
self.send_event_sync(|r| {
Event::UpdateSettings(
Settings {
server_url: Some(url),
username: Some(user),
token: None,
},
r,
)
})
}
fn server_url(&self) -> Result<String, dbus::MethodErr> {
@@ -126,13 +182,16 @@ impl DbusSettings for ServerImpl {
}
fn set_server_url(&self, value: String) -> Result<(), dbus::MethodErr> {
self.send_event_sync(|r|
Event::UpdateSettings(Settings {
server_url: Some(value),
username: None,
token: None,
}, r)
)
self.send_event_sync(|r| {
Event::UpdateSettings(
Settings {
server_url: Some(value),
username: None,
token: None,
},
r,
)
})
}
fn username(&self) -> Result<String, dbus::MethodErr> {
@@ -141,13 +200,32 @@ impl DbusSettings for ServerImpl {
}
fn set_username(&self, value: String) -> Result<(), dbus::MethodErr> {
self.send_event_sync(|r|
Event::UpdateSettings(Settings {
server_url: None,
username: Some(value),
token: None,
}, r)
)
self.send_event_sync(|r| {
Event::UpdateSettings(
Settings {
server_url: None,
username: Some(value),
token: None,
},
r,
)
})
}
}
impl DbusAttachment for Attachment {
fn file_path(&self) -> Result<String, dbus::MethodErr> {
Ok(self.path.as_os_str().to_os_string().into_string().unwrap())
}
fn downloaded(&self) -> Result<bool, dbus::MethodErr> {
Ok(self.downloaded)
}
fn delete(&mut self) -> Result<(), dbus::MethodErr> {
// Mostly a placeholder method because dbuscodegen for some reason barfs on this
// if there are no methods defined.
todo!()
}
}
@@ -172,4 +250,4 @@ where
.join()
})
.expect("Error joining runtime thread")
}
}