xpc: implement GetConversations
This commit is contained in:
@@ -17,6 +17,7 @@ use futures::{
|
||||
|
||||
const SERVICE_NAME: &str = "net.buzzert.kordophonecd\0";
|
||||
const GET_VERSION_METHOD: &str = "GetVersion";
|
||||
const GET_CONVERSATIONS_METHOD: &str = "GetConversations";
|
||||
|
||||
// We can't use XPCClient from xpc-connection because of some strange decisions with which flags
|
||||
// are passed to xpc_connection_create_mach_service.
|
||||
@@ -164,7 +165,58 @@ impl DaemonInterface for XpcDaemonInterface {
|
||||
|
||||
// Remaining methods unimplemented on macOS
|
||||
async fn print_conversations(&mut self) -> Result<()> {
|
||||
Err(anyhow::anyhow!("Feature not implemented for XPC"))
|
||||
// Connect
|
||||
let mach_port_name = Self::build_service_name()?;
|
||||
let mut client = XPCClient::connect(&mach_port_name);
|
||||
|
||||
// Build arguments: limit=100, offset=0 (string-encoded for portability)
|
||||
let mut args = HashMap::new();
|
||||
args.insert(CString::new("limit").unwrap(), Message::String(CString::new("100").unwrap()));
|
||||
args.insert(CString::new("offset").unwrap(), Message::String(CString::new("0").unwrap()));
|
||||
|
||||
// Call
|
||||
let reply = self
|
||||
.call_method(&mut client, GET_CONVERSATIONS_METHOD, Some(args))
|
||||
.await?;
|
||||
|
||||
// Expect an array under "conversations"
|
||||
match reply.get(&CString::new("conversations").unwrap()) {
|
||||
Some(Message::Array(items)) => {
|
||||
println!("Number of conversations: {}", items.len());
|
||||
|
||||
for item in items {
|
||||
if let Message::Dictionary(map) = item {
|
||||
// Convert to PrintableConversation
|
||||
let guid = Self::get_string(map, "guid").map(|s| s.to_string_lossy().into_owned()).unwrap_or_default();
|
||||
let display_name = Self::get_string(map, "display_name").map(|s| s.to_string_lossy().into_owned());
|
||||
let last_preview = Self::get_string(map, "last_message_preview").map(|s| s.to_string_lossy().into_owned());
|
||||
|
||||
let unread_count = match map.get(&CString::new("unread_count").unwrap()) { Some(Message::String(v)) => v.to_string_lossy().parse().unwrap_or(0), _ => 0 };
|
||||
let date_ts: i64 = match map.get(&CString::new("date").unwrap()) { Some(Message::String(v)) => v.to_string_lossy().parse().unwrap_or(0), _ => 0 };
|
||||
|
||||
let participants: Vec<String> = match map.get(&CString::new("participants").unwrap()) {
|
||||
Some(Message::Array(arr)) => arr.iter().filter_map(|m| match m { Message::String(s) => Some(s.to_string_lossy().into_owned()), _ => None }).collect(),
|
||||
_ => Vec::new(),
|
||||
};
|
||||
|
||||
// Build PrintableConversation directly
|
||||
let conv = crate::printers::PrintableConversation {
|
||||
guid,
|
||||
display_name,
|
||||
last_message_preview: last_preview,
|
||||
unread_count,
|
||||
date: time::OffsetDateTime::from_unix_timestamp(date_ts).unwrap_or_else(|_| time::OffsetDateTime::UNIX_EPOCH),
|
||||
participants,
|
||||
};
|
||||
|
||||
println!("{}", crate::printers::ConversationPrinter::new(&conv));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Some(other) => Err(anyhow::anyhow!("Unexpected conversations payload: {:?}", other)),
|
||||
None => Err(anyhow::anyhow!("Missing conversations in reply")),
|
||||
}
|
||||
}
|
||||
async fn sync_conversations(&mut self, _conversation_id: Option<String>) -> Result<()> {
|
||||
Err(anyhow::anyhow!("Feature not implemented for XPC"))
|
||||
|
||||
Reference in New Issue
Block a user