Files
discourse/app/serializers/user_bookmark_serializer.rb
Martin Brennan 27699648ef FEATURE: Go to last unread for topic-level bookmark links (#14396)
Instead of going to the OP of the topic for topic-level bookmarks
(which are bookmarks where for_topic is true) when clicking on the
bookmark in the quick access menu or on the user bookmark list,
this commit takes the user to the last unread post in
the topic instead. This should be generally more useful than landing
on the unchanging OP.

To make this work nicely, I needed to add the last_read_post_number to
the BookmarkQuery based on the TopicUser association. It should not add
too much extra weight to the query, because it is limited to the user
that we are fetching bookmarks for.

Also fixed an issue where the bookmark serializer highest_post_number was
not taking into account whether the user was staff, which is when we
should use highest_staff_post_number instead.
2021-09-21 13:49:56 +10:00

135 lines
2.1 KiB
Ruby

# frozen_string_literal: true
require_relative 'post_item_excerpt'
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
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
post.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
end