FIX: Do not call :post_edited webhook twice when editing OP (PR #13112)

When editing the first post for the topic we do two AJAX requests to two separate controllers in this order:

PUT /t/topic-name PUT /posts/2489523

This causes two post revisor calls, which end up triggering the :post_edited DiscourseEvent twice. This is then picked up and sent as a WebHook event twice. However we do not need to send a :post_edited webhook event if the first post is being edited and topic_changed is true from the :post_edited DiscourseEvent, because a second event will shortly come through for just the post.

See Post webhook fires two times on post_edited for first post in a topic - bug - Discourse Meta

Continued on from FIX: prevent duplicate `post_edited` webhooks on first post edit. by vinothkannans · Pull Request #10590 · discourse/discourse · GitHub

GitHub

@SamSaffron I am just a little bit worried that there may be people out there relying on the double :post_edited webhook event. Do you think we should make a post on meta about it to allow people to update their expectations and subscribe to the :topic_edited and :post_edited hooks which is the correct way?

Here is a comparison of the :post_edited and :topic_edited webhook event:

# POST EDITED
[{"retry"=>true,
  "queue"=>"default",
  "class"=>"Jobs::EmitWebHookEvent",
  "args"=>
   [{"id"=>26240,
     "category_id"=>1,
     "tag_ids"=>[],
     "payload"=>
      "{\"id\":26240,\"name\":\"Bruce Wayne\",\"username\":\"bruce8\",\"avatar_template\":\"/letter_avatar_proxy/v4/letter/b/45deac/{size}.png\",\"created_at\":\"2021-05-21T05:45:25.988Z\",\"cooked\":\"\\u003cp\\u003eHello world\\u003c/p\\u003e\",\"post_number\":1,\"post_type\":1,\"updated_at\":\"2021-05-21T05:45:26.082Z\",\"reply_count\":0,\"reply_to_post_number\":null,\"quote_count\":0,\"incoming_link_count\":0,\"reads\":0,\"score\":0,\"topic_id\":24274,\"topic_slug\":\"this-is-a-new-title-for-the-topic\",\"topic_title\":\"This is a new title for the topic\",\"category_id\":1,\"display_username\":\"Bruce Wayne\",\"primary_group_name\":null,\"version\":2,\"user_title\":null,\"bookmarked\":false,\"raw\":\"Hello world\",\"moderator\":false,\"admin\":false,\"staff\":false,\"user_id\":9,\"hidden\":false,\"trust_level\":1,\"deleted_at\":null,\"user_deleted\":false,\"edit_reason\":null,\"wiki\":false,\"reviewable_id\":null,\"reviewable_score_count\":0,\"reviewable_score_pending_count\":0,\"topic_posts_count\":1,\"topic_filtered_posts_count\":1,\"topic_archetype\":\"regular\",\"category_slug\":\"uncategorized\"}",
     "web_hook_id"=>142,
     "event_name"=>"post_edited",
     "event_type"=>"post",
     "current_site_id"=>"default"}],
  "jid"=>"51a9952f3991dd71809cc967",
  "created_at"=>1621575926.1154542,
  "enqueued_at"=>1621575926.1154842},


# TOPIC EDITED
 {"retry"=>true,
  "queue"=>"default",
  "class"=>"Jobs::EmitWebHookEvent",
  "args"=>
   [{"id"=>24274,
     "category_id"=>1,
     "tag_ids"=>[],
     "payload"=>
      "{\"tags\":[],\"id\":24274,\"title\":\"This is a new title for the topic\",\"fancy_title\":\"This is a new title for the topic\",\"posts_count\":1,\"created_at\":\"2021-05-21T05:45:25.864Z\",\"views\":0,\"reply_count\":0,\"like_count\":0,\"last_posted_at\":null,\"visible\":true,\"closed\":false,\"archived\":false,\"archetype\":\"regular\",\"slug\":\"this-is-a-new-title-for-the-topic\",\"category_id\":1,\"word_count\":null,\"deleted_at\":null,\"user_id\":2,\"featured_link\":null,\"pinned_globally\":false,\"pinned_at\":null,\"pinned_until\":null,\"unpinned\":null,\"pinned\":false,\"highest_post_number\":1,\"deleted_by\":null,\"has_deleted\":false,\"bookmarked\":false,\"participant_count\":1,\"thumbnails\":null,\"created_by\":{\"id\":2,\"username\":\"bruce1\",\"name\":\"Bruce Wayne\",\"avatar_template\":\"/letter_avatar_proxy/v4/letter/b/9de053/{size}.png\"},\"last_poster\":{\"id\":2,\"username\":\"bruce1\",\"name\":\"Bruce Wayne\",\"avatar_template\":\"/letter_avatar_proxy/v4/letter/b/9de053/{size}.png\"}}",
     "web_hook_id"=>143,
     "event_name"=>"topic_edited",
     "event_type"=>"topic",
     "current_site_id"=>"default"}],
  "jid"=>"e9054be7020f9d2dd698d1bb",
  "created_at"=>1621575926.138321,
  "enqueued_at"=>1621575926.1383522}]

You can see in the :post_edited event it has stuff like the category_id and the topic_title, so people could conceivably be watching only that event for topic changes.