cargo fmt
This commit is contained in:
@@ -278,18 +278,27 @@ impl DaemonInterface for XpcDaemonInterface {
|
||||
|
||||
if let Some(id) = _conversation_id {
|
||||
let mut args = HashMap::new();
|
||||
args.insert(Self::key("conversation_id"), Message::String(CString::new(id).unwrap()));
|
||||
let _ = self.call_method(&mut client, "SyncConversation", Some(args)).await?;
|
||||
args.insert(
|
||||
Self::key("conversation_id"),
|
||||
Message::String(CString::new(id).unwrap()),
|
||||
);
|
||||
let _ = self
|
||||
.call_method(&mut client, "SyncConversation", Some(args))
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let _ = self.call_method(&mut client, "SyncAllConversations", None).await?;
|
||||
let _ = self
|
||||
.call_method(&mut client, "SyncAllConversations", None)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
async fn sync_conversations_list(&mut self) -> Result<()> {
|
||||
let mach_port_name = Self::build_service_name()?;
|
||||
let mut client = XPCClient::connect(&mach_port_name);
|
||||
let _ = self.call_method(&mut client, "SyncConversationList", None).await?;
|
||||
let _ = self
|
||||
.call_method(&mut client, "SyncConversationList", None)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
async fn print_messages(
|
||||
@@ -301,24 +310,39 @@ impl DaemonInterface for XpcDaemonInterface {
|
||||
let mut client = XPCClient::connect(&mach_port_name);
|
||||
|
||||
let mut args = HashMap::new();
|
||||
args.insert(Self::key("conversation_id"), Message::String(CString::new(_conversation_id).unwrap()));
|
||||
args.insert(
|
||||
Self::key("conversation_id"),
|
||||
Message::String(CString::new(_conversation_id).unwrap()),
|
||||
);
|
||||
if let Some(last) = _last_message_id {
|
||||
args.insert(Self::key("last_message_id"), Message::String(CString::new(last).unwrap()));
|
||||
args.insert(
|
||||
Self::key("last_message_id"),
|
||||
Message::String(CString::new(last).unwrap()),
|
||||
);
|
||||
}
|
||||
|
||||
let reply = self.call_method(&mut client, "GetMessages", Some(args)).await?;
|
||||
let reply = self
|
||||
.call_method(&mut client, "GetMessages", Some(args))
|
||||
.await?;
|
||||
match reply.get(&Self::key("messages")) {
|
||||
Some(Message::Array(items)) => {
|
||||
println!("Number of messages: {}", items.len());
|
||||
for item in items {
|
||||
if let Message::Dictionary(map) = item {
|
||||
let guid = Self::get_string(map, "id").map(|s| s.to_string_lossy().into_owned()).unwrap_or_default();
|
||||
let sender = Self::get_string(map, "sender").map(|s| s.to_string_lossy().into_owned()).unwrap_or_default();
|
||||
let text = Self::get_string(map, "text").map(|s| s.to_string_lossy().into_owned()).unwrap_or_default();
|
||||
let guid = Self::get_string(map, "id")
|
||||
.map(|s| s.to_string_lossy().into_owned())
|
||||
.unwrap_or_default();
|
||||
let sender = Self::get_string(map, "sender")
|
||||
.map(|s| s.to_string_lossy().into_owned())
|
||||
.unwrap_or_default();
|
||||
let text = Self::get_string(map, "text")
|
||||
.map(|s| s.to_string_lossy().into_owned())
|
||||
.unwrap_or_default();
|
||||
let date_ts = Self::get_i64_from_str(map, "date").unwrap_or(0);
|
||||
let msg = crate::printers::PrintableMessage {
|
||||
guid,
|
||||
date: time::OffsetDateTime::from_unix_timestamp(date_ts).unwrap_or_else(|_| time::OffsetDateTime::UNIX_EPOCH),
|
||||
date: time::OffsetDateTime::from_unix_timestamp(date_ts)
|
||||
.unwrap_or_else(|_| time::OffsetDateTime::UNIX_EPOCH),
|
||||
sender,
|
||||
text,
|
||||
file_transfer_guids: vec![],
|
||||
@@ -340,10 +364,20 @@ impl DaemonInterface for XpcDaemonInterface {
|
||||
let mach_port_name = Self::build_service_name()?;
|
||||
let mut client = XPCClient::connect(&mach_port_name);
|
||||
let mut args = HashMap::new();
|
||||
args.insert(Self::key("conversation_id"), Message::String(CString::new(_conversation_id).unwrap()));
|
||||
args.insert(Self::key("text"), Message::String(CString::new(_text).unwrap()));
|
||||
let reply = self.call_method(&mut client, "SendMessage", Some(args)).await?;
|
||||
if let Some(uuid) = Self::get_string(&reply, "uuid") { println!("Outgoing message ID: {}", uuid.to_string_lossy()); }
|
||||
args.insert(
|
||||
Self::key("conversation_id"),
|
||||
Message::String(CString::new(_conversation_id).unwrap()),
|
||||
);
|
||||
args.insert(
|
||||
Self::key("text"),
|
||||
Message::String(CString::new(_text).unwrap()),
|
||||
);
|
||||
let reply = self
|
||||
.call_method(&mut client, "SendMessage", Some(args))
|
||||
.await?;
|
||||
if let Some(uuid) = Self::get_string(&reply, "uuid") {
|
||||
println!("Outgoing message ID: {}", uuid.to_string_lossy());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
async fn wait_for_signals(&mut self) -> Result<()> {
|
||||
@@ -351,7 +385,10 @@ impl DaemonInterface for XpcDaemonInterface {
|
||||
let mut client = XPCClient::connect(&mach_port_name);
|
||||
|
||||
// Send a subscription/warm-up message so the server loop starts selecting for this client
|
||||
client.send_message(Message::Dictionary(Self::build_request("SubscribeSignals", None)));
|
||||
client.send_message(Message::Dictionary(Self::build_request(
|
||||
"SubscribeSignals",
|
||||
None,
|
||||
)));
|
||||
|
||||
println!("Waiting for XPC signals...");
|
||||
while let Some(msg) = client.next().await {
|
||||
@@ -359,7 +396,10 @@ impl DaemonInterface for XpcDaemonInterface {
|
||||
Message::Dictionary(map) => {
|
||||
let name_key = Self::key("name");
|
||||
let args_key = Self::key("arguments");
|
||||
let name = match map.get(&name_key) { Some(Message::String(s)) => s.to_string_lossy().into_owned(), _ => continue };
|
||||
let name = match map.get(&name_key) {
|
||||
Some(Message::String(s)) => s.to_string_lossy().into_owned(),
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
match name.as_str() {
|
||||
"ConversationsUpdated" => {
|
||||
@@ -367,8 +407,13 @@ impl DaemonInterface for XpcDaemonInterface {
|
||||
}
|
||||
"MessagesUpdated" => {
|
||||
if let Some(Message::Dictionary(args)) = map.get(&args_key) {
|
||||
if let Some(Message::String(cid)) = args.get(&Self::key("conversation_id")) {
|
||||
println!("Signal: Messages updated for conversation {}", cid.to_string_lossy());
|
||||
if let Some(Message::String(cid)) =
|
||||
args.get(&Self::key("conversation_id"))
|
||||
{
|
||||
println!(
|
||||
"Signal: Messages updated for conversation {}",
|
||||
cid.to_string_lossy()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -377,23 +422,52 @@ impl DaemonInterface for XpcDaemonInterface {
|
||||
}
|
||||
"AttachmentDownloadCompleted" => {
|
||||
if let Some(Message::Dictionary(args)) = map.get(&args_key) {
|
||||
if let Some(Message::String(aid)) = args.get(&Self::key("attachment_id")) {
|
||||
println!("Signal: Attachment downloaded: {}", aid.to_string_lossy());
|
||||
if let Some(Message::String(aid)) =
|
||||
args.get(&Self::key("attachment_id"))
|
||||
{
|
||||
println!(
|
||||
"Signal: Attachment downloaded: {}",
|
||||
aid.to_string_lossy()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
"AttachmentDownloadFailed" => {
|
||||
if let Some(Message::Dictionary(args)) = map.get(&args_key) {
|
||||
if let Some(Message::String(aid)) = args.get(&Self::key("attachment_id")) {
|
||||
eprintln!("Signal: Attachment download failed: {}", aid.to_string_lossy());
|
||||
if let Some(Message::String(aid)) =
|
||||
args.get(&Self::key("attachment_id"))
|
||||
{
|
||||
eprintln!(
|
||||
"Signal: Attachment download failed: {}",
|
||||
aid.to_string_lossy()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
"AttachmentUploadCompleted" => {
|
||||
if let Some(Message::Dictionary(args)) = map.get(&args_key) {
|
||||
let upload = args.get(&Self::key("upload_guid")).and_then(|v| match v { Message::String(s) => Some(s.to_string_lossy().into_owned()), _ => None }).unwrap_or_default();
|
||||
let attachment = args.get(&Self::key("attachment_guid")).and_then(|v| match v { Message::String(s) => Some(s.to_string_lossy().into_owned()), _ => None }).unwrap_or_default();
|
||||
println!("Signal: Attachment uploaded: upload={}, attachment={}", upload, attachment);
|
||||
let upload = args
|
||||
.get(&Self::key("upload_guid"))
|
||||
.and_then(|v| match v {
|
||||
Message::String(s) => {
|
||||
Some(s.to_string_lossy().into_owned())
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.unwrap_or_default();
|
||||
let attachment = args
|
||||
.get(&Self::key("attachment_guid"))
|
||||
.and_then(|v| match v {
|
||||
Message::String(s) => {
|
||||
Some(s.to_string_lossy().into_owned())
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.unwrap_or_default();
|
||||
println!(
|
||||
"Signal: Attachment uploaded: upload={}, attachment={}",
|
||||
upload, attachment
|
||||
);
|
||||
}
|
||||
}
|
||||
"ConfigChanged" => {
|
||||
@@ -413,23 +487,40 @@ impl DaemonInterface for XpcDaemonInterface {
|
||||
let mut client = XPCClient::connect(&mach_port_name);
|
||||
match _cmd {
|
||||
ConfigCommands::Print => {
|
||||
let reply = self.call_method(&mut client, "GetAllSettings", None).await?;
|
||||
let server_url = Self::get_string(&reply, "server_url").map(|s| s.to_string_lossy().into_owned()).unwrap_or_default();
|
||||
let username = Self::get_string(&reply, "username").map(|s| s.to_string_lossy().into_owned()).unwrap_or_default();
|
||||
let table = prettytable::table!([b->"Server URL", &server_url], [b->"Username", &username]);
|
||||
let reply = self
|
||||
.call_method(&mut client, "GetAllSettings", None)
|
||||
.await?;
|
||||
let server_url = Self::get_string(&reply, "server_url")
|
||||
.map(|s| s.to_string_lossy().into_owned())
|
||||
.unwrap_or_default();
|
||||
let username = Self::get_string(&reply, "username")
|
||||
.map(|s| s.to_string_lossy().into_owned())
|
||||
.unwrap_or_default();
|
||||
let table =
|
||||
prettytable::table!([b->"Server URL", &server_url], [b->"Username", &username]);
|
||||
table.printstd();
|
||||
Ok(())
|
||||
}
|
||||
ConfigCommands::SetServerUrl { url } => {
|
||||
let mut args = HashMap::new();
|
||||
args.insert(Self::key("server_url"), Message::String(CString::new(url).unwrap()));
|
||||
let _ = self.call_method(&mut client, "UpdateSettings", Some(args)).await?;
|
||||
args.insert(
|
||||
Self::key("server_url"),
|
||||
Message::String(CString::new(url).unwrap()),
|
||||
);
|
||||
let _ = self
|
||||
.call_method(&mut client, "UpdateSettings", Some(args))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
ConfigCommands::SetUsername { username } => {
|
||||
let mut args = HashMap::new();
|
||||
args.insert(Self::key("username"), Message::String(CString::new(username).unwrap()));
|
||||
let _ = self.call_method(&mut client, "UpdateSettings", Some(args)).await?;
|
||||
args.insert(
|
||||
Self::key("username"),
|
||||
Message::String(CString::new(username).unwrap()),
|
||||
);
|
||||
let _ = self
|
||||
.call_method(&mut client, "UpdateSettings", Some(args))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -437,33 +528,55 @@ impl DaemonInterface for XpcDaemonInterface {
|
||||
async fn delete_all_conversations(&mut self) -> Result<()> {
|
||||
let mach_port_name = Self::build_service_name()?;
|
||||
let mut client = XPCClient::connect(&mach_port_name);
|
||||
let _ = self.call_method(&mut client, "DeleteAllConversations", None).await?;
|
||||
let _ = self
|
||||
.call_method(&mut client, "DeleteAllConversations", None)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
async fn download_attachment(&mut self, _attachment_id: String) -> Result<()> {
|
||||
let mach_port_name = Self::build_service_name()?;
|
||||
let mut client = XPCClient::connect(&mach_port_name);
|
||||
let mut args = HashMap::new();
|
||||
args.insert(Self::key("attachment_id"), Message::String(CString::new(_attachment_id).unwrap()));
|
||||
args.insert(Self::key("preview"), Message::String(CString::new("false").unwrap()));
|
||||
let _ = self.call_method(&mut client, "DownloadAttachment", Some(args)).await?;
|
||||
args.insert(
|
||||
Self::key("attachment_id"),
|
||||
Message::String(CString::new(_attachment_id).unwrap()),
|
||||
);
|
||||
args.insert(
|
||||
Self::key("preview"),
|
||||
Message::String(CString::new("false").unwrap()),
|
||||
);
|
||||
let _ = self
|
||||
.call_method(&mut client, "DownloadAttachment", Some(args))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
async fn upload_attachment(&mut self, _path: String) -> Result<()> {
|
||||
let mach_port_name = Self::build_service_name()?;
|
||||
let mut client = XPCClient::connect(&mach_port_name);
|
||||
let mut args = HashMap::new();
|
||||
args.insert(Self::key("path"), Message::String(CString::new(_path).unwrap()));
|
||||
let reply = self.call_method(&mut client, "UploadAttachment", Some(args)).await?;
|
||||
if let Some(guid) = Self::get_string(&reply, "upload_guid") { println!("Upload GUID: {}", guid.to_string_lossy()); }
|
||||
args.insert(
|
||||
Self::key("path"),
|
||||
Message::String(CString::new(_path).unwrap()),
|
||||
);
|
||||
let reply = self
|
||||
.call_method(&mut client, "UploadAttachment", Some(args))
|
||||
.await?;
|
||||
if let Some(guid) = Self::get_string(&reply, "upload_guid") {
|
||||
println!("Upload GUID: {}", guid.to_string_lossy());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
async fn mark_conversation_as_read(&mut self, _conversation_id: String) -> Result<()> {
|
||||
let mach_port_name = Self::build_service_name()?;
|
||||
let mut client = XPCClient::connect(&mach_port_name);
|
||||
let mut args = HashMap::new();
|
||||
args.insert(Self::key("conversation_id"), Message::String(CString::new(_conversation_id).unwrap()));
|
||||
let _ = self.call_method(&mut client, "MarkConversationAsRead", Some(args)).await?;
|
||||
args.insert(
|
||||
Self::key("conversation_id"),
|
||||
Message::String(CString::new(_conversation_id).unwrap()),
|
||||
);
|
||||
let _ = self
|
||||
.call_method(&mut client, "MarkConversationAsRead", Some(args))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user