2024-05-29 12:39:58 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Admin::Config::FlagsController < Admin::AdminController
|
|
|
|
def toggle
|
2024-09-04 00:30:22 +08:00
|
|
|
Flags::ToggleFlag.call do
|
2024-05-29 12:39:58 +08:00
|
|
|
on_success do
|
|
|
|
Discourse.request_refresh!
|
|
|
|
render(json: success_json)
|
|
|
|
end
|
|
|
|
on_failure { render(json: failed_json, status: 422) }
|
|
|
|
on_model_not_found(:message) { raise Discourse::NotFound }
|
|
|
|
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
|
|
|
|
on_failed_contract do |contract|
|
|
|
|
render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def index
|
|
|
|
end
|
2024-06-05 11:27:06 +08:00
|
|
|
|
2024-07-03 06:45:37 +08:00
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2024-09-04 00:30:22 +08:00
|
|
|
Flags::CreateFlag.call do
|
2024-07-03 06:45:37 +08:00
|
|
|
on_success do
|
|
|
|
Discourse.request_refresh!
|
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
|
|
|
render json: result.flag, serializer: FlagSerializer, used_flag_ids: Flag.used_flag_ids
|
2024-07-03 06:45:37 +08:00
|
|
|
end
|
|
|
|
on_failure { render(json: failed_json, status: 422) }
|
|
|
|
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
|
2024-09-30 07:17:19 +08:00
|
|
|
on_failed_policy(:unique_name) { render_json_error(I18n.t("flags.errors.unique_name")) }
|
2024-07-03 06:45:37 +08:00
|
|
|
on_failed_contract do |contract|
|
|
|
|
render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2024-09-04 00:30:22 +08:00
|
|
|
Flags::UpdateFlag.call do
|
2024-07-03 06:45:37 +08:00
|
|
|
on_success do
|
|
|
|
Discourse.request_refresh!
|
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
|
|
|
render json: result.flag, serializer: FlagSerializer, used_flag_ids: Flag.used_flag_ids
|
2024-07-03 06:45:37 +08:00
|
|
|
end
|
|
|
|
on_failure { render(json: failed_json, status: 422) }
|
|
|
|
on_model_not_found(:message) { raise Discourse::NotFound }
|
|
|
|
on_failed_policy(:not_system) { render_json_error(I18n.t("flags.errors.system")) }
|
|
|
|
on_failed_policy(:not_used) { render_json_error(I18n.t("flags.errors.used")) }
|
|
|
|
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
|
2024-09-30 07:17:19 +08:00
|
|
|
on_failed_policy(:unique_name) { render_json_error(I18n.t("flags.errors.unique_name")) }
|
2024-07-03 06:45:37 +08:00
|
|
|
on_failed_contract do |contract|
|
|
|
|
render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-05 11:27:06 +08:00
|
|
|
def reorder
|
2024-09-04 00:30:22 +08:00
|
|
|
Flags::ReorderFlag.call do
|
2024-06-05 11:27:06 +08:00
|
|
|
on_success do
|
|
|
|
Discourse.request_refresh!
|
|
|
|
render(json: success_json)
|
|
|
|
end
|
|
|
|
on_failure { render(json: failed_json, status: 422) }
|
|
|
|
on_model_not_found(:message) { raise Discourse::NotFound }
|
|
|
|
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
|
|
|
|
on_failed_policy(:invalid_move) { render_json_error(I18n.t("flags.errors.wrong_move")) }
|
|
|
|
on_failed_contract do |contract|
|
|
|
|
render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-07-03 06:45:37 +08:00
|
|
|
|
|
|
|
def destroy
|
2024-09-04 00:30:22 +08:00
|
|
|
Flags::DestroyFlag.call do
|
2024-07-03 06:45:37 +08:00
|
|
|
on_success do
|
|
|
|
Discourse.request_refresh!
|
|
|
|
render(json: success_json)
|
|
|
|
end
|
|
|
|
on_failure { render(json: failed_json, status: 422) }
|
|
|
|
on_failed_policy(:not_system) { render_json_error(I18n.t("flags.errors.system")) }
|
|
|
|
on_failed_policy(:not_used) { render_json_error(I18n.t("flags.errors.used")) }
|
|
|
|
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
|
|
|
|
on_failed_contract do |contract|
|
|
|
|
render(json: failed_json.merge(errors: contract.errors.full_messages), status: 400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-05-29 12:39:58 +08:00
|
|
|
end
|