Skip to content

Commit a319c1e

Browse files
authored
feat: add AccountsChanged and AccountsItemChanged events (#6118)
- **feat: add `AccountsChanged` and `AccountsItemChanged` events** - **emit event and add tests** closes #6106 TODO: - [x] test receiving synced config from second device - [x] bug: investigate how to delay the configuration event until it is actually configured - because desktop gets the event but still shows account as if it was unconfigured, maybe event is emitted before the value is written to the database? - [x] update node bindings constants
1 parent 5db574b commit a319c1e

File tree

16 files changed

+286
-4
lines changed

16 files changed

+286
-4
lines changed

deltachat-ffi/deltachat.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6413,6 +6413,25 @@ void dc_event_unref(dc_event_t* event);
64136413

64146414
#define DC_EVENT_CHATLIST_ITEM_CHANGED 2301
64156415

6416+
/**
6417+
* Inform that the list of accounts has changed (an account removed or added or (not yet implemented) the account order changes)
6418+
*
6419+
* This event is only emitted by the account manager.
6420+
*/
6421+
6422+
#define DC_EVENT_ACCOUNTS_CHANGED 2302
6423+
6424+
/**
6425+
* Inform that an account property that might be shown in the account list changed, namely:
6426+
* - is_configured (see dc_is_configured())
6427+
* - displayname
6428+
* - selfavatar
6429+
* - private_tag
6430+
*
6431+
* This event is emitted from the account whose property changed.
6432+
*/
6433+
6434+
#define DC_EVENT_ACCOUNTS_ITEM_CHANGED 2303
64166435

64176436
/**
64186437
* Inform that some events have been skipped due to event channel overflow.

deltachat-ffi/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,8 @@ pub unsafe extern "C" fn dc_event_get_id(event: *mut dc_event_t) -> libc::c_int
569569
EventType::AccountsBackgroundFetchDone => 2200,
570570
EventType::ChatlistChanged => 2300,
571571
EventType::ChatlistItemChanged { .. } => 2301,
572+
EventType::AccountsChanged => 2302,
573+
EventType::AccountsItemChanged => 2303,
572574
EventType::EventChannelOverflow { .. } => 2400,
573575
#[allow(unreachable_patterns)]
574576
#[cfg(test)]
@@ -601,8 +603,10 @@ pub unsafe extern "C" fn dc_event_get_data1_int(event: *mut dc_event_t) -> libc:
601603
| EventType::ConfigSynced { .. }
602604
| EventType::IncomingMsgBunch { .. }
603605
| EventType::ErrorSelfNotInGroup(_)
604-
| EventType::AccountsBackgroundFetchDone => 0,
605-
EventType::ChatlistChanged => 0,
606+
| EventType::AccountsBackgroundFetchDone
607+
| EventType::ChatlistChanged
608+
| EventType::AccountsChanged
609+
| EventType::AccountsItemChanged => 0,
606610
EventType::IncomingReaction { contact_id, .. }
607611
| EventType::IncomingWebxdcNotify { contact_id, .. } => contact_id.to_u32() as libc::c_int,
608612
EventType::MsgsChanged { chat_id, .. }
@@ -676,6 +680,8 @@ pub unsafe extern "C" fn dc_event_get_data2_int(event: *mut dc_event_t) -> libc:
676680
| EventType::AccountsBackgroundFetchDone
677681
| EventType::ChatlistChanged
678682
| EventType::ChatlistItemChanged { .. }
683+
| EventType::AccountsChanged
684+
| EventType::AccountsItemChanged
679685
| EventType::ConfigSynced { .. }
680686
| EventType::ChatModified(_)
681687
| EventType::WebxdcRealtimeAdvertisementReceived { .. }
@@ -751,6 +757,8 @@ pub unsafe extern "C" fn dc_event_get_data2_str(event: *mut dc_event_t) -> *mut
751757
| EventType::IncomingMsgBunch { .. }
752758
| EventType::ChatlistItemChanged { .. }
753759
| EventType::ChatlistChanged
760+
| EventType::AccountsChanged
761+
| EventType::AccountsItemChanged
754762
| EventType::WebxdcRealtimeAdvertisementReceived { .. }
755763
| EventType::EventChannelOverflow { .. } => ptr::null_mut(),
756764
EventType::ConfigureProgress { comment, .. } => {

deltachat-jsonrpc/src/api/types/events.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,20 @@ pub enum EventType {
285285
#[serde(rename_all = "camelCase")]
286286
ChatlistItemChanged { chat_id: Option<u32> },
287287

288+
/// Inform that the list of accounts has changed (an account removed or added or (not yet implemented) the account order changes)
289+
///
290+
/// This event is only emitted by the account manager
291+
AccountsChanged,
292+
293+
/// Inform that an account property that might be shown in the account list changed, namely:
294+
/// - is_configured (see is_configured())
295+
/// - displayname
296+
/// - selfavatar
297+
/// - private_tag
298+
///
299+
/// This event is emitted from the account whose property changed.
300+
AccountsItemChanged,
301+
288302
/// Inform than some events have been skipped due to event channel overflow.
289303
EventChannelOverflow { n: u64 },
290304
}
@@ -426,6 +440,8 @@ impl From<CoreEventType> for EventType {
426440
},
427441
CoreEventType::ChatlistChanged => ChatlistChanged,
428442
CoreEventType::EventChannelOverflow { n } => EventChannelOverflow { n },
443+
CoreEventType::AccountsChanged => AccountsChanged,
444+
CoreEventType::AccountsItemChanged => AccountsItemChanged,
429445
#[allow(unreachable_patterns)]
430446
#[cfg(test)]
431447
_ => unreachable!("This is just to silence a rust_analyzer false-positive"),

deltachat-rpc-client/src/deltachat_rpc_client/const.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class EventType(str, Enum):
6161
WEBXDC_INSTANCE_DELETED = "WebxdcInstanceDeleted"
6262
CHATLIST_CHANGED = "ChatlistChanged"
6363
CHATLIST_ITEM_CHANGED = "ChatlistItemChanged"
64+
ACCOUNTS_CHANGED = "AccountsChanged"
65+
ACCOUNTS_ITEM_CHANGED = "AccountsItemChanged"
6466
CONFIG_SYNCED = "ConfigSynced"
6567
WEBXDC_REALTIME_DATA = "WebxdcRealtimeData"
6668
WEBXDC_REALTIME_ADVERTISEMENT_RECEIVED = "WebxdcRealtimeAdvertisementReceived"
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
from deltachat_rpc_client import EventType
6+
7+
if TYPE_CHECKING:
8+
from deltachat_rpc_client.pytestplugin import ACFactory
9+
10+
11+
def test_event_on_configuration(acfactory: ACFactory) -> None:
12+
"""
13+
Test if ACCOUNTS_ITEM_CHANGED event is emitted on configure
14+
"""
15+
16+
account = acfactory.new_preconfigured_account()
17+
account.clear_all_events()
18+
assert not account.is_configured()
19+
future = account.configure.future()
20+
while True:
21+
event = account.wait_for_event()
22+
if event.kind == EventType.ACCOUNTS_ITEM_CHANGED:
23+
break
24+
assert account.is_configured()
25+
26+
future()
27+
28+
29+
# other tests are written in rust: src/tests/account_events.rs

node/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ module.exports = {
3131
DC_DOWNLOAD_IN_PROGRESS: 1000,
3232
DC_DOWNLOAD_UNDECIPHERABLE: 30,
3333
DC_EVENT_ACCOUNTS_BACKGROUND_FETCH_DONE: 2200,
34+
DC_EVENT_ACCOUNTS_CHANGED: 2302,
35+
DC_EVENT_ACCOUNTS_ITEM_CHANGED: 2303,
3436
DC_EVENT_CHANNEL_OVERFLOW: 2400,
3537
DC_EVENT_CHATLIST_CHANGED: 2300,
3638
DC_EVENT_CHATLIST_ITEM_CHANGED: 2301,

node/events.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,7 @@ module.exports = {
4444
2200: 'DC_EVENT_ACCOUNTS_BACKGROUND_FETCH_DONE',
4545
2300: 'DC_EVENT_CHATLIST_CHANGED',
4646
2301: 'DC_EVENT_CHATLIST_ITEM_CHANGED',
47+
2302: 'DC_EVENT_ACCOUNTS_CHANGED',
48+
2303: 'DC_EVENT_ACCOUNTS_ITEM_CHANGED',
4749
2400: 'DC_EVENT_CHANNEL_OVERFLOW'
4850
}

node/lib/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export enum C {
3131
DC_DOWNLOAD_IN_PROGRESS = 1000,
3232
DC_DOWNLOAD_UNDECIPHERABLE = 30,
3333
DC_EVENT_ACCOUNTS_BACKGROUND_FETCH_DONE = 2200,
34+
DC_EVENT_ACCOUNTS_CHANGED = 2302,
35+
DC_EVENT_ACCOUNTS_ITEM_CHANGED = 2303,
3436
DC_EVENT_CHANNEL_OVERFLOW = 2400,
3537
DC_EVENT_CHATLIST_CHANGED = 2300,
3638
DC_EVENT_CHATLIST_ITEM_CHANGED = 2301,
@@ -353,5 +355,7 @@ export const EventId2EventName: { [key: number]: string } = {
353355
2200: 'DC_EVENT_ACCOUNTS_BACKGROUND_FETCH_DONE',
354356
2300: 'DC_EVENT_CHATLIST_CHANGED',
355357
2301: 'DC_EVENT_CHATLIST_ITEM_CHANGED',
358+
2302: 'DC_EVENT_ACCOUNTS_CHANGED',
359+
2303: 'DC_EVENT_ACCOUNTS_ITEM_CHANGED',
356360
2400: 'DC_EVENT_CHANNEL_OVERFLOW',
357361
}

src/accounts.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ impl Accounts {
139139
ctx.open("".to_string()).await?;
140140

141141
self.accounts.insert(account_config.id, ctx);
142+
self.emit_event(EventType::AccountsChanged);
142143

143144
Ok(account_config.id)
144145
}
@@ -156,6 +157,7 @@ impl Accounts {
156157
.build()
157158
.await?;
158159
self.accounts.insert(account_config.id, ctx);
160+
self.emit_event(EventType::AccountsChanged);
159161

160162
Ok(account_config.id)
161163
}
@@ -190,6 +192,7 @@ impl Accounts {
190192
.context("failed to remove account data")?;
191193
}
192194
self.config.remove_account(id).await?;
195+
self.emit_event(EventType::AccountsChanged);
193196

194197
Ok(())
195198
}

src/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,12 @@ impl Context {
791791
self.sql.set_raw_config(key.as_ref(), value).await?;
792792
}
793793
}
794+
if matches!(
795+
key,
796+
Config::Displayname | Config::Selfavatar | Config::PrivateTag
797+
) {
798+
self.emit_event(EventType::AccountsItemChanged);
799+
}
794800
if key.is_synced() {
795801
self.emit_event(EventType::ConfigSynced { key });
796802
}

0 commit comments

Comments
 (0)