Skip to content

Commit

Permalink
New group consistency algorithm on the receiver side
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Jan 6, 2025
1 parent 4412449 commit 3258c92
Showing 1 changed file with 81 additions and 1 deletion.
82 changes: 81 additions & 1 deletion src/receive_imf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,29 @@ pub(crate) async fn receive_imf_inner(
}
};

let recipients = if mime_parser.get_header(HeaderDef::ChatVersion).is_some() {
// Chat messages should not have `Cc` field.
// Use only the `To` field.
&mime_parser.to
} else {
// Use `To` and `Cc` fields as recipients.
&mime_parser.recipients
};
let to_ids = add_or_lookup_contacts_by_address_list(
context,
&mime_parser.recipients,
recipients,
if !mime_parser.incoming {
Origin::OutgoingTo
} else if incoming_origin.is_known() {
Origin::IncomingTo
} else {
Origin::IncomingUnknownTo
},
)
.await?;
let past_ids = add_or_lookup_contacts_by_address_list(
context,
&mime_parser.past_members,
if !mime_parser.incoming {
Origin::OutgoingTo
} else if incoming_origin.is_known() {
Expand Down Expand Up @@ -418,6 +438,7 @@ pub(crate) async fn receive_imf_inner(
&mut mime_parser,
imf_raw,
&to_ids,
&past_ids,
rfc724_mid_orig,
from_id,
seen,
Expand Down Expand Up @@ -689,6 +710,7 @@ async fn add_parts(
mime_parser: &mut MimeMessage,
imf_raw: &[u8],
to_ids: &[ContactId],
past_ids: &[ContactId],
rfc724_mid: &str,
from_id: ContactId,
seen: bool,
Expand Down Expand Up @@ -906,6 +928,7 @@ async fn add_parts(
group_chat_id,
from_id,
to_ids,
past_ids,
is_partial_download.is_some(),
&verified_encryption,
)
Expand Down Expand Up @@ -1175,6 +1198,7 @@ async fn add_parts(
chat_id,
from_id,
to_ids,
past_ids,
is_partial_download.is_some(),
&verified_encryption,
)
Expand Down Expand Up @@ -2081,6 +2105,8 @@ async fn create_group(
/// original system message. If the better message is empty, the original system message should be
/// just omitted.
///
/// * `to_ids` - contents of the `To` header for chat messages
/// or combined `To` and `Cc` for non-chat messages
/// * `is_partial_download` - whether the message is not fully downloaded.
#[allow(clippy::too_many_arguments)]
async fn apply_group_changes(
Expand All @@ -2089,6 +2115,7 @@ async fn apply_group_changes(
chat_id: ChatId,
from_id: ContactId,
to_ids: &[ContactId],
past_ids: &[ContactId],
is_partial_download: bool,
verified_encryption: &VerifiedEncryption,
) -> Result<(Vec<String>, Option<String>)> {
Expand All @@ -2106,6 +2133,59 @@ async fn apply_group_changes(
let mut better_msg = None;
let mut group_changes_msgs = Vec::new();

if let Some(chat_group_member_timestamps) =
mime_parser.get_header(HeaderDef::ChatGroupMemberTimestamps)
{
let chat_group_member_timestamps: Vec<i64> = chat_group_member_timestamps
.split_ascii_whitespace()
.filter_map(|ts| ts.parse::<i64>().ok())
.collect();

let expected_timestamps_count = to_ids.len() + past_ids.len();
if chat_group_member_timestamps.len() == expected_timestamps_count {
context
.sql
.transaction(|transaction| {
for (contact_id, ts) in std::iter::zip(
to_ids.iter(),
chat_group_member_timestamps.iter().take(to_ids.len()),
) {
transaction.execute(
"INSERT INTO chats_contacts (chat_id, contact_id, add_timestamp)
VALUES (?1, ?2, ?3)
ON CONFLICT (chat_id, contact_id)
DO UPDATE SET add_timestamp=MAX(add_timestamp, ?3)",
(chat_id, contact_id, ts),
)?;
}

for (contact_id, ts) in std::iter::zip(
past_ids.iter(),
chat_group_member_timestamps.iter().skip(to_ids.len()),
) {
transaction.execute(
"UPDATE chats_contacts
SET remove_timestamp=MAX(remove_timestamp, ?)
WHERE chat_id=? AND contact_id=?",
(ts, chat_id, contact_id),
)?;
}

Ok(())
})
.await?;
} else {
warn!(
context,
"Chat-Group-Member-Timestamps has wrong number of timestamps, got {}, expected {}.",
chat_group_member_timestamps.len(),
expected_timestamps_count
);
}

return Ok((group_changes_msgs, better_msg));
}

// True if a Delta Chat client has explicitly added our current primary address.
let self_added =
if let Some(added_addr) = mime_parser.get_header(HeaderDef::ChatGroupMemberAdded) {
Expand Down

0 comments on commit 3258c92

Please sign in to comment.