mirror of
https://github.com/discourse/discourse.git
synced 2024-11-29 08:13:51 +08:00
849631188f
A custom date and time can now be selected for a bookmark reminder The reminder will not happen at the exact time but rather at the next 5 minute interval of the bookmark reminder schedule. This PR also fixes issues with bulk deleting topic bookmarks.
74 lines
1.9 KiB
Ruby
74 lines
1.9 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class BookmarkManager
|
|
include HasErrors
|
|
|
|
def initialize(user)
|
|
@user = user
|
|
end
|
|
|
|
def create(post_id:, name: nil, reminder_type: nil, reminder_at: nil)
|
|
reminder_type = Bookmark.reminder_types[reminder_type.to_sym] if reminder_type.present?
|
|
|
|
bookmark = Bookmark.create(
|
|
user_id: @user.id,
|
|
topic_id: topic_id_for_post(post_id),
|
|
post_id: post_id,
|
|
name: name,
|
|
reminder_type: reminder_type,
|
|
reminder_at: reminder_at,
|
|
reminder_set_at: Time.zone.now
|
|
)
|
|
|
|
if bookmark.errors.any?
|
|
return add_errors_from(bookmark)
|
|
end
|
|
|
|
BookmarkReminderNotificationHandler.cache_pending_at_desktop_reminder(@user)
|
|
bookmark
|
|
end
|
|
|
|
def destroy(bookmark_id)
|
|
bookmark = Bookmark.find_by(id: bookmark_id)
|
|
|
|
raise Discourse::NotFound if bookmark.blank?
|
|
raise Discourse::InvalidAccess.new if !Guardian.new(@user).can_delete?(bookmark)
|
|
|
|
bookmark.destroy
|
|
clear_at_desktop_cache_if_required
|
|
end
|
|
|
|
def destroy_for_topic(topic)
|
|
topic_bookmarks = Bookmark.where(user_id: @user.id, topic_id: topic.id)
|
|
|
|
Bookmark.transaction do
|
|
topic_bookmarks.each do |bookmark|
|
|
raise Discourse::InvalidAccess.new if !Guardian.new(@user).can_delete?(bookmark)
|
|
bookmark.destroy
|
|
end
|
|
end
|
|
|
|
clear_at_desktop_cache_if_required
|
|
end
|
|
|
|
def self.send_reminder_notification(id)
|
|
bookmark = Bookmark.find_by(id: id)
|
|
BookmarkReminderNotificationHandler.send_notification(bookmark)
|
|
end
|
|
|
|
private
|
|
|
|
def topic_id_for_post(post_id)
|
|
Post.where(id: post_id).pluck_first(:topic_id)
|
|
end
|
|
|
|
def clear_at_desktop_cache_if_required
|
|
return if user_has_any_pending_at_desktop_reminders?
|
|
Discourse.redis.del(BookmarkReminderNotificationHandler::PENDING_AT_DESKTOP_KEY_PREFIX + @user.id.to_s)
|
|
end
|
|
|
|
def user_has_any_pending_at_desktop_reminders?
|
|
Bookmark.at_desktop_reminders_for_user(@user).any?
|
|
end
|
|
end
|