diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 09fac3f1e..3948ecd61 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -4460,7 +4460,7 @@ mod jsonrpc { } let cmd_api = - deltachat_jsonrpc::api::CommandApi::new_from_arc((*account_manager).inner.clone()); + deltachat_jsonrpc::api::CommandApi::from_arc((*account_manager).inner.clone()); let (request_handle, receiver) = RpcClient::new(); let handle = RpcSession::new(request_handle, cmd_api); diff --git a/deltachat-jsonrpc/Cargo.toml b/deltachat-jsonrpc/Cargo.toml index f28cb8dd9..5666489dd 100644 --- a/deltachat-jsonrpc/Cargo.toml +++ b/deltachat-jsonrpc/Cargo.toml @@ -1,6 +1,7 @@ [package] name = "deltachat-jsonrpc" version = "1.86.0" +description = "DeltaChat JSON-RPC API" authors = ["Delta Chat Developers (ML) "] edition = "2021" default-run = "deltachat-jsonrpc-server" diff --git a/deltachat-jsonrpc/src/api/events.rs b/deltachat-jsonrpc/src/api/events.rs index 57f19b7e1..ad0fb5faf 100644 --- a/deltachat-jsonrpc/src/api/events.rs +++ b/deltachat-jsonrpc/src/api/events.rs @@ -62,8 +62,9 @@ pub fn event_to_json_rpc_notification(event: Event) -> Value { } => (json!(msg_id), json!(status_update_serial)), }; + let id: EventTypeName = event.typ.into(); json!({ - "id": event_type_to_string(event.typ), + "id": id, "contextId": event.id, "field1": field1, "field2": field2 @@ -103,38 +104,40 @@ pub enum EventTypeName { WebxdcStatusUpdate, } -fn event_type_to_string(event: EventType) -> EventTypeName { - use EventTypeName::*; - match event { - EventType::Info(_) => Info, - EventType::SmtpConnected(_) => SmtpConnected, - EventType::ImapConnected(_) => ImapConnected, - EventType::SmtpMessageSent(_) => SmtpMessageSent, - EventType::ImapMessageDeleted(_) => ImapMessageDeleted, - EventType::ImapMessageMoved(_) => ImapMessageMoved, - EventType::NewBlobFile(_) => NewBlobFile, - EventType::DeletedBlobFile(_) => DeletedBlobFile, - EventType::Warning(_) => Warning, - EventType::Error(_) => Error, - EventType::ErrorSelfNotInGroup(_) => ErrorSelfNotInGroup, - EventType::MsgsChanged { .. } => MsgsChanged, - EventType::IncomingMsg { .. } => IncomingMsg, - EventType::MsgsNoticed(_) => MsgsNoticed, - EventType::MsgDelivered { .. } => MsgDelivered, - EventType::MsgFailed { .. } => MsgFailed, - EventType::MsgRead { .. } => MsgRead, - EventType::ChatModified(_) => ChatModified, - EventType::ChatEphemeralTimerModified { .. } => ChatEphemeralTimerModified, - EventType::ContactsChanged(_) => ContactsChanged, - EventType::LocationChanged(_) => LocationChanged, - EventType::ConfigureProgress { .. } => ConfigureProgress, - EventType::ImexProgress(_) => ImexProgress, - EventType::ImexFileWritten(_) => ImexFileWritten, - EventType::SecurejoinInviterProgress { .. } => SecurejoinInviterProgress, - EventType::SecurejoinJoinerProgress { .. } => SecurejoinJoinerProgress, - EventType::ConnectivityChanged => ConnectivityChanged, - EventType::SelfavatarChanged => SelfavatarChanged, - EventType::WebxdcStatusUpdate { .. } => WebxdcStatusUpdate, +impl From for EventTypeName { + fn from(event: EventType) -> Self { + use EventTypeName::*; + match event { + EventType::Info(_) => Info, + EventType::SmtpConnected(_) => SmtpConnected, + EventType::ImapConnected(_) => ImapConnected, + EventType::SmtpMessageSent(_) => SmtpMessageSent, + EventType::ImapMessageDeleted(_) => ImapMessageDeleted, + EventType::ImapMessageMoved(_) => ImapMessageMoved, + EventType::NewBlobFile(_) => NewBlobFile, + EventType::DeletedBlobFile(_) => DeletedBlobFile, + EventType::Warning(_) => Warning, + EventType::Error(_) => Error, + EventType::ErrorSelfNotInGroup(_) => ErrorSelfNotInGroup, + EventType::MsgsChanged { .. } => MsgsChanged, + EventType::IncomingMsg { .. } => IncomingMsg, + EventType::MsgsNoticed(_) => MsgsNoticed, + EventType::MsgDelivered { .. } => MsgDelivered, + EventType::MsgFailed { .. } => MsgFailed, + EventType::MsgRead { .. } => MsgRead, + EventType::ChatModified(_) => ChatModified, + EventType::ChatEphemeralTimerModified { .. } => ChatEphemeralTimerModified, + EventType::ContactsChanged(_) => ContactsChanged, + EventType::LocationChanged(_) => LocationChanged, + EventType::ConfigureProgress { .. } => ConfigureProgress, + EventType::ImexProgress(_) => ImexProgress, + EventType::ImexFileWritten(_) => ImexFileWritten, + EventType::SecurejoinInviterProgress { .. } => SecurejoinInviterProgress, + EventType::SecurejoinJoinerProgress { .. } => SecurejoinJoinerProgress, + EventType::ConnectivityChanged => ConnectivityChanged, + EventType::SelfavatarChanged => SelfavatarChanged, + EventType::WebxdcStatusUpdate { .. } => WebxdcStatusUpdate, + } } } diff --git a/deltachat-jsonrpc/src/api/mod.rs b/deltachat-jsonrpc/src/api/mod.rs index c9c20f17f..bf1cb122e 100644 --- a/deltachat-jsonrpc/src/api/mod.rs +++ b/deltachat-jsonrpc/src/api/mod.rs @@ -41,7 +41,7 @@ impl CommandApi { } #[allow(dead_code)] - pub fn new_from_arc(accounts: Arc>) -> Self { + pub fn from_arc(accounts: Arc>) -> Self { CommandApi { accounts } } @@ -258,7 +258,7 @@ impl CommandApi { query_contact_id.map(ContactId::new), ) .await?; - let mut l: Vec = Vec::new(); + let mut l: Vec = Vec::with_capacity(list.len()); for i in 0..list.len() { l.push(ChatListEntry( list.get_chat_id(i)?.to_u32(), @@ -275,8 +275,9 @@ impl CommandApi { ) -> Result> { // todo custom json deserializer for ChatListEntry? let ctx = self.get_context(account_id).await?; - let mut result: HashMap = HashMap::new(); - for (_i, entry) in entries.iter().enumerate() { + let mut result: HashMap = + HashMap::with_capacity(entries.len()); + for entry in entries.iter() { result.insert( entry.0, match get_chat_list_item_by_id(&ctx, entry).await { @@ -301,7 +302,7 @@ impl CommandApi { chat_id: u32, ) -> Result { let ctx = self.get_context(account_id).await?; - FullChat::from_dc_chat_id(&ctx, chat_id).await + FullChat::try_from_dc_chat_id(&ctx, chat_id).await } async fn accept_chat(&self, account_id: u32, chat_id: u32) -> Result<()> { diff --git a/deltachat-jsonrpc/src/api/types/account.rs b/deltachat-jsonrpc/src/api/types/account.rs index 7bb6d3bbc..eeebc991f 100644 --- a/deltachat-jsonrpc/src/api/types/account.rs +++ b/deltachat-jsonrpc/src/api/types/account.rs @@ -9,7 +9,6 @@ use super::color_int_to_hex_string; #[derive(Serialize, TypeDef)] #[serde(tag = "type")] pub enum Account { - //#[serde(rename_all = "camelCase")] Configured { id: u32, display_name: Option, diff --git a/deltachat-jsonrpc/src/api/types/chat.rs b/deltachat-jsonrpc/src/api/types/chat.rs index 20e04b2fc..33a2d332e 100644 --- a/deltachat-jsonrpc/src/api/types/chat.rs +++ b/deltachat-jsonrpc/src/api/types/chat.rs @@ -35,13 +35,13 @@ pub struct FullChat { } impl FullChat { - pub async fn from_dc_chat_id(context: &Context, chat_id: u32) -> Result { + pub async fn try_from_dc_chat_id(context: &Context, chat_id: u32) -> Result { let rust_chat_id = ChatId::new(chat_id); let chat = Chat::load_from_db(context, rust_chat_id).await?; let contact_ids = get_chat_contacts(context, rust_chat_id).await?; - let mut contacts = Vec::new(); + let mut contacts = Vec::with_capacity(contact_ids.len()); for contact_id in &contact_ids { contacts.push(