discourse/app/serializers/user_bookmark_serializer.rb
Martin Brennan 3e4621c2cb
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 0cd502a558 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 08:23:42 +10:00

173 lines
3.5 KiB
Ruby

# frozen_string_literal: true
require_relative 'post_item_excerpt'
# TODO (martin) [POLYBOOK] Not relevant once polymorphic bookmarks are implemented.
class UserBookmarkSerializer < ApplicationSerializer
include PostItemExcerpt
include TopicTagsMixin
attributes :id,
:created_at,
:updated_at,
:topic_id,
:linked_post_number,
:post_id,
:name,
:reminder_at,
:pinned,
:title,
:fancy_title,
:deleted,
:hidden,
:category_id,
:closed,
:archived,
:archetype,
:highest_post_number,
:last_read_post_number,
:bumped_at,
:slug,
:post_user_username,
:post_user_avatar_template,
:post_user_name,
:for_topic,
:bookmarkable_id,
:bookmarkable_type,
:bookmarkable_user_username,
:bookmarkable_user_avatar_template,
:bookmarkable_user_name,
def topic_id
post.topic_id
end
def topic
@topic ||= object.topic
end
def post
@post ||= object.post
end
def closed
topic.closed
end
def archived
topic.archived
end
def linked_post_number
post.post_number
end
def title
topic.title
end
def fancy_title
topic.fancy_title
end
def deleted
topic.deleted_at.present? || post.deleted_at.present?
end
def hidden
post.hidden
end
def category_id
topic.category_id
end
def archetype
topic.archetype
end
def archived
topic.archived
end
def closed
topic.closed
end
def highest_post_number
scope.is_staff? ? topic.highest_staff_post_number : topic.highest_post_number
end
def last_read_post_number
topic_user&.last_read_post_number
end
def topic_user
@topic_user ||= topic.topic_users.find { |tu| tu.user_id == scope.user.id }
end
def bumped_at
topic.bumped_at
end
def raw
post.raw
end
def cooked
@cooked ||= \
if object.for_topic && last_read_post_number.present?
for_topic_cooked_post
else
post.cooked
end
end
def for_topic_cooked_post
post_number = [last_read_post_number + 1, highest_post_number].min
posts = Post.where(topic: topic, post_type: Post.types[:regular]).order(:post_number)
first_unread_cooked = posts.where("post_number >= ?", post_number).pluck_first(:cooked)
# 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_cooked || posts.last.cooked
end
def slug
topic.slug
end
def post_user
@post_user ||= post.user
end
def post_user_username
post_user.username
end
def post_user_avatar_template
post_user.avatar_template
end
def post_user_name
post_user.name
end
# TODO (martin) [POLYBOOK] Not relevant once polymorphic bookmarks are implemented.
# Note...these are just stub methods for compatability with the user-bookmark-list.hbs
# changes in a transition period for polymorphic bookmarks.
def bookmarkable_user_username
post_user.username
end
def bookmarkable_user_avatar_template
post_user.avatar_template
end
def bookmarkable_user_name
post_user.name
end
end