2020-03-12 08:16:00 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BookmarkManager
|
|
|
|
include HasErrors
|
|
|
|
|
|
|
|
def initialize(user)
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
2021-09-21 06:45:47 +08:00
|
|
|
##
|
|
|
|
# Creates a bookmark for a post where both the post and the topic are
|
|
|
|
# not deleted. Only allows creation of bookmarks for posts the user
|
|
|
|
# can access via Guardian.
|
|
|
|
#
|
|
|
|
# Any ActiveModel validation errors raised by the Bookmark model are
|
|
|
|
# hoisted to the instance of this class for further reporting.
|
|
|
|
#
|
|
|
|
# Also handles setting the associated TopicUser.bookmarked value for
|
|
|
|
# the post's topic for the user that is creating the bookmark.
|
|
|
|
#
|
|
|
|
# @param post_id A post ID for a post that is not deleted.
|
|
|
|
# @param name A short note for the bookmark, shown on the user bookmark list
|
|
|
|
# and on hover of reminder notifications.
|
|
|
|
# @param reminder_at The datetime when a bookmark reminder should be sent after.
|
|
|
|
# Note this is not the exact time a reminder will be sent, as
|
|
|
|
# we send reminders on a rolling schedule.
|
|
|
|
# See Jobs::BookmarkReminderNotifications
|
|
|
|
# @param for_topic Whether we are creating a topic-level bookmark which
|
|
|
|
# has different behaviour in the UI. Only bookmarks for
|
|
|
|
# posts with post_number 1 can be marked as for_topic.
|
|
|
|
# @params options Additional options when creating a bookmark
|
|
|
|
# - auto_delete_preference:
|
|
|
|
# See Bookmark.auto_delete_preferences,
|
|
|
|
# this is used to determine when to delete a bookmark
|
|
|
|
# automatically.
|
|
|
|
def create(
|
|
|
|
post_id:,
|
|
|
|
name: nil,
|
|
|
|
reminder_at: nil,
|
|
|
|
for_topic: false,
|
|
|
|
options: {}
|
|
|
|
)
|
2020-09-07 12:52:14 +08:00
|
|
|
post = Post.find_by(id: post_id)
|
2020-03-12 08:16:00 +08:00
|
|
|
|
2020-09-07 12:52:14 +08:00
|
|
|
# no bookmarking deleted posts or topics
|
|
|
|
raise Discourse::InvalidAccess if post.blank? || post.topic.blank?
|
|
|
|
|
|
|
|
if !Guardian.new(@user).can_see_post?(post) || !Guardian.new(@user).can_see_topic?(post.topic)
|
|
|
|
raise Discourse::InvalidAccess
|
|
|
|
end
|
2020-03-23 19:04:39 +08:00
|
|
|
|
2020-03-12 08:16:00 +08:00
|
|
|
bookmark = Bookmark.create(
|
2020-05-07 11:37:39 +08:00
|
|
|
{
|
|
|
|
user_id: @user.id,
|
|
|
|
post: post,
|
|
|
|
name: name,
|
|
|
|
reminder_at: reminder_at,
|
2021-09-21 06:45:47 +08:00
|
|
|
reminder_set_at: Time.zone.now,
|
|
|
|
for_topic: for_topic
|
2020-07-21 08:00:39 +08:00
|
|
|
}.merge(options)
|
2020-03-12 08:16:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if bookmark.errors.any?
|
|
|
|
return add_errors_from(bookmark)
|
|
|
|
end
|
|
|
|
|
2020-07-29 07:43:32 +08:00
|
|
|
update_topic_user_bookmarked(post.topic)
|
2020-04-16 09:32:21 +08:00
|
|
|
|
2020-03-12 08:16:00 +08:00
|
|
|
bookmark
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy(bookmark_id)
|
2021-03-22 07:50:22 +08:00
|
|
|
bookmark = find_bookmark_and_check_access(bookmark_id)
|
2020-03-12 08:16:00 +08:00
|
|
|
|
|
|
|
bookmark.destroy
|
2020-04-20 11:30:04 +08:00
|
|
|
|
2020-07-29 07:43:32 +08:00
|
|
|
bookmarks_remaining_in_topic = update_topic_user_bookmarked(bookmark.topic)
|
2020-04-28 14:19:25 +08:00
|
|
|
|
|
|
|
{ topic_bookmarked: bookmarks_remaining_in_topic }
|
2020-03-12 08:16:00 +08:00
|
|
|
end
|
|
|
|
|
2020-07-29 07:43:32 +08:00
|
|
|
def destroy_for_topic(topic, filter = {}, opts = {})
|
2021-09-15 08:16:54 +08:00
|
|
|
topic_bookmarks = Bookmark.for_user_in_topic(@user.id, topic.id)
|
2020-07-29 07:43:32 +08:00
|
|
|
topic_bookmarks = topic_bookmarks.where(filter)
|
2020-03-12 08:16:00 +08:00
|
|
|
|
|
|
|
Bookmark.transaction do
|
|
|
|
topic_bookmarks.each do |bookmark|
|
2020-03-12 08:52:15 +08:00
|
|
|
raise Discourse::InvalidAccess.new if !Guardian.new(@user).can_delete?(bookmark)
|
2020-03-12 08:16:00 +08:00
|
|
|
bookmark.destroy
|
|
|
|
end
|
2020-04-16 09:32:21 +08:00
|
|
|
|
2020-07-29 07:43:32 +08:00
|
|
|
update_topic_user_bookmarked(topic, opts)
|
2020-03-12 08:16:00 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.send_reminder_notification(id)
|
|
|
|
bookmark = Bookmark.find_by(id: id)
|
|
|
|
BookmarkReminderNotificationHandler.send_notification(bookmark)
|
|
|
|
end
|
|
|
|
|
2022-01-04 09:19:27 +08:00
|
|
|
def update(bookmark_id:, name:, reminder_at:, options: {})
|
2021-03-22 07:50:22 +08:00
|
|
|
bookmark = find_bookmark_and_check_access(bookmark_id)
|
2020-04-17 09:08:07 +08:00
|
|
|
|
2020-04-22 08:44:04 +08:00
|
|
|
success = bookmark.update(
|
2020-05-07 11:37:39 +08:00
|
|
|
{
|
|
|
|
name: name,
|
|
|
|
reminder_at: reminder_at,
|
|
|
|
reminder_set_at: Time.zone.now
|
2020-07-21 08:00:39 +08:00
|
|
|
}.merge(options)
|
2020-04-17 09:08:07 +08:00
|
|
|
)
|
2020-04-22 08:44:04 +08:00
|
|
|
|
|
|
|
if bookmark.errors.any?
|
|
|
|
return add_errors_from(bookmark)
|
|
|
|
end
|
|
|
|
|
|
|
|
success
|
2020-04-17 09:08:07 +08:00
|
|
|
end
|
|
|
|
|
2021-03-22 07:50:22 +08:00
|
|
|
def toggle_pin(bookmark_id:)
|
|
|
|
bookmark = find_bookmark_and_check_access(bookmark_id)
|
|
|
|
bookmark.pinned = !bookmark.pinned
|
|
|
|
success = bookmark.save
|
|
|
|
|
|
|
|
if bookmark.errors.any?
|
|
|
|
return add_errors_from(bookmark)
|
|
|
|
end
|
|
|
|
|
|
|
|
success
|
|
|
|
end
|
|
|
|
|
2020-03-12 08:16:00 +08:00
|
|
|
private
|
|
|
|
|
2021-03-22 07:50:22 +08:00
|
|
|
def find_bookmark_and_check_access(bookmark_id)
|
|
|
|
bookmark = Bookmark.find_by(id: bookmark_id)
|
|
|
|
raise Discourse::NotFound if !bookmark
|
|
|
|
raise Discourse::InvalidAccess.new if !Guardian.new(@user).can_edit?(bookmark)
|
|
|
|
bookmark
|
|
|
|
end
|
|
|
|
|
2020-07-29 07:43:32 +08:00
|
|
|
def update_topic_user_bookmarked(topic, opts = {})
|
|
|
|
# PostCreator can specify whether auto_track is enabled or not, don't want to
|
|
|
|
# create a TopicUser in that case
|
2021-09-15 08:16:54 +08:00
|
|
|
bookmarks_remaining_in_topic = Bookmark.for_user_in_topic(@user.id, topic.id).exists?
|
2020-07-29 07:43:32 +08:00
|
|
|
return bookmarks_remaining_in_topic if opts.key?(:auto_track) && !opts[:auto_track]
|
|
|
|
|
|
|
|
TopicUser.change(@user.id, topic, bookmarked: bookmarks_remaining_in_topic)
|
|
|
|
bookmarks_remaining_in_topic
|
2020-04-16 09:32:21 +08:00
|
|
|
end
|
2020-03-12 08:16:00 +08:00
|
|
|
end
|