Skip to content

Commit bdfd970

Browse files
committed
Centralise the methods to send chatlist events
this is the first step to debouncing
1 parent 16d8dd4 commit bdfd970

File tree

13 files changed

+127
-128
lines changed

13 files changed

+127
-128
lines changed

src/chat.rs

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use crate::tools::{
4646
create_smeared_timestamps, get_abs_path, gm2local_offset, improve_single_line_input,
4747
smeared_time, strip_rtlo_characters, time, IsNoneOrEmpty,
4848
};
49+
use crate::ui_events;
4950
use crate::webxdc::WEBXDC_SUFFIX;
5051

5152
/// An chat item, such as a message or a marker.
@@ -309,10 +310,8 @@ impl ChatId {
309310
}
310311
};
311312
context.emit_msgs_changed_without_ids();
312-
context.emit_event(EventType::UIChatListChanged);
313-
context.emit_event(EventType::UIChatListItemChanged {
314-
chat_id: Some(chat_id),
315-
});
313+
ui_events::emit_chatlist_changed(context);
314+
ui_events::emit_chatlist_item_changed(context, chat_id);
316315
Ok(chat_id)
317316
}
318317

@@ -429,7 +428,7 @@ impl ChatId {
429428
}
430429
}
431430
}
432-
context.emit_event(EventType::UIChatListChanged);
431+
ui_events::emit_chatlist_changed(context);
433432

