2024-05-23 10:19:07 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Flag < ActiveRecord::Base
|
2024-07-18 12:00:27 +08:00
|
|
|
# TODO(2025-01-15): krisk remove
|
|
|
|
self.ignored_columns = ["custom_type"]
|
|
|
|
|
2024-07-03 06:45:37 +08:00
|
|
|
DEFAULT_VALID_APPLIES_TO = %w[Post Topic]
|
2024-05-23 10:19:07 +08:00
|
|
|
MAX_SYSTEM_FLAG_ID = 1000
|
2024-07-03 06:45:37 +08:00
|
|
|
MAX_NAME_LENGTH = 200
|
|
|
|
MAX_DESCRIPTION_LENGTH = 1000
|
2024-05-23 10:19:07 +08:00
|
|
|
scope :enabled, -> { where(enabled: true) }
|
|
|
|
scope :system, -> { where("id < 1000") }
|
2024-08-06 08:50:12 +08:00
|
|
|
scope :custom, -> { where("id >= 1000") }
|
2024-05-23 10:19:07 +08:00
|
|
|
|
|
|
|
before_save :set_position
|
|
|
|
before_save :set_name_key
|
2024-08-13 11:22:57 +08:00
|
|
|
after_commit { reset_flag_settings! if !skip_reset_flag_callback }
|
|
|
|
|
|
|
|
attr_accessor :skip_reset_flag_callback
|
2024-05-23 10:19:07 +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
|
|
|
default_scope do
|
|
|
|
order(:position).where(score_type: false).where.not(id: PostActionType::LIKE_POST_ACTION_ID)
|
|
|
|
end
|
2024-05-23 10:19:07 +08:00
|
|
|
|
|
|
|
def used?
|
|
|
|
PostAction.exists?(post_action_type_id: self.id) ||
|
|
|
|
ReviewableScore.exists?(reviewable_score_type: self.id)
|
|
|
|
end
|
|
|
|
|
2024-07-03 06:45:37 +08:00
|
|
|
def self.valid_applies_to_types
|
|
|
|
Set.new(DEFAULT_VALID_APPLIES_TO | DiscoursePluginRegistry.flag_applies_to_types)
|
|
|
|
end
|
|
|
|
|
2024-05-23 10:19:07 +08:00
|
|
|
def self.reset_flag_settings!
|
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
|
|
|
# Flags are cached in Redis for better performance. After the update,
|
|
|
|
# we need to reload them in all processes.
|
2024-05-23 10:19:07 +08:00
|
|
|
PostActionType.reload_types
|
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
|
|
|
end
|
|
|
|
|
|
|
|
def self.used_flag_ids
|
|
|
|
PostAction.distinct(:post_action_type_id).pluck(:post_action_type_id) |
|
|
|
|
ReviewableScore.distinct(:reviewable_score_type).pluck(:reviewable_score_type)
|
2024-05-23 10:19:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def system?
|
2024-09-11 13:30:20 +08:00
|
|
|
self.id.present? && self.id < MAX_SYSTEM_FLAG_ID
|
2024-05-23 10:19:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def applies_to?(type)
|
|
|
|
self.applies_to.include?(type)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def reset_flag_settings!
|
|
|
|
self.class.reset_flag_settings!
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_position
|
|
|
|
self.position = Flag.maximum(:position).to_i + 1 if !self.position
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_name_key
|
2024-09-11 13:30:20 +08:00
|
|
|
prefix = self.system? ? "" : "custom_"
|
|
|
|
self.name_key = "#{prefix}#{self.name.squeeze(" ").gsub(" ", "_").gsub(/[^\w]/, "").downcase}"
|
2024-05-23 10:19:07 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: flags
|
|
|
|
#
|
|
|
|
# id :bigint not null, primary key
|
|
|
|
# name :string
|
|
|
|
# name_key :string
|
|
|
|
# description :text
|
|
|
|
# notify_type :boolean default(FALSE), not null
|
|
|
|
# auto_action_type :boolean default(FALSE), not null
|
2024-07-18 08:10:22 +08:00
|
|
|
# require_message :boolean default(FALSE), not null
|
2024-05-23 10:19:07 +08:00
|
|
|
# applies_to :string not null, is an Array
|
|
|
|
# position :integer not null
|
|
|
|
# enabled :boolean default(TRUE), not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2024-05-29 12:39:58 +08:00
|
|
|
# score_type :boolean default(FALSE), not null
|
2024-05-23 10:19:07 +08:00
|
|
|
#
|