2020-03-12 08:16:00 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BookmarkManager
|
|
|
|
include HasErrors
|
|
|
|
|
|
|
|
def initialize(user)
|
|
|
|
@user = user
|
2022-03-30 10:43:11 +08:00
|
|
|
@guardian = Guardian.new(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.bookmark_metadata(bookmark, user)
|
2022-05-23 08:07:15 +08:00
|
|
|
bookmark.registered_bookmarkable.bookmark_metadata(bookmark, user)
|
2022-03-30 10:43:11 +08:00
|
|
|
end
|
|
|
|
|
2022-05-23 08:07:15 +08:00
|
|
|
##
|
|
|
|
# Creates a bookmark for a registered bookmarkable (see Bookmark.register_bookmarkable
|
|
|
|
# and RegisteredBookmarkable for details on this).
|
|
|
|
#
|
|
|
|
# Only allows creation of bookmarks for records 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.
|
|
|
|
#
|
|
|
|
# Before creation validations, after create callbacks, and after delete
|
|
|
|
# callbacks are all RegisteredBookmarkable specific and should be defined
|
|
|
|
# there.
|
|
|
|
#
|
|
|
|
# @param [Integer] bookmarkable_id The ID of the ActiveRecord model to attach the bookmark to.
|
|
|
|
# @param [String] bookmarkable_type The class name of the ActiveRecord model to attach the bookmark to.
|
|
|
|
# @param [String] 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
|
|
|
|
# @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.
|
2022-03-30 10:43:11 +08:00
|
|
|
def create_for(bookmarkable_id:, bookmarkable_type:, name: nil, reminder_at: nil, options: {})
|
2022-05-11 07:51:03 +08:00
|
|
|
registered_bookmarkable = Bookmark.registered_bookmarkable_from_type(bookmarkable_type)
|
2023-01-05 06:43:58 +08:00
|
|
|
|
|
|
|
if registered_bookmarkable.blank?
|
|
|
|
return add_error(I18n.t("bookmarks.errors.invalid_bookmarkable", type: bookmarkable_type))
|
|
|
|
end
|
|
|
|
|
2022-05-24 09:13:21 +08:00
|
|
|
bookmarkable = registered_bookmarkable.model.find_by(id: bookmarkable_id)
|
2022-05-11 07:51:03 +08:00
|
|
|
registered_bookmarkable.validate_before_create(@guardian, bookmarkable)
|
2022-03-30 10:43:11 +08:00
|
|
|
|
2023-01-05 06:43:58 +08:00
|
|
|
bookmark =
|
|
|
|
Bookmark.create(
|
|
|
|
{
|
|
|
|
user_id: @user.id,
|
|
|
|
bookmarkable: bookmarkable,
|
|
|
|
name: name,
|
|
|
|
reminder_at: reminder_at,
|
|
|
|
reminder_set_at: Time.zone.now,
|
|
|
|
}.merge(bookmark_model_options_with_defaults(options)),
|
|
|
|
)
|
2022-03-30 10:43:11 +08:00
|
|
|
|
|
|
|
return add_errors_from(bookmark) if bookmark.errors.any?
|
|
|
|
|
2022-05-11 07:51:03 +08:00
|
|
|
registered_bookmarkable.after_create(@guardian, bookmark, options)
|
2022-03-30 10:43:11 +08:00
|
|
|
|
|
|
|
bookmark
|
2020-03-12 08:16:00 +08:00
|
|
|
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
|
|
|
|
2022-05-23 08:07:15 +08:00
|
|
|
bookmark.registered_bookmarkable.after_destroy(@guardian, bookmark)
|
2020-04-28 14:19:25 +08:00
|
|
|
|
2022-03-30 10:43:11 +08:00
|
|
|
bookmark
|
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|
|
2022-03-30 10:43:11 +08:00
|
|
|
raise Discourse::InvalidAccess.new if !@guardian.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)
|
2022-05-23 08:07:15 +08:00
|
|
|
BookmarkReminderNotificationHandler.new(Bookmark.find_by(id: id)).send_notification
|
2020-03-12 08:16:00 +08:00
|
|
|
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
|
|
|
|
2022-03-09 01:44:18 +08:00
|
|
|
if bookmark.reminder_at != reminder_at
|
|
|
|
bookmark.reminder_at = reminder_at
|
|
|
|
bookmark.reminder_last_sent_at = nil
|
|
|
|
end
|
|
|
|
|
2023-01-05 06:43:58 +08:00
|
|
|
success =
|
|
|
|
bookmark.update(
|
|
|
|
{ name: name, reminder_set_at: Time.zone.now }.merge(
|
|
|
|
bookmark_model_options_with_defaults(options),
|
|
|
|
),
|
|
|
|
)
|
2020-04-22 08:44:04 +08:00
|
|
|
|
2023-01-05 06:43:58 +08:00
|
|
|
return add_errors_from(bookmark) if bookmark.errors.any?
|
2020-04-22 08:44:04 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-01-05 06:43:58 +08:00
|
|
|
return add_errors_from(bookmark) if bookmark.errors.any?
|
2021-03-22 07:50:22 +08:00
|
|
|
|
|
|
|
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
|
2022-03-30 10:43:11 +08:00
|
|
|
raise Discourse::InvalidAccess.new if !@guardian.can_edit?(bookmark)
|
2021-03-22 07:50:22 +08:00
|
|
|
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
|
2022-03-30 10:43:11 +08:00
|
|
|
return if opts.key?(:auto_track) && !opts[:auto_track]
|
2023-01-05 06:43:58 +08:00
|
|
|
TopicUser.change(
|
|
|
|
@user.id,
|
|
|
|
topic,
|
|
|
|
bookmarked: Bookmark.for_user_in_topic(@user.id, topic.id).exists?,
|
|
|
|
)
|
2020-04-16 09:32:21 +08:00
|
|
|
end
|
2022-03-09 01:44:18 +08:00
|
|
|
|
2022-05-24 09:13:21 +08:00
|
|
|
def bookmark_model_options_with_defaults(options)
|
2023-01-05 06:43:58 +08:00
|
|
|
model_options = { pinned: options[:pinned] }
|
2022-08-30 07:21:41 +08:00
|
|
|
|
2022-05-24 09:13:21 +08:00
|
|
|
if options[:auto_delete_preference].blank?
|
2023-01-05 06:43:58 +08:00
|
|
|
model_options[:auto_delete_preference] = if user_auto_delete_preference.present?
|
|
|
|
user_auto_delete_preference
|
|
|
|
else
|
|
|
|
Bookmark.auto_delete_preferences[:clear_reminder]
|
|
|
|
end
|
2022-08-30 07:21:41 +08:00
|
|
|
else
|
|
|
|
model_options[:auto_delete_preference] = options[:auto_delete_preference]
|
2022-05-24 09:13:21 +08:00
|
|
|
end
|
|
|
|
|
2022-08-30 07:21:41 +08:00
|
|
|
model_options
|
2022-03-09 01:44:18 +08:00
|
|
|
end
|
2023-01-05 06:43:58 +08:00
|
|
|
|
|
|
|
def user_auto_delete_preference
|
|
|
|
@guardian.user.user_option&.bookmark_auto_delete_preference
|
|
|
|
end
|
2020-03-12 08:16:00 +08:00
|
|
|
end
|