mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Add post_alerter_after_save_post
event (#14388)
Also promote the `create_notification_alert` and `push_notification` methods from instance methods to class methods so that plugins can call them. This is temporary until we add a more comprehensive API for extending `PostAlerter`.
This commit is contained in:
parent
a7c342ccb9
commit
37a3bf9c11
@ -13,6 +13,48 @@ class PostAlerter
|
|||||||
post
|
post
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.create_notification_alert(user:, post:, notification_type:, excerpt: nil, username: nil)
|
||||||
|
if post_url = post.url
|
||||||
|
payload = {
|
||||||
|
notification_type: notification_type,
|
||||||
|
post_number: post.post_number,
|
||||||
|
topic_title: post.topic.title,
|
||||||
|
topic_id: post.topic.id,
|
||||||
|
excerpt: excerpt || post.excerpt(400, text_entities: true, strip_links: true, remap_emoji: true),
|
||||||
|
username: username || post.username,
|
||||||
|
post_url: post_url
|
||||||
|
}
|
||||||
|
|
||||||
|
DiscourseEvent.trigger(:pre_notification_alert, user, payload)
|
||||||
|
MessageBus.publish("/notification-alert/#{user.id}", payload, user_ids: [user.id])
|
||||||
|
push_notification(user, payload)
|
||||||
|
DiscourseEvent.trigger(:post_notification_alert, user, payload)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.push_notification(user, payload)
|
||||||
|
return if user.do_not_disturb?
|
||||||
|
|
||||||
|
if user.push_subscriptions.exists?
|
||||||
|
Jobs.enqueue(:send_push_notification, user_id: user.id, payload: payload)
|
||||||
|
end
|
||||||
|
|
||||||
|
if SiteSetting.allow_user_api_key_scopes.split("|").include?("push") && SiteSetting.allowed_user_api_push_urls.present?
|
||||||
|
clients = user.user_api_keys
|
||||||
|
.joins(:scopes)
|
||||||
|
.where("user_api_key_scopes.name IN ('push', 'notifications')")
|
||||||
|
.where("push_url IS NOT NULL AND push_url <> ''")
|
||||||
|
.where("position(push_url IN ?) > 0", SiteSetting.allowed_user_api_push_urls)
|
||||||
|
.where("revoked_at IS NULL")
|
||||||
|
.order(client_id: :asc)
|
||||||
|
.pluck(:client_id, :push_url)
|
||||||
|
|
||||||
|
if clients.length > 0
|
||||||
|
Jobs.enqueue(:push_notification, clients: clients, payload: payload, user_id: user.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(default_opts = {})
|
def initialize(default_opts = {})
|
||||||
@default_opts = default_opts
|
@default_opts = default_opts
|
||||||
end
|
end
|
||||||
@ -115,6 +157,8 @@ class PostAlerter
|
|||||||
notify_first_post_watchers(post, watchers)
|
notify_first_post_watchers(post, watchers)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
DiscourseEvent.trigger(:post_alerter_after_save_post, post, new_record, notified)
|
||||||
end
|
end
|
||||||
|
|
||||||
def group_watchers(topic)
|
def group_watchers(topic)
|
||||||
@ -144,7 +188,7 @@ class PostAlerter
|
|||||||
|
|
||||||
# Don't notify the OP
|
# Don't notify the OP
|
||||||
user_ids -= [post.user_id]
|
user_ids -= [post.user_id]
|
||||||
users = User.where(id: user_ids)
|
users = User.where(id: user_ids).includes(:do_not_disturb_timings)
|
||||||
|
|
||||||
DiscourseEvent.trigger(:before_create_notifications_for_users, users, post)
|
DiscourseEvent.trigger(:before_create_notifications_for_users, users, post)
|
||||||
each_user_in_batches(users) do |user|
|
each_user_in_batches(users) do |user|
|
||||||
@ -466,45 +510,17 @@ class PostAlerter
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create_notification_alert(user:, post:, notification_type:, excerpt: nil, username: nil)
|
def create_notification_alert(user:, post:, notification_type:, excerpt: nil, username: nil)
|
||||||
if post_url = post.url
|
self.class.create_notification_alert(
|
||||||
payload = {
|
user: user,
|
||||||
notification_type: notification_type,
|
post: post,
|
||||||
post_number: post.post_number,
|
notification_type: notification_type,
|
||||||
topic_title: post.topic.title,
|
excerpt: excerpt,
|
||||||
topic_id: post.topic.id,
|
username: username
|
||||||
excerpt: excerpt || post.excerpt(400, text_entities: true, strip_links: true, remap_emoji: true),
|
)
|
||||||
username: username || post.username,
|
|
||||||
post_url: post_url
|
|
||||||
}
|
|
||||||
|
|
||||||
DiscourseEvent.trigger(:pre_notification_alert, user, payload)
|
|
||||||
MessageBus.publish("/notification-alert/#{user.id}", payload, user_ids: [user.id])
|
|
||||||
push_notification(user, payload)
|
|
||||||
DiscourseEvent.trigger(:post_notification_alert, user, payload)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def push_notification(user, payload)
|
def push_notification(user, payload)
|
||||||
return if user.do_not_disturb?
|
self.class.push_notification(user, payload)
|
||||||
|
|
||||||
if user.push_subscriptions.exists?
|
|
||||||
Jobs.enqueue(:send_push_notification, user_id: user.id, payload: payload)
|
|
||||||
end
|
|
||||||
|
|
||||||
if SiteSetting.allow_user_api_key_scopes.split("|").include?("push") && SiteSetting.allowed_user_api_push_urls.present?
|
|
||||||
clients = user.user_api_keys
|
|
||||||
.joins(:scopes)
|
|
||||||
.where("user_api_key_scopes.name IN ('push', 'notifications')")
|
|
||||||
.where("push_url IS NOT NULL AND push_url <> ''")
|
|
||||||
.where("position(push_url IN ?) > 0", SiteSetting.allowed_user_api_push_urls)
|
|
||||||
.where("revoked_at IS NULL")
|
|
||||||
.order(client_id: :asc)
|
|
||||||
.pluck(:client_id, :push_url)
|
|
||||||
|
|
||||||
if clients.length > 0
|
|
||||||
Jobs.enqueue(:push_notification, clients: clients, payload: payload, user_id: user.id)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def expand_group_mentions(groups, post)
|
def expand_group_mentions(groups, post)
|
||||||
@ -527,7 +543,7 @@ class PostAlerter
|
|||||||
groups = nil if groups.empty?
|
groups = nil if groups.empty?
|
||||||
|
|
||||||
if mentions.present?
|
if mentions.present?
|
||||||
users = User.where(username_lower: mentions).where.not(id: post.user_id)
|
users = User.where(username_lower: mentions).includes(:do_not_disturb_timings).where.not(id: post.user_id)
|
||||||
users = nil if users.empty?
|
users = nil if users.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -776,7 +792,7 @@ class PostAlerter
|
|||||||
def each_user_in_batches(users)
|
def each_user_in_batches(users)
|
||||||
# This is race-condition-safe, unlike #find_in_batches
|
# This is race-condition-safe, unlike #find_in_batches
|
||||||
users.pluck(:id).each_slice(USER_BATCH_SIZE) do |user_ids_batch|
|
users.pluck(:id).each_slice(USER_BATCH_SIZE) do |user_ids_batch|
|
||||||
User.where(id: user_ids_batch).each { |user| yield(user) }
|
User.where(id: user_ids_batch).includes(:do_not_disturb_timings).each { |user| yield(user) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user