2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-31 03:04:05 +08:00
|
|
|
class PostActionCreator
|
2019-01-04 01:03:01 +08:00
|
|
|
class CreateResult < PostActionResult
|
|
|
|
attr_accessor :post_action, :reviewable, :reviewable_score
|
|
|
|
end
|
|
|
|
|
|
|
|
# Shortcut methods for easier invocation
|
|
|
|
class << self
|
2021-12-27 22:25:37 +08:00
|
|
|
def create(
|
|
|
|
created_by,
|
|
|
|
post,
|
|
|
|
action_key,
|
|
|
|
message: nil,
|
|
|
|
created_at: nil,
|
|
|
|
reason: nil,
|
|
|
|
silent: false
|
|
|
|
)
|
2020-06-02 23:49:02 +08:00
|
|
|
new(
|
|
|
|
created_by,
|
|
|
|
post,
|
|
|
|
PostActionType.types[action_key],
|
|
|
|
message: message,
|
|
|
|
created_at: created_at,
|
2021-12-27 22:25:37 +08:00
|
|
|
reason: reason,
|
|
|
|
silent: silent,
|
2020-06-02 23:49:02 +08:00
|
|
|
).perform
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|
|
|
|
|
2022-05-10 08:42:18 +08:00
|
|
|
%i[like off_topic spam inappropriate].each do |action|
|
2021-12-27 22:25:37 +08:00
|
|
|
define_method(action) do |created_by, post, silent = false|
|
|
|
|
create(created_by, post, action, silent: silent)
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|
|
|
|
end
|
2024-02-07 10:12:22 +08:00
|
|
|
%i[notify_moderators notify_user illegal].each do |action|
|
2019-01-04 01:03:01 +08:00
|
|
|
define_method(action) do |created_by, post, message = nil|
|
|
|
|
create(created_by, post, action, message: message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(
|
|
|
|
created_by,
|
|
|
|
post,
|
|
|
|
post_action_type_id,
|
|
|
|
is_warning: false,
|
|
|
|
message: nil,
|
|
|
|
take_action: false,
|
|
|
|
flag_topic: false,
|
2020-06-02 23:49:02 +08:00
|
|
|
created_at: nil,
|
2021-03-11 19:21:24 +08:00
|
|
|
queue_for_review: false,
|
2021-12-27 22:25:37 +08:00
|
|
|
reason: nil,
|
|
|
|
silent: false
|
2019-01-04 01:03:01 +08:00
|
|
|
)
|
|
|
|
@created_by = created_by
|
|
|
|
@created_at = created_at || Time.zone.now
|
2015-12-31 03:04:05 +08:00
|
|
|
|
|
|
|
@post = post
|
2019-01-04 01:03:01 +08:00
|
|
|
@post_action_type_id = post_action_type_id
|
FIX: serialize Flags instead of PostActionType (#28362)
### 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
```
2024-08-14 10:13:46 +08:00
|
|
|
@post_action_type_view = PostActionTypeView.new
|
|
|
|
@post_action_name = @post_action_type_view.types[@post_action_type_id]
|
2019-01-04 01:03:01 +08:00
|
|
|
|
|
|
|
@is_warning = is_warning
|
|
|
|
@take_action = take_action && guardian.is_staff?
|
|
|
|
|
|
|
|
@message = message
|
|
|
|
@flag_topic = flag_topic
|
|
|
|
@meta_post = nil
|
2021-03-11 19:21:24 +08:00
|
|
|
|
2020-06-02 23:49:02 +08:00
|
|
|
@reason = reason
|
2021-03-11 19:21:24 +08:00
|
|
|
@queue_for_review = queue_for_review
|
|
|
|
|
|
|
|
@reason = "queued_by_staff" if reason.nil? && @queue_for_review
|
2021-12-27 22:25:37 +08:00
|
|
|
|
|
|
|
@silent = silent
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|
|
|
|
|
2020-03-04 08:56:37 +08:00
|
|
|
def post_can_act?
|
|
|
|
guardian.post_can_act?(
|
2019-01-04 01:03:01 +08:00
|
|
|
@post,
|
|
|
|
@post_action_name,
|
|
|
|
opts: {
|
|
|
|
is_warning: @is_warning,
|
2023-09-28 14:53:48 +08:00
|
|
|
taken_actions: taken_actions,
|
2019-01-04 01:03:01 +08:00
|
|
|
},
|
|
|
|
)
|
2020-03-04 08:56:37 +08:00
|
|
|
end
|
|
|
|
|
2023-09-28 14:53:48 +08:00
|
|
|
def taken_actions
|
|
|
|
return @taken_actions if defined?(@taken_actions)
|
|
|
|
@taken_actions = PostAction.counts_for([@post].compact, @created_by)[@post&.id]
|
|
|
|
end
|
|
|
|
|
2020-03-04 08:56:37 +08:00
|
|
|
def perform
|
|
|
|
result = CreateResult.new
|
|
|
|
|
2021-03-11 19:21:24 +08:00
|
|
|
if !post_can_act? || (@queue_for_review && !guardian.is_staff?)
|
2019-01-04 01:03:01 +08:00
|
|
|
result.forbidden = true
|
2023-09-28 14:53:48 +08:00
|
|
|
|
FIX: serialize Flags instead of PostActionType (#28362)
### 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
```
2024-08-14 10:13:46 +08:00
|
|
|
if taken_actions&.keys&.include?(@post_action_type_view.types[@post_action_name])
|
2023-09-28 14:53:48 +08:00
|
|
|
result.add_error(I18n.t("action_already_performed"))
|
|
|
|
else
|
|
|
|
result.add_error(I18n.t("invalid_access"))
|
|
|
|
end
|
2019-01-04 01:03:01 +08:00
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
PostAction.limit_action!(@created_by, @post, @post_action_type_id)
|
|
|
|
|
2020-02-15 00:43:48 +08:00
|
|
|
reviewable = Reviewable.includes(:reviewable_scores).find_by(target: @post)
|
|
|
|
|
|
|
|
if reviewable && flagging_post? && cannot_flag_again?(reviewable)
|
|
|
|
result.add_error(I18n.t("reviewables.already_handled"))
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
# create meta topic / post if needed
|
2024-02-07 10:12:22 +08:00
|
|
|
if @message.present? &&
|
FIX: serialize Flags instead of PostActionType (#28362)
### 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
```
2024-08-14 10:13:46 +08:00
|
|
|
(@post_action_type_view.additional_message_types.keys | %i[spam illegal]).include?(
|
2024-07-18 08:10:22 +08:00
|
|
|
@post_action_name,
|
|
|
|
)
|
2019-01-04 01:03:01 +08:00
|
|
|
creator = create_message_creator
|
2023-10-11 01:21:57 +08:00
|
|
|
# We need to check if the creator exists because it's possible `create_message_creator` returns nil
|
|
|
|
# in the event that a `post_action_notify_user_handler` evaluated to false, haulting the post creation.
|
|
|
|
if creator
|
|
|
|
post = creator.create
|
|
|
|
if creator.errors.present?
|
|
|
|
result.add_errors_from(creator)
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
@meta_post = post
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
post_action = create_post_action
|
|
|
|
|
|
|
|
if post_action.blank? || post_action.errors.present?
|
|
|
|
result.add_errors_from(post_action)
|
|
|
|
else
|
|
|
|
create_reviewable(result)
|
|
|
|
enforce_rules
|
|
|
|
UserActionManager.post_action_created(post_action)
|
2021-12-27 22:25:37 +08:00
|
|
|
PostActionNotifier.post_action_created(post_action) if !@silent
|
2019-01-04 01:03:01 +08:00
|
|
|
notify_subscribers
|
|
|
|
|
2019-04-09 00:39:18 +08:00
|
|
|
# agree with other flags
|
|
|
|
if @take_action && reviewable = @post.reviewable_flag
|
|
|
|
result.reviewable.perform(@created_by, :agree_and_keep)
|
|
|
|
post_action.try(:update_counters)
|
|
|
|
end
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
result.success = true
|
|
|
|
result.post_action = post_action
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordNotUnique
|
2021-05-21 09:43:47 +08:00
|
|
|
# If the user already performed this action, it's probably due to a different browser tab
|
2019-01-04 01:03:01 +08:00
|
|
|
# or non-debounced clicking. We can ignore.
|
|
|
|
result.success = true
|
|
|
|
result.post_action =
|
|
|
|
PostAction.find_by(
|
|
|
|
user: @created_by,
|
|
|
|
post: @post,
|
|
|
|
post_action_type_id: @post_action_type_id,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
2015-12-31 03:04:05 +08:00
|
|
|
end
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
private
|
|
|
|
|
2020-02-15 00:43:48 +08:00
|
|
|
def flagging_post?
|
FIX: serialize Flags instead of PostActionType (#28362)
### 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
```
2024-08-14 10:13:46 +08:00
|
|
|
@post_action_type_view.notify_flag_type_ids.include?(@post_action_type_id)
|
2020-02-15 00:43:48 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def cannot_flag_again?(reviewable)
|
FIX: serialize Flags instead of PostActionType (#28362)
### 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
```
2024-08-14 10:13:46 +08:00
|
|
|
return false if @post_action_type_id == @post_action_type_view.types[:notify_moderators]
|
2021-12-09 01:12:24 +08:00
|
|
|
flag_type_already_used =
|
|
|
|
reviewable.reviewable_scores.any? do |rs|
|
|
|
|
rs.reviewable_score_type == @post_action_type_id && !rs.pending?
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2020-02-15 00:43:48 +08:00
|
|
|
not_edited_since_last_review =
|
|
|
|
@post.last_version_at.blank? || reviewable.updated_at > @post.last_version_at
|
2020-02-21 01:43:33 +08:00
|
|
|
handled_recently =
|
|
|
|
reviewable.updated_at > SiteSetting.cooldown_hours_until_reflag.to_i.hours.ago
|
2020-02-15 00:43:48 +08:00
|
|
|
|
2020-06-10 06:26:10 +08:00
|
|
|
flag_type_already_used && not_edited_since_last_review && handled_recently
|
2020-02-15 00:43:48 +08:00
|
|
|
end
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
def notify_subscribers
|
2021-11-11 00:22:26 +08:00
|
|
|
if @post_action_name == :like
|
2022-05-10 04:23:39 +08:00
|
|
|
@post.publish_change_to_clients! :liked,
|
|
|
|
{
|
|
|
|
likes_count: @post.like_count + 1,
|
|
|
|
user_id: @created_by.id,
|
|
|
|
}
|
2021-11-11 00:22:26 +08:00
|
|
|
elsif self.class.notify_types.include?(@post_action_name)
|
2019-01-04 01:03:01 +08:00
|
|
|
@post.publish_change_to_clients! :acted
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.notify_types
|
2021-11-11 00:22:26 +08:00
|
|
|
@notify_types ||= PostActionType.notify_flag_types.keys
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def enforce_rules
|
|
|
|
auto_close_if_threshold_reached
|
|
|
|
auto_hide_if_needed
|
|
|
|
SpamRule::AutoSilence.new(@post.user, @post).perform
|
|
|
|
end
|
|
|
|
|
|
|
|
def auto_close_if_threshold_reached
|
|
|
|
return if topic.nil? || topic.closed?
|
|
|
|
return unless topic.auto_close_threshold_reached?
|
|
|
|
|
|
|
|
# the threshold has been reached, we will close the topic waiting for intervention
|
|
|
|
topic.update_status(
|
|
|
|
"closed",
|
|
|
|
true,
|
|
|
|
Discourse.system_user,
|
|
|
|
message:
|
|
|
|
I18n.t("temporarily_closed_due_to_flags", count: SiteSetting.num_hours_to_close_topic),
|
|
|
|
)
|
2017-09-08 13:07:22 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
topic.set_or_create_timer(
|
|
|
|
TopicTimer.types[:open],
|
|
|
|
SiteSetting.num_hours_to_close_topic,
|
|
|
|
by_user: Discourse.system_user,
|
|
|
|
)
|
2015-12-31 03:04:05 +08:00
|
|
|
end
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
def auto_hide_if_needed
|
|
|
|
return if @post.hidden?
|
|
|
|
return if !@created_by.staff? && @post.user&.staff?
|
2021-03-11 19:21:24 +08:00
|
|
|
|
FIX: serialize Flags instead of PostActionType (#28362)
### 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
```
2024-08-14 10:13:46 +08:00
|
|
|
not_auto_action_flag_type =
|
|
|
|
!@post_action_type_view.auto_action_flag_types.include?(@post_action_name)
|
2021-03-11 19:21:24 +08:00
|
|
|
return if not_auto_action_flag_type && !@queue_for_review
|
|
|
|
|
|
|
|
if @queue_for_review
|
2024-04-29 08:34:46 +08:00
|
|
|
if @post.is_first_post?
|
|
|
|
@post.topic.update_status(
|
|
|
|
"visible",
|
|
|
|
false,
|
|
|
|
@created_by,
|
|
|
|
{ visibility_reason_id: Topic.visibility_reasons[:op_flag_threshold_reached] },
|
|
|
|
)
|
|
|
|
end
|
2021-03-11 19:21:24 +08:00
|
|
|
|
|
|
|
@post.hide!(
|
|
|
|
@post_action_type_id,
|
|
|
|
Post.hidden_reasons[:flag_threshold_reached],
|
|
|
|
custom_message: :queued_by_staff,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
end
|
2019-01-04 01:03:01 +08:00
|
|
|
|
2020-07-22 22:42:15 +08:00
|
|
|
if trusted_spam_flagger?
|
2019-09-18 03:58:30 +08:00
|
|
|
@post.hide!(@post_action_type_id, Post.hidden_reasons[:flagged_by_tl3_user])
|
|
|
|
return
|
2019-09-20 01:17:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
score = ReviewableFlaggedPost.find_by(target: @post)&.score || 0
|
2023-10-30 08:24:35 +08:00
|
|
|
if score >= Reviewable.score_required_to_hide_post || @take_action
|
|
|
|
@post.hide!(@post_action_type_id)
|
|
|
|
end
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|
|
|
|
|
2020-07-22 22:42:15 +08:00
|
|
|
# Special case: If you have TL3 and the user is TL0, and the flag is spam,
|
|
|
|
# hide it immediately.
|
|
|
|
def trusted_spam_flagger?
|
|
|
|
SiteSetting.high_trust_flaggers_auto_hide_posts && @post_action_name == :spam &&
|
|
|
|
@created_by.has_trust_level?(TrustLevel[3]) && @post.user&.trust_level == TrustLevel[0]
|
|
|
|
end
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
def create_post_action
|
|
|
|
@targets_topic = !!(@post.topic.reload.posts_count != 1 if @flag_topic && @post.topic)
|
|
|
|
|
|
|
|
where_attrs = {
|
|
|
|
post_id: @post.id,
|
|
|
|
user_id: @created_by.id,
|
|
|
|
post_action_type_id: @post_action_type_id,
|
|
|
|
}
|
|
|
|
|
|
|
|
action_attrs = {
|
|
|
|
staff_took_action: @take_action,
|
|
|
|
related_post_id: @meta_post&.id,
|
|
|
|
targets_topic: @targets_topic,
|
|
|
|
created_at: @created_at,
|
|
|
|
}
|
|
|
|
|
|
|
|
# First try to revive a trashed record
|
|
|
|
post_action = PostAction.where(where_attrs).with_deleted.where("deleted_at IS NOT NULL").first
|
|
|
|
|
|
|
|
if post_action
|
|
|
|
post_action.recover!
|
2019-05-07 09:27:05 +08:00
|
|
|
action_attrs.each { |attr, val| post_action.public_send("#{attr}=", val) }
|
2019-01-04 01:03:01 +08:00
|
|
|
post_action.save
|
|
|
|
else
|
|
|
|
post_action = PostAction.create(where_attrs.merge(action_attrs))
|
|
|
|
if post_action && post_action.errors.count == 0
|
|
|
|
BadgeGranter.queue_badge_grant(Badge::Trigger::PostAction, post_action: post_action)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-12 08:07:29 +08:00
|
|
|
if post_action
|
|
|
|
case @post_action_type_id
|
FIX: serialize Flags instead of PostActionType (#28362)
### 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
```
2024-08-14 10:13:46 +08:00
|
|
|
when *@post_action_type_view.notify_flag_type_ids
|
2023-08-10 01:55:00 +08:00
|
|
|
DiscourseEvent.trigger(:flag_created, post_action, self)
|
FIX: serialize Flags instead of PostActionType (#28362)
### 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
```
2024-08-14 10:13:46 +08:00
|
|
|
when @post_action_type_view.types[:like]
|
2023-08-10 01:55:00 +08:00
|
|
|
DiscourseEvent.trigger(:like_created, post_action, self)
|
2021-03-12 08:07:29 +08:00
|
|
|
end
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|
|
|
|
|
FIX: serialize Flags instead of PostActionType (#28362)
### 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
```
2024-08-14 10:13:46 +08:00
|
|
|
if @post_action_type_id == @post_action_type_view.types[:like]
|
2019-01-04 01:03:01 +08:00
|
|
|
GivenDailyLike.increment_for(@created_by.id)
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2019-01-04 01:03:01 +08:00
|
|
|
|
|
|
|
post_action
|
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
# can happen despite being .create
|
|
|
|
PostAction.where(where_attrs).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_message_creator
|
|
|
|
title =
|
|
|
|
I18n.t(
|
|
|
|
"post_action_types.#{@post_action_name}.email_title",
|
|
|
|
title: @post.topic.title,
|
|
|
|
locale: SiteSetting.default_locale,
|
2024-07-18 08:10:22 +08:00
|
|
|
default: I18n.t("post_action_types.illegal.email_title"),
|
2019-01-04 01:03:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
body =
|
|
|
|
I18n.t(
|
|
|
|
"post_action_types.#{@post_action_name}.email_body",
|
|
|
|
message: @message,
|
|
|
|
link: "#{Discourse.base_url}#{@post.url}",
|
|
|
|
locale: SiteSetting.default_locale,
|
2024-07-18 08:10:22 +08:00
|
|
|
default: I18n.t("post_action_types.illegal.email_body"),
|
2019-01-04 01:03:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
create_args = {
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
is_warning: @is_warning,
|
|
|
|
title: title.truncate(SiteSetting.max_topic_title_length, separator: /\s/),
|
|
|
|
raw: body,
|
|
|
|
}
|
|
|
|
|
2024-07-18 08:10:22 +08:00
|
|
|
if @post_action_name == :notify_user
|
|
|
|
create_args[:subtype] = TopicSubtype.notify_user
|
|
|
|
|
|
|
|
create_args[:target_usernames] = @post.user.username
|
|
|
|
|
|
|
|
# Evaluate DiscoursePluginRegistry.post_action_notify_user_handlers.
|
|
|
|
# If any return false, return early from this method
|
|
|
|
handler_values =
|
|
|
|
DiscoursePluginRegistry.post_action_notify_user_handlers.map do |handler|
|
|
|
|
handler.call(@created_by, @post, @message)
|
|
|
|
end
|
|
|
|
return if handler_values.any? { |value| value == false }
|
|
|
|
else
|
2019-01-04 01:03:01 +08:00
|
|
|
create_args[:subtype] = TopicSubtype.notify_moderators
|
2021-08-12 06:11:22 +08:00
|
|
|
create_args[:target_group_names] = [Group[:moderators].name]
|
|
|
|
|
2024-09-04 09:38:46 +08:00
|
|
|
if SiteSetting.enable_category_group_moderation? && @post.topic&.category
|
|
|
|
create_args[:target_group_names].push(
|
|
|
|
*Group
|
|
|
|
.joins(:category_moderation_groups)
|
|
|
|
.where("category_moderation_groups.category_id": @post.topic.category.id)
|
|
|
|
.pluck(:name),
|
|
|
|
)
|
2021-08-12 06:11:22 +08:00
|
|
|
end
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
PostCreator.new(@created_by, create_args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_reviewable(result)
|
2020-02-15 00:43:48 +08:00
|
|
|
return unless flagging_post?
|
2019-01-04 01:03:01 +08:00
|
|
|
return if @post.user_id.to_i < 0
|
|
|
|
|
|
|
|
result.reviewable =
|
|
|
|
ReviewableFlaggedPost.needs_review!(
|
|
|
|
created_by: @created_by,
|
|
|
|
target: @post,
|
|
|
|
topic: @post.topic,
|
|
|
|
reviewable_by_moderator: true,
|
FIX: serialize Flags instead of PostActionType (#28362)
### 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
```
2024-08-14 10:13:46 +08:00
|
|
|
potential_spam: @post_action_type_id == @post_action_type_view.types[:spam],
|
2024-11-27 17:23:57 +08:00
|
|
|
potentially_illegal: @post_action_type_id == @post_action_type_view.types[:illegal],
|
2019-01-04 01:03:01 +08:00
|
|
|
payload: {
|
|
|
|
targets_topic: @targets_topic,
|
|
|
|
},
|
|
|
|
)
|
2021-03-11 19:21:24 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
result.reviewable_score =
|
|
|
|
result.reviewable.add_score(
|
|
|
|
@created_by,
|
|
|
|
@post_action_type_id,
|
|
|
|
created_at: @created_at,
|
|
|
|
take_action: @take_action,
|
|
|
|
meta_topic_id: @meta_post&.topic_id,
|
2020-07-22 22:42:15 +08:00
|
|
|
reason: @reason,
|
|
|
|
force_review: trusted_spam_flagger?,
|
2019-01-04 01:03:01 +08:00
|
|
|
)
|
|
|
|
end
|
2015-12-31 03:04:05 +08:00
|
|
|
|
|
|
|
def guardian
|
2019-01-04 01:03:01 +08:00
|
|
|
@guardian ||= Guardian.new(@created_by)
|
|
|
|
end
|
|
|
|
|
|
|
|
def topic
|
|
|
|
@post.topic
|
2015-12-31 03:04:05 +08:00
|
|
|
end
|
|
|
|
end
|