Skip to content

Commit b501af0

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 b501af0

File tree

7 files changed

+119
-11
lines changed

7 files changed

+119
-11
lines changed

src/chat.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3031,6 +3031,11 @@ pub(crate) async fn create_send_msg_jobs(context: &Context, msg: &mut Message) -
30313031
msg.state = MessageState::OutDelivered;
30323032
return Ok(Vec::new());
30333033
}
3034+
if msg.param.get_cmd() == SystemMessage::GroupNameChanged {
3035+
msg.chat_id
3036+
.update_timestamp(context, Param::GroupNameTimestamp, msg.timestamp_sort)
3037+
.await?;
3038+
}
30343039

30353040
let rendered_msg = match mimefactory.render(context).await {
30363041
Ok(res) => Ok(res),

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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,12 @@ impl MimeFactory {
11811181
"Chat-Group-Name",
11821182
mail_builder::headers::text::Text::new(chat.name.to_string()).into(),
11831183
));
1184+
if let Some(ts) = chat.param.get_i64(Param::GroupNameTimestamp) {
1185+
headers.push((
1186+
"Chat-Group-Name-Timestamp",
1187+
mail_builder::headers::text::Text::new(ts.to_string()).into(),
1188+
));
1189+
}
11841190

11851191
match command {
11861192
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: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,9 +2332,19 @@ async fn apply_group_changes(
23322332
}
23332333

23342334
better_msg = Some(stock_str::msg_add_member_local(context, added_addr, from_id).await);
2335-
} else if let Some(old_name) = mime_parser
2335+
}
2336+
2337+
let group_name_timestamp = mime_parser
2338+
.get_header(HeaderDef::ChatGroupNameTimestamp)
2339+
.and_then(|s| s.parse::<i64>().ok());
2340+
if let Some(old_name) = mime_parser
23362341
.get_header(HeaderDef::ChatGroupNameChanged)
23372342
.map(|s| s.trim())
2343+
.or(match group_name_timestamp {
2344+
Some(0) => None,
2345+
Some(_) => Some(chat.name.as_str()),
2346+
None => None,
2347+
})
23382348
{
23392349
if let Some(grpname) = mime_parser
23402350
.get_header(HeaderDef::ChatGroupName)
@@ -2343,13 +2353,15 @@ async fn apply_group_changes(
23432353
{
23442354
let grpname = &sanitize_single_line(grpname);
23452355
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?
2356+
2357+
let chat_group_name_timestamp =
2358+
chat.param.get_i64(Param::GroupNameTimestamp).unwrap_or(0);
2359+
let group_name_timestamp = group_name_timestamp.unwrap_or(mime_parser.timestamp_sent);
2360+
// To provide group name consistency, compare names if timestamps are equal.
2361+
if (chat_group_name_timestamp, grpname) < (group_name_timestamp, old_name)
2362+
&& chat_id
2363+
.update_timestamp(context, Param::GroupNameTimestamp, group_name_timestamp)
2364+
.await?
23532365
{
23542366
info!(context, "Updating grpname for chat {chat_id}.");
23552367
context
@@ -2358,10 +2370,18 @@ async fn apply_group_changes(
23582370
.await?;
23592371
send_event_chat_modified = true;
23602372
}
2361-
2362-
better_msg = Some(stock_str::msg_grp_name(context, old_name, grpname, from_id).await);
2373+
if mime_parser
2374+
.get_header(HeaderDef::ChatGroupNameChanged)
2375+
.is_some()
2376+
{
2377+
better_msg.get_or_insert(
2378+
stock_str::msg_grp_name(context, old_name, grpname, from_id).await,
2379+
);
2380+
}
23632381
}
2364-
} else if let Some(value) = mime_parser.get_header(HeaderDef::ChatContent) {
2382+
}
2383+
2384+
if let (Some(value), None) = (mime_parser.get_header(HeaderDef::ChatContent), &better_msg) {
23652385
if value == "group-avatar-changed" {
23662386
if let Some(avatar_action) = &mime_parser.group_avatar {
23672387
// this is just an explicit message containing the group-avatar,

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", "bob@example.net").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", "orange@example.net").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 <bob@example.org>\n\
220+
To: alice@example.org\n\
221+
Message-ID: <msg4@example.org>\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 <bob@example.org>\n\
239+
To: alice@example.org\n\
240+
Message-ID: <msg5@example.org>\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)