Skip to content

Commit df4a9d4

Browse files
committed
feat: expire past members after 60 days
1 parent fa016b3 commit df4a9d4

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/chat.rs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3464,16 +3464,19 @@ pub async fn get_chat_contacts(context: &Context, chat_id: ChatId) -> Result<Vec
34643464

34653465
/// Returns a vector of contact IDs for given chat ID that are no longer part of the group.
34663466
pub async fn get_past_chat_contacts(context: &Context, chat_id: ChatId) -> Result<Vec<ContactId>> {
3467+
let now = time();
34673468
let list = context
34683469
.sql
34693470
.query_map(
34703471
"SELECT cc.contact_id
34713472
FROM chats_contacts cc
34723473
LEFT JOIN contacts c
34733474
ON c.id=cc.contact_id
3474-
WHERE cc.chat_id=? AND cc.add_timestamp < cc.remove_timestamp
3475+
WHERE cc.chat_id=?
3476+
AND cc.add_timestamp < cc.remove_timestamp
3477+
AND ? < cc.remove_timestamp
34753478
ORDER BY c.id=1, c.last_seen DESC, c.id DESC",
3476-
(chat_id,),
3479+
(chat_id, now.saturating_sub(60 * 24 * 3600)),
34773480
|row| row.get::<_, ContactId>(0),
34783481
|ids| ids.collect::<Result<Vec<_>, _>>().map_err(Into::into),
34793482
)
@@ -7896,4 +7899,40 @@ mod tests {
78967899

78977900
Ok(())
78987901
}
7902+
7903+
/// Test that past members expire after 60 days.
7904+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
7905+
async fn test_expire_past_members_after_60_days() -> Result<()> {
7906+
let mut tcm = TestContextManager::new();
7907+
7908+
let alice = &tcm.alice().await;
7909+
let fiona_addr = "[email protected]";
7910+
let alice_fiona_contact_id = Contact::create(alice, "Fiona", fiona_addr).await?;
7911+
7912+
let alice_chat_id =
7913+
create_group_chat(alice, ProtectionStatus::Unprotected, "Group chat").await?;
7914+
add_contact_to_chat(alice, alice_chat_id, alice_fiona_contact_id).await?;
7915+
alice
7916+
.send_text(alice_chat_id, "Hi! I created a group.")
7917+
.await;
7918+
remove_contact_from_chat(alice, alice_chat_id, alice_fiona_contact_id).await?;
7919+
assert_eq!(get_past_chat_contacts(alice, alice_chat_id).await?.len(), 1);
7920+
7921+
SystemTime::shift(Duration::from_secs(60 * 24 * 60 * 60 + 1));
7922+
assert_eq!(get_past_chat_contacts(alice, alice_chat_id).await?.len(), 0);
7923+
7924+
let bob = &tcm.bob().await;
7925+
let bob_addr = bob.get_config(Config::Addr).await?.unwrap();
7926+
let alice_bob_contact_id = Contact::create(alice, "Bob", &bob_addr).await?;
7927+
add_contact_to_chat(alice, alice_chat_id, alice_bob_contact_id).await?;
7928+
7929+
let add_message = alice.pop_sent_msg().await;
7930+
assert_eq!(add_message.payload.contains(fiona_addr), false);
7931+
let bob_add_message = bob.recv_msg(&add_message).await;
7932+
let bob_chat_id = bob_add_message.chat_id;
7933+
assert_eq!(get_chat_contacts(bob, bob_chat_id).await?.len(), 2);
7934+
assert_eq!(get_past_chat_contacts(bob, bob_chat_id).await?.len(), 0);
7935+
7936+
Ok(())
7937+
}
78997938
}

src/mimefactory.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ fn new_address_with_name(name: &str, address: String) -> Address {
154154

155155
impl MimeFactory {
156156
pub async fn from_msg(context: &Context, msg: Message) -> Result<MimeFactory> {
157+
let now = time();
157158
let chat = Chat::load_from_db(context, msg.chat_id).await?;
158159
let attach_profile_data = Self::should_attach_profile_data(&msg);
159160
let undisclosed_recipients = chat.typ == Chattype::Broadcast;
@@ -239,7 +240,7 @@ impl MimeFactory {
239240
}
240241
}
241242
recipient_ids.insert(id);
242-
} else {
243+
} else if remove_timestamp.saturating_add(60 * 24 * 3600) > now {
243244
// Row is a tombstone,
244245
// member is not actually part of the group.
245246
if !recipients_contain_addr(&past_members, &addr) {

0 commit comments

Comments
 (0)