diff --git a/CHANGELOG.md b/CHANGELOG.md index 66deb3c6d..21e3da55a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### API-Changes - jsonrpc: add `mailingListAddress` property to `FullChat` #3607 +- jsonrpc: add `MessageNotificationInfo` & `messageGetNotificationInfo()` #3614 ### Changes - truncate incoming messages by lines instead of just length #3480 diff --git a/deltachat-jsonrpc/src/api/mod.rs b/deltachat-jsonrpc/src/api/mod.rs index 708a50771..410b7123f 100644 --- a/deltachat-jsonrpc/src/api/mod.rs +++ b/deltachat-jsonrpc/src/api/mod.rs @@ -39,7 +39,7 @@ use types::webxdc::WebxdcMessageInfo; use self::types::{ chat::{BasicChat, MuteDuration}, - message::MessageViewtype, + message::{MessageNotificationInfo, MessageViewtype}, }; #[derive(Clone, Debug)] @@ -642,6 +642,16 @@ impl CommandApi { Ok(messages) } + /// Fetch info desktop needs for creating a notification for a message + async fn message_get_notification_info( + &self, + account_id: u32, + message_id: u32, + ) -> Result { + let ctx = self.get_context(account_id).await?; + MessageNotificationInfo::from_msg_id(&ctx, MsgId::new(message_id)).await + } + /// Delete messages. The messages are deleted on the current device and /// on the IMAP server. async fn delete_messages(&self, account_id: u32, message_ids: Vec) -> Result<()> { diff --git a/deltachat-jsonrpc/src/api/types/message.rs b/deltachat-jsonrpc/src/api/types/message.rs index 62c867c39..f98fc2767 100644 --- a/deltachat-jsonrpc/src/api/types/message.rs +++ b/deltachat-jsonrpc/src/api/types/message.rs @@ -1,4 +1,5 @@ use anyhow::{anyhow, Result}; +use deltachat::chat::Chat; use deltachat::contact::Contact; use deltachat::context::Context; use deltachat::download; @@ -286,3 +287,61 @@ impl From for DownloadState { } } } + +#[derive(Serialize, TypeDef)] +#[serde(rename_all = "camelCase")] +pub struct MessageNotificationInfo { + id: u32, + chat_id: u32, + account_id: u32, + + image: Option, + image_mime_type: Option, + + chat_name: String, + chat_profile_image: Option, + + /// also known as summary_text1 + summary_prefix: Option, + /// also known as summary_text2 + summary_text: String, +} + +impl MessageNotificationInfo { + pub async fn from_msg_id(context: &Context, msg_id: MsgId) -> Result { + let message = Message::load_from_db(context, msg_id).await?; + let chat = Chat::load_from_db(context, message.get_chat_id()).await?; + + let image = if matches!( + message.get_viewtype(), + Viewtype::Image | Viewtype::Gif | Viewtype::Sticker + ) { + message + .get_file(context) + .map(|path_buf| path_buf.to_str().map(|s| s.to_owned())) + .unwrap_or_default() + } else { + None + }; + + let chat_profile_image = chat + .get_profile_image(context) + .await? + .map(|path_buf| path_buf.to_str().map(|s| s.to_owned())) + .unwrap_or_default(); + + let summary = message.get_summary(context, Some(&chat)).await?; + + Ok(MessageNotificationInfo { + id: msg_id.to_u32(), + chat_id: message.get_chat_id().to_u32(), + account_id: context.get_id(), + image, + image_mime_type: message.get_filemime(), + chat_name: chat.name, + chat_profile_image, + summary_prefix: summary.prefix.map(|s| s.to_string()), + summary_text: summary.text, + }) + } +} diff --git a/deltachat-jsonrpc/typescript/generated/client.ts b/deltachat-jsonrpc/typescript/generated/client.ts index 2b4667c49..be8be28b1 100644 --- a/deltachat-jsonrpc/typescript/generated/client.ts +++ b/deltachat-jsonrpc/typescript/generated/client.ts @@ -400,6 +400,13 @@ export class RawClient { return (this._transport.request('message_get_messages', [accountId, messageIds] as RPC.Params)) as Promise>; } + /** + * Fetch info desktop needs for creating a notification for a message + */ + public messageGetNotificationInfo(accountId: T.U32, messageId: T.U32): Promise { + return (this._transport.request('message_get_notification_info', [accountId, messageId] as RPC.Params)) as Promise; + } + /** * Delete messages. The messages are deleted on the current device and * on the IMAP server. diff --git a/deltachat-jsonrpc/typescript/generated/types.ts b/deltachat-jsonrpc/typescript/generated/types.ts index 56ffc455f..a930ee5da 100644 --- a/deltachat-jsonrpc/typescript/generated/types.ts +++ b/deltachat-jsonrpc/typescript/generated/types.ts @@ -137,5 +137,14 @@ export type WebxdcMessageInfo={ "internetAccess":boolean;}; export type DownloadState=("Done"|"Available"|"Failure"|"InProgress"); export type Message={"id":U32;"chatId":U32;"fromId":U32;"quote":(MessageQuote|null);"parentId":(U32|null);"text":(string|null);"hasLocation":boolean;"hasHtml":boolean;"viewType":Viewtype;"state":U32;"timestamp":I64;"sortTimestamp":I64;"receivedTimestamp":I64;"hasDeviatingTimestamp":boolean;"subject":string;"showPadlock":boolean;"isSetupmessage":boolean;"isInfo":boolean;"isForwarded":boolean;"duration":I32;"dimensionsHeight":I32;"dimensionsWidth":I32;"videochatType":(U32|null);"videochatUrl":(string|null);"overrideSenderName":(string|null);"sender":Contact;"setupCodeBegin":(string|null);"file":(string|null);"fileMime":(string|null);"fileBytes":U64;"fileName":(string|null);"webxdcInfo":(WebxdcMessageInfo|null);"downloadState":DownloadState;}; +export type MessageNotificationData={"id":U32;"chatId":U32;"accountId":U32;"image":(string|null);"imageMimeType":(string|null);"chatName":string;"chatProfileImage":(string|null); +/** + * also known as summary_text1 + */ +"summaryPrefix":(string|null); +/** + * also known as summary_text2 + */ +"summaryText":string;}; export type F64=number; -export type __AllTyps=[string,boolean,Record,U32,U32,null,(U32)[],U32,null,(U32|null),(Account)[],U32,Account,U32,string,(ProviderInfo|null),U32,boolean,U32,Record,U32,string,(string|null),null,U32,Record,null,U32,string,null,U32,string,Qr,U32,string,(string|null),U32,(string)[],Record,U32,null,U32,null,U32,(U32)[],U32,U32,Usize,U32,string,U32,U32,string,null,U32,(U32|null),(string|null),(U32|null),(ChatListEntry)[],U32,(ChatListEntry)[],Record,U32,U32,FullChat,U32,U32,BasicChat,U32,U32,null,U32,U32,null,U32,U32,null,U32,U32,string,U32,(U32|null),[string,string],U32,U32,null,U32,U32,U32,null,U32,U32,U32,null,U32,string,string,U32,U32,U32,null,U32,U32,(U32|null),U32,U32,MuteDuration,null,U32,U32,boolean,U32,(U32)[],null,U32,U32,U32,(U32)[],U32,U32,Message,U32,(U32)[],Record,U32,(U32)[],null,U32,U32,string,U32,U32,Contact,U32,string,(string|null),U32,U32,U32,U32,U32,U32,null,U32,U32,null,U32,(Contact)[],U32,U32,(string|null),(U32)[],U32,U32,(string|null),(Contact)[],U32,(U32)[],Record,U32,U32,string,U32,(U32|null),Viewtype,(Viewtype|null),(Viewtype|null),(U32)[],null,U32,U32,U32,string,U32,U32,string,string,null,U32,U32,U32,string,U32,U32,WebxdcMessageInfo,U32,(U32)[],U32,null,U32,U32,null,U32,U32,(Message|null),U32,string,U32,U32,U32,U32,(string|null),(string|null),([F64,F64]|null),(U32|null),[U32,Message],U32,U32,(string|null),(string|null),(U32|null),null]; +export type __AllTyps=[string,boolean,Record,U32,U32,null,(U32)[],U32,null,(U32|null),(Account)[],U32,Account,U32,string,(ProviderInfo|null),U32,boolean,U32,Record,U32,string,(string|null),null,U32,Record,null,U32,string,null,U32,string,Qr,U32,string,(string|null),U32,(string)[],Record,U32,null,U32,null,U32,(U32)[],U32,U32,Usize,U32,string,U32,U32,string,null,U32,(U32|null),(string|null),(U32|null),(ChatListEntry)[],U32,(ChatListEntry)[],Record,U32,U32,FullChat,U32,U32,BasicChat,U32,U32,null,U32,U32,null,U32,U32,null,U32,U32,string,U32,(U32|null),[string,string],U32,U32,null,U32,U32,U32,null,U32,U32,U32,null,U32,string,string,U32,U32,U32,null,U32,U32,(U32|null),U32,U32,MuteDuration,null,U32,U32,boolean,U32,(U32)[],null,U32,U32,U32,(U32)[],U32,U32,Message,U32,(U32)[],Record,U32,U32,MessageNotificationData,U32,(U32)[],null,U32,U32,string,U32,U32,Contact,U32,string,(string|null),U32,U32,U32,U32,U32,U32,null,U32,U32,null,U32,(Contact)[],U32,U32,(string|null),(U32)[],U32,U32,(string|null),(Contact)[],U32,(U32)[],Record,U32,U32,string,U32,(U32|null),Viewtype,(Viewtype|null),(Viewtype|null),(U32)[],null,U32,U32,U32,string,U32,U32,string,string,null,U32,U32,U32,string,U32,U32,WebxdcMessageInfo,U32,(U32)[],U32,null,U32,U32,null,U32,U32,(Message|null),U32,string,U32,U32,U32,U32,(string|null),(string|null),([F64,F64]|null),(U32|null),[U32,Message],U32,U32,(string|null),(string|null),(U32|null),null];