Skip to content

Commit 75f38f1

Browse files
committed
feat: Don't reset key-contact status if Chat-User-Avatar header is absent
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 2dde286 commit 75f38f1

File tree

7 files changed

+80
-25
lines changed

7 files changed

+80
-25
lines changed

src/chat.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4048,12 +4048,12 @@ pub(crate) async fn add_contact_to_chat_ex(
40484048
Ok(true)
40494049
}
40504050

4051-
/// Returns true if an avatar should be attached in the given chat.
4051+
/// Returns whether profile data should be attached when sending to the given chat.
40524052
///
4053-
/// This function does not check if the avatar is set.
4053+
/// This function does not check if the avatar/status is set.
40544054
/// If avatar is not set and this function returns `true`,
40554055
/// a `Chat-User-Avatar: 0` header should be sent to reset the avatar.
4056-
pub(crate) async fn shall_attach_selfavatar(context: &Context, chat_id: ChatId) -> Result<bool> {
4056+
pub(crate) async fn should_attach_profile(context: &Context, chat_id: ChatId) -> Result<bool> {
40574057
let timestamp_some_days_ago = time() - DC_RESEND_USER_AVATAR_DAYS * 24 * 60 * 60;
40584058
let needs_attach = context
40594059
.sql
@@ -4068,8 +4068,8 @@ pub(crate) async fn shall_attach_selfavatar(context: &Context, chat_id: ChatId)
40684068
let mut needs_attach = false;
40694069
for row in rows {
40704070
let row = row?;
4071-
let selfavatar_sent = row?;
4072-
if selfavatar_sent < timestamp_some_days_ago {
4071+
let profile_sent = row?;
4072+
if profile_sent < timestamp_some_days_ago {
40734073
needs_attach = true;
40744074
}
40754075
}

src/chat/chat_tests.rs

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

15441544
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1545-
async fn test_shall_attach_selfavatar() -> Result<()> {
1545+
async fn test_should_attach_profile() -> Result<()> {
15461546
let mut tcm = TestContextManager::new();
15471547
let alice = &tcm.alice().await;
15481548
let bob = &tcm.bob().await;
15491549

15501550
let chat_id = create_group_chat(alice, ProtectionStatus::Unprotected, "foo").await?;
1551-
assert!(!shall_attach_selfavatar(alice, chat_id).await?);
1551+
assert!(!should_attach_profile(alice, chat_id).await?);
15521552

15531553
let contact_id = alice.add_or_lookup_contact_id(bob).await;
15541554
add_contact_to_chat(alice, chat_id, contact_id).await?;
1555-
assert!(shall_attach_selfavatar(alice, chat_id).await?);
1555+
assert!(should_attach_profile(alice, chat_id).await?);
15561556

15571557
chat_id.set_selfavatar_timestamp(alice, time()).await?;
1558-
assert!(!shall_attach_selfavatar(alice, chat_id).await?);
1558+
assert!(!should_attach_profile(alice, chat_id).await?);
15591559

15601560
alice.set_config(Config::Selfavatar, None).await?; // setting to None also forces re-sending
1561-
assert!(shall_attach_selfavatar(alice, chat_id).await?);
1561+
assert!(should_attach_profile(alice, chat_id).await?);
15621562
Ok(())
15631563
}
15641564

@@ -1582,7 +1582,7 @@ async fn test_profile_data_on_group_leave() -> Result<()> {
15821582
tokio::fs::write(&file, bytes).await?;
15831583
t.set_config(Config::Selfavatar, Some(file.to_str().unwrap()))
15841584
.await?;
1585-
assert!(shall_attach_selfavatar(t, chat_id).await?);
1585+
assert!(should_attach_profile(t, chat_id).await?);
15861586

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

src/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,14 @@ impl Context {
754754
let better_value;
755755

756756
match key {
757+
Config::Selfstatus => {
758+
// Ensure that future versions send the self-status that after upgrade. Currently we
759+
// send it in every appropriate message.
760+
self.sql
761+
.execute("UPDATE contacts SET selfavatar_sent=0", ())
762+
.await?;
763+
self.sql.set_raw_config(key.as_ref(), value).await?;
764+
}
757765
Config::Selfavatar => {
758766
self.sql
759767
.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
@@ -181,7 +181,7 @@ impl MimeFactory {
181181
pub async fn from_msg(context: &Context, msg: Message) -> Result<MimeFactory> {
182182
let now = time();
183183
let chat = Chat::load_from_db(context, msg.chat_id).await?;
184-
let attach_profile_data = Self::should_attach_profile_data(&msg);
184+
let can_transfer_profile = Self::can_transfer_profile(&msg);
185185
let undisclosed_recipients = chat.typ == Chattype::OutBroadcast;
186186

187187
let from_addr = context.get_primary_self_addr().await?;
@@ -193,7 +193,7 @@ impl MimeFactory {
193193
if let Some(override_name) = msg.param.get(Param::OverrideSenderDisplayname) {
194194
(override_name.to_string(), Some(config_displayname))
195195
} else {
196-
let name = match attach_profile_data {
196+
let name = match can_transfer_profile {
197197
true => config_displayname,
198198
false => "".to_string(),
199199
};
@@ -301,7 +301,7 @@ impl MimeFactory {
301301
} else {
302302
addr
303303
};
304-
let name = match attach_profile_data {
304+
let name = match can_transfer_profile {
305305
true => authname,
306306
false => "".to_string(),
307307
};
@@ -453,14 +453,18 @@ impl MimeFactory {
453453
.split_ascii_whitespace()
454454
.map(|s| s.trim_start_matches('<').trim_end_matches('>').to_string())
455455
.collect();
456-
let selfstatus = match attach_profile_data {
456+
let should_attach_profile = Self::should_attach_profile(context, &msg).await;
457+
// TODO: (2025-08) Attach self-status in every message for compatibility with older
458+
// versions. Should be replaced with
459+
// `should_attach_profile || !is_encrypted && can_transfer_profile`.
460+
let selfstatus = match can_transfer_profile {
457461
true => context
458462
.get_config(Config::Selfstatus)
459463
.await?
460464
.unwrap_or_default(),
461465
false => "".to_string(),
462466
};
463-
let attach_selfavatar = Self::should_attach_selfavatar(context, &msg).await;
467+
let attach_selfavatar = should_attach_profile;
464468

465469
ensure_and_debug_assert!(
466470
member_timestamps.is_empty()
@@ -553,7 +557,7 @@ impl MimeFactory {
553557
}
554558
}
555559

556-
fn should_attach_profile_data(msg: &Message) -> bool {
560+
fn can_transfer_profile(msg: &Message) -> bool {
557561
msg.param.get_cmd() != SystemMessage::SecurejoinMessage || {
558562
let step = msg.param.get(Param::Arg).unwrap_or_default();
559563
// Don't attach profile data at the earlier SecureJoin steps:
@@ -568,14 +572,14 @@ impl MimeFactory {
568572
}
569573
}
570574

571-
async fn should_attach_selfavatar(context: &Context, msg: &Message) -> bool {
572-
Self::should_attach_profile_data(msg)
573-
&& match chat::shall_attach_selfavatar(context, msg.chat_id).await {
575+
async fn should_attach_profile(context: &Context, msg: &Message) -> bool {
576+
Self::can_transfer_profile(msg)
577+
&& match chat::should_attach_profile(context, msg.chat_id).await {
574578
Ok(should) => should,
575579
Err(err) => {
576580
warn!(
577581
context,
578-
"should_attach_selfavatar: cannot get selfavatar state: {err:#}."
582+
"should_attach_profile: chat::should_attach_profile: {err:#}."
579583
);
580584
false
581585
}
@@ -640,7 +644,7 @@ impl MimeFactory {
640644
return Ok(format!("Re: {}", remove_subject_prefix(last_subject)));
641645
}
642646

643-
let self_name = match Self::should_attach_profile_data(msg) {
647+
let self_name = match Self::can_transfer_profile(msg) {
644648
true => context.get_config(Config::Displayname).await?,
645649
false => None,
646650
};

src/receive_imf.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -939,8 +939,11 @@ pub(crate) async fn receive_imf_inner(
939939
}
940940
}
941941

942-
// Ignore footers from mailinglists as they are often created or modified by the mailinglist software.
943-
if let Some(footer) = &mime_parser.footer {
942+
if let Some(footer) = mime_parser.footer.as_ref().filter(|footer| {
943+
!footer.is_empty() || mime_parser.user_avatar.is_some() || !mime_parser.was_encrypted()
944+
}) {
945+
// Ignore footers from mailinglists as they are often created or modified by the mailinglist
946+
// software.
944947
if !mime_parser.is_mailinglist_message()
945948
&& from_id != ContactId::UNDEFINED
946949
&& context
@@ -954,7 +957,7 @@ pub(crate) async fn receive_imf_inner(
954957
if let Err(err) = contact::set_status(
955958
context,
956959
from_id,
957-
footer.to_string(),
960+
footer.clone(),
958961
mime_parser.was_encrypted(),
959962
mime_parser.has_chat_version(),
960963
)

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)