Webhooks for the first post in a series: topic or post?

webhook

(Johnaschroeder) #1

When we first implemented webhooks, we were surprised to find that the first entry in a series of posts came through as post_created instead of topic_created. You can see a live example (minus the PIA) below. We wanted to treat the first post in a series (i.e. topic creation) differently than replies, so we looked at post_number and if it is equal to one we treat it as a new topic, if it’s > 1 it’s a reply.

This left me scratching my head. We can see the system creates a new topic_created header for backup, but that was it. Until today. We just saw a first post in a series by a user logged as topic_created. This was what we originally expected, but wasn’t happening until today. Any further information or explanation would be greatly appreciated.

Thanks!

			"X-Discourse-Instance": "http://discourse.petpartnersusa.com",
			"X-Discourse-Event-Id": "35",
			"X-Discourse-Event-Type": "post",
			"X-Discourse-Event": "post_created"
{
	"post": {
		"id": 1123,
		"name": "Name Here",
		"username": "usernamehere",
		"avatar_template": "/user_avatar/discourse.petpartnersusa.com/username/{size}/249_1.png",
		"created_at": "2017-11-21T21:51:15.649Z",
		"cooked": "<p>We're reaching out to see what other clinics are doing for pain<br>management after the recent restriction on Schedule II opioids. We<br>regularly used Hydromorphone and Fentanyl TDG patches on our orthopedic<br>patients to control post-op pain. I can't seem to locate any vendor that<br>has any in stock and the ETA on them being back are pretty vague.</p>\n\n<p>If anyone has any suggestions, we would appreciate it.</p>",
		"post_number": 1,
		"post_type": 1,
		"updated_at": "2017-11-21T21:51:15.649Z",
		"reply_count": 0,
		"reply_to_post_number": null,
		"quote_count": 0,
		"avg_time": null,
		"incoming_link_count": 0,
		"reads": 1,
		"score": 0,
		"yours": false,
		"topic_id": 561,
		"topic_slug": "schedule-11-restrictions",
		"topic_title": "Schedule 11 Restrictions",
		"display_username": "User Name",
		"primary_group_name": null,
		"primary_group_flair_url": null,
		"primary_group_flair_bg_color": null,
		"primary_group_flair_color": null,
		"version": 1,
		"user_title": null,
		"actions_summary": [{
				"id": 2,
				"can_act": true
			},
			{
				"id": 3,
				"can_act": true
			},
			{
				"id": 4,
				"can_act": true
			},
			{
				"id": 5,
				"hidden": true,
				"can_act": true
			},
			{
				"id": 6,
				"can_act": true
			},
			{
				"id": 7,
				"can_act": true
			},
			{
				"id": 8,
				"can_act": true
			}
		],
		"moderator": false,
		"admin": false,
		"staff": false,
		"user_id": 118,
		"hidden": false,
		"hidden_reason_id": null,
		"trust_level": 1,
		"deleted_at": null,
		"user_deleted": false,
		"edit_reason": null,
		"can_view_edit_history": true,
		"wiki": false
	}
}

Webhooks: how best to differentiate a PM from a public post
(Johnaschroeder) #2

A bit of additional information:

  • Impersonating the user above lists this post in her setttings/activities under topics, but not under replies
  • The topic_created webhook we saw for the public user-generated post today did not have an associated post_created webhook where post_number = 1
  • We have selected topic events and post events, but not users in the api/webhook settings

One thought that might help is to keep consistent references, preferably topics and replies, and do away with the entire post.

Hmmm, an ask the duck moment. Are topics and posts in the API intended to be hierarchical/mutually exclusive? ie topics gets me only topics created, posts gets me topics + replies, thus the different names? Thus posts are intended to give more detail than topics, and leads to unexpected behaviour if they are?


(Alan Tan) #3

When you create a topic, you should receive a topic_created webhook and a post_created webhook. I’m getting that in dev, are you not getting it on your instance?


(Alan Tan) #4

You can also verify if the web hooks are getting sent properly at /admin/api/web_hooks.


(Johnaschroeder) #5

We never saw any user generated topic_created web hooks at all until yesterday in either our logs or in /admin/api/web_hooks. I checked /admin/api/web_hooks for a day on either side of the above post_created web hook with post_number=1. There was no corresponding topic_created.

We turned on a web hook when we kicked off development. We just caught them and wrote the payload out to a log, then cross-checked that with /admin/api/web_hooks to make sure we weren’t missing anything.


(Alan Tan) #6

I’m abit confused here. What do you mean by “user generated topic_created web hooks”? When you said “until” yesterday", do you mean you weren’t getting the topic_created web hooks until yesterday where they started appearing?


(Johnaschroeder) #7

Exactly. We did not see topic_created for the first post in a topic, only post_created with post_number=1. we looked in admin/api//webhooks for 24 hours on either side of the post i documented above as well as in our logs. Then yesterday it reversed: we got a topic_created but no post_created with post_number =1.