Skip to content

Commit 14e5c35

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 3656337 commit 14e5c35

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
@@ -4025,12 +4025,12 @@ pub(crate) async fn add_contact_to_chat_ex(
40254025
Ok(true)
40264026
}
40274027

4028-
/// Returns true if an avatar should be attached in the given chat.
4028+
/// Returns whether profile data should be attached when sending to the given chat.
40294029
///
4030-
/// This function does not check if the avatar is set.
4030+
/// This function does not check if the avatar/status is set.
40314031
/// If avatar is not set and this function returns `true`,
40324032
/// a `Chat-User-Avatar: 0` header should be sent to reset the avatar.
4033-
pub(crate) async fn shall_attach_selfavatar(context: &Context, chat_id: ChatId) -> Result<bool> {
4033+
pub(crate) async fn should_attach_profile(context: &Context, chat_id: ChatId) -> Result<bool> {
40344034
let timestamp_some_days_ago = time() - DC_RESEND_USER_AVATAR_DAYS * 24 * 60 * 60;
40354035
let needs_attach = context
40364036
.sql
@@ -4045,8 +4045,8 @@ pub(crate) async fn shall_attach_selfavatar(context: &Context, chat_id: ChatId)
40454045
let mut needs_attach = false;
40464046
for row in rows {
40474047
let row = row?;
4048-
let selfavatar_sent = row?;
4049-
if selfavatar_sent < timestamp_some_days_ago {
4048+
let profile_sent = row?;
4049+
if profile_sent < timestamp_some_days_ago {
40504050
needs_attach = true;
40514051
}
40524052
}

src/chat/chat_tests.rs

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

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

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

15511551
let contact_id = alice.add_or_lookup_contact_id(bob).await;
15521552
add_contact_to_chat(alice, chat_id, contact_id).await?;
1553-
assert!(shall_attach_selfavatar(alice, chat_id).await?);
1553+
assert!(should_attach_profile(alice, chat_id).await?);
15541554

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

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

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

15851585
remove_contact_from_chat(t, chat_id, ContactId::SELF).await?;
15861586
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
@@ -759,6 +759,14 @@ impl Context {
759759
let better_value;
760760

761761
match key {
762+
Config::Selfstatus => {
763+
// Ensure that future versions send the self-status that after upgrade. Currently we
764+
// send it in every appropriate message.
765+
self.sql
766+
.execute("UPDATE contacts SET selfavatar_sent=0", ())
767+
.await?;
768+
self.sql.set_raw_config(key.as_ref(), value).await?;
769+
}
762770
Config::Selfavatar => {
763771
self.sql
764772
.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
@@ -940,8 +940,11 @@ pub(crate) async fn receive_imf_inner(
940940
}
941941
}
942942

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

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)