434433
if sync.into() {
435434
// NB: For a 1:1 chat this currently triggers `Contact::block()` on other devices.
@@ -452,6 +451,8 @@ impl ChatId {
452451
pub(crate) async fn unblock_ex(self, context: &Context, sync: sync::Sync) -> Result<()> {
453452
self.set_blocked(context, Blocked::Not).await?;
454453

454+
ui_events::emit_chatlist_changed(context);
455+
455456
if sync.into() {
456457
let chat = Chat::load_from_db(context, self).await?;
457458
// TODO: For a 1:1 chat this currently triggers `Contact::unblock()` on other devices.
@@ -461,9 +462,8 @@ impl ChatId {
461462
.await
462463
.log_err(context)
463464
.ok();
464-
465-
context.emit_event(EventType::UIChatListChanged);
466465
}
466+
467467
Ok(())
468468
}
469469

@@ -507,9 +507,7 @@ impl ChatId {
507507

508508
if self.set_blocked(context, Blocked::Not).await? {
509509
context.emit_event(EventType::ChatModified(self));
510-
context.emit_event(EventType::UIChatListItemChanged {
511-
chat_id: Some(self),
512-
});
510+
ui_events::emit_chatlist_item_changed(context, self);
513511
}
514512

515513
if sync.into() {
@@ -552,9 +550,7 @@ impl ChatId {
552550
.await?;
553551

554552
context.emit_event(EventType::ChatModified(self));
555-
context.emit_event(EventType::UIChatListItemChanged {
556-
chat_id: Some(self),
557-
});
553+
ui_events::emit_chatlist_item_changed(context, self);
558554

559555
// make sure, the receivers will get all keys
560556
self.reset_gossiped_timestamp(context).await?;
@@ -603,9 +599,7 @@ impl ChatId {
603599
if protection_status_modified {
604600
self.add_protection_msg(context, protect, contact_id, timestamp_sort)
605601
.await?;
606-
context.emit_event(EventType::UIChatListItemChanged {
607-
chat_id: Some(self),
608-
});
602+
ui_events::emit_chatlist_item_changed(context, self);
609603
}
610604
Ok(())
611605
}
@@ -692,10 +686,8 @@ impl ChatId {
692686
.await?;
693687

694688
context.emit_msgs_changed_without_ids();
695-
context.emit_event(EventType::UIChatListChanged);
696-
context.emit_event(EventType::UIChatListItemChanged {
697-
chat_id: Some(self),
698-
});
689+
ui_events::emit_chatlist_changed(context);
690+
ui_events::emit_chatlist_item_changed(context, self);
699691

700692
if sync.into() {
701693
let chat = Chat::load_from_db(context, self).await?;
@@ -802,7 +794,7 @@ impl ChatId {
802794
.await?;
803795

804796
context.emit_msgs_changed_without_ids();
805-
context.emit_event(EventType::UIChatListChanged);
797+
ui_events::emit_chatlist_changed(context);
806798

807799
context.set_config(Config::LastHousekeeping, None).await?;
808800
context.scheduler.interrupt_inbox().await;
@@ -812,7 +804,7 @@ impl ChatId {
812804
msg.text = stock_str::self_deleted_msg_body(context).await;
813805
add_device_msg(context, None, Some(&mut msg)).await?;
814806
}
815-
context.emit_event(EventType::UIChatListChanged);
807+
ui_events::emit_chatlist_changed(context);
816808

817809
Ok(())
818810
}
@@ -3115,13 +3107,9 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()>
31153107
.await?;
31163108
for chat_id_in_archive in chat_ids_in_archive {
31173109
context.emit_event(EventType::MsgsNoticed(chat_id_in_archive));
3118-
context.emit_event(EventType::UIChatListItemChanged {
3119-
chat_id: Some(chat_id_in_archive),
3120-
});
3110+
ui_events::emit_chatlist_item_changed(context, chat_id_in_archive);
31213111
}
3122-
context.emit_event(EventType::UIChatListItemChanged {
3123-
chat_id: Some(DC_CHAT_ID_ARCHIVED_LINK),
3124-
});
3112+
ui_events::emit_chatlist_item_changed(context, DC_CHAT_ID_ARCHIVED_LINK);
31253113
} else {
31263114
let exists = context
31273115
.sql
@@ -3148,9 +3136,7 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()>
31483136
}
31493137

31503138
context.emit_event(EventType::MsgsNoticed(chat_id));
3151-
context.emit_event(EventType::UIChatListItemChanged {
3152-
chat_id: Some(chat_id),
3153-
});
3139+
ui_events::emit_chatlist_item_changed(context, chat_id);
31543140

31553141
Ok(())
31563142
}
@@ -3218,7 +3204,7 @@ pub(crate) async fn mark_old_messages_as_noticed(
32183204

32193205
for c in changed_chats {
32203206
context.emit_event(EventType::MsgsNoticed(c));
3221-
context.emit_event(EventType::UIChatListItemChanged { chat_id: Some(c) });
3207+
ui_events::emit_chatlist_item_changed(context, c);
32223208
}
32233209

32243210
Ok(())
@@ -3381,10 +3367,8 @@ pub async fn create_group_chat(
33813367
}
33823368

33833369
context.emit_msgs_changed_without_ids();
3384-
context.emit_event(EventType::UIChatListChanged);
3385-
context.emit_event(EventType::UIChatListItemChanged {
3386-
chat_id: Some(chat_id),
3387-
});
3370+
ui_events::emit_chatlist_changed(context);
3371+
ui_events::emit_chatlist_item_changed(context, chat_id);
33883372

33893373
if protect == ProtectionStatus::Protected {
33903374
chat_id
@@ -3472,7 +3456,7 @@ pub(crate) async fn create_broadcast_list_ex(
34723456
let chat_id = ChatId::new(u32::try_from(row_id)?);
34733457

34743458
context.emit_msgs_changed_without_ids();
3475-
context.emit_event(EventType::UIChatListChanged);
3459+
ui_events::emit_chatlist_changed(context);
34763460

34773461
if sync.into() {
34783462
let id = SyncId::Grpid(grpid);
@@ -3750,9 +3734,7 @@ pub(crate) async fn set_muted_ex(
37503734
.await
37513735
.context(format!("Failed to set mute duration for {chat_id}"))?;
37523736
context.emit_event(EventType::ChatModified(chat_id));
3753-
context.emit_event(EventType::UIChatListItemChanged {
3754-
chat_id: Some(chat_id),
3755-
});
3737+
ui_events::emit_chatlist_item_changed(context, chat_id);
37563738
if sync.into() {
37573739
let chat = Chat::load_from_db(context, chat_id).await?;
37583740
chat.sync(context, SyncAction::SetMuted(duration))
@@ -3913,9 +3895,7 @@ async fn rename_ex(
39133895
sync = Nosync;
39143896
}
39153897
context.emit_event(EventType::ChatModified(chat_id));
3916-
context.emit_event(EventType::UIChatListItemChanged {
3917-
chat_id: Some(chat_id),
3918-
});
3898+
ui_events::emit_chatlist_item_changed(context, chat_id);
39193899
success = true;
39203900
}
39213901
}
@@ -3976,9 +3956,7 @@ pub async fn set_chat_profile_image(
39763956
context.emit_msgs_changed(chat_id, msg.id);
39773957
}
39783958
context.emit_event(EventType::ChatModified(chat_id));
3979-
context.emit_event(EventType::UIChatListItemChanged {
3980-
chat_id: Some(chat_id),
3981-
});
3959+
ui_events::emit_chatlist_item_changed(context, chat_id);
39823960
Ok(())
39833961
}
39843962

@@ -4125,9 +4103,7 @@ pub async fn resend_msgs(context: &Context, msg_ids: &[MsgId]) -> Result<()> {
41254103
msg_id: msg.id,
41264104
});
41274105
// note(treefit): only matters if it is the last message in chat (but probably to expensive to check, debounce also solves it)
4128-
context.emit_event(EventType::UIChatListItemChanged {
4129-
chat_id: Some(msg.chat_id),
4130-
});
4106+
ui_events::emit_chatlist_item_changed(context, msg.chat_id);
41314107
if !create_send_msg_jobs(context, &mut msg).await?.is_empty() {
41324108
context.scheduler.interrupt_smtp().await;
41334109
}

src/contact.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::tools::{
3838
duration_to_str, get_abs_path, improve_single_line_input, strip_rtlo_characters, time,
3939
EmailAddress,
4040
};
41-
use crate::{chat, stock_str};
41+
use crate::{chat, stock_str, ui_events};
4242

4343
/// Time during which a contact is considered as seen recently.
4444
const SEEN_RECENTLY_SECONDS: i64 = 600;
@@ -658,7 +658,6 @@ impl Contact {
658658
);
659659

660660
let mut update_addr = false;
661-
let mut updated_name = false;
662661

663662
let row_id = context.sql.transaction(|transaction| {
664663
let row = transaction.query_row(
@@ -762,7 +761,7 @@ impl Contact {
762761
if count > 0 {
763762
// Chat name updated
764763
context.emit_event(EventType::ChatModified(chat_id));
765-
updated_name = true;
764+
ui_events::emit_chatlist_items_changed_for_contact(context, contact_id);
766765
}
767766
}
768767
}
@@ -801,16 +800,6 @@ impl Contact {
801800

802801
let contact_id = ContactId::new(row_id);
803802

804-
if updated_name {
805-
// update the chats the contact that changed their name is part of
806-
// (treefit): could make sense to only update chats where the last message is from the contact, but the db query for that is more expensive
807-
for chat_id in Contact::get_chats_with_contact(context, &contact_id).await? {
808-
context.emit_event(EventType::UIChatListItemChanged {
809-
chat_id: Some(chat_id),
810-
});
811-
}
812-
}
813-
814803
Ok((contact_id, sth_modified))
815804
}
816805

@@ -1564,7 +1553,7 @@ WHERE type=? AND id IN (
15641553
}
15651554
}
15661555

1567-
context.emit_event(EventType::UIChatListChanged);
1556+
ui_events::emit_chatlist_changed(context);
15681557
Ok(())
15691558
}
15701559

@@ -1613,7 +1602,7 @@ pub(crate) async fn set_profile_image(
16131602
if changed {
16141603
contact.update_param(context).await?;
16151604
context.emit_event(EventType::ContactsChanged(Some(contact_id)));
1616-
// TODO update DM chat
1605+
ui_events::emit_chatlist_item_changed_for_contacts_dm_chat(context, contact_id);
16171606
}
16181607
Ok(())
16191608
}
@@ -1820,7 +1809,10 @@ impl RecentlySeenLoop {
18201809
// Timeout, notify about contact.
18211810
if let Some(contact_id) = contact_id {
18221811
context.emit_event(EventType::ContactsChanged(Some(*contact_id)));
1823-
// TODO update DM chat
1812+
ui_events::emit_chatlist_item_changed_for_contacts_dm_chat(
1813+
&context,
1814+
*contact_id,
1815+
);
18241816
unseen_queue.pop();
18251817
}
18261818
}
@@ -1850,7 +1842,10 @@ impl RecentlySeenLoop {
18501842
// Event is already in the past.
18511843
if let Some(contact_id) = contact_id {
18521844
context.emit_event(EventType::ContactsChanged(Some(*contact_id)));
1853-
// TODO update DM chat
1845+
ui_events::emit_chatlist_item_changed_for_contacts_dm_chat(
1846+
&context,
1847+
*contact_id,
1848+
);
18541849
}
18551850
unseen_queue.pop();
18561851
}

src/context.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::sql::Sql;
2828
use crate::stock_str::StockStrings;
2929
use crate::timesmearing::SmearedTimestamp;
3030
use crate::tools::{duration_to_str, time};
31+
use crate::ui_events;
3132

3233
/// Builder for the [`Context`].
3334
///
@@ -485,19 +486,15 @@ impl Context {
485486
/// Emits a MsgsChanged event with specified chat and message ids
486487
pub fn emit_msgs_changed(&self, chat_id: ChatId, msg_id: MsgId) {
487488
self.emit_event(EventType::MsgsChanged { chat_id, msg_id });
488-
self.emit_event(EventType::UIChatListChanged);
489-
self.emit_event(EventType::UIChatListItemChanged {
490-
chat_id: Some(chat_id),
491-
});
489+
ui_events::emit_chatlist_changed(self);
490+
ui_events::emit_chatlist_item_changed(self, chat_id);
492491
}
493492

494493
/// Emits an IncomingMsg event with specified chat and message ids
495494
pub fn emit_incoming_msg(&self, chat_id: ChatId, msg_id: MsgId) {
496495
self.emit_event(EventType::IncomingMsg { chat_id, msg_id });
497-
self.emit_event(EventType::UIChatListChanged);
498-
self.emit_event(EventType::UIChatListItemChanged {
499-
chat_id: Some(chat_id),
500-
});
496+
ui_events::emit_chatlist_changed(self);
497+
ui_events::emit_chatlist_item_changed(self, chat_id);
501498
}
502499

503500
/// Returns a receiver for emitted events.

src/download.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::imap::{Imap, ImapActionResult};
1313
use crate::message::{Message, MsgId, Viewtype};
1414
use crate::mimeparser::{MimeMessage, Part};
1515
use crate::tools::time;
16-
use crate::{stock_str, EventType};
16+
use crate::{stock_str, ui_events, EventType};
1717

1818
/// Download limits should not be used below `MIN_DOWNLOAD_LIMIT`.
1919
///
@@ -115,9 +115,7 @@ impl MsgId {
115115
chat_id: msg.chat_id,
116116
msg_id: self,
117117
});
118-
context.emit_event(EventType::UIChatListItemChanged {
119-
chat_id: Some(msg.chat_id),
120-
});
118+
ui_events::emit_chatlist_item_changed(context, msg.chat_id);
121119
Ok(())
122120
}
123121
}

src/events.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use async_channel::{self as channel, Receiver, Sender, TrySendError};
44
use pin_project::pin_project;
55

66
mod payload;
7+
pub(crate) mod ui_events;
78

89
pub use self::payload::EventType;
910

0 commit comments

Comments
 (0)