Skip to content

Commit ec27e8c

Browse files
committed
fix: Update group name on self-additions and missing messages (#6412)
Updating the group name on self-additions and missing messages was forgotten. The only thing is that we don't add a system message (`MsgGrpNameChangedBy`) in these cases because we don't want to blame anyone.
1 parent 7f7c76f commit ec27e8c

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

src/receive_imf.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,8 +2137,8 @@ async fn apply_group_changes(
21372137
.filter(|t| *t <= mime_parser.timestamp_sent)
21382138
.is_some();
21392139
// Whether to rebuild the member list from scratch.
2140-
let recreate_member_list = {
2141-
// Always recreate membership list if SELF has been added. The older versions of DC
2140+
let reset_chat = {
2141+
// Always recreate member list etc. if SELF has been added. The older versions of DC
21422142
// don't always set "In-Reply-To" to the latest message they sent, but to the latest
21432143
// delivered message (so it's a race), so we have this heuristic here.
21442144
self_added
@@ -2222,9 +2222,15 @@ async fn apply_group_changes(
22222222
info!(context, "Ignoring addition of {added_addr:?} to {chat_id}.");
22232223
}
22242224
better_msg.get_or_insert_with(Default::default);
2225-
} else if let Some(old_name) = mime_parser
2225+
}
2226+
2227+
if let Some(old_name) = mime_parser
22262228
.get_header(HeaderDef::ChatGroupNameChanged)
22272229
.map(|s| s.trim())
2230+
.or(match reset_chat {
2231+
true => Some(chat.name.as_str()),
2232+
false => None,
2233+
})
22282234
{
22292235
if let Some(grpname) = mime_parser
22302236
.get_header(HeaderDef::ChatGroupName)
@@ -2248,10 +2254,17 @@ async fn apply_group_changes(
22482254
.await?;
22492255
send_event_chat_modified = true;
22502256
}
2251-
2252-
better_msg = Some(stock_str::msg_grp_name(context, old_name, grpname, from_id).await);
2257+
if mime_parser
2258+
.get_header(HeaderDef::ChatGroupNameChanged)
2259+
.is_some()
2260+
{
2261+
better_msg.get_or_insert(
2262+
stock_str::msg_grp_name(context, old_name, grpname, from_id).await,
2263+
);
2264+
}
22532265
}
2254-
} else if let Some(value) = mime_parser.get_header(HeaderDef::ChatContent) {
2266+
}
2267+
if let (Some(value), None) = (mime_parser.get_header(HeaderDef::ChatContent), &better_msg) {
22552268
if value == "group-avatar-changed" {
22562269
if let Some(avatar_action) = &mime_parser.group_avatar {
22572270
// this is just an explicit message containing the group-avatar,
@@ -2280,7 +2293,7 @@ async fn apply_group_changes(
22802293
let mut added_ids = HashSet::<ContactId>::new();
22812294
let mut removed_ids = HashSet::<ContactId>::new();
22822295

2283-
if !recreate_member_list {
2296+
if !reset_chat {
22842297
if sync_member_list {
22852298
added_ids = new_members.difference(&chat_contacts).copied().collect();
22862299
} else if let Some(added_id) = added_id {
@@ -2303,7 +2316,7 @@ async fn apply_group_changes(
23032316
if let Some(removed_id) = removed_id {
23042317
new_members.remove(&removed_id);
23052318
}
2306-
if recreate_member_list {
2319+
if reset_chat {
23072320
if self_added {
23082321
// ... then `better_msg` is already set.
23092322
} else if chat.blocked == Blocked::Request || !chat_contacts.contains(&ContactId::SELF)
@@ -2364,7 +2377,7 @@ async fn apply_group_changes(
23642377
}
23652378
if sync_member_list {
23662379
let mut ts = mime_parser.timestamp_sent;
2367-
if recreate_member_list {
2380+
if reset_chat {
23682381
// Reject all older membership changes. See `allow_member_list_changes` to know how
23692382
// this works.
23702383
ts += timestamp_sent_tolerance;

src/receive_imf/tests.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4216,7 +4216,7 @@ async fn test_dont_recreate_contacts_on_add_remove() -> Result<()> {
42164216
}
42174217

42184218
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
4219-
async fn test_recreate_contact_list_on_missing_messages() -> Result<()> {
4219+
async fn test_reset_chat_on_missing_messages() -> Result<()> {
42204220
let alice = TestContext::new_alice().await;
42214221
let bob = TestContext::new_bob().await;
42224222
let chat_id = create_group_chat(&alice, ProtectionStatus::Unprotected, "Group").await?;
@@ -4236,6 +4236,10 @@ async fn test_recreate_contact_list_on_missing_messages() -> Result<()> {
42364236
let bob_chat_id = bob.recv_msg(&alice.pop_sent_msg().await).await.chat_id;
42374237
bob_chat_id.accept(&bob).await?;
42384238

4239+
// Bob changes the group name.
4240+
chat::set_chat_name(&bob, bob_chat_id, "Renamed").await?;
4241+
bob.pop_sent_msg().await;
4242+
42394243
// bob removes a member
42404244
let bob_contact_fiona = Contact::create(&bob, "fiona", "[email protected]").await?;
42414245
remove_contact_from_chat(&bob, bob_chat_id, bob_contact_fiona).await?;
@@ -4251,6 +4255,8 @@ async fn test_recreate_contact_list_on_missing_messages() -> Result<()> {
42514255

42524256
// alice only receives the second member addition
42534257
alice.recv_msg(&add_msg).await;
4258+
let chat = Chat::load_from_db(&alice, chat_id).await?;
4259+
assert_eq!(chat.get_name(), "Renamed");
42544260

42554261
// since we missed messages, a new contact list should be build
42564262
assert_eq!(get_chat_contacts(&alice, chat_id).await?.len(), 4);

test-data/golden/receive_imf_recreate_contact_list_on_missing_messages

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Group#Chat#10: Group [5 member(s)]
1+
Group#Chat#10: Renamed [5 member(s)]
22
--------------------------------------------------------------------------------
33
Msg#10: Me (Contact#Contact#Self): populate √
44
Msg#11: info (Contact#Contact#Info): Member [email protected] added. [NOTICED][INFO]

0 commit comments

Comments
 (0)