mirror of
https://github.com/chatmail/core.git
synced 2026-05-02 04:46:29 +03:00
Refactor hiding of expired messages
Now there is only one function: hide_device_expired_messages(). If any messages are hidden event DC_EVENT_MSGS_CHANGED(0,0) is emitted now, which is more correct than DC_EVENT_CHAT_MODIFIED and also triggers chatlist reload.
This commit is contained in:
88
src/chat.rs
88
src/chat.rs
@@ -378,38 +378,6 @@ impl ChatId {
|
|||||||
Ok(self.get_param(context)?.exists(Param::Devicetalk))
|
Ok(self.get_param(context)?.exists(Param::Devicetalk))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Hides or deletes messages which are expired according to
|
|
||||||
/// "delete_device_after" setting.
|
|
||||||
///
|
|
||||||
/// Returns true if any message is hidden, so event can be emitted. If
|
|
||||||
/// nothing has been hidden, returns false.
|
|
||||||
pub fn delete_device_expired_messages(self, context: &Context) -> Result<bool, Error> {
|
|
||||||
if self.is_special() || self.is_self_talk(context)? || self.is_device_talk(context)? {
|
|
||||||
return Ok(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(delete_device_after) = context.get_config_delete_device_after() {
|
|
||||||
let threshold_timestamp = time() - delete_device_after;
|
|
||||||
|
|
||||||
// Hide expired messages
|
|
||||||
//
|
|
||||||
// Only update the rows that have to be updated, to avoid emitting
|
|
||||||
// unnecessary "chat modified" events.
|
|
||||||
let rows_modified = context.sql.execute(
|
|
||||||
"UPDATE msgs \
|
|
||||||
SET txt = 'DELETED', hidden = 1 \
|
|
||||||
WHERE timestamp < ? \
|
|
||||||
AND chat_id == ? \
|
|
||||||
AND NOT hidden",
|
|
||||||
params![threshold_timestamp, self],
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(rows_modified > 0)
|
|
||||||
} else {
|
|
||||||
Ok(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Bad evil escape hatch.
|
/// Bad evil escape hatch.
|
||||||
///
|
///
|
||||||
/// Avoid using this, eventually types should be cleaned up enough
|
/// Avoid using this, eventually types should be cleaned up enough
|
||||||
@@ -1498,11 +1466,14 @@ pub fn get_chat_msgs(
|
|||||||
flags: u32,
|
flags: u32,
|
||||||
marker1before: Option<MsgId>,
|
marker1before: Option<MsgId>,
|
||||||
) -> Vec<MsgId> {
|
) -> Vec<MsgId> {
|
||||||
match chat_id.delete_device_expired_messages(context) {
|
match hide_device_expired_messages(context) {
|
||||||
Err(err) => warn!(context, "Failed to delete expired messages: {}", err),
|
Err(err) => warn!(context, "Failed to delete expired messages: {}", err),
|
||||||
Ok(messages_deleted) => {
|
Ok(messages_deleted) => {
|
||||||
if messages_deleted {
|
if messages_deleted {
|
||||||
context.call_cb(Event::ChatModified(chat_id));
|
context.call_cb(Event::MsgsChanged {
|
||||||
|
msg_id: MsgId::new(0),
|
||||||
|
chat_id: ChatId::new(0),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1641,26 +1612,41 @@ pub fn marknoticed_all_chats(context: &Context) -> Result<(), Error> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete_device_expired_messages_all_chats(context: &Context) -> Result<(), Error> {
|
/// Hides messages which are expired according to "delete_device_after" setting.
|
||||||
let chat_ids = context.sql.query_map(
|
///
|
||||||
"SELECT id FROM chats WHERE id > 9",
|
/// Returns true if any message is hidden, so event can be emitted. If nothing
|
||||||
params![],
|
/// has been hidden, returns false.
|
||||||
|row| row.get::<_, ChatId>(0),
|
pub fn hide_device_expired_messages(context: &Context) -> Result<bool, Error> {
|
||||||
|ids| {
|
if let Some(delete_device_after) = context.get_config_delete_device_after() {
|
||||||
let mut ret = Vec::new();
|
let threshold_timestamp = time() - delete_device_after;
|
||||||
for id in ids {
|
|
||||||
if let Ok(chat_id) = id {
|
let self_chat_id = lookup_by_contact_id(context, DC_CONTACT_ID_SELF)?.0;
|
||||||
ret.push(chat_id)
|
let device_chat_id = lookup_by_contact_id(context, DC_CONTACT_ID_DEVICE)?.0;
|
||||||
}
|
|
||||||
}
|
// Hide expired messages
|
||||||
Ok(ret)
|
//
|
||||||
},
|
// Only update the rows that have to be updated, to avoid emitting
|
||||||
|
// unnecessary "chat modified" events.
|
||||||
|
let rows_modified = context.sql.execute(
|
||||||
|
"UPDATE msgs \
|
||||||
|
SET txt = 'DELETED', hidden = 1 \
|
||||||
|
WHERE timestamp < ? \
|
||||||
|
AND chat_id > ? \
|
||||||
|
AND chat_id != ? \
|
||||||
|
AND chat_id != ? \
|
||||||
|
AND NOT hidden",
|
||||||
|
params![
|
||||||
|
threshold_timestamp,
|
||||||
|
DC_CHAT_ID_LAST_SPECIAL,
|
||||||
|
self_chat_id,
|
||||||
|
device_chat_id
|
||||||
|
],
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
for chat_id in chat_ids {
|
Ok(rows_modified > 0)
|
||||||
chat_id.delete_device_expired_messages(context)?;
|
} else {
|
||||||
|
Ok(false)
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_chat_media(
|
pub fn get_chat_media(
|
||||||
|
|||||||
@@ -92,7 +92,9 @@ impl Chatlist {
|
|||||||
query: Option<&str>,
|
query: Option<&str>,
|
||||||
query_contact_id: Option<u32>,
|
query_contact_id: Option<u32>,
|
||||||
) -> Result<Self> {
|
) -> Result<Self> {
|
||||||
delete_device_expired_messages_all_chats(context)?;
|
// Note that we do not emit DC_EVENT_MSGS_MODIFIED here even if some
|
||||||
|
// messages get hidden to avoid reloading the same chatlist.
|
||||||
|
hide_device_expired_messages(context)?;
|
||||||
|
|
||||||
let mut add_archived_link_item = false;
|
let mut add_archived_link_item = false;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user