# No email notifications for posts that require approval

**URL:** https://meta.discourse.org/t/no-email-notifications-for-posts-that-require-approval/48792
**Category:** Bug
**Created:** [Août 18, 2016, 10:34 UTC](https://meta.discourse.org/t/no-email-notifications-for-posts-that-require-approval/48792 "2016-08-18T10:34:10Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![tkrunning](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tkrunning/32/119481_2.png) [@tkrunning](https://meta.discourse.org/u/tkrunning)
#### Post date: [Août 18, 2016, 10:34 UTC](https://meta.discourse.org/t/no-email-notifications-for-posts-that-require-approval/48792/1 "2016-08-18T10:34:10Z")

</div>

Continuing the discussion from [Get notified of new topics, but not posts](https://meta.discourse.org/t/get-notified-of-new-topics-but-not-posts/12809/66):

We’re having issues with only some new topics generating emails to people watching a particular category.

Thanks @eviltrout for having a look at this initially. After some additional debugging it seems the issue is not related to to “Watching first post” users only, but also users that are “Watching” the category. However, it seems to be working for mailing list mode users, as well as for people watching that particular thread.

I think the issue is related to posts ending up in the moderation queue. We currently moderate the first 5 posts a new users submits (approve post count = 5), and previously we also had “approve unless trust level = 2”, but turned that off now for debugging:

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

I would expect that once a post in the moderation queue is approved, that it would trigger the same email notifications as if it was posted without being subject to moderation. But that does not seem to be the case.

Currently:

- If a post is not subject to moderation: Triggers all expected email notifications (mailing list, watching category, watching first post, watching topic)
- If a post is subject to moderation: After being approved, only trigger some email notifications: (mailing list, watching topic)

I assume this is a bug?

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [Août 18, 2016, 5:30 UTC](https://meta.discourse.org/t/no-email-notifications-for-posts-that-require-approval/48792/2 "2016-08-18T17:30:07Z")

</div>

Are you 100% certain of this?

I just spent a while trying to reproduce this but couldn’t.

Approving queued posts goes through our `PostCreator` object and in tests I was able to confirm approving a post entered the code path for watching the first post.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [Août 18, 2016, 7:20 UTC](https://meta.discourse.org/t/no-email-notifications-for-posts-that-require-approval/48792/3 "2016-08-18T19:20:48Z")

</div>

Are you sure those users are not active on the website? Discourse will not email someone who is currently browsing the website, because we would be double notifying them.

---

<div class="post-metadata">

### Author: ![tkrunning](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tkrunning/32/119481_2.png) [@tkrunning](https://meta.discourse.org/u/tkrunning)
#### Post date: [Août 19, 2016, 11:41 UTC](https://meta.discourse.org/t/no-email-notifications-for-posts-that-require-approval/48792/4 "2016-08-19T11:41:13Z")

</div>

Yes, I’m 100% sure they are not all active (we’re talking hundreds or thousands of users per category). I’ve also checked the box for a couple users that they’ll receive emails even when active on the site—still nothing (except when the poster is TL2+).

We’re running v1.6.0.beta12 +64, if that matters. I’ll ask one of our devs to chime in here.

---

<div class="post-metadata">

### Author: ![msokk](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/msokk/32/121584_2.png) [@msokk](https://meta.discourse.org/u/msokk)
#### Post date: [Août 23, 2016, 12:19 UTC](https://meta.discourse.org/t/no-email-notifications-for-posts-that-require-approval/48792/5 "2016-08-23T12:19:32Z")

</div>

I looked into the code and after some debugging, found out that [PostAlert](https://github.com/discourse/discourse/blob/master/app/jobs/regular/post_alert.rb#L6) job skipped running the  
`PostAlerter.post_created(post) if post && post.topic` line as the post was not yet commited to database due to [QueuedPost wrapping the whole thing into a transaction](https://github.com/discourse/discourse/blob/master/app/models/queued_post.rb#L64).

I’ll be delaying the jobs by a few seconds as a workaround, but I think that is not a real fix to this or is it?

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [Août 23, 2016, 3:31 UTC](https://meta.discourse.org/t/no-email-notifications-for-posts-that-require-approval/48792/6 "2016-08-23T15:31:10Z")

</div>

Does the above help you repro @eviltrout?

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [Août 23, 2016, 4:35 UTC](https://meta.discourse.org/t/no-email-notifications-for-posts-that-require-approval/48792/7 "2016-08-23T16:35:04Z")

</div>

This is almost certainly the cause, thanks! We’ve seen this before where transactions aren’t complete before sidekiq processes the jobs (because it’s too fast).

Delaying by a few seconds is a bit of a dangerous fix as if the database is slow or if there is another delay it will continue to break.

It seems `PostCreator` is not transaction safe. Perhaps we should adjust it so that it only performs the enqueing after the post/topic is committed?

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [Août 23, 2016, 8:09 UTC](https://meta.discourse.org/t/no-email-notifications-for-posts-that-require-approval/48792/8 "2016-08-23T20:09:06Z")

</div>

I briefly looked into what was involved with `PostCreator` to detect whether it was in a transaction and to enqueue the jobs afterwards but it seemed like a nightmare due to Rails’ internals. I think long term there is a better way to structure the `PostCreator` object to prevent this, but the following fix should solve this particular case:

[https://github.com/discourse/discourse/commit/2cb4cb7b7286f8bcfc9da094b35c0aedf7d7ce0a](https://github.com/discourse/discourse/commit/2cb4cb7b7286f8bcfc9da094b35c0aedf7d7ce0a)

---

<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: [Août 23, 2016, 10:15 UTC](https://meta.discourse.org/t/no-email-notifications-for-posts-that-require-approval/48792/9 "2016-08-23T22:15:20Z")

</div>

Can you add that to the large comment at the top of post\_creator.rb?

skip\_jobs: This is required to be `true` if you’re running PostCreator in a transaction. Call #enqueue\_jobs after the transaction finishes.

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [Août 24, 2016, 3:08 UTC](https://meta.discourse.org/t/no-email-notifications-for-posts-that-require-approval/48792/10 "2016-08-24T15:08:39Z")

</div>

Good call:

[https://github.com/discourse/discourse/commit/79245a25a38ab556eeb0c36ea25faa48947cc8d8](https://github.com/discourse/discourse/commit/79245a25a38ab556eeb0c36ea25faa48947cc8d8)

Obligatory “Code Comments” meme:

 ![](https://global.discourse-cdn.com/meta/original/3X/a/b/ab1555efeb0a5d2e5415491f2874a98c1b4cafe5.jpg)
