discourse/lib/reviewable/actions.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

72 lines
1.9 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "reviewable/collection"
class Reviewable < ActiveRecord::Base
class Actions < Reviewable::Collection
FIX: Don't mix up action labels between different reviewables (#23365) Currently, if the review queue has both a flagged post and a flagged chat message, one of the two will have some of the labels of their actions replaced by those of the other. In other words, the labels are getting mixed up. For example, a flagged chat message might show up with an action labelled "Delete post". This is happening because when using bundles, we are sending along the actions in a separate part of the response, so they can be shared by many reviewables. The bundles then index into this bag of actions by their ID, which is something generic describing the server action, e.g. "agree_and_delete". The problem here is the same action can have different labels depending on the type of reviewable. Now that the bag of actions contains multiple actions with the same ID, which one is chosen is arbitrary. I.e. it doesn't distinguish based on the type of the reviewable. This change adds an additional field to the actions, server_action, which now contains what used to be the ID. Meanwhile, the ID has been turned into a concatenation of the reviewable type and the server action, e.g. post-agree_and_delete. This still provides the upside of denormalizing the actions while allowing for different reviewable types to have different labels and descriptions. At first I thought I would prepend the reviewable type to the ID, but this doesn't work well because the ID is used on the server-side to determine which actions are possible, and these need to be shared between different reviewables. Hence the introduction of server_action, which now serves that purpose. I also thought about changing the way that the bundle indexes into the bag of actions, but this is happening through some EmberJS mechanism, so we don't own that code.
2023-09-05 22:57:30 -04:00
attr_reader :bundles, :reviewable
def initialize(reviewable, guardian, args = nil)
super(reviewable, guardian, args)
@bundles = []
end
# Add common actions here to make them easier for reviewables to re-use. If it's a
# one off, add it manually.
def self.common_actions
{
approve: Action.new(:approve, "thumbs-up", "reviewables.actions.approve.title"),
reject: Action.new(:reject, "thumbs-down", "reviewables.actions.reject.title"),
delete: Action.new(:delete, "trash-alt", "reviewables.actions.delete_single.title"),
}
end
class Bundle < Item
attr_accessor :icon, :label, :actions
def initialize(id, icon: nil, label: nil)
super(id)
@icon = icon
@label = label
@actions = []
end
end
class Action < Item
attr_accessor :icon,
:button_class,
:label,
:description,
:confirm_message,
:client_action,
:require_reject_reason,
:custom_modal
def initialize(id, icon = nil, button_class = nil, label = nil)
super(id)
@icon, @button_class, @label = icon, button_class, label
end
FIX: Don't mix up action labels between different reviewables (#23365) Currently, if the review queue has both a flagged post and a flagged chat message, one of the two will have some of the labels of their actions replaced by those of the other. In other words, the labels are getting mixed up. For example, a flagged chat message might show up with an action labelled "Delete post". This is happening because when using bundles, we are sending along the actions in a separate part of the response, so they can be shared by many reviewables. The bundles then index into this bag of actions by their ID, which is something generic describing the server action, e.g. "agree_and_delete". The problem here is the same action can have different labels depending on the type of reviewable. Now that the bag of actions contains multiple actions with the same ID, which one is chosen is arbitrary. I.e. it doesn't distinguish based on the type of the reviewable. This change adds an additional field to the actions, server_action, which now contains what used to be the ID. Meanwhile, the ID has been turned into a concatenation of the reviewable type and the server action, e.g. post-agree_and_delete. This still provides the upside of denormalizing the actions while allowing for different reviewable types to have different labels and descriptions. At first I thought I would prepend the reviewable type to the ID, but this doesn't work well because the ID is used on the server-side to determine which actions are possible, and these need to be shared between different reviewables. Hence the introduction of server_action, which now serves that purpose. I also thought about changing the way that the bundle indexes into the bag of actions, but this is happening through some EmberJS mechanism, so we don't own that code.
2023-09-05 22:57:30 -04:00
def server_action
id.split("-").last
end
end
def add_bundle(id, icon: nil, label: nil)
bundle = Bundle.new(id, icon: icon, label: label)
@bundles << bundle
bundle
end
def add(id, bundle: nil)
FIX: Don't mix up action labels between different reviewables (#23365) Currently, if the review queue has both a flagged post and a flagged chat message, one of the two will have some of the labels of their actions replaced by those of the other. In other words, the labels are getting mixed up. For example, a flagged chat message might show up with an action labelled "Delete post". This is happening because when using bundles, we are sending along the actions in a separate part of the response, so they can be shared by many reviewables. The bundles then index into this bag of actions by their ID, which is something generic describing the server action, e.g. "agree_and_delete". The problem here is the same action can have different labels depending on the type of reviewable. Now that the bag of actions contains multiple actions with the same ID, which one is chosen is arbitrary. I.e. it doesn't distinguish based on the type of the reviewable. This change adds an additional field to the actions, server_action, which now contains what used to be the ID. Meanwhile, the ID has been turned into a concatenation of the reviewable type and the server action, e.g. post-agree_and_delete. This still provides the upside of denormalizing the actions while allowing for different reviewable types to have different labels and descriptions. At first I thought I would prepend the reviewable type to the ID, but this doesn't work well because the ID is used on the server-side to determine which actions are possible, and these need to be shared between different reviewables. Hence the introduction of server_action, which now serves that purpose. I also thought about changing the way that the bundle indexes into the bag of actions, but this is happening through some EmberJS mechanism, so we don't own that code.
2023-09-05 22:57:30 -04:00
id = [reviewable.target_type&.underscore, id].compact_blank.join("-")
action = Actions.common_actions[id] || Action.new(id)
yield action if block_given?
@content << action
bundle ||= add_bundle(id)
bundle.actions << action
end
end
end