mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 23:43:43 +08:00
e82e255531
### Why? Before, all flags were static. Therefore, they were stored in class variables and serialized by SiteSerializer. Recently, we added an option for admins to add their own flags or disable existing flags. Therefore, the class variable had to be dropped because it was unsafe for a multisite environment. However, it started causing performance problems. ### Solution When a new Flag system is used, instead of using PostActionType, we can serialize Flags and use fragment cache for performance reasons. At the same time, we are still supporting deprecated `replace_flags` API call. When it is used, we fall back to the old solution and the admin cannot add custom flags. In a couple of months, we will be able to drop that API function and clean that code properly. However, because it may still be used, redis cache was introduced to improve performance. To test backward compatibility you can add this code to any plugin ```ruby replace_flags do |flag_settings| flag_settings.add( 4, :inappropriate, topic_type: true, notify_type: true, auto_action_type: true, ) flag_settings.add(1001, :trolling, topic_type: true, notify_type: true, auto_action_type: true) end ```
100 lines
2.5 KiB
Ruby
100 lines
2.5 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class PostActionDestroyer
|
|
class DestroyResult < PostActionResult
|
|
attr_accessor :post
|
|
end
|
|
|
|
def initialize(destroyed_by, post, post_action_type_id, opts = {})
|
|
@destroyed_by, @post, @post_action_type_id, @opts =
|
|
destroyed_by,
|
|
post,
|
|
post_action_type_id,
|
|
opts
|
|
end
|
|
|
|
def self.destroy(destroyed_by, post, action_key, opts = {})
|
|
new(destroyed_by, post, PostActionType.types[action_key], opts).perform
|
|
end
|
|
|
|
def post_action_type_view
|
|
@post_action_type_view ||= PostActionTypeView.new
|
|
end
|
|
|
|
def perform
|
|
result = DestroyResult.new
|
|
|
|
if @post.blank?
|
|
result.not_found = true
|
|
return result
|
|
end
|
|
|
|
finder =
|
|
PostAction.where(user: @destroyed_by, post: @post, post_action_type_id: @post_action_type_id)
|
|
finder = finder.with_deleted if @destroyed_by.staff?
|
|
post_action = finder.first
|
|
|
|
if post_action.blank?
|
|
result.not_found = true
|
|
return result
|
|
end
|
|
|
|
unless @opts[:skip_delete_check] == true || guardian.can_delete?(post_action)
|
|
result.forbidden = true
|
|
result.add_error(I18n.t("invalid_access"))
|
|
return result
|
|
end
|
|
|
|
RateLimiter.new(
|
|
@destroyed_by,
|
|
"post_action-#{@post.id}_#{@post_action_type_id}",
|
|
4,
|
|
1.minute,
|
|
).performed!
|
|
|
|
post_action.remove_act!(@destroyed_by)
|
|
post_action.post.unhide! if post_action.staff_took_action
|
|
if @post_action_type_id == post_action_type_view.types[:like]
|
|
GivenDailyLike.decrement_for(@destroyed_by.id)
|
|
end
|
|
|
|
case @post_action_type_id
|
|
when *post_action_type_view.notify_flag_type_ids
|
|
DiscourseEvent.trigger(:flag_destroyed, post_action, self)
|
|
when post_action_type_view.types[:like]
|
|
DiscourseEvent.trigger(:like_destroyed, post_action, self)
|
|
end
|
|
|
|
UserActionManager.post_action_destroyed(post_action)
|
|
PostActionNotifier.post_action_deleted(post_action)
|
|
result.success = true
|
|
result.post = @post.reload
|
|
|
|
notify_subscribers
|
|
|
|
result
|
|
end
|
|
|
|
protected
|
|
|
|
def self.notify_types
|
|
@notify_types ||= PostActionType.notify_flag_types.keys
|
|
end
|
|
|
|
def notify_subscribers
|
|
name = post_action_type_view.types[@post_action_type_id]
|
|
if name == :like
|
|
@post.publish_change_to_clients!(
|
|
:unliked,
|
|
{ likes_count: @post.like_count, user_id: @destroyed_by.id },
|
|
)
|
|
elsif self.class.notify_types.include?(name)
|
|
@post.publish_change_to_clients!(:acted)
|
|
end
|
|
end
|
|
|
|
def guardian
|
|
@guardian ||= Guardian.new(@destroyed_by)
|
|
end
|
|
end
|