Skip to content

Commit

Permalink
Centralise the methods to send chatlist events
Browse files Browse the repository at this point in the history
this is the first step to debouncing
  • Loading branch information
Simon-Laux committed Mar 17, 2024
1 parent d89df9b commit 54ab9c3
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 128 deletions.
74 changes: 25 additions & 49 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use crate::tools::{
create_smeared_timestamps, get_abs_path, gm2local_offset, improve_single_line_input,
smeared_time, strip_rtlo_characters, time, IsNoneOrEmpty, SystemTime,
};
use crate::ui_events;
use crate::webxdc::WEBXDC_SUFFIX;

/// An chat item, such as a message or a marker.
Expand Down Expand Up @@ -308,10 +309,8 @@ impl ChatId {
}
};
context.emit_msgs_changed_without_ids();
context.emit_event(EventType::UIChatListChanged);
context.emit_event(EventType::UIChatListItemChanged {
chat_id: Some(chat_id),
});
ui_events::emit_chatlist_changed(context);
ui_events::emit_chatlist_item_changed(context, chat_id);
Ok(chat_id)
}

Expand Down Expand Up @@ -428,7 +427,7 @@ impl ChatId {
}
}
}
context.emit_event(EventType::UIChatListChanged);
ui_events::emit_chatlist_changed(context);

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

ui_events::emit_chatlist_changed(context);

if sync.into() {
let chat = Chat::load_from_db(context, self).await?;
// TODO: For a 1:1 chat this currently triggers `Contact::unblock()` on other devices.
Expand All @@ -460,9 +461,8 @@ impl ChatId {
.await
.log_err(context)
.ok();

context.emit_event(EventType::UIChatListChanged);
}

Ok(())
}

