2020-03-11 19:16:00 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BookmarkManager
|
|
|
|
include HasErrors
|
|
|
|
|
|
|
|
def initialize(user)
|
|
|
|
@user = user
|
2022-03-29 21:43:11 -05:00
|
|
|
@guardian = Guardian.new(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.bookmark_metadata(bookmark, user)
|
|
|
|
data = {}
|
|
|
|
if SiteSetting.use_polymorphic_bookmarks
|
|
|
|
if bookmark.bookmarkable_type == "Topic"
|
|
|
|
data[:topic_bookmarked] = Bookmark.for_user_in_topic(user.id, bookmark.bookmarkable.id).exists?
|
|
|
|
elsif bookmark.bookmarkable_type == "Post"
|
|
|
|
data[:topic_bookmarked] = Bookmark.for_user_in_topic(user.id, bookmark.bookmarkable.topic.id).exists?
|
|
|
|
end
|
|
|
|
else
|
|
|
|
data[:topic_bookmarked] = Bookmark.for_user_in_topic(user.id, bookmark.topic.id).exists?
|
|
|
|
end
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO (martin) [POLYBOOK] This will be used in place of #create once
|
|
|
|
# polymorphic bookmarks are implemented.
|
|
|
|
def create_for(bookmarkable_id:, bookmarkable_type:, name: nil, reminder_at: nil, options: {})
|
|
|
|
raise NotImplementedError if !SiteSetting.use_polymorphic_bookmarks
|
|
|
|
|
|
|
|
bookmarkable = bookmarkable_type.constantize.find_by(id: bookmarkable_id)
|
|
|
|
self.send("validate_bookmarkable_#{bookmarkable_type.downcase}", bookmarkable)
|
|
|
|
|
|
|
|
bookmark = Bookmark.create(
|
|
|
|
{
|
|
|
|
user_id: @user.id,
|
|
|
|
bookmarkable: bookmarkable,
|
|
|
|
name: name,
|
|
|
|
reminder_at: reminder_at,
|
|
|
|
reminder_set_at: Time.zone.now
|
|
|
|
}.merge(options)
|
|
|
|
)
|
|
|
|
|
|
|
|
return add_errors_from(bookmark) if bookmark.errors.any?
|
|
|
|
|
|
|
|
self.send("after_create_bookmarkable_#{bookmarkable_type.downcase}", bookmarkable)
|
|
|
|
update_user_option(bookmark)
|
|
|
|
|
|
|
|
bookmark
|
2020-03-11 19:16:00 -05:00
|
|
|
end
|
|
|
|
|
2021-09-20 17:45:47 -05: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-06 23:52:14 -05:00
|
|
|
post = Post.find_by(id: post_id)
|
2022-03-29 21:43:11 -05:00
|
|
|
validate_bookmarkable_post(post)
|
2020-03-23 06:04:39 -05:00
|
|
|
|
2020-03-11 19:16:00 -05:00
|
|
|
bookmark = Bookmark.create(
|
2020-05-06 22:37:39 -05:00
|
|
|
{
|
|
|
|
user_id: @user.id,
|
|
|
|
post: post,
|
|
|
|
name: name,
|
|
|
|
reminder_at: reminder_at,
|
2021-09-20 17:45:47 -05:00
|
|
|
reminder_set_at: Time.zone.now,
|
|
|
|
for_topic: for_topic
|
2020-07-20 19:00:39 -05:00
|
|
|
}.merge(options)
|
2020-03-11 19:16:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
if bookmark.errors.any?
|
|
|
|
return add_errors_from(bookmark)
|
|
|
|
end
|
|
|
|
|
2020-07-28 18:43:32 -05:00
|
|
|
update_topic_user_bookmarked(post.topic)
|
2022-03-08 11:44:18 -06:00
|
|
|
update_user_option(bookmark)
|
2020-04-15 20:32:21 -05:00
|
|
|
|
2020-03-11 19:16:00 -05:00
|
|
|
bookmark
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy(bookmark_id)
|
2021-03-21 18:50:22 -05:00
|
|
|
bookmark = find_bookmark_and_check_access(bookmark_id)
|
2020-03-11 19:16:00 -05:00
|
|
|
|
|
|
|
bookmark.destroy
|
2020-04-19 22:30:04 -05:00
|
|
|
|
2022-03-29 21:43:11 -05:00
|
|
|
if SiteSetting.use_polymorphic_bookmarks
|
|
|
|
self.send("after_destroy_bookmarkable_#{bookmark.bookmarkable_type.downcase}", bookmark)
|
|
|
|
else
|
|
|
|
update_topic_user_bookmarked(bookmark.topic)
|
|
|
|
end
|
2020-04-28 01:19:25 -05:00
|
|
|
|
2022-03-29 21:43:11 -05:00
|
|
|
bookmark
|
2020-03-11 19:16:00 -05:00
|
|
|
end
|
|
|
|
|
2020-07-28 18:43:32 -05:00
|
|
|
def destroy_for_topic(topic, filter = {}, opts = {})
|
2021-09-14 19:16:54 -05:00
|
|
|
topic_bookmarks = Bookmark.for_user_in_topic(@user.id, topic.id)
|
2020-07-28 18:43:32 -05:00
|
|
|
topic_bookmarks = topic_bookmarks.where(filter)
|
2020-03-11 19:16:00 -05:00
|
|
|
|
|
|
|
Bookmark.transaction do
|
|
|
|
topic_bookmarks.each do |bookmark|
|
2022-03-29 21:43:11 -05:00
|
|
|
raise Discourse::InvalidAccess.new if !@guardian.can_delete?(bookmark)
|
2020-03-11 19:16:00 -05:00
|
|
|
bookmark.destroy
|
|
|
|
end
|
2020-04-15 20:32:21 -05:00
|
|
|
|
2020-07-28 18:43:32 -05:00
|
|
|
update_topic_user_bookmarked(topic, opts)
|
2020-03-11 19:16:00 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.send_reminder_notification(id)
|
|
|
|
bookmark = Bookmark.find_by(id: id)
|
2022-05-08 18:37:23 -05:00
|
|
|
BookmarkReminderNotificationHandler.new(bookmark).send_notification
|
2020-03-11 19:16:00 -05:00
|
|
|
end
|
|
|
|
|
2022-01-03 19:19:27 -06:00
|
|
|
def update(bookmark_id:, name:, reminder_at:, options: {})
|
2021-03-21 18:50:22 -05:00
|
|
|
bookmark = find_bookmark_and_check_access(bookmark_id)
|
2020-04-16 20:08:07 -05:00
|
|
|
|
2022-03-08 11:44:18 -06:00
|
|
|
if bookmark.reminder_at != reminder_at
|
|
|
|
bookmark.reminder_at = reminder_at
|
|
|
|
bookmark.reminder_last_sent_at = nil
|
|
|
|
end
|
|
|
|
|
2020-04-21 19:44:04 -05:00
|
|
|
success = bookmark.update(
|
2020-05-06 22:37:39 -05:00
|
|
|
{
|
|
|
|
name: name,
|
2022-03-08 11:44:18 -06:00
|
|
|
reminder_set_at: Time.zone.now,
|
2020-07-20 19:00:39 -05:00
|
|
|
}.merge(options)
|
2020-04-16 20:08:07 -05:00
|
|
|
)
|
2020-04-21 19:44:04 -05:00
|
|
|
|
|
|
|
if bookmark.errors.any?
|
|
|
|
return add_errors_from(bookmark)
|
|
|
|
end
|
|
|
|
|
2022-03-08 11:44:18 -06:00
|
|
|
update_user_option(bookmark)
|
|
|
|
|
2020-04-21 19:44:04 -05:00
|
|
|
success
|
2020-04-16 20:08:07 -05:00
|
|
|
end
|
|
|
|
|
2021-03-21 18:50:22 -05: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-11 19:16:00 -05:00
|
|
|
private
|
|
|
|
|
2021-03-21 18:50:22 -05:00
|
|
|
def find_bookmark_and_check_access(bookmark_id)
|
|
|
|
bookmark = Bookmark.find_by(id: bookmark_id)
|
|
|
|
raise Discourse::NotFound if !bookmark
|
2022-03-29 21:43:11 -05:00
|
|
|
raise Discourse::InvalidAccess.new if !@guardian.can_edit?(bookmark)
|
2021-03-21 18:50:22 -05:00
|
|
|
bookmark
|
|
|
|
end
|
|
|
|
|
2020-07-28 18:43:32 -05: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
|
2022-03-29 21:43:11 -05:00
|
|
|
return if opts.key?(:auto_track) && !opts[:auto_track]
|
|
|
|
TopicUser.change(@user.id, topic, bookmarked: Bookmark.for_user_in_topic(@user.id, topic.id).exists?)
|
2020-04-15 20:32:21 -05:00
|
|
|
end
|
2022-03-08 11:44:18 -06:00
|
|
|
|
|
|
|
def update_user_option(bookmark)
|
|
|
|
@user.user_option.update!(bookmark_auto_delete_preference: bookmark.auto_delete_preference)
|
|
|
|
end
|
2022-03-29 21:43:11 -05:00
|
|
|
|
|
|
|
def after_create_bookmarkable_post(post, opts = {})
|
|
|
|
update_topic_user_bookmarked(post.topic, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_create_bookmarkable_topic(topic, opts = {})
|
|
|
|
update_topic_user_bookmarked(topic, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_destroy_bookmarkable_post(bookmark)
|
|
|
|
update_topic_user_bookmarked(bookmark.bookmarkable.topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_destroy_bookmarkable_topic(bookmark)
|
|
|
|
update_topic_user_bookmarked(bookmark.bookmarkable)
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_bookmarkable_post(post)
|
|
|
|
# no bookmarking deleted posts or topics
|
|
|
|
raise Discourse::InvalidAccess if post.blank? || !@guardian.can_see_post?(post)
|
|
|
|
validate_bookmarkable_topic(post.topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_bookmarkable_topic(topic)
|
|
|
|
# no bookmarking deleted posts or topics
|
|
|
|
raise Discourse::InvalidAccess if topic.blank? || !@guardian.can_see_topic?(topic)
|
|
|
|
end
|
2020-03-11 19:16:00 -05:00
|
|
|
end
|