FIX: topic post counts for webhook post_destroyed event (#29853)

* FIX: topic post counts for webhook post_destroyed event

- Generate webhook data after posts are destroyed
- Don't count user_deleted posts

* Remove unnecessary conditional
This commit is contained in:
Angus McLeod 2024-11-27 20:36:51 +01:00 committed by GitHub
parent 8c311dcbd5
commit 6acf673f8d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 37 additions and 15 deletions

View File

@ -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

View File

@ -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

View File

@ -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])