From ffd57772e9415f04e56eb9130545b76df10a3530 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Sun, 6 Nov 2022 18:17:48 +0100 Subject: [PATCH] Add DC_EVENT_INCOMING_MSG_BUNCH event (#3643) * Add DC_EVENT_INCOMING_MSG event * Fix lots of compile errors * Docs * Changelog * Fix python tests Adding DC_EVENT_INCOMING_MSG_BUNCH made the python tests fail because they use `get_matching("DC_EVENT_INCOMING_MSG")`, which also matches DC_EVENT_INCOMING_MSG_BUNCH, so the tests got confused. This fixes `get_matching()` to only match whole event names. * Also fix test_ac_setup_message_twice() The built regex was ^EVENT_NAME1|EVENT_NAME2$, which becomes parsed as "^EVENT_NAME1" OR "EVENT_NAME2$". Introduce a group (parentheses) to fix this. * desktop will use DC_EVENT_INCOMING_MSG_BUNCH, so I would not call it experimental anymore * add generated node constants * msg_ids in the event as Vec number[] in js land this is way more convinient than a json encoded string. * Apply suggestions from code review Co-authored-by: bjoern Co-authored-by: Simon Laux Co-authored-by: Simon Laux Co-authored-by: bjoern --- CHANGELOG.md | 5 ++++- deltachat-ffi/deltachat.h | 11 +++++++++++ deltachat-ffi/src/lib.rs | 8 ++++++++ deltachat-jsonrpc/src/api/events.rs | 13 +++++++++++++ deltachat-jsonrpc/typescript/generated/events.ts | 8 ++++++++ node/constants.js | 1 + node/events.js | 1 + node/lib/constants.ts | 2 ++ python/src/deltachat/events.py | 2 +- src/events.rs | 4 ++++ src/imap.rs | 6 ++++++ 11 files changed, 59 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f0b0cafc..6d4f9f131 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,7 +28,10 @@ - `miscSendTextMessage(accountId, text, chatId)` -> `miscSendTextMessage(accountId, chatId, text)` - jsonrpc: add `SystemMessageType` to `Message` - cffi: add missing `DC_INFO_` constants - +- Add DC_EVENT_INCOMING_MSG_BUNCH event #3643 +- Python bindings: Make get_matching() only match the + whole event name, e.g. events.get_matching("DC_EVENT_INCOMING_MSG") + won't match DC_EVENT_INCOMING_MSG_BUNCH anymore #3643 - Rust: Introduce a ContextBuilder #3698 diff --git a/deltachat-ffi/deltachat.h b/deltachat-ffi/deltachat.h index ccb084c91..5e6a1b963 100644 --- a/deltachat-ffi/deltachat.h +++ b/deltachat-ffi/deltachat.h @@ -5634,6 +5634,17 @@ void dc_event_unref(dc_event_t* event); */ #define DC_EVENT_INCOMING_MSG 2005 +/** + * Downloading a bunch of messages just finished. This is an experimental + * event to allow the UI to only show one notification per message bunch, + * instead of cluttering the user with many notifications. + * For each of the msg_ids, an additional #DC_EVENT_INCOMING_MSG event was emitted before. + * + * @param data1 0 + * @param data2 (char*) msg_ids, a json object with the message ids. + */ +#define DC_EVENT_INCOMING_MSG_BUNCH 2006 + /** * Messages were marked noticed or seen. diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 402627206..fa7b01c04 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -503,6 +503,7 @@ pub unsafe extern "C" fn dc_event_get_id(event: *mut dc_event_t) -> libc::c_int EventType::MsgsChanged { .. } => 2000, EventType::ReactionsChanged { .. } => 2001, EventType::IncomingMsg { .. } => 2005, + EventType::IncomingMsgBunch { .. } => 2006, EventType::MsgsNoticed { .. } => 2008, EventType::MsgDelivered { .. } => 2010, EventType::MsgFailed { .. } => 2012, @@ -544,6 +545,7 @@ pub unsafe extern "C" fn dc_event_get_data1_int(event: *mut dc_event_t) -> libc: | EventType::Error(_) | EventType::ConnectivityChanged | EventType::SelfavatarChanged + | EventType::IncomingMsgBunch { .. } | EventType::ErrorSelfNotInGroup(_) => 0, EventType::MsgsChanged { chat_id, .. } | EventType::ReactionsChanged { chat_id, .. } @@ -600,6 +602,7 @@ pub unsafe extern "C" fn dc_event_get_data2_int(event: *mut dc_event_t) -> libc: | EventType::MsgsNoticed(_) | EventType::ConnectivityChanged | EventType::WebxdcInstanceDeleted { .. } + | EventType::IncomingMsgBunch { .. } | EventType::SelfavatarChanged => 0, EventType::ChatModified(_) => 0, EventType::MsgsChanged { msg_id, .. } @@ -671,6 +674,11 @@ pub unsafe extern "C" fn dc_event_get_data2_str(event: *mut dc_event_t) -> *mut let data2 = file.to_c_string().unwrap_or_default(); data2.into_raw() } + EventType::IncomingMsgBunch { msg_ids } => serde_json::to_string(msg_ids) + .unwrap_or_default() + .to_c_string() + .unwrap_or_default() + .into_raw(), } } diff --git a/deltachat-jsonrpc/src/api/events.rs b/deltachat-jsonrpc/src/api/events.rs index 9e2a63b67..70dadb52e 100644 --- a/deltachat-jsonrpc/src/api/events.rs +++ b/deltachat-jsonrpc/src/api/events.rs @@ -120,6 +120,16 @@ pub enum JSONRPCEventType { msg_id: u32, }, + /// Downloading a bunch of messages just finished. This is an experimental + /// event to allow the UI to only show one notification per message bunch, + /// instead of cluttering the user with many notifications. + /// + /// msg_ids contains the message ids. + #[serde(rename_all = "camelCase")] + IncomingMsgBunch { + msg_ids: Vec, + }, + /// Messages were seen or noticed. /// chat id is always set. #[serde(rename_all = "camelCase")] @@ -305,6 +315,9 @@ impl From for JSONRPCEventType { chat_id: chat_id.to_u32(), msg_id: msg_id.to_u32(), }, + EventType::IncomingMsgBunch { msg_ids } => IncomingMsgBunch { + msg_ids: msg_ids.into_iter().map(|id| id.to_u32()).collect(), + }, EventType::MsgsNoticed(chat_id) => MsgsNoticed { chat_id: chat_id.to_u32(), }, diff --git a/deltachat-jsonrpc/typescript/generated/events.ts b/deltachat-jsonrpc/typescript/generated/events.ts index 287a9150d..39c0b60ef 100644 --- a/deltachat-jsonrpc/typescript/generated/events.ts +++ b/deltachat-jsonrpc/typescript/generated/events.ts @@ -88,6 +88,14 @@ export type Event=(({ * There is no extra #DC_EVENT_MSGS_CHANGED event send together with this event. */ "type":"IncomingMsg";}&{"chatId":U32;"msgId":U32;})|({ +/** + * Downloading a bunch of messages just finished. This is an experimental + * event to allow the UI to only show one notification per message bunch, + * instead of cluttering the user with many notifications. + * + * msg_ids contains the message ids. + */ +"type":"IncomingMsgBunch";}&{"msgIds":(U32)[];})|({ /** * Messages were seen or noticed. * chat id is always set. diff --git a/node/constants.js b/node/constants.js index 5b6c401a9..6991e3af0 100644 --- a/node/constants.js +++ b/node/constants.js @@ -42,6 +42,7 @@ module.exports = { DC_EVENT_IMEX_FILE_WRITTEN: 2052, DC_EVENT_IMEX_PROGRESS: 2051, DC_EVENT_INCOMING_MSG: 2005, + DC_EVENT_INCOMING_MSG_BUNCH: 2006, DC_EVENT_INFO: 100, DC_EVENT_LOCATION_CHANGED: 2035, DC_EVENT_MSGS_CHANGED: 2000, diff --git a/node/events.js b/node/events.js index dba37e240..5130473bc 100644 --- a/node/events.js +++ b/node/events.js @@ -16,6 +16,7 @@ module.exports = { 2000: 'DC_EVENT_MSGS_CHANGED', 2001: 'DC_EVENT_REACTIONS_CHANGED', 2005: 'DC_EVENT_INCOMING_MSG', + 2006: 'DC_EVENT_INCOMING_MSG_BUNCH', 2008: 'DC_EVENT_MSGS_NOTICED', 2010: 'DC_EVENT_MSG_DELIVERED', 2012: 'DC_EVENT_MSG_FAILED', diff --git a/node/lib/constants.ts b/node/lib/constants.ts index 594d54626..cce01ea27 100644 --- a/node/lib/constants.ts +++ b/node/lib/constants.ts @@ -42,6 +42,7 @@ export enum C { DC_EVENT_IMEX_FILE_WRITTEN = 2052, DC_EVENT_IMEX_PROGRESS = 2051, DC_EVENT_INCOMING_MSG = 2005, + DC_EVENT_INCOMING_MSG_BUNCH = 2006, DC_EVENT_INFO = 100, DC_EVENT_LOCATION_CHANGED = 2035, DC_EVENT_MSGS_CHANGED = 2000, @@ -296,6 +297,7 @@ export const EventId2EventName: { [key: number]: string } = { 2000: 'DC_EVENT_MSGS_CHANGED', 2001: 'DC_EVENT_REACTIONS_CHANGED', 2005: 'DC_EVENT_INCOMING_MSG', + 2006: 'DC_EVENT_INCOMING_MSG_BUNCH', 2008: 'DC_EVENT_MSGS_NOTICED', 2010: 'DC_EVENT_MSG_DELIVERED', 2012: 'DC_EVENT_MSG_FAILED', diff --git a/python/src/deltachat/events.py b/python/src/deltachat/events.py index ace7a8b88..ccb9f877f 100644 --- a/python/src/deltachat/events.py +++ b/python/src/deltachat/events.py @@ -105,7 +105,7 @@ class FFIEventTracker: yield self.get(timeout=timeout, check_error=check_error) def get_matching(self, event_name_regex, check_error=True, timeout=None): - rex = re.compile("(?:{}).*".format(event_name_regex)) + rex = re.compile("^(?:{})$".format(event_name_regex)) for ev in self.iter_events(timeout=timeout, check_error=check_error): if rex.match(ev.name): return ev diff --git a/src/events.rs b/src/events.rs index 7ea3c67a3..d0033c4ae 100644 --- a/src/events.rs +++ b/src/events.rs @@ -189,6 +189,10 @@ pub enum EventType { msg_id: MsgId, }, + IncomingMsgBunch { + msg_ids: Vec, + }, + /// Messages were seen or noticed. /// chat id is always set. MsgsNoticed(ChatId), diff --git a/src/imap.rs b/src/imap.rs index b732bfbd9..ba1fb96e3 100644 --- a/src/imap.rs +++ b/src/imap.rs @@ -902,6 +902,12 @@ impl Imap { info!(context, "{} mails read from \"{}\".", read_cnt, folder); + let msg_ids = received_msgs + .iter() + .flat_map(|m| m.msg_ids.clone()) + .collect(); + context.emit_event(EventType::IncomingMsgBunch { msg_ids }); + chat::mark_old_messages_as_noticed(context, received_msgs).await?; Ok(read_cnt > 0)