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 Jan 11, 2024
1 parent 7483aa5 commit 2c37341
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 @@ -46,6 +46,7 @@ use crate::tools::{
create_smeared_timestamps, get_abs_path, gm2local_offset, improve_single_line_input,
smeared_time, strip_rtlo_characters, time, IsNoneOrEmpty,
};
use crate::ui_events;
use crate::webxdc::WEBXDC_SUFFIX;

/// An chat item, such as a message or a marker.
Expand Down Expand Up @@ -309,10 +310,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 @@ -429,7 +428,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 @@ -452,6 +451,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 @@ -461,9 +462,8 @@ impl ChatId {
.await
.log_err(context)
.ok();

context.emit_event(EventType::UIChatListChanged);
}

Ok(())
}

Expand Down Expand Up @@ -507,9 +507,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 @@ -552,9 +550,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 @@ -603,9 +599,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 @@ -692,10 +686,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 @@ -802,7 +794,7 @@ impl ChatId {
.await?;

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

context.set_config(Config::LastHousekeeping, None).await?;
context.scheduler.interrupt_inbox().await;
Expand All @@ -812,7 +804,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 @@ -3102,13 +3094,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 @@ -3135,9 +3123,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 @@ -3205,7 +3191,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 @@ -3368,10 +3354,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 @@ -3459,7 +3443,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 @@ -3737,9 +3721,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 @@ -3900,9 +3882,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 @@ -3963,9 +3943,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 @@ -4112,9 +4090,7 @@ pub async fn resend_msgs(context: &Context, msg_ids: &[MsgId]) -> Result<()> {
msg_id: msg.id,
});
// 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 @@ -38,7 +38,7 @@ use crate::tools::{
duration_to_str, get_abs_path, improve_single_line_input, strip_rtlo_characters, time,
EmailAddress,
};
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 @@ -658,7 +658,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 @@ -762,7 +761,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 @@ -801,16 +800,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 @@ -1564,7 +1553,7 @@ WHERE type=? AND id IN (
}
}

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

Expand Down Expand Up @@ -1613,7 +1602,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 @@ -1820,7 +1809,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 @@ -1850,7 +1842,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 @@ -28,6 +28,7 @@ use crate::sql::Sql;
use crate::stock_str::StockStrings;
use crate::timesmearing::SmearedTimestamp;
use crate::tools::{duration_to_str, time};
use crate::ui_events;

/// Builder for the [`Context`].
///
Expand Down Expand Up @@ -485,19 +486,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::{Imap, ImapActionResult};
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 2c37341

Please sign in to comment.