diff --git a/plugins/discourse-presence/plugin.rb b/plugins/discourse-presence/plugin.rb index 7d7a57ac8f7..a5e54fa88fd 100644 --- a/plugins/discourse-presence/plugin.rb +++ b/plugins/discourse-presence/plugin.rb @@ -100,71 +100,75 @@ after_initialize do before_action :ensure_logged_in def publish - data = params.permit(:response_needed, - current: [:action, :topic_id, :post_id], - previous: [:action, :topic_id, :post_id] - ) + data = params.permit( + :response_needed, + current: [:action, :topic_id, :post_id], + previous: [:action, :topic_id, :post_id] + ) - if data[:previous] && - data[:previous][:action].in?(['edit', 'reply']) + payload = {} + if data[:previous] && data[:previous][:action].in?(['edit', 'reply']) type = data[:previous][:post_id] ? 'post' : 'topic' id = data[:previous][:post_id] ? data[:previous][:post_id] : data[:previous][:topic_id] topic = if type == 'post' - Post.find_by(id: id).topic + Post.find_by(id: id)&.topic else Topic.find_by(id: id) end - guardian.ensure_can_see!(topic) + 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) + any_changes = false + any_changes ||= Presence::PresenceManager.remove(type, id, current_user.id) + any_changes ||= Presence::PresenceManager.cleanup(type, id) - users = Presence::PresenceManager.publish(type, id) if any_changes + users = Presence::PresenceManager.publish(type, id) if any_changes + end end - if data[:current] && - data[:current][:action].in?(['edit', 'reply']) - + if data[:current] && data[:current][:action].in?(['edit', 'reply']) type = data[:current][:post_id] ? 'post' : 'topic' id = data[:current][:post_id] ? data[:current][:post_id] : data[:current][:topic_id] topic = if type == 'post' - Post.find_by!(id: id).topic + Post.find_by(id: id)&.topic else - Topic.find_by!(id: id) + Topic.find_by(id: id) end - guardian.ensure_can_see!(topic) + 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) + any_changes = false + any_changes ||= Presence::PresenceManager.add(type, id, current_user.id) + any_changes ||= Presence::PresenceManager.cleanup(type, id) - users = Presence::PresenceManager.publish(type, id) if any_changes + users = Presence::PresenceManager.publish(type, id) if any_changes - if data[:response_needed] - users ||= Presence::PresenceManager.get_users(type, id) + if data[:response_needed] + users ||= Presence::PresenceManager.get_users(type, id) - serialized_users = users.map { |u| BasicUserSerializer.new(u, root: false) } + serialized_users = users.map { |u| BasicUserSerializer.new(u, root: false) } - messagebus_channel = Presence::PresenceManager.get_messagebus_channel(type, id) + messagebus_channel = Presence::PresenceManager.get_messagebus_channel(type, id) - render json: { - messagebus_channel: messagebus_channel, - messagebus_id: MessageBus.last_id(messagebus_channel), - users: serialized_users - } - return + { + messagebus_channel: messagebus_channel, + messagebus_id: MessageBus.last_id(messagebus_channel), + users: serialized_users + } + end + else + {} end end - render json: {} + render json: payload end end diff --git a/plugins/discourse-presence/spec/presence_controller_spec.rb b/plugins/discourse-presence/spec/requests/presence_controller_spec.rb similarity index 77% rename from plugins/discourse-presence/spec/presence_controller_spec.rb rename to plugins/discourse-presence/spec/requests/presence_controller_spec.rb index 28d62753b30..2e667e0c028 100644 --- a/plugins/discourse-presence/spec/presence_controller_spec.rb +++ b/plugins/discourse-presence/spec/requests/presence_controller_spec.rb @@ -1,7 +1,6 @@ require 'rails_helper' -describe ::Presence::PresencesController, type: :request do - +describe ::Presence::PresencesController do before do SiteSetting.presence_enabled = true end @@ -13,7 +12,7 @@ describe ::Presence::PresencesController, type: :request do let(:post1) { Fabricate(:post) } let(:post2) { Fabricate(:post) } - after(:each) do + after do $redis.del("presence:topic:#{post1.topic.id}") $redis.del("presence:topic:#{post2.topic.id}") $redis.del("presence:post:#{post1.id}") @@ -36,7 +35,6 @@ describe ::Presence::PresencesController, type: :request do end it "uses guardian to secure endpoint" do - # Private message private_post = Fabricate(:private_message_post) post '/presence/publish.json', params: { @@ -45,7 +43,6 @@ describe ::Presence::PresencesController, type: :request do expect(response.code.to_i).to eq(403) - # Secure category group = Fabricate(:group) category = Fabricate(:private_category, group: group) private_topic = Fabricate(:topic, category: category) @@ -64,7 +61,7 @@ describe ::Presence::PresencesController, type: :request do } end - expect(messages.count).to eq (1) + expect(messages.count).to eq(1) data = JSON.parse(response.body) @@ -80,7 +77,7 @@ describe ::Presence::PresencesController, type: :request do } end - expect(messages.count).to eq (1) + expect(messages.count).to eq(1) data = JSON.parse(response.body) expect(data).to eq({}) @@ -93,7 +90,7 @@ describe ::Presence::PresencesController, type: :request do } end - expect(messages.count).to eq (1) + expect(messages.count).to eq(1) messages = MessageBus.track_publish do post '/presence/publish.json', params: { @@ -101,7 +98,7 @@ describe ::Presence::PresencesController, type: :request do } end - expect(messages.count).to eq (0) + expect(messages.count).to eq(0) end it "clears 'previous' state when supplied" do @@ -116,7 +113,28 @@ describe ::Presence::PresencesController, type: :request do } end - expect(messages.count).to eq (3) + expect(messages.count).to eq(3) + end + + describe 'when post has been deleted' do + it 'should return an empty response' do + post1.destroy! + + post '/presence/publish.json', params: { + current: { compose_state: 'open', action: 'edit', post_id: post1.id } + } + + expect(response.status).to eq(200) + expect(JSON.parse(response.body)).to eq({}) + + post '/presence/publish.json', params: { + current: { compose_state: 'open', action: 'edit', post_id: post2.id }, + previous: { compose_state: 'open', action: 'edit', post_id: post1.id } + } + + expect(response.status).to eq(200) + expect(JSON.parse(response.body)).to eq({}) + end end end