FIX: Presence bug (#5329)

* Add failing test case

* FIX: new activity was not triggering cleanup
This commit is contained in:
David Taylor 2017-11-20 14:24:59 +00:00 committed by Régis Hanol
parent 7e841a0495
commit 28073413eb
2 changed files with 26 additions and 6 deletions

View File

@ -122,9 +122,9 @@ after_initialize do
if topic
guardian.ensure_can_see!(topic)
any_changes = false
any_changes ||= Presence::PresenceManager.remove(type, id, current_user.id)
any_changes ||= Presence::PresenceManager.cleanup(type, id)
removed = Presence::PresenceManager.remove(type, id, current_user.id)
any_removed = Presence::PresenceManager.cleanup(type, id)
any_changes = removed || any_removed
users = Presence::PresenceManager.publish(type, id) if any_changes
end
@ -144,9 +144,9 @@ after_initialize do
if topic
guardian.ensure_can_see!(topic)
any_changes = false
any_changes ||= Presence::PresenceManager.add(type, id, current_user.id)
any_changes ||= Presence::PresenceManager.cleanup(type, id)
added = Presence::PresenceManager.add(type, id, current_user.id)
any_removed = Presence::PresenceManager.cleanup(type, id)
any_changes = added || any_removed
users = Presence::PresenceManager.publish(type, id) if any_changes

View File

@ -12,6 +12,8 @@ describe ::Presence::PresencesController do
let(:post1) { Fabricate(:post) }
let(:post2) { Fabricate(:post) }
let(:manager) { ::Presence::PresenceManager }
after do
$redis.del("presence:topic:#{post1.topic.id}")
$redis.del("presence:topic:#{post2.topic.id}")
@ -116,6 +118,24 @@ describe ::Presence::PresencesController do
expect(messages.count).to eq(3)
end
it 'cleans up old users when requested' do
freeze_time Time.zone.now do
manager.add('topic', post1.topic.id, user2.id)
end
# Anything older than 20 seconds should be cleaned up
freeze_time 30.seconds.from_now do
post '/presence/publish.json', params: {
current: { compose_state: 'open', action: 'reply', topic_id: post1.topic.id }, response_needed: true
}
data = JSON.parse(response.body)
expect(data['users'].length).to eq(1)
end
end
describe 'when post has been deleted' do
it 'should return an empty response' do
post1.destroy!