Expand Down Expand Up @@ -506,9 +506,7 @@ impl ChatId {

if self.set_blocked(context, Blocked::Not).await? {
context.emit_event(EventType::ChatModified(self));
context.emit_event(EventType::UIChatListItemChanged {
chat_id: Some(self),
});
ui_events::emit_chatlist_item_changed(context, self);
}

if sync.into() {
Expand Down Expand Up @@ -551,9 +549,7 @@ impl ChatId {
.await?;

context.emit_event(EventType::ChatModified(self));
context.emit_event(EventType::UIChatListItemChanged {
chat_id: Some(self),
});
ui_events::emit_chatlist_item_changed(context, self);

// make sure, the receivers will get all keys
self.reset_gossiped_timestamp(context).await?;
Expand Down Expand Up @@ -602,9 +598,7 @@ impl ChatId {
if protection_status_modified {
self.add_protection_msg(context, protect, contact_id, timestamp_sort)
.await?;
context.emit_event(EventType::UIChatListItemChanged {
chat_id: Some(self),
});
ui_events::emit_chatlist_item_changed(context, self);
}
Ok(())
}
Expand Down Expand Up @@ -691,10 +685,8 @@ impl ChatId {
.await?;

context.emit_msgs_changed_without_ids();
context.emit_event(EventType::UIChatListChanged);
context.emit_event(EventType::UIChatListItemChanged {
chat_id: Some(self),
});
ui_events::emit_chatlist_changed(context);
ui_events::emit_chatlist_item_changed(context, self);

if sync.into() {
let chat = Chat::load_from_db(context, self).await?;
Expand Down Expand Up @@ -801,7 +793,7 @@ impl ChatId {
.await?;

context.emit_msgs_changed_without_ids();
context.emit_event(EventType::UIChatListChanged);
ui_events::emit_chatlist_changed(context);

context
.set_config_internal(Config::LastHousekeeping, None)
Expand All @@ -813,7 +805,7 @@ impl ChatId {
msg.text = stock_str::self_deleted_msg_body(context).await;
add_device_msg(context, None, Some(&mut msg)).await?;
}
context.emit_event(EventType::UIChatListChanged);
ui_events::emit_chatlist_changed(context);

Ok(())
}
Expand Down Expand Up @@ -3118,13 +3110,9 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()>
.await?;
for chat_id_in_archive in chat_ids_in_archive {
context.emit_event(EventType::MsgsNoticed(chat_id_in_archive));
context.emit_event(EventType::UIChatListItemChanged {
chat_id: Some(chat_id_in_archive),
});
ui_events::emit_chatlist_item_changed(context, chat_id_in_archive);
}
context.emit_event(EventType::UIChatListItemChanged {
chat_id: Some(DC_CHAT_ID_ARCHIVED_LINK),
});
ui_events::emit_chatlist_item_changed(context, DC_CHAT_ID_ARCHIVED_LINK);
} else {
let exists = context
.sql
Expand All @@ -3151,9 +3139,7 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()>
}

context.emit_event(EventType::MsgsNoticed(chat_id));
context.emit_event(EventType::UIChatListItemChanged {
chat_id: Some(chat_id),
});
ui_events::emit_chatlist_item_changed(context, chat_id);

Ok(())
}
Expand Down Expand Up @@ -3221,7 +3207,7 @@ pub(crate) async fn mark_old_messages_as_noticed(

for c in changed_chats {
context.emit_event(EventType::MsgsNoticed(c));
context.emit_event(EventType::UIChatListItemChanged { chat_id: Some(c) });
ui_events::emit_chatlist_item_changed(context, c);
}

Ok(())
Expand Down Expand Up @@ -3384,10 +3370,8 @@ pub async fn create_group_chat(
}

context.emit_msgs_changed_without_ids();
context.emit_event(EventType::UIChatListChanged);
context.emit_event(EventType::UIChatListItemChanged {
chat_id: Some(chat_id),
});
ui_events::emit_chatlist_changed(context);
ui_events::emit_chatlist_item_changed(context, chat_id);

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

context.emit_msgs_changed_without_ids();
context.emit_event(EventType::UIChatListChanged);
ui_events::emit_chatlist_changed(context);

if sync.into() {
let id = SyncId::Grpid(grpid);
Expand Down Expand Up @@ -3753,9 +3737,7 @@ pub(crate) async fn set_muted_ex(
.await
.context(format!("Failed to set mute duration for {chat_id}"))?;
context.emit_event(EventType::ChatModified(chat_id));
context.emit_event(EventType::UIChatListItemChanged {
chat_id: Some(chat_id),
});
ui_events::emit_chatlist_item_changed(context, chat_id);
if sync.into() {
let chat = Chat::load_from_db(context, chat_id).await?;
chat.sync(context, SyncAction::SetMuted(duration))
Expand Down Expand Up @@ -3916,9 +3898,7 @@ async fn rename_ex(
sync = Nosync;
}
context.emit_event(EventType::ChatModified(chat_id));
context.emit_event(EventType::UIChatListItemChanged {
chat_id: Some(chat_id),
});
ui_events::emit_chatlist_item_changed(context, chat_id);
success = true;
}
}
Expand Down Expand Up @@ -3979,9 +3959,7 @@ pub async fn set_chat_profile_image(
context.emit_msgs_changed(chat_id, msg.id);
}
context.emit_event(EventType::ChatModified(chat_id));
context.emit_event(EventType::UIChatListItemChanged {
chat_id: Some(chat_id),
});
ui_events::emit_chatlist_item_changed(context, chat_id);
Ok(())
}

Expand Down Expand Up @@ -4129,9 +4107,7 @@ pub async fn resend_msgs(context: &Context, msg_ids: &[MsgId]) -> Result<()> {
});
msg.timestamp_sort = create_smeared_timestamp(context);
// note(treefit): only matters if it is the last message in chat (but probably to expensive to check, debounce also solves it)
context.emit_event(EventType::UIChatListItemChanged {
chat_id: Some(msg.chat_id),
});
ui_events::emit_chatlist_item_changed(context, msg.chat_id);
if !create_send_msg_jobs(context, &mut msg).await?.is_empty() {
context.scheduler.interrupt_smtp().await;
}
Expand Down
29 changes: 12 additions & 17 deletions src/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::tools::{
duration_to_str, get_abs_path, improve_single_line_input, strip_rtlo_characters, time,
EmailAddress, SystemTime,
};
use crate::{chat, stock_str};
use crate::{chat, stock_str, ui_events};

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

let mut update_addr = false;
let mut updated_name = false;

let row_id = context.sql.transaction(|transaction| {
let row = transaction.query_row(
Expand Down Expand Up @@ -761,7 +760,7 @@ impl Contact {
if count > 0 {
// Chat name updated
context.emit_event(EventType::ChatModified(chat_id));
updated_name = true;
ui_events::emit_chatlist_items_changed_for_contact(context, contact_id);
}
}
}
Expand Down Expand Up @@ -800,16 +799,6 @@ impl Contact {

let contact_id = ContactId::new(row_id);

if updated_name {
// update the chats the contact that changed their name is part of
// (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
for chat_id in Contact::get_chats_with_contact(context, &contact_id).await? {
context.emit_event(EventType::UIChatListItemChanged {
chat_id: Some(chat_id),
});
}
}

Ok((contact_id, sth_modified))
}

Expand Down Expand Up @@ -1563,7 +1552,7 @@ WHERE type=? AND id IN (
}
}

context.emit_event(EventType::UIChatListChanged);
ui_events::emit_chatlist_changed(context);
Ok(())
}

Expand Down Expand Up @@ -1614,7 +1603,7 @@ pub(crate) async fn set_profile_image(
if changed {
contact.update_param(context).await?;
context.emit_event(EventType::ContactsChanged(Some(contact_id)));
// TODO update DM chat
ui_events::emit_chatlist_item_changed_for_contacts_dm_chat(context, contact_id);
}
Ok(())
}
Expand Down Expand Up @@ -1825,7 +1814,10 @@ impl RecentlySeenLoop {
// Timeout, notify about contact.
if let Some(contact_id) = contact_id {
context.emit_event(EventType::ContactsChanged(Some(*contact_id)));
// TODO update DM chat
ui_events::emit_chatlist_item_changed_for_contacts_dm_chat(
&context,
*contact_id,
);
unseen_queue.pop();
}
}
Expand Down Expand Up @@ -1855,7 +1847,10 @@ impl RecentlySeenLoop {
// Event is already in the past.
if let Some(contact_id) = contact_id {
context.emit_event(EventType::ContactsChanged(Some(*contact_id)));
// TODO update DM chat
ui_events::emit_chatlist_item_changed_for_contacts_dm_chat(
&context,
*contact_id,
);
}
unseen_queue.pop();
}
Expand Down
13 changes: 5 additions & 8 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use crate::sql::Sql;
use crate::stock_str::StockStrings;
use crate::timesmearing::SmearedTimestamp;
use crate::tools::{self, create_id, duration_to_str, time, time_elapsed};
use crate::ui_events;

/// Builder for the [`Context`].
///
Expand Down Expand Up @@ -593,19 +594,15 @@ impl Context {
/// Emits a MsgsChanged event with specified chat and message ids
pub fn emit_msgs_changed(&self, chat_id: ChatId, msg_id: MsgId) {
self.emit_event(EventType::MsgsChanged { chat_id, msg_id });
self.emit_event(EventType::UIChatListChanged);
self.emit_event(EventType::UIChatListItemChanged {
chat_id: Some(chat_id),
});
ui_events::emit_chatlist_changed(self);
ui_events::emit_chatlist_item_changed(self, chat_id);
}

/// Emits an IncomingMsg event with specified chat and message ids
pub fn emit_incoming_msg(&self, chat_id: ChatId, msg_id: MsgId) {
self.emit_event(EventType::IncomingMsg { chat_id, msg_id });
self.emit_event(EventType::UIChatListChanged);
self.emit_event(EventType::UIChatListItemChanged {
chat_id: Some(chat_id),
});
ui_events::emit_chatlist_changed(self);
ui_events::emit_chatlist_item_changed(self, chat_id);
}

/// Returns a receiver for emitted events.
Expand Down
6 changes: 2 additions & 4 deletions src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::imap::session::Session;
use crate::message::{Message, MsgId, Viewtype};
use crate::mimeparser::{MimeMessage, Part};
use crate::tools::time;
use crate::{stock_str, EventType};
use crate::{stock_str, ui_events, EventType};

/// Download limits should not be used below `MIN_DOWNLOAD_LIMIT`.
///
Expand Down Expand Up @@ -115,9 +115,7 @@ impl MsgId {
chat_id: msg.chat_id,
msg_id: self,
});
context.emit_event(EventType::UIChatListItemChanged {
chat_id: Some(msg.chat_id),
});
ui_events::emit_chatlist_item_changed(context, msg.chat_id);
Ok(())
}
}
Expand Down
1 change: 1 addition & 0 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use async_channel::{self as channel, Receiver, Sender, TrySendError};
use pin_project::pin_project;

mod payload;
pub(crate) mod ui_events;

pub use self::payload::EventType;

Expand Down
Loading

0 comments on commit 54ab9c3

Please sign in to comment.