chore: Remove stale contact presence records (#5205)

fixes: chatwoot/product#493
This commit is contained in:
Sojan Jose
2022-08-05 12:51:39 +02:00
committed by GitHub
parent 124390a019
commit 8e75b3fc2a
3 changed files with 34 additions and 4 deletions

View File

@@ -21,4 +21,25 @@ describe OnlineStatusTracker do
expect(described_class.get_available_users(account.id).keys).to contain_exactly(user1.id.to_s, user2.id.to_s)
end
end
context 'when get_available_contacts' do
let(:online_contact) { create(:contact, account: account) }
let(:offline_contact) { create(:contact, account: account) }
before do
described_class.update_presence(account.id, 'Contact', online_contact.id)
# creating a stale record for offline contact presence
::Redis::Alfred.zadd(format(::Redis::Alfred::ONLINE_PRESENCE_CONTACTS, account_id: account.id),
(Time.zone.now - (OnlineStatusTracker::PRESENCE_DURATION + 20)).to_i, offline_contact.id)
end
it 'returns only the online contact ids with presence' do
expect(described_class.get_available_contacts(account.id).keys).to contain_exactly(online_contact.id.to_s)
end
it 'flushes the stale records from sorted set after the duration' do
described_class.get_available_contacts(account.id)
expect(::Redis::Alfred.zscore(format(::Redis::Alfred::ONLINE_PRESENCE_CONTACTS, account_id: account.id), offline_contact.id)).to be_nil
end
end
end