discourse/app/serializers/admin_user_action_serializer.rb
Bianca Nenciu a6c3369614
FIX: Posts can belong to hard-deleted topics (#17329)
* FIX: Posts can belong to hard-deleted topics

This was a problem when serializing deleted posts because they might
belong to a topic that was permanently deleted. This caused to DB
lookup to fail immediately and raise an exception. In this case, the
endpoint returned a 404.

* FIX: Remove N+1 queries

Deleted topics were not loaded because of the default scope that
filters out all deleted topics. It executed a query for each deleted
topic.
2022-07-05 10:51:21 +03:00

84 lines
1.4 KiB
Ruby

# frozen_string_literal: true
require_relative 'post_item_excerpt'
class AdminUserActionSerializer < ApplicationSerializer
include PostItemExcerpt
attributes(
:id,
:created_at,
:post_number,
:post_id,
:name,
:username,
:avatar_template,
:topic_id,
:slug,
:title,
:category_id,
:truncated,
:hidden,
:moderator_action,
:deleted,
:deleted_at,
:deleted_by,
:reply_to_post_number,
:action_type
)
def post_id
object.id
end
def deleted
deleted_at.present?
end
def name
object.user.name
end
def include_name?
SiteSetting.enable_names?
end
def username
object.user.username
end
def avatar_template
object.user.avatar_template
end
def slug
object.topic&.slug
end
def title
object.topic&.title
end
def category_id
object.topic&.category_id
end
def moderator_action
object.post_type == Post.types[:moderator_action] || object.post_type == Post.types[:small_action]
end
def deleted_by
BasicUserSerializer.new(object.deleted_by, root: false).as_json
end
def include_deleted_by?
object.trashed?
end
def action_type
object.user_actions.select { |ua| ua.user_id = object.user_id }
.select { |ua| [UserAction::REPLY, UserAction::RESPONSE].include? ua.action_type }
.first.try(:action_type)
end
end