Skip to content

Commit 1c282c0

Browse files
committed
feat: Don't reset key-contact status if Chat-User-Avatar header is absent (#7002)
This prepares for sending self-status only together with self-avatar in encrypted messages. The idea is that self-status normally doesn't change frequently, so it's not a problem to re-send the whole profile. Self-status is rather a biography, it even goes to "NOTE:" in vCards, so it's not a contact status at a particular moment like "online" or "busy", and to see it one should go to the contact profile. Don't check for "Chat-Version" header though. So if a non- Delta Chat key-contact removes footer, its "status" remains, but this shouldn't be a problem. For unencrypted messages self-status will still be always attached except MDNs, reactions and SecureJoin messages, so that it's visible as the message footer in other MUAs.
1 parent 0e30dd8 commit 1c282c0

File tree

7 files changed

+82
-25
lines changed

7 files changed

+82
-25
lines changed

src/chat.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3719,12 +3719,12 @@ pub(crate) async fn add_contact_to_chat_ex(
37193719
Ok(true)
37203720
}
37213721

3722-
/// Returns true if an avatar should be attached in the given chat.
3722+
/// Returns whether profile data should be attached when sending to the given chat.
37233723
///
3724-
/// This function does not check if the avatar is set.
3724+
/// This function does not check if the avatar/status is set.
37253725
/// If avatar is not set and this function returns `true`,
37263726
/// a `Chat-User-Avatar: 0` header should be sent to reset the avatar.
3727-
pub(crate) async fn shall_attach_selfavatar(context: &Context, chat_id: ChatId) -> Result<bool> {
3727+
pub(crate) async fn should_attach_profile(context: &Context, chat_id: ChatId) -> Result<bool> {
37283728
let timestamp_some_days_ago = time() - DC_RESEND_USER_AVATAR_DAYS * 24 * 60 * 60;
37293729
let needs_attach = context
37303730
.sql
@@ -3739,8 +3739,8 @@ pub(crate) async fn shall_attach_selfavatar(context: &Context, chat_id: ChatId)
37393739
let mut needs_attach = false;
37403740
for row in rows {
37413741
let row = row?;
3742-
let selfavatar_sent = row?;
3743-
if selfavatar_sent < timestamp_some_days_ago {
3742+
let profile_sent = row?;
3743+
if profile_sent < timestamp_some_days_ago {
37443744
needs_attach = true;
37453745
}
37463746
}

src/chat/chat_tests.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,23 +1531,23 @@ async fn test_create_same_chat_twice() {
15311531
}
15321532

15331533
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1534-
async fn test_shall_attach_selfavatar() -> Result<()> {
1534+
async fn test_should_attach_profile() -> Result<()> {
15351535
let mut tcm = TestContextManager::new();
15361536
let alice = &tcm.alice().await;
15371537
let bob = &tcm.bob().await;
15381538

15391539
let chat_id = create_group(alice, "foo").await?;
1540-
assert!(!shall_attach_selfavatar(alice, chat_id).await?);
1540+
assert!(!should_attach_profile(alice, chat_id).await?);
15411541

15421542
let contact_id = alice.add_or_lookup_contact_id(bob).await;
15431543
add_contact_to_chat(alice, chat_id, contact_id).await?;
1544-
assert!(shall_attach_selfavatar(alice, chat_id).await?);
1544+
assert!(should_attach_profile(alice, chat_id).await?);
15451545

15461546
chat_id.set_selfavatar_timestamp(alice, time()).await?;
1547-
assert!(!shall_attach_selfavatar(alice, chat_id).await?);
1547+
assert!(!should_attach_profile(alice, chat_id).await?);
15481548

15491549
alice.set_config(Config::Selfavatar, None).await?; // setting to None also forces re-sending
1550-
assert!(shall_attach_selfavatar(alice, chat_id).await?);
1550+
assert!(should_attach_profile(alice, chat_id).await?);
15511551
Ok(())
15521552
}
15531553

@@ -1571,7 +1571,7 @@ async fn test_profile_data_on_group_leave() -> Result<()> {
15711571
tokio::fs::write(&file, bytes).await?;
15721572
t.set_config(Config::Selfavatar, Some(file.to_str().unwrap()))
15731573
.await?;
1574-
assert!(shall_attach_selfavatar(t, chat_id).await?);
1574+
assert!(should_attach_profile(t, chat_id).await?);
15751575

15761576
remove_contact_from_chat(t, chat_id, ContactId::SELF).await?;
15771577
let sent_msg = t.pop_sent_msg().await;

src/config.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,16 @@ impl Context {
758758
let better_value;
759759

760760
match key {
761+
Config::Selfstatus => {
762+
// Currently we send the self-status in every appropriate message, but in the future
763+
// (when most users upgrade to "feat: Don't reset key-contact status if
764+
// Chat-User-Avatar header is absent") we want to send it periodically together with
765+
// the self-avatar. This ensures the correct behavior after a possible Core upgrade.
766+
self.sql
767+
.execute("UPDATE contacts SET selfavatar_sent=0", ())
768+
.await?;
769+
self.sql.set_raw_config(key.as_ref(), value).await?;
770+
}
761771
Config::Selfavatar => {
762772
self.sql
763773
.execute("UPDATE contacts SET selfavatar_sent=0;", ())

src/headerdef.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ pub enum HeaderDef {
6060
ChatGroupNameTimestamp,
6161
ChatVerified,
6262
ChatGroupAvatar,
63+
64+
/// If present, contact's avatar and status should be applied from the message.
65+
/// "Chat-User-Avatar: 0" means that the contact has no avatar. Contact's status is transferred
66+
/// in the message footer.
6367
ChatUserAvatar,
68+
6469
ChatVoiceMessage,
6570
ChatGroupMemberRemoved,
6671
ChatGroupMemberAdded,

src/mimefactory.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ impl MimeFactory {
182182
pub async fn from_msg(context: &Context, msg: Message) -> Result<MimeFactory> {
183183
let now = time();
184184
let chat = Chat::load_from_db(context, msg.chat_id).await?;
185-
let attach_profile_data = Self::should_attach_profile_data(&msg);
185+
let can_transfer_profile = Self::can_transfer_profile(&msg);
186186
let undisclosed_recipients = chat.typ == Chattype::OutBroadcast;
187187

188188
let from_addr = context.get_primary_self_addr().await?;
@@ -194,7 +194,7 @@ impl MimeFactory {
194194
if let Some(override_name) = msg.param.get(Param::OverrideSenderDisplayname) {
195195
(override_name.to_string(), Some(config_displayname))
196196
} else {
197-
let name = match attach_profile_data {
197+
let name = match can_transfer_profile {
198198
true => config_displayname,
199199
false => "".to_string(),
200200
};
@@ -302,7 +302,7 @@ impl MimeFactory {
302302
} else {
303303
addr
304304
};
305-
let name = match attach_profile_data {
305+
let name = match can_transfer_profile {
306306
true => authname,
307307
false => "".to_string(),
308308
};
@@ -451,14 +451,18 @@ impl MimeFactory {
451451
.split_ascii_whitespace()
452452
.map(|s| s.trim_start_matches('<').trim_end_matches('>').to_string())
453453
.collect();
454-
let selfstatus = match attach_profile_data {
454+
let should_attach_profile = Self::should_attach_profile(context, &msg).await;
455+
// TODO: (2025-08) Attach self-status in every message for compatibility with older
456+
// versions. Should be replaced with
457+
// `should_attach_profile || !is_encrypted && can_transfer_profile`.
458+
let selfstatus = match can_transfer_profile {
455459
true => context
456460
.get_config(Config::Selfstatus)
457461
.await?
458462
.unwrap_or_default(),
459463
false => "".to_string(),
460464
};
461-
let attach_selfavatar = Self::should_attach_selfavatar(context, &msg).await;
465+
let attach_selfavatar = should_attach_profile;
462466

463467
ensure_and_debug_assert!(
464468
member_timestamps.is_empty()
@@ -551,7 +555,7 @@ impl MimeFactory {
551555
}
552556
}
553557

554-
fn should_attach_profile_data(msg: &Message) -> bool {
558+
fn can_transfer_profile(msg: &Message) -> bool {
555559
msg.param.get_cmd() != SystemMessage::SecurejoinMessage || {
556560
let step = msg.param.get(Param::Arg).unwrap_or_default();
557561
// Don't attach profile data at the earlier SecureJoin steps:
@@ -566,14 +570,14 @@ impl MimeFactory {
566570
}
567571
}
568572

569-
async fn should_attach_selfavatar(context: &Context, msg: &Message) -> bool {
570-
Self::should_attach_profile_data(msg)
571-
&& match chat::shall_attach_selfavatar(context, msg.chat_id).await {
573+
async fn should_attach_profile(context: &Context, msg: &Message) -> bool {
574+
Self::can_transfer_profile(msg)
575+
&& match chat::should_attach_profile(context, msg.chat_id).await {
572576
Ok(should) => should,
573577
Err(err) => {
574578
warn!(
575579
context,
576-
"should_attach_selfavatar: cannot get selfavatar state: {err:#}."
580+
"should_attach_profile: chat::should_attach_profile: {err:#}."
577581
);
578582
false
579583
}
@@ -638,7 +642,7 @@ impl MimeFactory {
638642
return Ok(format!("Re: {}", remove_subject_prefix(last_subject)));
639643
}
640644

641-
let self_name = match Self::should_attach_profile_data(msg) {
645+
let self_name = match Self::can_transfer_profile(msg) {
642646
true => context.get_config(Config::Displayname).await?,
643647
false => None,
644648
};

src/receive_imf.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -908,8 +908,11 @@ pub(crate) async fn receive_imf_inner(
908908
}
909909
}
910910

911-
// Ignore footers from mailinglists as they are often created or modified by the mailinglist software.
912-
if let Some(footer) = &mime_parser.footer {
911+
if let Some(footer) = mime_parser.footer.as_ref().filter(|footer| {
912+
!footer.is_empty() || mime_parser.user_avatar.is_some() || !mime_parser.was_encrypted()
913+
}) {
914+
// Ignore footers from mailinglists as they are often created or modified by the mailinglist
915+
// software.
913916
if !mime_parser.is_mailinglist_message()
914917
&& from_id != ContactId::UNDEFINED
915918
&& context
@@ -923,7 +926,7 @@ pub(crate) async fn receive_imf_inner(
923926
if let Err(err) = contact::set_status(
924927
context,
925928
from_id,
926-
footer.to_string(),
929+
footer.clone(),
927930
mime_parser.was_encrypted(),
928931
mime_parser.has_chat_version(),
929932
)

src/receive_imf/receive_imf_tests.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,6 +2254,41 @@ sig thursday",
22542254
Ok(())
22552255
}
22562256

2257+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
2258+
async fn test_contact_status_from_encrypted_msg() -> Result<()> {
2259+
let mut tcm = TestContextManager::new();
2260+
let alice = &tcm.alice().await;
2261+
let bob = &tcm.bob().await;
2262+
2263+
let alice_chat_id = alice.create_chat(bob).await.id;
2264+
let sent = alice.send_text(alice_chat_id, "hi").await;
2265+
bob.recv_msg(&sent).await;
2266+
alice.set_config(Config::Selfstatus, Some("status")).await?;
2267+
let sent = alice.send_text(alice_chat_id, "I've set self-status").await;
2268+
bob.recv_msg(&sent).await;
2269+
let bob_alice = bob.add_or_lookup_contact(alice).await;
2270+
assert_eq!(bob_alice.get_status(), "status");
2271+
2272+
alice
2273+
.set_config(Config::Selfstatus, Some("status1"))
2274+
.await?;
2275+
alice
2276+
.send_text(alice_chat_id, "I changed self-status")
2277+
.await;
2278+
2279+
// Currently we send self-status in every appropriate message.
2280+
let sent = alice
2281+
.send_text(alice_chat_id, "This message also contains my status")
2282+
.await;
2283+
let parsed_msg = bob.parse_msg(&sent).await;
2284+
assert!(parsed_msg.was_encrypted());
2285+
assert!(parsed_msg.get_header(HeaderDef::ChatUserAvatar).is_none());
2286+
bob.recv_msg(&sent).await;
2287+
let bob_alice = bob.add_or_lookup_contact(alice).await;
2288+
assert_eq!(bob_alice.get_status(), "status1");
2289+
Ok(())
2290+
}
2291+
22572292
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
22582293
async fn test_chat_assignment_private_classical_reply() {
22592294
for outgoing_is_classical in &[true, false] {

0 commit comments

Comments
 (0)