2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-03 23:54:20 +08:00
discourse/app/serializers/flag_serializer.rb
Alan Guo Xiang Tan 684569c30e
PERF: Avoid slow preloading in SiteSerializer (#33283)
`SiteSerializer#post_action_types` and `SiteSerializer#topic_flag_types`
both call the `Flag.used_flag_ids` method which executes queries that
becomes very slow on sites with alot of records in either the
`post_actions` or `reviewable_scores` table.

On the `post_actions` table, we execute `SELECT DISTINCT
post_action_type_id FROM post_actions`. On the `reviewable_scores`
table, we execute `SELECT DISTINCT reviewable_scope_type FROM
reviewable_scopes`. The problem with both queries is that it requires
the PG planner to scan through every single row in those tables.

For our use case, all we actually need is to check if a flag is being
referenced by a record in either the `post_actions` or
`reviewable_scores` tables. This commit updates the `Flag.used_flag_ids`
to accept a `flag_ids`
argument and use the argument to check whether the flag ids are
referenced in the `post_action_type_id` or `reviewable_scope_type`
foreign keys.
2025-06-25 15:03:34 +08:00

55 lines
1.2 KiB
Ruby

# frozen_string_literal: true
class FlagSerializer < ApplicationSerializer
attributes :id,
:name,
:name_key,
:description,
:short_description,
:applies_to,
:position,
:require_message,
:enabled,
:is_flag,
:applies_to,
:is_used,
:auto_action_type,
:system
def i18n_prefix
"#{@options[:target] || "post_action"}_types.#{object.name_key}"
end
def name
# system flags are using i18n translations when custom flags are using value entered by admin
I18n.t("#{i18n_prefix}.title", default: object.name)
end
def description
I18n.t(
"#{i18n_prefix}.description",
default: object.description.to_s,
base_path: Discourse.base_path,
)
end
def short_description
I18n.t("#{i18n_prefix}.short_description", base_path: Discourse.base_path, default: "")
end
def is_flag
!object.score_type && object.id != PostActionType::LIKE_POST_ACTION_ID
end
def is_used
@options[:used_flag_ids]&.include?(object.id) || object.used?
end
def applies_to
Array.wrap(object.applies_to)
end
def system
object.system?
end
end