discourse/app/serializers/post_stream_serializer_mixin.rb
Martin Brennan 1b9e2ff4f9
FEATURE: Add attribution to staff notice and rename functionality (#30920)
The name "Staff Notice" was not quite right since TL4 users
can also add these notices. This commit changes the wording to
"Official Notice".

In addition to this, currently you have to go look into the staff
action logs to see who is responsible for a notice. This commit
stores the ID of the user who created the notice, then shows this
information on each notice to staff users.

Finally, I migrated the ChangePostNoticeModal component to gjs.
2025-01-24 09:29:22 +10:00

90 lines
1.9 KiB
Ruby

# frozen_string_literal: true
module PostStreamSerializerMixin
def self.included(klass)
klass.attributes :post_stream
klass.attributes :timeline_lookup
klass.attributes :user_badges
end
def include_stream?
true
end
def include_gaps?
true
end
def include_user_badges?
badges_to_include.present?
end
def user_badges
object.user_badges(badges_to_include)
end
def badges_to_include
@badges_to_include ||= theme_modifier_helper.serialize_post_user_badges
end
def post_stream
result = { posts: posts }
if include_stream?
if !object.is_mega_topic?
result[:stream] = object.filtered_post_ids
else
result[:isMegaTopic] = true
result[:lastId] = object.last_post_id
end
end
if include_gaps? && object.gaps.present?
result[:gaps] = GapSerializer.new(object.gaps, root: false)
end
result
end
def include_timeline_lookup?
!object.is_mega_topic?
end
def timeline_lookup
TimelineLookup.build(object.filtered_post_stream)
end
def posts
@posts ||=
begin
(object.posts || []).map do |post|
post.topic = object.topic
serializer = PostSerializer.new(post, scope: scope, root: false)
serializer.add_raw = true if @options[:include_raw]
serializer.topic_view = object
serializer.notice_created_by_users = post_notice_created_by_users if scope.is_staff?
serializer.as_json
end
end
end
def post_notice_created_by_users
@post_notice_created_by_users ||=
begin
user_ids =
object
.post_custom_fields
.filter_map { |_, custom_fields| custom_fields.dig(Post::NOTICE, "created_by_user_id") }
.uniq
User.real.where(id: user_ids)
end
end
def theme_modifier_helper
@theme_modifier_helper ||= ThemeModifierHelper.new(request: scope.request)
end
end