FIX: Don't raise an error when post has been destroyed.

This commit is contained in:
Guo Xiang Tan 2017-10-02 12:00:43 +08:00
parent c872225762
commit 95358304d9
2 changed files with 65 additions and 43 deletions

View File

@ -100,71 +100,75 @@ after_initialize do
before_action :ensure_logged_in before_action :ensure_logged_in
def publish def publish
data = params.permit(:response_needed, data = params.permit(
current: [:action, :topic_id, :post_id], :response_needed,
previous: [:action, :topic_id, :post_id] current: [:action, :topic_id, :post_id],
) previous: [:action, :topic_id, :post_id]
)
if data[:previous] && payload = {}
data[:previous][:action].in?(['edit', 'reply'])
if data[:previous] && data[:previous][:action].in?(['edit', 'reply'])
type = data[:previous][:post_id] ? 'post' : 'topic' type = data[:previous][:post_id] ? 'post' : 'topic'
id = data[:previous][:post_id] ? data[:previous][:post_id] : data[:previous][:topic_id] id = data[:previous][:post_id] ? data[:previous][:post_id] : data[:previous][:topic_id]
topic = topic =
if type == 'post' if type == 'post'
Post.find_by(id: id).topic Post.find_by(id: id)&.topic
else else
Topic.find_by(id: id) Topic.find_by(id: id)
end end
guardian.ensure_can_see!(topic) if topic
guardian.ensure_can_see!(topic)
any_changes = false any_changes = false
any_changes ||= Presence::PresenceManager.remove(type, id, current_user.id) any_changes ||= Presence::PresenceManager.remove(type, id, current_user.id)
any_changes ||= Presence::PresenceManager.cleanup(type, 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 end
if data[:current] && if data[:current] && data[:current][:action].in?(['edit', 'reply'])
data[:current][:action].in?(['edit', 'reply'])
type = data[:current][:post_id] ? 'post' : 'topic' type = data[:current][:post_id] ? 'post' : 'topic'
id = data[:current][:post_id] ? data[:current][:post_id] : data[:current][:topic_id] id = data[:current][:post_id] ? data[:current][:post_id] : data[:current][:topic_id]
topic = topic =
if type == 'post' if type == 'post'
Post.find_by!(id: id).topic Post.find_by(id: id)&.topic
else else
Topic.find_by!(id: id) Topic.find_by(id: id)
end end
guardian.ensure_can_see!(topic) if topic
guardian.ensure_can_see!(topic)
any_changes = false any_changes = false
any_changes ||= Presence::PresenceManager.add(type, id, current_user.id) any_changes ||= Presence::PresenceManager.add(type, id, current_user.id)
any_changes ||= Presence::PresenceManager.cleanup(type, 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] if data[:response_needed]
users ||= Presence::PresenceManager.get_users(type, id) 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_channel: messagebus_channel,
messagebus_id: MessageBus.last_id(messagebus_channel), messagebus_id: MessageBus.last_id(messagebus_channel),
users: serialized_users users: serialized_users
} }
return end
else
{}
end end
end end
render json: {} render json: payload
end end
end end

View File

@ -1,7 +1,6 @@
require 'rails_helper' require 'rails_helper'
describe ::Presence::PresencesController, type: :request do describe ::Presence::PresencesController do
before do before do
SiteSetting.presence_enabled = true SiteSetting.presence_enabled = true
end end
@ -13,7 +12,7 @@ describe ::Presence::PresencesController, type: :request do
let(:post1) { Fabricate(:post) } let(:post1) { Fabricate(:post) }
let(:post2) { Fabricate(:post) } let(:post2) { Fabricate(:post) }
after(:each) do after do
$redis.del("presence:topic:#{post1.topic.id}") $redis.del("presence:topic:#{post1.topic.id}")
$redis.del("presence:topic:#{post2.topic.id}") $redis.del("presence:topic:#{post2.topic.id}")
$redis.del("presence:post:#{post1.id}") $redis.del("presence:post:#{post1.id}")
@ -36,7 +35,6 @@ describe ::Presence::PresencesController, type: :request do
end end
it "uses guardian to secure endpoint" do it "uses guardian to secure endpoint" do
# Private message
private_post = Fabricate(:private_message_post) private_post = Fabricate(:private_message_post)
post '/presence/publish.json', params: { post '/presence/publish.json', params: {
@ -45,7 +43,6 @@ describe ::Presence::PresencesController, type: :request do
expect(response.code.to_i).to eq(403) expect(response.code.to_i).to eq(403)
# Secure category
group = Fabricate(:group) group = Fabricate(:group)
category = Fabricate(:private_category, group: group) category = Fabricate(:private_category, group: group)
private_topic = Fabricate(:topic, category: category) private_topic = Fabricate(:topic, category: category)
@ -64,7 +61,7 @@ describe ::Presence::PresencesController, type: :request do
} }
end end
expect(messages.count).to eq (1) expect(messages.count).to eq(1)
data = JSON.parse(response.body) data = JSON.parse(response.body)
@ -80,7 +77,7 @@ describe ::Presence::PresencesController, type: :request do
} }
end end
expect(messages.count).to eq (1) expect(messages.count).to eq(1)
data = JSON.parse(response.body) data = JSON.parse(response.body)
expect(data).to eq({}) expect(data).to eq({})
@ -93,7 +90,7 @@ describe ::Presence::PresencesController, type: :request do
} }
end end
expect(messages.count).to eq (1) expect(messages.count).to eq(1)
messages = MessageBus.track_publish do messages = MessageBus.track_publish do
post '/presence/publish.json', params: { post '/presence/publish.json', params: {
@ -101,7 +98,7 @@ describe ::Presence::PresencesController, type: :request do
} }
end end
expect(messages.count).to eq (0) expect(messages.count).to eq(0)
end end
it "clears 'previous' state when supplied" do it "clears 'previous' state when supplied" do
@ -116,7 +113,28 @@ describe ::Presence::PresencesController, type: :request do
} }
end 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
end end