Post Destroyed Webhook Event

AI-generated summary

In a support thread titled ‘Post Destroyed Webhook Event’, thisnthat reports that the post_destroyed webhook event is not being triggered in Discourse v2.3.0.beta2, despite successfully triggering post_created, post_edited, and post_recovered events. After testing and reviewing logs and webhook event lists, thisnthat discovers the issue stems from tag_ids not being included in the payload during post deletion, which breaks webhooks relying on tags. They fix it locally by modifying lib/post_destroyer.rb to include tag_ids in the webhook enqueue call thisnthat. vinothkannans initially confirms the event works in the latest version and suggests checking webhook event logs and server logs for failures. After thisnthat provides detailed logs and confirms no errors, vinothkannans acknowledges the bug and links to the official fix commit: 167d85c. The issue is resolved in a later version of Discourse.

I am currently using v2.3.0.beta2 +46

I have a webhook setup for post events. The only event I do not appear to see is post_destroyed. I have testing deleting and restoring multiple posts and I see every post_recovered event, but I do not see any events for the post being deleted.

To reproduce, create a webhook and enable post events. Delete a post on a topic that would trigger that webhook and see no delete event created.

Can you repro this @vinothkannans?

No, this is working fine for me in latest version. I’m able to see triggered post_destroyed events in URL /admin/api/web_hooks/1/events. @thisnthat did you checked the events list page?

I have, this is the process I used to test it with a snippet of the header and payload of each event

I created the post

Header

User-Agent: Discourse/2.3.0.beta2
X-Discourse-Event-Id: 90
X-Discourse-Event-Type: post
X-Discourse-Event: post_created

Payload

{
  "post": {
    "id": 44144,
    ...
    "deleted_at": null,

I deleted the post and no event was triggered

I restored the post

Header

User-Agent: Discourse/2.3.0.beta2
X-Discourse-Event-Id: 91
X-Discourse-Event-Type: post
X-Discourse-Event: post_recovered

Payload

{
  "post": {
    "id": 44144,
    ...
    "deleted_at": null,

I deleted it again – No event

I edited the post

Header

User-Agent: Discourse/2.3.0.beta2
X-Discourse-Event-Id: 92
X-Discourse-Event-Type: post
X-Discourse-Event: post_edited

Payload

{
  "post": {
    "id": 44144,
    ....
    "deleted_at": "2019-02-28T17:07:28.427Z",

I restored the post

Header

User-Agent: Discourse/2.3.0.beta2
X-Discourse-Event-Id: 93
X-Discourse-Event-Type: post
X-Discourse-Event: post_recovered

Payload

{
  "post": {
    "id": 44144,
    ...
    "deleted_at": null,

I will recommend you to look at the URL /admin/api/web_hooks/WEBHOOK_ID/events for more info. If a triggered webhook event is failed then it will have the details. Also look at the /logs at the time of post_destroyed event. If you provide more details about the fail it will help me to debug the issue.

I have reviewed /admin/api/web_hooks/WEBHOOK_ID/events and the details of the events are in my previous post. Event id 90 was apost_ create, 91 post_recovered, 92 post_edited, 93 post recovered.

I tested again this morning. I reviewed the logs rails/production.log, rails/production_errors.log, rails/unicorn.stderr.log, rails/unicorn.stdout.log,

This is a snippet from the access logs for the previously mentioned events

[28/Feb/2019:17:04:50 +0000] "POST /posts HTTP/2.0" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36" "posts/create" 200 1517 "***/t/testing-discord-bot/10137/17" 1.268 1.268 "Thisnthat"
[28/Feb/2019:17:04:55 +0000] "PUT /posts/44144/recover HTTP/2.0" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36" "posts/recover" 200 1516 "***/t/testing-discord-bot/10137/16" 0.404 0.407 "Thisnthat"
[28/Feb/2019:17:07:28 +0000] "DELETE /posts/44144 HTTP/2.0" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36" "posts/destroy" 200 902 "***/t/testing-discord-bot/10137/16" 0.316 0.319 "Thisnthat"
[28/Feb/2019:17:07:35 +0000] "PUT /posts/44144 HTTP/2.0" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36" "posts/update" 200 1957 "***/t/testing-discord-bot/10137/16" 0.844 0.845 "Thisnthat"
[28/Feb/2019:17:12:55 +0000] "PUT /posts/44144/recover HTTP/2.0" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36" "posts/recover" 200 1602 "***/t/testing-discord-bot/10137/18" 0.520 0.520 "Thisnthat"

I also checked the /logs/ page and there was nothing related to this. To be sure I cleared the logs and tested it again and no error logs were generated.

I was able to track down the cause of this issue. The webhook I am using depends on a specific tag.

The issue appears to be in lib/post_destroyer.rb. tag_ids is not set on delete webhook events like it is when going through enqueue_post_hooks on the WebHook class. After making the following change, my destroy events started working correctly.

I replaced

    WebHook.enqueue_hooks(:post, :post_destroyed,
      id: @post.id,
      category_id: @post&.topic&.category_id,
      payload: payload
    ) if WebHook.active_web_hooks(:post).exists?

with

    WebHook.enqueue_hooks(:post, :post_destroyed,
      id: @post.id,
      category_id: @post&.topic&.category_id,
      tag_ids: @post&.topic&.tags&.pluck(:id),
      payload: payload
    ) if WebHook.active_web_hooks(:post).exists?

Thanks for the report. This is fixed in the commit

https://github.com/discourse/discourse/commit/167d85c21fd1d0ecab206c4cb45384bae41f259c