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
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe FlagSerializer do
|
|
|
|
let(:flag) { Flag.find_by(name: "illegal") }
|
|
|
|
|
|
|
|
context "when system flag" do
|
|
|
|
it "returns translated name" do
|
|
|
|
serialized = described_class.new(flag, used_flag_ids: []).as_json
|
|
|
|
expect(serialized[:flag][:name]).to eq(I18n.t("post_action_types.illegal.title"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns translated description" do
|
|
|
|
serialized = described_class.new(flag, used_flag_ids: []).as_json
|
|
|
|
expect(serialized[:flag][:description]).to eq(I18n.t("post_action_types.illegal.description"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when custom flag" do
|
2024-08-28 15:03:43 +08:00
|
|
|
it "returns translated name and description" do
|
|
|
|
flag = Fabricate(:flag, name: "custom title", description: "custom description")
|
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
|
|
|
serialized = described_class.new(flag, used_flag_ids: []).as_json
|
|
|
|
expect(serialized[:flag][:name]).to eq("custom title")
|
|
|
|
expect(serialized[:flag][:description]).to eq("custom description")
|
2024-08-28 15:03:43 +08:00
|
|
|
flag.destroy!
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns is_flag true for flags" do
|
|
|
|
serialized = described_class.new(flag, used_flag_ids: []).as_json
|
|
|
|
expect(serialized[:flag][:is_flag]).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns is_flag false for like" do
|
|
|
|
flag = Flag.unscoped.find_by(name: "like")
|
|
|
|
serialized = described_class.new(flag, used_flag_ids: []).as_json
|
|
|
|
expect(serialized[:flag][:is_flag]).to be false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns is_used false when not used" do
|
|
|
|
serialized = described_class.new(flag, used_flag_ids: []).as_json
|
|
|
|
expect(serialized[:flag][:is_used]).to be false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns is_used true when used" do
|
|
|
|
serialized = described_class.new(flag, used_flag_ids: [flag.id]).as_json
|
|
|
|
expect(serialized[:flag][:is_used]).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns applies_to" do
|
|
|
|
serialized = described_class.new(flag, used_flag_ids: []).as_json
|
|
|
|
expect(serialized[:flag][:applies_to]).to eq(%w[Post Topic Chat::Message])
|
|
|
|
end
|
2024-08-26 21:44:12 +08:00
|
|
|
|
|
|
|
describe "#description" do
|
|
|
|
let(:serializer) { described_class.new(flag, scope: Guardian.new, root: false) }
|
|
|
|
let(:flag) { Flag.find_by(name_key: :inappropriate) }
|
|
|
|
|
|
|
|
before { allow(Discourse).to receive(:base_path).and_return("discourse.org") }
|
|
|
|
|
|
|
|
it "returns properly interpolated translation" do
|
|
|
|
expect(serializer.description).to match(%r{discourse\.org/guidelines})
|
|
|
|
end
|
|
|
|
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
|
|
|
end
|