2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
class ReviewableScoreSerializer < ApplicationSerializer
|
2021-10-07 23:41:57 +08:00
|
|
|
REASONS_AND_SETTINGS = {
|
|
|
|
post_count: 'approve_post_count',
|
|
|
|
trust_level: 'approve_unless_trust_level',
|
|
|
|
new_topics_unless_trust_level: 'approve_new_topics_unless_trust_level',
|
|
|
|
fast_typer: 'min_first_post_typing_time',
|
2021-10-11 21:24:41 +08:00
|
|
|
auto_silence_regex: 'auto_silence_first_post_regex',
|
2021-10-07 23:41:57 +08:00
|
|
|
staged: 'approve_unless_staged',
|
|
|
|
must_approve_users: 'must_approve_users',
|
|
|
|
invite_only: 'invite_only',
|
|
|
|
email_spam: 'email_in_spam_header',
|
|
|
|
suspect_user: 'approve_suspect_users',
|
|
|
|
contains_media: 'review_media_unless_trust_level',
|
|
|
|
}
|
2019-01-04 01:03:01 +08:00
|
|
|
|
2019-04-11 23:11:35 +08:00
|
|
|
attributes :id, :score, :agree_stats, :status, :reason, :created_at, :reviewed_at
|
2019-01-04 01:03:01 +08:00
|
|
|
has_one :user, serializer: BasicUserSerializer, root: 'users'
|
|
|
|
has_one :score_type, serializer: ReviewableScoreTypeSerializer
|
|
|
|
has_one :reviewable_conversation, serializer: ReviewableConversationSerializer
|
2019-04-10 04:43:08 +08:00
|
|
|
has_one :reviewed_by, serializer: BasicUserSerializer, root: 'users'
|
2019-01-04 01:03:01 +08:00
|
|
|
|
|
|
|
def agree_stats
|
|
|
|
{
|
|
|
|
agreed: user.user_stat.flags_agreed,
|
|
|
|
disagreed: user.user_stat.flags_disagreed,
|
|
|
|
ignored: user.user_stat.flags_ignored
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-04-11 23:11:35 +08:00
|
|
|
def reason
|
|
|
|
return unless object.reason
|
|
|
|
|
2021-10-07 23:41:57 +08:00
|
|
|
link_text = setting_name_for_reason(object.reason)
|
|
|
|
link_text = I18n.t("reviewables.reasons.links.#{object.reason}", default: nil) if link_text.nil?
|
2021-10-05 03:55:09 +08:00
|
|
|
|
|
|
|
if link_text
|
|
|
|
link = build_link_for(object.reason, link_text)
|
|
|
|
text = I18n.t("reviewables.reasons.#{object.reason}", link: link, default: nil)
|
|
|
|
else
|
|
|
|
text = I18n.t("reviewables.reasons.#{object.reason}", default: nil)
|
2019-04-11 23:11:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
text
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_reason?
|
|
|
|
reason.present?
|
|
|
|
end
|
|
|
|
|
2021-10-07 23:41:57 +08:00
|
|
|
def setting_name_for_reason(reason)
|
|
|
|
setting_name = REASONS_AND_SETTINGS[reason.to_sym]
|
|
|
|
|
|
|
|
if setting_name.nil?
|
|
|
|
plugin_options = DiscoursePluginRegistry.reviewable_score_links
|
|
|
|
option = plugin_options.detect { |o| o[:reason] == reason.to_sym }
|
|
|
|
|
|
|
|
setting_name = option[:setting] if option
|
|
|
|
end
|
|
|
|
|
|
|
|
setting_name
|
|
|
|
end
|
|
|
|
|
2021-10-05 03:55:09 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def url_for(reason, text)
|
|
|
|
case reason
|
|
|
|
when 'watched_word'
|
|
|
|
"#{Discourse.base_url}/admin/customize/watched_words"
|
|
|
|
when 'category'
|
|
|
|
"#{Discourse.base_url}/c/#{object.reviewable.category&.name}/edit/settings"
|
|
|
|
else
|
|
|
|
"#{Discourse.base_url}/admin/site_settings/category/all_results?filter=#{text}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_link_for(reason, text)
|
|
|
|
return text.gsub('_', ' ') unless scope.is_staff?
|
|
|
|
|
|
|
|
"<a href=\"#{url_for(reason, text)}\">#{text.gsub('_', ' ')}</a>"
|
|
|
|
end
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|