xpc: implement GetConversations
This commit is contained in:
@@ -133,6 +133,50 @@ async fn dispatch(agent: &XpcAgent, root: &HashMap<CString, Message>) -> Message
|
||||
}
|
||||
}
|
||||
|
||||
"GetConversations" => {
|
||||
// Defaults
|
||||
let mut limit: i32 = 100;
|
||||
let mut offset: i32 = 0;
|
||||
|
||||
if let Some(args) = get_dictionary_field(root, "arguments") {
|
||||
if let Some(Message::String(v)) = args.get(&cstr("limit")) { limit = v.to_string_lossy().parse().unwrap_or(100); }
|
||||
if let Some(Message::String(v)) = args.get(&cstr("offset")) { offset = v.to_string_lossy().parse().unwrap_or(0); }
|
||||
}
|
||||
|
||||
match agent.send_event(|r| Event::GetAllConversations(limit, offset, r)).await {
|
||||
Ok(conversations) => {
|
||||
// Build array of conversation dictionaries
|
||||
let mut items: Vec<Message> = Vec::with_capacity(conversations.len());
|
||||
for conv in conversations {
|
||||
let mut m: HashMap<CString, Message> = HashMap::new();
|
||||
m.insert(cstr("guid"), Message::String(cstr(&conv.guid)));
|
||||
m.insert(cstr("display_name"), Message::String(cstr(&conv.display_name.unwrap_or_default())));
|
||||
m.insert(cstr("unread_count"), Message::String(cstr(&(conv.unread_count as i64).to_string())));
|
||||
m.insert(cstr("last_message_preview"), Message::String(cstr(&conv.last_message_preview.unwrap_or_default())));
|
||||
|
||||
// participants -> array of strings
|
||||
let participants: Vec<Message> = conv
|
||||
.participants
|
||||
.into_iter()
|
||||
.map(|p| Message::String(cstr(&p.display_name())))
|
||||
.collect();
|
||||
m.insert(cstr("participants"), Message::Array(participants));
|
||||
|
||||
// date as unix timestamp (i64)
|
||||
m.insert(cstr("date"), Message::String(cstr(&conv.date.and_utc().timestamp().to_string())));
|
||||
|
||||
items.push(Message::Dictionary(m));
|
||||
}
|
||||
|
||||
let mut reply: HashMap<CString, Message> = HashMap::new();
|
||||
reply.insert(cstr("type"), Message::String(cstr("GetConversationsResponse")));
|
||||
reply.insert(cstr("conversations"), Message::Array(items));
|
||||
Message::Dictionary(reply)
|
||||
}
|
||||
Err(e) => make_error_reply("DaemonError", &format!("{}", e)),
|
||||
}
|
||||
}
|
||||
|
||||
// Unknown method fallback
|
||||
other => make_error_reply("UnknownMethod", other),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user