diff --git a/src/chat.rs b/src/chat.rs index 29de1fc71..e0f39dcea 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -445,6 +445,7 @@ impl ChatId { cmd, dc_create_smeared_timestamp(context).await, None, + None, ) .await?; } @@ -3384,7 +3385,9 @@ pub(crate) async fn add_info_msg_with_cmd( chat_id: ChatId, text: &str, cmd: SystemMessage, - timestamp: i64, + timestamp_sort: i64, + // Timestamp to show to the user (if this is None, `timestamp_sort` will be shown to the user) + timestamp_sent_rcvd: Option, parent: Option<&Message>, ) -> Result { let rfc724_mid = dc_create_outgoing_rfc724_mid(None, "@device"); @@ -3397,12 +3400,15 @@ pub(crate) async fn add_info_msg_with_cmd( let row_id = context.sql.insert( - "INSERT INTO msgs (chat_id,from_id,to_id,timestamp,type,state,txt,rfc724_mid,ephemeral_timer, param,mime_in_reply_to) VALUES (?,?,?, ?,?,?, ?,?,?, ?,?);", + "INSERT INTO msgs (chat_id,from_id,to_id,timestamp,timestamp_sent,timestamp_rcvd,type,state,txt,rfc724_mid,ephemeral_timer, param,mime_in_reply_to) + VALUES (?,?,?, ?,?,?,?,?, ?,?,?, ?,?);", paramsv![ chat_id, ContactId::INFO, ContactId::INFO, - timestamp, + timestamp_sort, + timestamp_sent_rcvd.unwrap_or(0), + timestamp_sent_rcvd.unwrap_or(0), Viewtype::Text, MessageState::InNoticed, text, @@ -3432,6 +3438,7 @@ pub(crate) async fn add_info_msg( SystemMessage::Unknown, timestamp, None, + None, ) .await } @@ -4434,6 +4441,7 @@ mod tests { SystemMessage::EphemeralTimerChanged, 10000, None, + None, ) .await?; diff --git a/src/chatlist.rs b/src/chatlist.rs index c6ee3f838..4366662cb 100644 --- a/src/chatlist.rs +++ b/src/chatlist.rs @@ -355,6 +355,10 @@ impl Chatlist { pub fn get_index_for_id(&self, id: ChatId) -> Option { self.ids.iter().position(|(chat_id, _)| chat_id == &id) } + + pub fn iter(&self) -> impl Iterator)> { + self.ids.iter() + } } /// Returns the number of archived chats diff --git a/src/peerstate.rs b/src/peerstate.rs index fdeed903b..e2852e938 100644 --- a/src/peerstate.rs +++ b/src/peerstate.rs @@ -9,6 +9,8 @@ use crate::chatlist::Chatlist; use crate::context::Context; use crate::events::EventType; use crate::key::{DcKey, Fingerprint, SignedPublicKey}; +use crate::message::Message; +use crate::mimeparser::SystemMessage; use crate::sql::Sql; use crate::stock_str; use anyhow::{bail, Result}; @@ -275,10 +277,31 @@ impl Peerstate { .await .unwrap(); let msg = stock_str::contact_setup_changed(context, self.addr.clone()).await; - for chat_index in 0..chats.len() { - let chat_id = chats.get_chat_id(chat_index).unwrap(); - chat::add_info_msg(context, chat_id, &msg, timestamp).await?; - context.emit_event(EventType::ChatModified(chat_id)); + for (chat_id, msg_id) in chats.iter() { + let timestamp_sort = if let Some(msg_id) = msg_id { + let lastmsg = Message::load_from_db(context, *msg_id).await?; + lastmsg.timestamp_sort + } else { + context + .sql + .query_get_value( + "SELECT created_timestamp FROM chats WHERE id=?;", + paramsv![chat_id], + ) + .await? + .unwrap_or(0) + }; + chat::add_info_msg_with_cmd( + context, + *chat_id, + &msg, + SystemMessage::Unknown, + timestamp_sort, + Some(timestamp), + None, + ) + .await?; + context.emit_event(EventType::ChatModified(*chat_id)); } } else { bail!("contact with peerstate.addr {:?} not found", &self.addr); diff --git a/src/webxdc.rs b/src/webxdc.rs index d8e77126b..05dca9f28 100644 --- a/src/webxdc.rs +++ b/src/webxdc.rs @@ -219,6 +219,7 @@ impl Context { info.as_str(), SystemMessage::Unknown, timestamp, + None, Some(instance), ) .await?;