# PostAlerter after\_save\_post 行为

**URL:** <https://meta.discourse.org/t/postalerter-after-save-post-behaviour/140537>\
**Category:** Development\
**Created:** [2020年二月4日 04:14 UTC](https://meta.discourse.org/t/postalerter-after-save-post-behaviour/140537 "2020-02-04T04:14:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)\
**Post date:** [2020年二月4日 04:14 UTC](https://meta.discourse.org/t/postalerter-after-save-post-behaviour/140537/1 "2020-02-04T04:14:28Z")

</div>

[PostAlerter](https://github.com/discourse/discourse/blob/master/app/services/post_alerter.rb) 包含一套检查和触发机制，用于在常规帖子保存后发送通知，其起点是 [after\_save\_post](https://github.com/discourse/discourse/blob/master/app/services/post_alerter.rb#L51) 方法。

我一直在为 [Follow 插件](https://github.com/paviliondev/discourse-follow) 研究这个方法（具体可参见 [discourse-follow/plugin.rb at main · discourse/discourse-follow · GitHub](https://github.com/paviliondev/discourse-follow/blob/master/plugin.rb#L111%EF%BC%89%EF%BC%8C%E5%B9%B6%E6%9C%89%E4%B8%80%E4%BA%9B%E6%83%B3%E6%B3%95%E5%92%8C%E9%97%AE%E9%A2%98%EF%BC%8C%E5%BE%88%E5%B8%8C%E6%9C%9B%E8%83%BD%E5%90%AC%E5%90%AC%E5%A4%A7%E5%AE%B6%E7%9A%84%E6%84%8F%E8%A7%81%E3%80%82)

鉴于 PostAlerter 的性质，我认为这次讨论的意义超出了 Follow 插件本身。我可以看到下面的建议同样适用于 assign 插件、polls 插件以及其他插件。

我目前的思路如下（我很乐意针对以下任何一点提交 PR）：

1. 在 `after_save_post` 方法中添加一个钩子或事件触发器，允许插件在该方法的检查和计数上下文中添加（或排除）需要发送通知的用户。发送通知时，有三个关键考量：

2. 对通知类型的调整使用。目前 PostAlerter 中使用了几个通知类型常量，并在特定的检查和计数中进行迭代或过滤，例如 `NOTIFIABLE_TYPES` 和 `COLLAPSED_NOTIFIABLE_TYPES`。
