diff --git a/app/serializers/web_hook_post_serializer.rb b/app/serializers/web_hook_post_serializer.rb index c4b737703d2..1095d5c46ce 100644 --- a/app/serializers/web_hook_post_serializer.rb +++ b/app/serializers/web_hook_post_serializer.rb @@ -32,12 +32,16 @@ class WebHookPostSerializer < PostSerializer mentioned_users ].each { |attr| define_method("include_#{attr}?") { false } } + def topic_posts + @topic_posts ||= object.topic.posts.where(user_deleted: false) + end + def topic_posts_count - object.topic ? object.topic.posts_count : 0 + object.topic ? topic_posts.count : 0 end def topic_filtered_posts_count - object.topic ? object.topic.posts.where(post_type: Post.types[:regular]).count : 0 + object.topic ? topic_posts.where(post_type: Post.types[:regular]).count : 0 end def topic_archetype diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb index e302f3228d1..b7b8a6be840 100644 --- a/lib/post_destroyer.rb +++ b/lib/post_destroyer.rb @@ -60,17 +60,6 @@ class PostDestroyer end def destroy - payload = WebHook.generate_payload(:post, @post) if WebHook.active_web_hooks( - :post_destroyed, - ).exists? - is_first_post = @post.is_first_post? && @topic - has_topic_web_hooks = is_first_post && WebHook.active_web_hooks(:topic_destroyed).exists? - - if has_topic_web_hooks - topic_view = TopicView.new(@topic.id, Discourse.system_user, skip_staff_action: true) - topic_payload = WebHook.generate_payload(:topic, topic_view, WebHookTopicViewSerializer) - end - delete_removed_posts_after = @opts[:delete_removed_posts_after] || SiteSetting.delete_removed_posts_after @@ -84,14 +73,22 @@ class PostDestroyer UserActionManager.post_destroyed(@post) DiscourseEvent.trigger(:post_destroyed, @post, @opts, @user) - WebHook.enqueue_post_hooks(:post_destroyed, @post, payload) + if WebHook.active_web_hooks(:post_destroyed).exists? + payload = WebHook.generate_payload(:post, @post) + WebHook.enqueue_post_hooks(:post_destroyed, @post, payload) + end Jobs.enqueue(:sync_topic_user_bookmarked, topic_id: @topic.id) if @topic + is_first_post = @post.is_first_post? && @topic if is_first_post UserProfile.remove_featured_topic_from_all_profiles(@topic) UserActionManager.topic_destroyed(@topic) DiscourseEvent.trigger(:topic_destroyed, @topic, @user) - WebHook.enqueue_topic_hooks(:topic_destroyed, @topic, topic_payload) if has_topic_web_hooks + if WebHook.active_web_hooks(:topic_destroyed).exists? + topic_view = TopicView.new(@topic.id, Discourse.system_user, skip_staff_action: true) + topic_payload = WebHook.generate_payload(:topic, topic_view, WebHookTopicViewSerializer) + WebHook.enqueue_topic_hooks(:topic_destroyed, @topic, topic_payload) + end if SiteSetting.tos_topic_id == @topic.id || SiteSetting.privacy_topic_id == @topic.id Discourse.clear_urls! end diff --git a/spec/models/web_hook_spec.rb b/spec/models/web_hook_spec.rb index 5e631c8edcc..27fa2a1bbbe 100644 --- a/spec/models/web_hook_spec.rb +++ b/spec/models/web_hook_spec.rb @@ -363,6 +363,27 @@ RSpec.describe WebHook do expect(payload["id"]).to eq(post.topic.id) end + it "should serialize the right topic posts counts when a post is deleted" do + Fabricate(:web_hook) + + Jobs::EmitWebHookEvent.jobs.clear + + post2 = + PostCreator.create!( + user, + raw: "post", + topic_id: topic.id, + reply_to_post_number: post.post_number, + skip_validations: true, + ) + PostDestroyer.new(user, post2).destroy + + job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first + payload = JSON.parse(job_args["payload"]) + expect(payload["topic_posts_count"]).to eq(1) + expect(payload["topic_filtered_posts_count"]).to eq(1) + end + it "should enqueue the destroyed hooks with tag filter for post events" do tag = Fabricate(:tag) Fabricate(:web_hook, tags: [tag])