mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 11:15:05 +08:00
5108cf8ddf
Make sure the topic_user.bookmarked column is set correctly when user bookmarks/unbookmarks any post in a topic. For example, you bookmarked a post in the topic that was not the OP, the bookmark icon in the topic list would not be shown. Same if deleting a bookmark for the last bookmarked post in a topic, the bookmark icon in the topic list would not be removed. Previously this was only setting it to true if bookmarking the OP/topic, which was not correct -- we want to show the icon on the topic list if any post is bookmarked. Also set to false if unbookmarking the last bookmarked post in the topic. Also in this PR is a migration to correct any out of sync topic_user.bookmarked columns, based on the new logic.
115 lines
3.1 KiB
Ruby
115 lines
3.1 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)
|
|
post = Post.unscoped.includes(:topic).find(post_id)
|
|
reminder_type = parse_reminder_type(reminder_type)
|
|
|
|
raise Discourse::InvalidAccess.new if !Guardian.new(@user).can_see_post?(post)
|
|
|
|
bookmark = Bookmark.create(
|
|
user_id: @user.id,
|
|
topic: post.topic,
|
|
post: post,
|
|
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
|
|
|
|
update_topic_user_bookmarked(topic: post.topic, bookmarked: true)
|
|
|
|
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
|
|
|
|
bookmarks_remaining_in_topic = Bookmark.exists?(topic_id: bookmark.topic_id, user: @user)
|
|
if !bookmarks_remaining_in_topic
|
|
update_topic_user_bookmarked(topic: bookmark.topic, bookmarked: false)
|
|
end
|
|
|
|
{ topic_bookmarked: bookmarks_remaining_in_topic }
|
|
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
|
|
|
|
update_topic_user_bookmarked(topic: topic, bookmarked: false)
|
|
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
|
|
|
|
def update(bookmark_id:, name:, reminder_type:, reminder_at:)
|
|
bookmark = Bookmark.find_by(id: bookmark_id)
|
|
|
|
raise Discourse::NotFound if bookmark.blank?
|
|
raise Discourse::InvalidAccess.new if !Guardian.new(@user).can_edit?(bookmark)
|
|
|
|
reminder_type = parse_reminder_type(reminder_type)
|
|
|
|
success = bookmark.update(
|
|
name: name,
|
|
reminder_at: reminder_at,
|
|
reminder_type: reminder_type,
|
|
reminder_set_at: Time.zone.now
|
|
)
|
|
|
|
if bookmark.errors.any?
|
|
return add_errors_from(bookmark)
|
|
end
|
|
|
|
success
|
|
end
|
|
|
|
private
|
|
|
|
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
|
|
|
|
def update_topic_user_bookmarked(topic:, bookmarked:)
|
|
TopicUser.change(@user.id, topic, bookmarked: bookmarked)
|
|
end
|
|
|
|
def parse_reminder_type(reminder_type)
|
|
return if reminder_type.blank?
|
|
reminder_type.is_a?(Integer) ? reminder_type : Bookmark.reminder_types[reminder_type.to_sym]
|
|
end
|
|
end
|