Skip to content

Commit 54ab9c3

Browse files
committed
Centralise the methods to send chatlist events
this is the first step to debouncing
1 parent d89df9b commit 54ab9c3

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
@@ -45,6 +45,7 @@ use crate::tools::{
4545
create_smeared_timestamps, get_abs_path, gm2local_offset, improve_single_line_input,
4646
smeared_time, strip_rtlo_characters, time, IsNoneOrEmpty, SystemTime,
4747
};
48+
use crate::ui_events;
4849
use crate::webxdc::WEBXDC_SUFFIX;
4950

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

@@ -428,7 +427,7 @@ impl ChatId {
428427
}
429428
}
430429
}
431-
context.emit_event(EventType::UIChatListChanged);
430+
ui_events::emit_chatlist_changed(context);
432431

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

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

@@ -506,9 +506,7 @@ impl ChatId {
506506

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

514512
if sync.into() {
@@ -551,9 +549,7 @@ impl ChatId {
551549
.await?;
552550

553551
context.emit_event(EventType::ChatModified(self));
554-
context.emit_event(EventType::UIChatListItemChanged {
555-
chat_id: Some(self),
556-
});
552+
ui_events::emit_chatlist_item_changed(context, self);
557553

558554
// make sure, the receivers will get all keys
559555
self.reset_gossiped_timestamp(context).await?;
@@ -602,9 +598,7 @@ impl ChatId {
602598
if protection_status_modified {
603599
self.add_protection_msg(context, protect, contact_id, timestamp_sort)
604600
.await?;
605-
context.emit_event(EventType::UIChatListItemChanged {
606-
chat_id: Some(self),
607-
});
601+
ui_events::emit_chatlist_item_changed(context, self);
608602
}
609603
Ok(())
610604
}
@@ -691,10 +685,8 @@ impl ChatId {
691685
.await?;
692686

693687
context.emit_msgs_changed_without_ids();
694-
context.emit_event(EventType::UIChatListChanged);
695-
context.emit_event(EventType::UIChatListItemChanged {
696-
chat_id: Some(self),
697-
});
688+
ui_events::emit_chatlist_changed(context);
689+
ui_events::emit_chatlist_item_changed(context, self);
698690

699691
if sync.into() {
700692
let chat = Chat::load_from_db(context, self).await?;
@@ -801,7 +793,7 @@ impl ChatId {
801793
.await?;
802794

803795
context.emit_msgs_changed_without_ids();
804-
context.emit_event(EventType::UIChatListChanged);
796+
ui_events::emit_chatlist_changed(context);
805797

806798
context
807799
.set_config_internal(Config::LastHousekeeping, None)
@@ -813,7 +805,7 @@ impl ChatId {
813805
msg.text = stock_str::self_deleted_msg_body(context).await;
814806
add_device_msg(context, None, Some(&mut msg)).await?;
815807
}
816-
context.emit_event(EventType::UIChatListChanged);
808+
ui_events::emit_chatlist_changed(context);
817809

818810
Ok(())
819811
}
@@ -3118,13 +3110,9 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()>
31183110
.await?;
31193111
for chat_id_in_archive in chat_ids_in_archive {
31203112
context.emit_event(EventType::MsgsNoticed(chat_id_in_archive));
3121-
context.emit_event(EventType::UIChatListItemChanged {
3122-
chat_id: Some(chat_id_in_archive),
3123-
});
3113+
ui_events::emit_chatlist_item_changed(context, chat_id_in_archive);
31243114
}
3125-
context.emit_event(EventType::UIChatListItemChanged {
3126-
chat_id: Some(DC_CHAT_ID_ARCHIVED_LINK),
3127-
});
3115+
ui_events::emit_chatlist_item_changed(context, DC_CHAT_ID_ARCHIVED_LINK);
31283116
} else {
31293117
let exists = context
31303118
.sql
@@ -3151,9 +3139,7 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()>
31513139
}
31523140

31533141
context.emit_event(EventType::MsgsNoticed(chat_id));
3154-
context.emit_event(EventType::UIChatListItemChanged {
3155-
chat_id: Some(chat_id),
3156-
});
3142+
ui_events::emit_chatlist_item_changed(context, chat_id);
31573143

31583144
Ok(())
31593145
}
@@ -3221,7 +3207,7 @@ pub(crate) async fn mark_old_messages_as_noticed(
32213207

32223208
for c in changed_chats {
32233209
context.emit_event(EventType::MsgsNoticed(c));
3224-
context.emit_event(EventType::UIChatListItemChanged { chat_id: Some(c) });
3210+
ui_events::emit_chatlist_item_changed(context, c);
32253211
}
32263212

32273213
Ok(())
@@ -3384,10 +3370,8 @@ pub async fn create_group_chat(
33843370
}
33853371

33863372
context.emit_msgs_changed_without_ids();
3387-
context.emit_event(EventType::UIChatListChanged);
3388-
context.emit_event(EventType::UIChatListItemChanged {
3389-
chat_id: Some(chat_id),
3390-
});
3373+
ui_events::emit_chatlist_changed(context);
3374+
ui_events::emit_chatlist_item_changed(context, chat_id);
33913375

33923376
if protect == ProtectionStatus::Protected {
33933377
chat_id
@@ -3475,7 +3459,7 @@ pub(crate) async fn create_broadcast_list_ex(
34753459
let chat_id = ChatId::new(u32::try_from(row_id)?);
34763460

34773461
context.emit_msgs_changed_without_ids();
3478-
context.emit_event(EventType::UIChatListChanged);
3462+
ui_events::emit_chatlist_changed(context);
34793463

34803464
if sync.into() {
34813465
let id = SyncId::Grpid(grpid);
@@ -3753,9 +3737,7 @@ pub(crate) async fn set_muted_ex(
37533737
.await
37543738
.context(format!("Failed to set mute duration for {chat_id}"))?;
37553739
context.emit_event(EventType::ChatModified(chat_id));
3756-
context.emit_event(EventType::UIChatListItemChanged {
3757-
chat_id: Some(chat_id),
3758-
});
3740+
ui_events::emit_chatlist_item_changed(context, chat_id);
37593741
if sync.into() {
37603742
let chat = Chat::load_from_db(context, chat_id).await?;
37613743
chat.sync(context, SyncAction::SetMuted(duration))
@@ -3916,9 +3898,7 @@ async fn rename_ex(
39163898
sync = Nosync;
39173899
}
39183900
context.emit_event(EventType::ChatModified(chat_id));
3919-
context.emit_event(EventType::UIChatListItemChanged {
3920-
chat_id: Some(chat_id),
3921-
});
3901+
ui_events::emit_chatlist_item_changed(context, chat_id);
39223902
success = true;
39233903
}
39243904
}
@@ -3979,9 +3959,7 @@ pub async fn set_chat_profile_image(
39793959
context.emit_msgs_changed(chat_id, msg.id);
39803960
}
39813961
context.emit_event(EventType::ChatModified(chat_id));
3982-
context.emit_event(EventType::UIChatListItemChanged {
3983-
chat_id: Some(chat_id),
3984-
});
3962+
ui_events::emit_chatlist_item_changed(context, chat_id);
39853963
Ok(())
39863964
}
39873965

@@ -4129,9 +4107,7 @@ pub async fn resend_msgs(context: &Context, msg_ids: &[MsgId]) -> Result<()> {
41294107
});
41304108
msg.timestamp_sort = create_smeared_timestamp(context);
41314109
// note(treefit): only matters if it is the last message in chat (but probably to expensive to check, debounce also solves it)
4132-
context.emit_event(EventType::UIChatListItemChanged {
4133-
chat_id: Some(msg.chat_id),
4134-
});
4110+
ui_events::emit_chatlist_item_changed(context, msg.chat_id);
41354111
if !create_send_msg_jobs(context, &mut msg).await?.is_empty() {
41364112
context.scheduler.interrupt_smtp().await;
41374113
}

src/contact.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use crate::tools::{
3737
duration_to_str, get_abs_path, improve_single_line_input, strip_rtlo_characters, time,
3838
EmailAddress, SystemTime,
3939
};
40-
use crate::{chat, stock_str};
40+
use crate::{chat, stock_str, ui_events};
4141

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

659659
let mut update_addr = false;
660-
let mut updated_name = false;
661660

662661
let row_id = context.sql.transaction(|transaction| {
663662
let row = transaction.query_row(
@@ -761,7 +760,7 @@ impl Contact {
761760
if count > 0 {
762761
// Chat name updated
763762
context.emit_event(EventType::ChatModified(chat_id));
764-
updated_name = true;
763+
ui_events::emit_chatlist_items_changed_for_contact(context, contact_id);
765764
}
766765
}
767766
}
@@ -800,16 +799,6 @@ impl Contact {
800799

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

803-
if updated_name {
804-
// update the chats the contact that changed their name is part of
805-
// (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
806-
for chat_id in Contact::get_chats_with_contact(context, &contact_id).await? {
807-
context.emit_event(EventType::UIChatListItemChanged {
808-
chat_id: Some(chat_id),
809-
});
810-
}
811-
}
812-
813802
Ok((contact_id, sth_modified))
814803
}
815804

@@ -1563,7 +1552,7 @@ WHERE type=? AND id IN (
15631552
}
15641553
}
15651554

1566-
context.emit_event(EventType::UIChatListChanged);
1555+
ui_events::emit_chatlist_changed(context);
15671556
Ok(())
15681557
}
15691558

@@ -1614,7 +1603,7 @@ pub(crate) async fn set_profile_image(
16141603
if changed {
16151604
contact.update_param(context).await?;
16161605
context.emit_event(EventType::ContactsChanged(Some(contact_id)));
1617-
// TODO update DM chat
1606+
ui_events::emit_chatlist_item_changed_for_contacts_dm_chat(context, contact_id);
16181607
}
16191608
Ok(())
16201609
}
@@ -1825,7 +1814,10 @@ impl RecentlySeenLoop {
18251814
// Timeout, notify about contact.
18261815
if let Some(contact_id) = contact_id {
18271816
context.emit_event(EventType::ContactsChanged(Some(*contact_id)));
1828-
// TODO update DM chat
1817+
ui_events::emit_chatlist_item_changed_for_contacts_dm_chat(
1818+
&context,
1819+
*contact_id,
1820+
);
18291821
unseen_queue.pop();
18301822
}
18311823
}
@@ -1855,7 +1847,10 @@ impl RecentlySeenLoop {
18551847
// Event is already in the past.
18561848
if let Some(contact_id) = contact_id {
18571849
context.emit_event(EventType::ContactsChanged(Some(*contact_id)));
1858-
// TODO update DM chat
1850+
ui_events::emit_chatlist_item_changed_for_contacts_dm_chat(
1851+
&context,
1852+
*contact_id,
1853+
);
18591854
}
18601855
unseen_queue.pop();
18611856
}

src/context.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use crate::sql::Sql;
3737
use crate::stock_str::StockStrings;
3838
use crate::timesmearing::SmearedTimestamp;
3939
use crate::tools::{self, create_id, duration_to_str, time, time_elapsed};
40+
use crate::ui_events;
4041

4142
/// Builder for the [`Context`].
4243
///
@@ -593,19 +594,15 @@ impl Context {
593594
/// Emits a MsgsChanged event with specified chat and message ids
594595
pub fn emit_msgs_changed(&self, chat_id: ChatId, msg_id: MsgId) {
595596
self.emit_event(EventType::MsgsChanged { chat_id, msg_id });
596-
self.emit_event(EventType::UIChatListChanged);
597-
self.emit_event(EventType::UIChatListItemChanged {
598-
chat_id: Some(chat_id),
599-
});
597+
ui_events::emit_chatlist_changed(self);
598+
ui_events::emit_chatlist_item_changed(self, chat_id);
600599
}
601600

602601
/// Emits an IncomingMsg event with specified chat and message ids
603602
pub fn emit_incoming_msg(&self, chat_id: ChatId, msg_id: MsgId) {
604603
self.emit_event(EventType::IncomingMsg { chat_id, msg_id });
605-
self.emit_event(EventType::UIChatListChanged);
606-
self.emit_event(EventType::UIChatListItemChanged {
607-
chat_id: Some(chat_id),
608-
});
604+
ui_events::emit_chatlist_changed(self);
605+
ui_events::emit_chatlist_item_changed(self, chat_id);
609606
}
610607

611608
/// 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::session::Session;
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)