DEV: Create post actions without creating a notification and store custom data. (#15397)

I plan to use this in an upcoming discourse-reactions PR, where I want to like a post without notifying the user, so I can instead create a reaction notification.

Additionally, we decouple the a11y attributes from the icon itself, which will let us extend the widget's icon without losing them.
This commit is contained in:
Roman Rizzi
2021-12-27 11:25:37 -03:00
committed by GitHub
parent 0b34d5ac6c
commit e005e3f153
5 changed files with 46 additions and 7 deletions

View File

@ -7,20 +7,21 @@ class PostActionCreator
# Shortcut methods for easier invocation
class << self
def create(created_by, post, action_key, message: nil, created_at: nil, reason: nil)
def create(created_by, post, action_key, message: nil, created_at: nil, reason: nil, silent: false)
new(
created_by,
post,
PostActionType.types[action_key],
message: message,
created_at: created_at,
reason: reason
reason: reason,
silent: silent
).perform
end
[:like, :off_topic, :spam, :inappropriate, :bookmark].each do |action|
define_method(action) do |created_by, post|
create(created_by, post, action)
define_method(action) do |created_by, post, silent = false|
create(created_by, post, action, silent: silent)
end
end
[:notify_moderators, :notify_user].each do |action|
@ -40,7 +41,8 @@ class PostActionCreator
flag_topic: false,
created_at: nil,
queue_for_review: false,
reason: nil
reason: nil,
silent: false
)
@created_by = created_by
@created_at = created_at || Time.zone.now
@ -62,6 +64,8 @@ class PostActionCreator
if reason.nil? && @queue_for_review
@reason = 'queued_by_staff'
end
@silent = silent
end
def post_can_act?
@ -113,7 +117,7 @@ class PostActionCreator
create_reviewable(result)
enforce_rules
UserActionManager.post_action_created(post_action)
PostActionNotifier.post_action_created(post_action)
PostActionNotifier.post_action_created(post_action) if !@silent
notify_subscribers
# agree with other flags