Skip to content

Commit 41c2a80

Browse files
committed
feat: sort past members by the timestamp of removal
1 parent 4f71c77 commit 41c2a80

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/chat.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3493,6 +3493,8 @@ pub async fn get_chat_contacts(context: &Context, chat_id: ChatId) -> Result<Vec
34933493
}
34943494

34953495
/// Returns a vector of contact IDs for given chat ID that are no longer part of the group.
3496+
///
3497+
/// Members that have been removed recently are in the beginning of the list.
34963498
pub async fn get_past_chat_contacts(context: &Context, chat_id: ChatId) -> Result<Vec<ContactId>> {
34973499
let now = time();
34983500
let list = context
@@ -3505,7 +3507,7 @@ pub async fn get_past_chat_contacts(context: &Context, chat_id: ChatId) -> Resul
35053507
WHERE cc.chat_id=?
35063508
AND cc.add_timestamp < cc.remove_timestamp
35073509
AND ? < cc.remove_timestamp
3508-
ORDER BY c.id=1, c.last_seen DESC, c.id DESC",
3510+
ORDER BY c.id=1, cc.remove_timestamp DESC, c.id DESC",
35093511
(chat_id, now.saturating_sub(60 * 24 * 3600)),
35103512
|row| row.get::<_, ContactId>(0),
35113513
|ids| ids.collect::<Result<Vec<_>, _>>().map_err(Into::into),

src/chat/chat_tests.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3418,6 +3418,61 @@ async fn test_expire_past_members_after_60_days() -> Result<()> {
34183418
Ok(())
34193419
}
34203420

3421+
/// Test that past members are ordered by the timestamp of their removal.
3422+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
3423+
async fn test_past_members_order() -> Result<()> {
3424+
let t = &TestContext::new_alice().await;
3425+
3426+
let bob_contact_id = Contact::create(t, "Bob", "[email protected]").await?;
3427+
let charlie_contact_id = Contact::create(t, "Charlie", "[email protected]").await?;
3428+
let fiona_contact_id = Contact::create(t, "Fiona", "[email protected]").await?;
3429+
3430+
let chat_id = create_group_chat(t, ProtectionStatus::Unprotected, "Group chat").await?;
3431+
add_contact_to_chat(t, chat_id, bob_contact_id).await?;
3432+
add_contact_to_chat(t, chat_id, charlie_contact_id).await?;
3433+
add_contact_to_chat(t, chat_id, fiona_contact_id).await?;
3434+
t.send_text(chat_id, "Hi! I created a group.").await;
3435+
3436+
assert_eq!(get_past_chat_contacts(t, chat_id).await?.len(), 0);
3437+
3438+
remove_contact_from_chat(t, chat_id, charlie_contact_id).await?;
3439+
3440+
let past_contacts = get_past_chat_contacts(t, chat_id).await?;
3441+
assert_eq!(past_contacts.len(), 1);
3442+
assert_eq!(past_contacts[0], charlie_contact_id);
3443+
3444+
SystemTime::shift(Duration::from_secs(5));
3445+
remove_contact_from_chat(t, chat_id, bob_contact_id).await?;
3446+
3447+
let past_contacts = get_past_chat_contacts(t, chat_id).await?;
3448+
assert_eq!(past_contacts.len(), 2);
3449+
assert_eq!(past_contacts[0], bob_contact_id);
3450+
assert_eq!(past_contacts[1], charlie_contact_id);
3451+
3452+
SystemTime::shift(Duration::from_secs(5));
3453+
remove_contact_from_chat(t, chat_id, fiona_contact_id).await?;
3454+
3455+
let past_contacts = get_past_chat_contacts(t, chat_id).await?;
3456+
assert_eq!(past_contacts.len(), 3);
3457+
assert_eq!(past_contacts[0], fiona_contact_id);
3458+
assert_eq!(past_contacts[1], bob_contact_id);
3459+
assert_eq!(past_contacts[2], charlie_contact_id);
3460+
3461+
// Adding and removing Bob
3462+
// moves him to the top of past member list.
3463+
SystemTime::shift(Duration::from_secs(5));
3464+
add_contact_to_chat(t, chat_id, bob_contact_id).await?;
3465+
remove_contact_from_chat(t, chat_id, bob_contact_id).await?;
3466+
3467+
let past_contacts = get_past_chat_contacts(t, chat_id).await?;
3468+
assert_eq!(past_contacts.len(), 3);
3469+
assert_eq!(past_contacts[0], bob_contact_id);
3470+
assert_eq!(past_contacts[1], fiona_contact_id);
3471+
assert_eq!(past_contacts[2], charlie_contact_id);
3472+
3473+
Ok(())
3474+
}
3475+
34213476
/// Test the case when Alice restores a backup older than 60 days
34223477
/// with outdated member list.
34233478
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]

0 commit comments

Comments
 (0)