From 40c4d06bdd46947ff36b7c400a4bea551c042c19 Mon Sep 17 00:00:00 2001 From: Mark VanLandingham Date: Mon, 7 Oct 2024 11:39:35 -0500 Subject: [PATCH] poc including badges in post stream --- app/models/post.rb | 2 ++ app/serializers/post_serializer.rb | 20 ++++++++++++++++++++ lib/topic_view.rb | 1 + 3 files changed, 23 insertions(+) diff --git a/app/models/post.rb b/app/models/post.rb index 8f9f3def332..bc927c59050 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -58,6 +58,8 @@ class Post < ActiveRecord::Base has_many :user_actions, foreign_key: :target_post_id + has_many :user_badges + belongs_to :image_upload, class_name: "Upload" has_many :post_hotlinked_media, dependent: :destroy, class_name: "PostHotlinkedMedia" diff --git a/app/serializers/post_serializer.rb b/app/serializers/post_serializer.rb index ebb9f45bb7c..ca2e61ea8c8 100644 --- a/app/serializers/post_serializer.rb +++ b/app/serializers/post_serializer.rb @@ -39,6 +39,7 @@ class PostSerializer < BasicPostSerializer :flair_color, :flair_group_id, :version, + :badges, :can_edit, :can_delete, :can_permanently_delete, @@ -584,6 +585,25 @@ class PostSerializer < BasicPostSerializer UserStatusSerializer.new(object.user&.user_status, root: false) end + def include_badges? + badges.present? + end + + def badges + @badges ||= + begin + badges = object.user_badges.filter { |badge| badge.user_id == object.user_id } + return if badges.blank? + + ActiveModel::ArraySerializer.new( + badges, + each_serializer: UserBadgeSerializer, + scope: scope, + root: false, + ) + end + end + def mentioned_users users = if @topic_view && (mentioned_users = @topic_view.mentioned_users[object.id]) diff --git a/lib/topic_view.rb b/lib/topic_view.rb index 23f21afa66c..5c04857fad9 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -859,6 +859,7 @@ class TopicView :deleted_by, :incoming_email, :image_upload, + :user_badges, ) @posts = @posts.includes({ user: :user_status }) if SiteSetting.enable_user_status