2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-04 01:15:08 +08:00
discourse/app/serializers/basic_post_serializer.rb
Natalie Tay 51ebe7064c
FEATURE: Show localized posts and topics based on user's locale (#32618)
Related:
- https://github.com/discourse/discourse-translator/pull/205
- https://github.com/discourse/discourse-translator/pull/274
- https://github.com/discourse/discourse-translator/pull/294

With this PR, we will start showing localized posts (if available) based
on the user's locale.

This work had been done in discourse-translator, but is now moving to
core.
2025-05-15 19:11:06 +08:00

58 lines
1.3 KiB
Ruby

# frozen_string_literal: true
# The most basic attributes of a topic that we need to create a link for it.
class BasicPostSerializer < ApplicationSerializer
attributes :id, :name, :username, :avatar_template, :created_at, :cooked, :cooked_hidden
attr_accessor :topic_view
def name
object.user && object.user.name
end
def username
object.user && object.user.username
end
def avatar_template
object.user && object.user.avatar_template
end
def cooked_hidden
object.hidden && !scope.is_staff?
end
def include_cooked_hidden?
cooked_hidden
end
def cooked
if cooked_hidden
if scope.current_user && object.user_id == scope.current_user.id
I18n.t("flagging.you_must_edit", path: "/my/messages")
else
I18n.t("flagging.user_must_edit")
end
else
cooked = object.filter_quotes(@parent_post)
translated_cooked =
object.get_localization&.cooked if ContentLocalization.show_translated_post?(object, scope)
translated_cooked || cooked
end
end
def include_name?
SiteSetting.enable_names?
end
def post_custom_fields
@post_custom_fields ||=
if @topic_view
(@topic_view.post_custom_fields || {})[object.id] || {}
else
object.custom_fields
end
end
end