mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 01:53:39 +08:00
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:
parent
0b34d5ac6c
commit
e005e3f153
|
@ -106,7 +106,10 @@ export const DefaultNotificationItem = createWidget(
|
||||||
},
|
},
|
||||||
|
|
||||||
icon(notificationName) {
|
icon(notificationName) {
|
||||||
let icon = iconNode(`notification.${notificationName}`);
|
return iconNode(`notification.${notificationName}`);
|
||||||
|
},
|
||||||
|
|
||||||
|
_addA11yAttrsTo(icon, notificationName) {
|
||||||
icon.properties.attributes["aria-label"] = I18n.t(
|
icon.properties.attributes["aria-label"] = I18n.t(
|
||||||
`notifications.titles.${notificationName}`
|
`notifications.titles.${notificationName}`
|
||||||
);
|
);
|
||||||
|
@ -131,6 +134,7 @@ export const DefaultNotificationItem = createWidget(
|
||||||
let { data } = attrs;
|
let { data } = attrs;
|
||||||
let text = emojiUnescape(this.text(notificationName, data));
|
let text = emojiUnescape(this.text(notificationName, data));
|
||||||
let icon = this.icon(notificationName, data);
|
let icon = this.icon(notificationName, data);
|
||||||
|
this._addA11yAttrsTo(icon, notificationName);
|
||||||
|
|
||||||
const title = this.notificationTitle(notificationName, data);
|
const title = this.notificationTitle(notificationName, data);
|
||||||
|
|
||||||
|
|
|
@ -473,6 +473,10 @@ class PostAlerter
|
||||||
display_username: opts[:display_username] || post.user.username,
|
display_username: opts[:display_username] || post.user.username,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if opts[:custom_data]&.is_a?(Hash)
|
||||||
|
opts[:custom_data].each { |k, v| notification_data[k] = v }
|
||||||
|
end
|
||||||
|
|
||||||
if group = opts[:group]
|
if group = opts[:group]
|
||||||
notification_data[:group_id] = group.id
|
notification_data[:group_id] = group.id
|
||||||
notification_data[:group_name] = group.name
|
notification_data[:group_name] = group.name
|
||||||
|
|
|
@ -7,20 +7,21 @@ class PostActionCreator
|
||||||
|
|
||||||
# Shortcut methods for easier invocation
|
# Shortcut methods for easier invocation
|
||||||
class << self
|
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(
|
new(
|
||||||
created_by,
|
created_by,
|
||||||
post,
|
post,
|
||||||
PostActionType.types[action_key],
|
PostActionType.types[action_key],
|
||||||
message: message,
|
message: message,
|
||||||
created_at: created_at,
|
created_at: created_at,
|
||||||
reason: reason
|
reason: reason,
|
||||||
|
silent: silent
|
||||||
).perform
|
).perform
|
||||||
end
|
end
|
||||||
|
|
||||||
[:like, :off_topic, :spam, :inappropriate, :bookmark].each do |action|
|
[:like, :off_topic, :spam, :inappropriate, :bookmark].each do |action|
|
||||||
define_method(action) do |created_by, post|
|
define_method(action) do |created_by, post, silent = false|
|
||||||
create(created_by, post, action)
|
create(created_by, post, action, silent: silent)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
[:notify_moderators, :notify_user].each do |action|
|
[:notify_moderators, :notify_user].each do |action|
|
||||||
|
@ -40,7 +41,8 @@ class PostActionCreator
|
||||||
flag_topic: false,
|
flag_topic: false,
|
||||||
created_at: nil,
|
created_at: nil,
|
||||||
queue_for_review: false,
|
queue_for_review: false,
|
||||||
reason: nil
|
reason: nil,
|
||||||
|
silent: false
|
||||||
)
|
)
|
||||||
@created_by = created_by
|
@created_by = created_by
|
||||||
@created_at = created_at || Time.zone.now
|
@created_at = created_at || Time.zone.now
|
||||||
|
@ -62,6 +64,8 @@ class PostActionCreator
|
||||||
if reason.nil? && @queue_for_review
|
if reason.nil? && @queue_for_review
|
||||||
@reason = 'queued_by_staff'
|
@reason = 'queued_by_staff'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@silent = silent
|
||||||
end
|
end
|
||||||
|
|
||||||
def post_can_act?
|
def post_can_act?
|
||||||
|
@ -113,7 +117,7 @@ class PostActionCreator
|
||||||
create_reviewable(result)
|
create_reviewable(result)
|
||||||
enforce_rules
|
enforce_rules
|
||||||
UserActionManager.post_action_created(post_action)
|
UserActionManager.post_action_created(post_action)
|
||||||
PostActionNotifier.post_action_created(post_action)
|
PostActionNotifier.post_action_created(post_action) if !@silent
|
||||||
notify_subscribers
|
notify_subscribers
|
||||||
|
|
||||||
# agree with other flags
|
# agree with other flags
|
||||||
|
|
|
@ -80,6 +80,16 @@ describe PostActionCreator do
|
||||||
expect(notification_data['display_username']).to eq(user.username)
|
expect(notification_data['display_username']).to eq(user.username)
|
||||||
expect(notification_data['username2']).to eq(nil)
|
expect(notification_data['username2']).to eq(nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'does not create a notification if silent mode is enabled' do
|
||||||
|
PostActionNotifier.enable
|
||||||
|
|
||||||
|
expect(
|
||||||
|
PostActionCreator.new(user, post, like_type_id, silent: true).perform.success
|
||||||
|
).to eq(true)
|
||||||
|
|
||||||
|
expect(Notification.where(notification_type: Notification.types[:liked]).exists?).to eq(false)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "flags" do
|
context "flags" do
|
||||||
|
|
|
@ -1869,4 +1869,21 @@ describe PostAlerter do
|
||||||
expect(email).to eq(last_email)
|
expect(email).to eq(last_email)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'storing custom data' do
|
||||||
|
let(:custom_data) { 'custom_string' }
|
||||||
|
|
||||||
|
it 'stores custom data inside a notification' do
|
||||||
|
PostAlerter.new.create_notification(
|
||||||
|
admin,
|
||||||
|
Notification.types[:liked],
|
||||||
|
post,
|
||||||
|
custom_data: { custom_key: custom_data }
|
||||||
|
)
|
||||||
|
|
||||||
|
liked_notification = Notification.where(notification_type: Notification.types[:liked]).last
|
||||||
|
|
||||||
|
expect(liked_notification.data_hash[:custom_key]).to eq(custom_data)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user