mirror of
https://github.com/discourse/discourse.git
synced 2025-02-07 20:08:26 +00:00
FIX: correctly notifies subscribers with post_action_destroyer (#16084)
This commit is contained in:
parent
3bf5692c72
commit
a558c5bd30
@ -48,15 +48,29 @@ class PostActionDestroyer
|
|||||||
|
|
||||||
UserActionManager.post_action_destroyed(post_action)
|
UserActionManager.post_action_destroyed(post_action)
|
||||||
PostActionNotifier.post_action_deleted(post_action)
|
PostActionNotifier.post_action_deleted(post_action)
|
||||||
|
|
||||||
result.success = true
|
result.success = true
|
||||||
result.post = @post.reload
|
result.post = @post.reload
|
||||||
|
|
||||||
|
notify_subscribers
|
||||||
|
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
|
def self.notify_types
|
||||||
|
@notify_types ||= PostActionType.notify_flag_types.keys
|
||||||
|
end
|
||||||
|
|
||||||
|
def notify_subscribers
|
||||||
|
name = PostActionType.types[@post_action_type_id]
|
||||||
|
if name == :like
|
||||||
|
@post.publish_change_to_clients!(:liked, { likes_count: @post.like_count })
|
||||||
|
elsif self.class.notify_types.include?(name)
|
||||||
|
@post.publish_change_to_clients!(:acted)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def guardian
|
def guardian
|
||||||
@guardian ||= Guardian.new(@destroyed_by)
|
@guardian ||= Guardian.new(@destroyed_by)
|
||||||
end
|
end
|
||||||
|
85
spec/lib/post_action_destroyer_spec.rb
Normal file
85
spec/lib/post_action_destroyer_spec.rb
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
describe PostActionDestroyer do
|
||||||
|
fab!(:admin) { Fabricate(:admin) }
|
||||||
|
fab!(:user) { Fabricate(:user) }
|
||||||
|
fab!(:post) { Fabricate(:post) }
|
||||||
|
|
||||||
|
describe '#perform' do
|
||||||
|
context 'like' do
|
||||||
|
context 'post action exists' do
|
||||||
|
before do
|
||||||
|
PostActionCreator.new(user, post, PostActionType.types[:like]).perform
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'destroys the post action' do
|
||||||
|
expect {
|
||||||
|
PostActionDestroyer.destroy(user, post, :like)
|
||||||
|
}.to change { PostAction.count }.by(-1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'notifies subscribers' do
|
||||||
|
expect(post.reload.like_count).to eq(1)
|
||||||
|
|
||||||
|
messages = MessageBus.track_publish do
|
||||||
|
PostActionDestroyer.destroy(user, post, :like)
|
||||||
|
end
|
||||||
|
|
||||||
|
message = messages.last.data
|
||||||
|
expect(message[:type]).to eq(:liked)
|
||||||
|
expect(message[:likes_count]).to eq(0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'post action doesn’t exist' do
|
||||||
|
describe 'perform' do
|
||||||
|
it 'fails' do
|
||||||
|
result = PostActionDestroyer.destroy(user, post, :like)
|
||||||
|
expect(result.success).to eq(false)
|
||||||
|
expect(result.not_found).to eq(true)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'any other notifiable type' do
|
||||||
|
before do
|
||||||
|
PostActionCreator.new(user, post, PostActionType.types[:spam]).perform
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'destroys the post action' do
|
||||||
|
expect {
|
||||||
|
PostActionDestroyer.destroy(user, post, :spam)
|
||||||
|
}.to change { PostAction.count }.by(-1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'notifies subscribers' do
|
||||||
|
messages = MessageBus.track_publish do
|
||||||
|
PostActionDestroyer.destroy(user, post, :spam)
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(messages.last.data[:type]).to eq(:acted)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'not notifyable type' do
|
||||||
|
before do
|
||||||
|
PostActionCreator.new(user, post, PostActionType.types[:bookmark]).perform
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'destroys the post action' do
|
||||||
|
expect {
|
||||||
|
PostActionDestroyer.destroy(user, post, :bookmark)
|
||||||
|
}.to change { PostAction.count }.by(-1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'doesn’t notify subscribers' do
|
||||||
|
messages = MessageBus.track_publish do
|
||||||
|
PostActionDestroyer.destroy(user, post, :bookmark)
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(messages).to be_blank
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user