2024-05-23 10:19:07 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe Flag, type: :model do
|
2024-05-24 10:55:21 +08:00
|
|
|
after(:each) { Flag.reset_flag_settings! }
|
2024-05-23 10:19:07 +08:00
|
|
|
|
|
|
|
it "has id lower than 1000 for system flags" do
|
|
|
|
flag = Fabricate(:flag, id: 1)
|
|
|
|
expect(flag.system?).to be true
|
2024-05-24 10:55:21 +08:00
|
|
|
flag.destroy!
|
2024-05-23 10:19:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "has id greater than 1000 for non-system flags" do
|
|
|
|
flag = Fabricate(:flag)
|
|
|
|
expect(flag.system?).to be false
|
|
|
|
expect(flag.id).to be > 1000
|
2024-05-24 10:55:21 +08:00
|
|
|
flag.destroy!
|
2024-05-23 10:19:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "has correct name key" do
|
2024-09-11 13:30:20 +08:00
|
|
|
flag = Fabricate(:flag, name: "FlAg!!!")
|
2024-05-23 10:19:07 +08:00
|
|
|
expect(flag.name_key).to eq("custom_flag")
|
|
|
|
|
|
|
|
flag.update!(name: "It's Illegal")
|
2024-09-11 13:30:20 +08:00
|
|
|
expect(flag.name_key).to eq("custom_its_illegal")
|
2024-05-23 10:19:07 +08:00
|
|
|
|
|
|
|
flag.update!(name: "THIS IS SPaM!+)(*&^%$#@@@!)")
|
2024-09-11 13:30:20 +08:00
|
|
|
expect(flag.name_key).to eq("custom_this_is_spam")
|
2024-05-24 10:55:21 +08:00
|
|
|
|
|
|
|
flag.destroy!
|
2024-05-23 10:19:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "updates post action types when created, modified or destroyed" do
|
|
|
|
expect(PostActionType.flag_types.keys).to eq(
|
2024-06-06 13:45:30 +08:00
|
|
|
%i[notify_user off_topic inappropriate spam illegal notify_moderators],
|
2024-05-23 10:19:07 +08:00
|
|
|
)
|
|
|
|
expect(ReviewableScore.types.keys).to eq(
|
2024-06-06 13:45:30 +08:00
|
|
|
%i[notify_user off_topic inappropriate spam illegal notify_moderators needs_approval],
|
2024-05-23 10:19:07 +08:00
|
|
|
)
|
|
|
|
|
2024-09-11 13:30:20 +08:00
|
|
|
flag = Fabricate(:flag, name: "flag")
|
2024-05-23 10:19:07 +08:00
|
|
|
expect(PostActionType.flag_types.keys).to eq(
|
2024-09-11 13:30:20 +08:00
|
|
|
%i[notify_user off_topic inappropriate spam illegal notify_moderators custom_flag],
|
2024-05-23 10:19:07 +08:00
|
|
|
)
|
|
|
|
expect(ReviewableScore.types.keys).to eq(
|
2024-09-11 13:30:20 +08:00
|
|
|
%i[
|
|
|
|
notify_user
|
|
|
|
off_topic
|
|
|
|
inappropriate
|
|
|
|
spam
|
|
|
|
illegal
|
|
|
|
notify_moderators
|
|
|
|
custom_flag
|
|
|
|
needs_approval
|
|
|
|
],
|
2024-05-23 10:19:07 +08:00
|
|
|
)
|
|
|
|
|
2024-09-11 13:30:20 +08:00
|
|
|
flag.update!(name: "edited_flag")
|
2024-05-23 10:19:07 +08:00
|
|
|
expect(PostActionType.flag_types.keys).to eq(
|
2024-09-11 13:30:20 +08:00
|
|
|
%i[notify_user off_topic inappropriate spam illegal notify_moderators custom_edited_flag],
|
2024-05-23 10:19:07 +08:00
|
|
|
)
|
|
|
|
expect(ReviewableScore.types.keys).to eq(
|
|
|
|
%i[
|
|
|
|
notify_user
|
|
|
|
off_topic
|
|
|
|
inappropriate
|
|
|
|
spam
|
|
|
|
illegal
|
2024-06-06 13:45:30 +08:00
|
|
|
notify_moderators
|
2024-09-11 13:30:20 +08:00
|
|
|
custom_edited_flag
|
2024-05-23 10:19:07 +08:00
|
|
|
needs_approval
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
flag.destroy!
|
|
|
|
expect(PostActionType.flag_types.keys).to eq(
|
2024-06-06 13:45:30 +08:00
|
|
|
%i[notify_user off_topic inappropriate spam illegal notify_moderators],
|
2024-05-23 10:19:07 +08:00
|
|
|
)
|
|
|
|
expect(ReviewableScore.types.keys).to eq(
|
2024-06-06 13:45:30 +08:00
|
|
|
%i[notify_user off_topic inappropriate spam illegal notify_moderators needs_approval],
|
2024-05-23 10:19:07 +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
|
|
|
|
|
|
|
describe ".used_flag_ids" do
|
|
|
|
fab!(:post_action) { Fabricate(:post_action, post_action_type_id: PostActionType.types[:like]) }
|
|
|
|
fab!(:post_action_2) do
|
|
|
|
Fabricate(:post_action, post_action_type_id: PostActionType.types[:like])
|
|
|
|
end
|
|
|
|
fab!(:post_action_3) do
|
|
|
|
Fabricate(:post_action, post_action_type_id: PostActionType.types[:off_topic])
|
|
|
|
end
|
|
|
|
fab!(:reviewable_score) do
|
|
|
|
Fabricate(:reviewable_score, reviewable_score_type: PostActionType.types[:off_topic])
|
|
|
|
end
|
|
|
|
fab!(:reviewable_score_2) do
|
|
|
|
Fabricate(:reviewable_score, reviewable_score_type: PostActionType.types[:illegal])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an array of unique flag ids" do
|
|
|
|
expect(Flag.used_flag_ids).to eq(
|
|
|
|
[
|
|
|
|
PostActionType.types[:like],
|
|
|
|
PostActionType.types[:off_topic],
|
|
|
|
PostActionType.types[:illegal],
|
|
|
|
],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2024-05-23 10:19:07 +08:00
|
|
|
end
|