# Distinguish private messages from posts?

**URL:** https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468
**Category:** Development
**Created:** [October 12, 2016, 9:12pm UTC](https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468 "2016-10-12T21:12:09Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![tim.smith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tim.smith/32/119954_2.png) [@tim.smith](https://meta.discourse.org/u/tim.smith)
#### Post date: [October 12, 2016, 9:12pm UTC](https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468/1 "2016-10-12T21:12:09Z")

</div>

In a server-side ruby plugin, I’m using `DiscourseEvent.on("post_created")` to get data on post replies. However, this is also being triggered when a person replies to a private message. Unfortunately, I can’t see any attributes on the event objects that allow me to distinguish a reply to a topic from a reply in a PM conversation. The `archetype` property is set to “regular” in both of these cases.

Does anyone have any ideas how to distinguish a private message reply from other messages?

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [October 12, 2016, 9:32pm UTC](https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468/2 "2016-10-12T21:32:06Z")

</div>

Something might be wrong, because the `topic.archetype == "private_message"` is the way to differentiate regular posts from PMs.

---

<div class="post-metadata">

### Author: ![tim.smith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tim.smith/32/119954_2.png) [@tim.smith](https://meta.discourse.org/u/tim.smith)
#### Post date: [October 12, 2016, 10:01pm UTC](https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468/3 "2016-10-12T22:01:53Z")

</div>

Yea, I’m seeing the _initial_ PM come through with that archetype, but replies after the initial message are “regular”.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [October 12, 2016, 10:08pm UTC](https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468/4 "2016-10-12T22:08:27Z")

</div>

But the archetype is on the topic, not on the post.

I just tried a new PM on my dev instance and the archetype is `private_message`.

---

<div class="post-metadata">

### Author: ![tim.smith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tim.smith/32/119954_2.png) [@tim.smith](https://meta.discourse.org/u/tim.smith)
#### Post date: [October 12, 2016, 10:09pm UTC](https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468/5 "2016-10-12T22:09:50Z")

</div>

> [@Falco](#):
>
> I just tried a new PM on my dev instance and the archetype is private\_message.

Yea, but what about subsequent messages in the PM conversation? Those have regular.

edit: I’m testing on 1.7.0.beta5

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [October 12, 2016, 10:11pm UTC](https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468/6 "2016-10-12T22:11:49Z")

</div>

> [@tim.smith](#):
>
> Yea, but what about subsequent messages in the PM conversation? Those have regular.

But isn’t the archetype on the **topic** instead of the post? In the database there is only one line per topic, and it’s set correctly. Where are you testing this info?

---

<div class="post-metadata">

### Author: ![tim.smith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tim.smith/32/119954_2.png) [@tim.smith](https://meta.discourse.org/u/tim.smith)
#### Post date: [October 12, 2016, 10:17pm UTC](https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468/7 "2016-10-12T22:17:17Z")

</div>

Here’s the code that I’m using to trace out the event objects: [Discourse-Webhooks/plugin.rb at master · purecloudlabs/Discourse-Webhooks · GitHub](https://github.com/MyPureCloud/Discourse-Webhooks/blob/master/plugin.rb#L85)

Here’s the result of that trace for a PM reply. I believe the topic is the second object in the params array. I’ve truncated the user object for brevity.

```plaintext
[
  {
    "id": 242,
    "user_id": 2,
    "topic_id": 177,
    "post_number": 3,
    "raw": "replying in a PM asdf1234",
    "cooked": "<p>replying in a PM asdf1234</p>",
    "created_at": "2016-10-12T21:03:33.334Z",
    "updated_at": "2016-10-12T21:03:33.334Z",
    "reply_to_post_number": null,
    "reply_count": 0,
    "quote_count": 0,
    "deleted_at": null,
    "off_topic_count": 0,
    "like_count": 0,
    "incoming_link_count": 0,
    "bookmark_count": 0,
    "avg_time": null,
    "score": null,
    "reads": 0,
    "post_type": 1,
    "vote_count": 0,
    "sort_order": 3,
    "last_editor_id": 2,
    "hidden": false,
    "hidden_reason_id": null,
    "notify_moderators_count": 0,
    "spam_count": 0,
    "illegal_count": 0,
    "inappropriate_count": 0,
    "last_version_at": "2016-10-12T21:03:33.350Z",
    "user_deleted": false,
    "reply_to_user_id": null,
    "percent_rank": 1,
    "notify_user_count": 0,
    "like_score": 0,
    "deleted_by_id": null,
    "edit_reason": null,
    "word_count": 5,
    "version": 1,
    "cook_method": 1,
    "wiki": false,
    "baked_at": "2016-10-12T21:03:33.334Z",
    "baked_version": 1,
    "hidden_at": null,
    "self_edits": 0,
    "reply_quoted": false,
    "via_email": false,
    "raw_email": null,
    "public_version": 1,
    "action_code": null
  },
  {
    "raw": "replying in a PM asdf1234",
    "topic_id": "177",
    "archetype": "regular",
    "category": "",
    "typing_duration_msecs": "1900",
    "composer_open_duration_msecs": "11839",
    "visible": true,
    "is_warning": false,
    "ip_address": "127.0.0.1",
    "user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36",
    "referrer": "https://developer.inindca.com/forum/t/hey-this-is-a-pm/177",
    "first_post_checks": true
  },
  {
    "id": 2,
    "username": "tim.smith",
    "avatar_template": "/forum/user_avatar/developer.inindca.com/tim.smith/{size}/58_1.png",
    "name": "Tim Smith",
    "last_posted_at": "2016-10-12T21:03:33.334Z",
    "last_seen_at": "2016-10-12T21:03:14.864Z"
  }
]

```

edit: here’s a screenshot showing that the post is indeed a reply to a PM:

 ![](https://global.discourse-cdn.com/meta/original/3X/7/0/70517ea419b060b315e097855fba7d9f7dd9de06.png)

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [October 12, 2016, 10:36pm UTC](https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468/8 "2016-10-12T22:36:38Z")

</div>

Hmmm I think this `params` variable isn’t used elsewhere.

And on brand new [official WebHooks for post created](https://github.com/discourse/discourse/blob/a300e01af364202d9e86b77f1ede0bf17214c3b7/app/models/web_hook.rb#L64) we only send down the topic id.

---

<div class="post-metadata">

### Author: ![tim.smith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tim.smith/32/119954_2.png) [@tim.smith](https://meta.discourse.org/u/tim.smith)
#### Post date: [October 12, 2016, 11:16pm UTC](https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468/9 "2016-10-12T23:16:59Z")

</div>

> [@Falco](#):
>
> Hmmm I think this params variable isn’t used elsewhere.

The params variable is the name of the variable in my plugin, not something in Discourse. It’s an array of the 3 parameters passed into a Discourse event, presumably via `DiscourseEvent.trigger(...)`. Here’s where it’s defined in my plugin: [Discourse-Webhooks/plugin.rb at master · purecloudlabs/Discourse-Webhooks · GitHub](https://github.com/MyPureCloud/Discourse-Webhooks/blob/master/plugin.rb#L34)

> [@Falco](#):
>
> And on brand new official WebHooks for post created we only send down the topic id.

I’m not using Discourse’s webhooks. I’m consuming events in my plugin that are raised by Discourse directly, parsing/formatting the event data, and sending my own webhook.

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [October 13, 2016, 7:27am UTC](https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468/10 "2016-10-13T07:27:11Z")

</div>

> [@tim.smith](#):
>
> . I’m consuming events in my plugin that are raised by Discourse directly, parsing/formatting the event data, and sending my own webhook.

In that case, you need to add post.topic.archetype as a field that gets sent.

Or, do `Guardian.new(plugin _user).can_see? post` and use the standard visibility checks.

---

<div class="post-metadata">

### Author: ![tim.smith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tim.smith/32/119954_2.png) [@tim.smith](https://meta.discourse.org/u/tim.smith)
#### Post date: [October 13, 2016, 1:32pm UTC](https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468/11 "2016-10-13T13:32:15Z")

</div>

> you need to add post.topic.archetype as a field that gets sent.

What do you mean? The field is there in the event data for PM replies. It’s just set to “regular” so I can’t distinguish it from a reply to a topic.

> Or, do Guardian.new(plugin \_user).can\_see? post and use the standard visibility checks.

Can you point me to some documentation on what Guardian is and why I need a new one? Also documentation for how to do a visibility check and which ones are standard?

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [October 13, 2016, 3:44pm UTC](https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468/12 "2016-10-13T15:44:10Z")

</div>

Are you confusing Topic archetype with Post post\_type?

Guardian files can be seen here

> **[discourse/lib/guardian at main · discourse/discourse](https://github.com/discourse/discourse/tree/main/lib/guardian)**
>
> main/lib/guardian

---

<div class="post-metadata">

### Author: ![tim.smith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tim.smith/32/119954_2.png) [@tim.smith](https://meta.discourse.org/u/tim.smith)
#### Post date: [October 13, 2016, 3:48pm UTC](https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468/13 "2016-10-13T15:48:34Z")

</div>

> [@Mittineague](#):
>
> Are you confusing Topic archetype with Post post\_type?

I don’t think so. Take a look at the JSON I posted above in [this post](https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468/7). The second JSON object in the array, which I think is the topic data, has `"archetype": "regular"`. The `post_type` property is always set to `1` in the first object in the array for all posts, which I think is the post data.

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [October 13, 2016, 4:02pm UTC](https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468/14 "2016-10-13T16:02:22Z")

</div>

Ah yes, now I see what you mean.

A post\_type “regular” and an archetype “regular” are not in the same set of definitions.  
For example, for topic the set includes regular, banner, private\_message but for a post the set includes regular, moderator\_action, system\_action

So yes, for a topic that is a private message it does seem that the archetype should be private\_message ant not regular.

Was this a topic that was admin wrenched into a message?

---

<div class="post-metadata">

### Author: ![tim.smith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tim.smith/32/119954_2.png) [@tim.smith](https://meta.discourse.org/u/tim.smith)
#### Post date: [October 13, 2016, 4:19pm UTC](https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468/15 "2016-10-13T16:19:18Z")

</div>

> [@Mittineague](#):
>
> Was this a topic that was admin wrenched into a message?

Nope. Just went to a user’s page and hit message. The first post does have private\_message set for the archetype, just not any of the subsequent replies in the PM; they’re all set to regular.

I did just find something really interesting. This is actually a UI bug, not a backend bug. When creating a reply, a POST is made to `{discourse site}/posts` with the following form data (formatted with linebreaks for your pleasure):

```plaintext
raw=this+is+another+reply+iok+asldfkjad
&unlist_topic=false
&category=
&topic_id=177
&is_warning=false
&whisper=false
&archetype=regular
&typing_duration_msecs=2000
&composer_open_duration_msecs=6168
&reply_to_post_number=6
&nested_post=true

```

Note how the UI is specifying `archetype=regular` in the post! I did some hacking and modified that same request to send `archetype=private_message`, and volia! The resulting event data has archetype set to private\_message. For fun, I set it to `archetype=shits_and_giggles`, and guess what? The resulting event also had archetype set to shits\_and\_giggles.

I’m rather surprised that UI code is providing data about the post and that discourse is sending that data back out without even validating it. It really seems like that data should be provided by the server, not the UI.

So, now that I’ve figured out where the “regular” value is coming from, could this be logged as a bug and fixed? Or point me to where the UI code is for this and I’ll see if I can figure out how to fix it?

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [October 13, 2016, 5:01pm UTC](https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468/16 "2016-10-13T17:01:32Z")

</div>

Interesting. But again, the best route is to simply reload the post from the database and pull all the fields you need from that.

Guardian is the class that takes care of all permission checks in Discourse.

---

<div class="post-metadata">

### Author: ![tim.smith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tim.smith/32/119954_2.png) [@tim.smith](https://meta.discourse.org/u/tim.smith)
#### Post date: [October 13, 2016, 5:04pm UTC](https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468/17 "2016-10-13T17:04:19Z")

</div>

> [@riking](#):
>
> the best route is to simply reload the post from the database

Seriously? What’s the point of sending data with an event if you shouldn’t use it or can’t rely on it being accurate? That’s really poor design.

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [October 13, 2016, 5:06pm UTC](https://meta.discourse.org/t/distinguish-private-messages-from-posts/51468/18 "2016-10-13T17:06:36Z")

</div>

Probably because nobody actually looked at what the event code was doing and asked whether it made sense? This isn’t exactly the most well-exercised portion of code in Discourse.

It’s also possible you’re printing out the PostCreator object, in which case, yeah, that’s supposed to have the raw form data.
