discourse/app/serializers/user_topic_bookmark_serializer.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

62 lines
1.5 KiB
Ruby
Raw Normal View History

FEATURE: Polymorphic bookmarks pt. 2 (lists, search) (#16335) This pull request follows on from https://github.com/discourse/discourse/pull/16308. This one does the following: * Changes `BookmarkQuery` to allow for querying more than just Post and Topic bookmarkables * Introduces a `Bookmark.register_bookmarkable` method which requires a model, serializer, fields and preload includes for searching. These registered `Bookmarkable` types are then used when validating new bookmarks, and also when determining which serializer to use for the bookmark list. The `Post` and `Topic` bookmarkables are registered by default. * Adds new specific types for Post and Topic bookmark serializers along with preloading of associations in `UserBookmarkList` * Changes to the user bookmark list template to allow for more generic bookmarkable types alongside the Post and Topic ones which need to display in a particular way All of these changes are gated behind the `use_polymorphic_bookmarks` site setting, apart from the .hbs changes where I have updated the original `UserBookmarkSerializer` with some stub methods. Following this PR will be several plugin PRs (for assign, chat, encrypt) that will register their own bookmarkable types or otherwise alter the bookmark serializers in their own way, also gated behind `use_polymorphic_bookmarks`. This commit also removes `BookmarkQuery.preloaded_custom_fields` and the functionality surrounding it. It was added in https://github.com/discourse/discourse/commit/0cd502a55838d5d27f96f13c0794f3669ac41fcc but only used by one plugin (discourse-assign) where it has since been removed, and is now used by no plugins. We don't need it anymore.
2022-04-22 06:23:42 +08:00
# frozen_string_literal: true
class UserTopicBookmarkSerializer < UserPostTopicBookmarkBaseSerializer
# it does not matter what the linked post number is for topic bookmarks,
# on the client we always take the user to the last unread post in the
# topic when the bookmark URL is clicked
def linked_post_number
1
end
def first_post
@first_post ||= topic.posts.find { |post| post.post_number == 1 }
end
def deleted
topic.deleted_at.present? || first_post.deleted_at.present?
end
def hidden
first_post.hidden
end
def raw
first_post.raw
end
def cooked
@cooked ||= \
if last_read_post_number.present?
for_topic_cooked_post
else
first_post.cooked
end
end
def for_topic_cooked_post
post_number = [last_read_post_number + 1, highest_post_number].min
sorted_regular_posts = topic.posts.sort_by(&:post_number).select do |post|
post.post_type == Post.types[:regular]
end
first_unread_post = sorted_regular_posts.find do |post|
post.post_number >= post_number
end
# if first_unread_cooked is blank this likely means that the last
# read post was either deleted or is a small action post.
# in this case we should just get the last regular post and
# use that for the cooked value so we have something to show
(first_unread_post || sorted_regular_posts.last).cooked
end
def bookmarkable_user
@bookmarkable_user ||= first_post.user
end
private
def topic
object.bookmarkable
end
end