Skip to content

Commit 95776cc

Browse files
committed
fix: Add Chat-Group-Name-Timestamp header and use it to update group names (#6412)
Add "Chat-Group-Name-Timestamp" message header and use the last-write-wins logic when updating group names (similar to group member timestamps). Note that if the "Chat-Group-Name-Changed" header is absent though, we don't add a system message (`MsgGrpNameChangedBy`) because we don't want to blame anyone.
1 parent 8ffdd55 commit 95776cc

File tree

6 files changed

+133
-14
lines changed

6 files changed

+133
-14
lines changed

src/headerdef.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub enum HeaderDef {
5757
ChatGroupId,
5858
ChatGroupName,
5959
ChatGroupNameChanged,
60+
ChatGroupNameTimestamp,
6061
ChatVerified,
6162
ChatGroupAvatar,
6263
ChatUserAvatar,

src/mimefactory.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ impl MimeFactory {
11491149
let Loaded::Message { chat, msg } = &self.loaded else {
11501150
bail!("Attempt to render MDN as a message");
11511151
};
1152-
let chat = chat.clone();
1152+
let mut chat = chat.clone();
11531153
let msg = msg.clone();
11541154
let command = msg.param.get_cmd();
11551155
let mut placeholdertext = None;
@@ -1181,6 +1181,23 @@ impl MimeFactory {
11811181
"Chat-Group-Name",
11821182
mail_builder::headers::text::Text::new(chat.name.to_string()).into(),
11831183
));
1184+
if chat.param.get_i64(Param::GroupNameTimestamp).is_none()
1185+
|| command == SystemMessage::GroupNameChanged
1186+
{
1187+
chat.param
1188+
.set_i64(Param::GroupNameTimestamp, self.timestamp);
1189+
chat.update_param(context).await?;
1190+
}
1191+
headers.push((
1192+
"Chat-Group-Name-Timestamp",
1193+
mail_builder::headers::text::Text::new(
1194+
chat.param
1195+
.get_i64(Param::GroupNameTimestamp)
1196+
.unwrap_or(0)
1197+
.to_string(),
1198+
)
1199+
.into(),
1200+
));
11841201

11851202
match command {
11861203
SystemMessage::MemberRemovedFromGroup => {

src/mimeparser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ impl MimeMessage {
445445
HeaderDef::ChatGroupId,
446446
HeaderDef::ChatGroupName,
447447
HeaderDef::ChatGroupNameChanged,
448+
HeaderDef::ChatGroupNameTimestamp,
448449
HeaderDef::ChatGroupAvatar,
449450
HeaderDef::ChatGroupMemberRemoved,
450451
HeaderDef::ChatGroupMemberAdded,

src/receive_imf.rs

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,14 +2104,16 @@ async fn create_group(
21042104
// serialization/deserialization #3650" issue. DC itself never creates group names with
21052105
// leading/trailing whitespace.
21062106
.trim();
2107+
let mut param = Params::new();
2108+
param.set_i64(Param::GroupNameTimestamp, mime_parser.timestamp_sent);
21072109
let new_chat_id = ChatId::create_multiuser_record(
21082110
context,
21092111
Chattype::Group,
21102112
grpid,
21112113
grpname,
21122114
create_blocked,
21132115
create_protected,
2114-
None,
2116+
Some(param.to_string()),
21152117
mime_parser.timestamp_sent,
21162118
)
21172119
.await
@@ -2332,9 +2334,18 @@ async fn apply_group_changes(
23322334
}
23332335

23342336
better_msg = Some(stock_str::msg_add_member_local(context, added_addr, from_id).await);
2335-
} else if let Some(old_name) = mime_parser
2337+
}
2338+
2339+
let group_name_timestamp = mime_parser
2340+
.get_header(HeaderDef::ChatGroupNameTimestamp)
2341+
.and_then(|s| s.parse::<i64>().ok());
2342+
if let Some(old_name) = mime_parser
23362343
.get_header(HeaderDef::ChatGroupNameChanged)
23372344
.map(|s| s.trim())
2345+
.or(match group_name_timestamp {
2346+
Some(_) => Some(chat.name.as_str()),
2347+
None => None,
2348+
})
23382349
{
23392350
if let Some(grpname) = mime_parser
23402351
.get_header(HeaderDef::ChatGroupName)
@@ -2343,13 +2354,17 @@ async fn apply_group_changes(
23432354
{
23442355
let grpname = &sanitize_single_line(grpname);
23452356
let old_name = &sanitize_single_line(old_name);
2346-
if chat_id
2347-
.update_timestamp(
2348-
context,
2349-
Param::GroupNameTimestamp,
2350-
mime_parser.timestamp_sent,
2351-
)
2352-
.await?
2357+
2358+
let chat_group_name_timestamp = chat.param.get_i64(Param::GroupNameTimestamp);
2359+
let group_name_timestamp = group_name_timestamp.unwrap_or(mime_parser.timestamp_sent);
2360+
// NB: For old chats (created before "Chat-Group-Name-Timestamp" header introduction)
2361+
// this may cause an extra name update and `ChatModified` event, but that's fine.
2362+
//
2363+
// To provide group name consistency, compare names if timestamps are equal.
2364+
if (chat_group_name_timestamp, grpname) < (Some(group_name_timestamp), old_name)
2365+
&& chat_id
2366+
.update_timestamp(context, Param::GroupNameTimestamp, group_name_timestamp)
2367+
.await?
23532368
{
23542369
info!(context, "Updating grpname for chat {chat_id}.");
23552370
context
@@ -2358,10 +2373,18 @@ async fn apply_group_changes(
23582373
.await?;
23592374
send_event_chat_modified = true;
23602375
}
2361-
2362-
better_msg = Some(stock_str::msg_grp_name(context, old_name, grpname, from_id).await);
2376+
if mime_parser
2377+
.get_header(HeaderDef::ChatGroupNameChanged)
2378+
.is_some()
2379+
{
2380+
better_msg.get_or_insert(
2381+
stock_str::msg_grp_name(context, old_name, grpname, from_id).await,
2382+
);
2383+
}
23632384
}
2364-
} else if let Some(value) = mime_parser.get_header(HeaderDef::ChatContent) {
2385+
}
2386+
2387+
if let (Some(value), None) = (mime_parser.get_header(HeaderDef::ChatContent), &better_msg) {
23652388
if value == "group-avatar-changed" {
23662389
if let Some(avatar_action) = &mime_parser.group_avatar {
23672390
// this is just an explicit message containing the group-avatar,
@@ -2837,14 +2860,16 @@ async fn create_adhoc_group(
28372860
return Ok(None);
28382861
}
28392862

2863+
let mut param = Params::new();
2864+
param.set_i64(Param::GroupNameTimestamp, mime_parser.timestamp_sent);
28402865
let new_chat_id: ChatId = ChatId::create_multiuser_record(
28412866
context,
28422867
Chattype::Group,
28432868
"", // Ad hoc groups have no ID.
28442869
grpname,
28452870
create_blocked,
28462871
ProtectionStatus::Unprotected,
2847-
None,
2872+
Some(param.to_string()),
28482873
mime_parser.timestamp_sent,
28492874
)
28502875
.await?;

src/receive_imf/receive_imf_tests.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5425,6 +5425,43 @@ Hello!"
54255425
Ok(())
54265426
}
54275427

5428+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
5429+
async fn test_rename_chat_on_missing_message() -> Result<()> {
5430+
let alice = TestContext::new_alice().await;
5431+
let bob = TestContext::new_bob().await;
5432+
let chat_id = create_group_chat(&alice, ProtectionStatus::Unprotected, "Group").await?;
5433+
add_to_chat_contacts_table(
5434+
&alice,
5435+
time(),
5436+
chat_id,
5437+
&[Contact::create(&alice, "bob", "[email protected]").await?],
5438+
)
5439+
.await?;
5440+
send_text_msg(&alice, chat_id, "populate".to_string()).await?;
5441+
let bob_chat_id = bob.recv_msg(&alice.pop_sent_msg().await).await.chat_id;
5442+
bob_chat_id.accept(&bob).await?;
5443+
5444+
// Bob changes the group name.
5445+
// TODO: If Bob does this too fast, it's not guaranteed that his group name wins because "Date"
5446+
// of his message may be less than one of Alice's message, and setting "Group-Name-Timestamp" to
5447+
// a value greater than "Date" doesn't look as a good solution. This should be fixed by
5448+
// adjusting "Date" on the replier side (limited to a few seconds).
5449+
SystemTime::shift(Duration::from_secs(3600));
5450+
chat::set_chat_name(&bob, bob_chat_id, "Renamed").await?;
5451+
bob.pop_sent_msg().await;
5452+
5453+
// Bob adds a new member.
5454+
let bob_orange = Contact::create(&bob, "orange", "[email protected]").await?;
5455+
add_contact_to_chat(&bob, bob_chat_id, bob_orange).await?;
5456+
let add_msg = bob.pop_sent_msg().await;
5457+
5458+
// Alice only receives the member addition.
5459+
alice.recv_msg(&add_msg).await;
5460+
let chat = Chat::load_from_db(&alice, chat_id).await?;
5461+
assert_eq!(chat.get_name(), "Renamed");
5462+
Ok(())
5463+
}
5464+
54285465
/// Tests that creating a group
54295466
/// is preferred over assigning message to existing
54305467
/// chat based on `In-Reply-To` and `References`.

src/update_helper.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,44 @@ mod tests {
213213
// Assert that the \n was correctly removed from the group name also in the system message
214214
assert_eq!(msg.text.contains('\n'), false);
215215

216+
// This doesn't update the name because Date is the same and name is greater.
217+
receive_imf(
218+
&t,
219+
b"From: Bob Authname <[email protected]>\n\
220+
221+
Message-ID: <[email protected]>\n\
222+
Chat-Version: 1.0\n\
223+
Chat-Group-ID: abcde123456\n\
224+
Chat-Group-Name: another name update 4\n\
225+
Chat-Group-Name-Changed: another name update\n\
226+
Date: Sun, 22 Mar 2021 03:00:00 +0000\n\
227+
\n\
228+
4th message\n",
229+
false,
230+
)
231+
.await?;
232+
let chat = Chat::load_from_db(&t, chat.id).await?;
233+
assert_eq!(chat.name, "another name update");
234+
235+
// This updates the name because Date is the same and name is lower.
236+
receive_imf(
237+
&t,
238+
b"From: Bob Authname <[email protected]>\n\
239+
240+
Message-ID: <[email protected]>\n\
241+
Chat-Version: 1.0\n\
242+
Chat-Group-ID: abcde123456\n\
243+
Chat-Group-Name: another name updat\n\
244+
Chat-Group-Name-Changed: another name update\n\
245+
Date: Sun, 22 Mar 2021 03:00:00 +0000\n\
246+
\n\
247+
5th message\n",
248+
false,
249+
)
250+
.await?;
251+
let chat = Chat::load_from_db(&t, chat.id).await?;
252+
assert_eq!(chat.name, "another name updat");
253+
216254
Ok(())
217255
}
218256
}

0 commit comments

Comments
 (0)