2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-04 01:15:08 +08:00
discourse/app/serializers/group_post_serializer.rb
Sérgio Saquetim 4013d1ffb8
DEV: Improve user handling and access in post components (#34013)
This pull request improves how user properties are managed and accessed
in post-related components and models. Notable improvements include:

- The `Post` model now provides a cached `user` property, ensuring a
consistent and up-to-date user object using relevant post fields,
instead of relying on `user` being set directly or via the `PostStream`.
- Components such as `PostAvatar` and `PostMetaDataPosterName` now
consistently reference the `user` property, improving reliability and
reducing the risk of missing or inconsistent user data.
- User context is more accurately passed to value transformers and
plugin outlets, ensuring that avatar and name rendering logic always
receives the correct user information.
- The `GroupPostSerializer` now includes the `user_id` attribute,
improving API consistency.
- Test cases have been updated to work with the new user property
approach, removing direct creation of user objects on posts and instead
relying on post fields.
- The `discourse-reactions` plugin was updated to align with the new
user property logic, ensuring reaction-related user data is accessed
consistently.

Overall, these changes improve data integrity, reduce the risk of
rendering errors due to user data inconsistencies, and streamline how
user information is passed through Discourse post-related features.
2025-08-06 17:00:34 -03:00

77 lines
1.4 KiB
Ruby

# frozen_string_literal: true
require_relative "post_item_excerpt"
class GroupPostSerializer < ApplicationSerializer
include PostItemExcerpt
attributes :id,
:created_at,
:topic_id,
:topic_title,
:topic_slug,
:topic_html_title,
:url,
:category_id,
:post_number,
:posts_count,
:post_type,
:user_id,
:username,
:name,
:avatar_template,
:user_title,
:primary_group_name
# TODO(keegan): Remove `embed: :object` after updating references in discourse-reactions
has_one :user, serializer: GroupPostUserSerializer, embed: :object
has_one :topic, serializer: BasicTopicSerializer, embed: :object
def topic_title
object.topic.title
end
def topic_html_title
object.topic.fancy_title
end
def topic_slug
object.topic.slug
end
def posts_count
object.topic.posts_count
end
def include_user_long_name?
SiteSetting.enable_names?
end
def category_id
object.topic.category_id
end
def user_id
object&.user&.id
end
def username
object&.user&.username
end
def name
object&.user&.name
end
def avatar_template
object&.user&.avatar_template
end
def user_title
object&.user&.title
end
def primary_group_name
object&.user&.primary_group&.name
end
end