2020-03-11 19:16:00 -05:00
|
|
|
# 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)
|
2020-03-23 06:04:39 -05:00
|
|
|
post = Post.unscoped.includes(:topic).find(post_id)
|
2020-04-21 19:44:04 -05:00
|
|
|
reminder_type = parse_reminder_type(reminder_type)
|
2020-03-11 19:16:00 -05:00
|
|
|
|
2020-03-23 06:04:39 -05:00
|
|
|
raise Discourse::InvalidAccess.new if !Guardian.new(@user).can_see_post?(post)
|
|
|
|
|
2020-03-11 19:16:00 -05:00
|
|
|
bookmark = Bookmark.create(
|
|
|
|
user_id: @user.id,
|
2020-03-23 06:04:39 -05:00
|
|
|
topic: post.topic,
|
|
|
|
post: post,
|
2020-03-11 19:16:00 -05:00
|
|
|
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
|
|
|
|
|
2020-04-28 01:19:25 -05:00
|
|
|
update_topic_user_bookmarked(topic: post.topic, bookmarked: true)
|
2020-04-15 20:32:21 -05:00
|
|
|
|
2020-03-11 19:16:00 -05:00
|
|
|
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
|
2020-04-19 22:30:04 -05:00
|
|
|
|
2020-04-28 01:19:25 -05:00
|
|
|
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 }
|
2020-03-11 19:16:00 -05:00
|
|
|
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|
|
2020-03-11 19:52:15 -05:00
|
|
|
raise Discourse::InvalidAccess.new if !Guardian.new(@user).can_delete?(bookmark)
|
2020-03-11 19:16:00 -05:00
|
|
|
bookmark.destroy
|
|
|
|
end
|
2020-04-15 20:32:21 -05:00
|
|
|
|
|
|
|
update_topic_user_bookmarked(topic: topic, bookmarked: false)
|
2020-03-11 19:16:00 -05:00
|
|
|
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
|
|
|
|
|
2020-04-16 20:08:07 -05:00
|
|
|
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)
|
|
|
|
|
2020-04-21 19:44:04 -05:00
|
|
|
reminder_type = parse_reminder_type(reminder_type)
|
2020-04-16 20:08:07 -05:00
|
|
|
|
2020-04-21 19:44:04 -05:00
|
|
|
success = bookmark.update(
|
2020-04-16 20:08:07 -05:00
|
|
|
name: name,
|
|
|
|
reminder_at: reminder_at,
|
|
|
|
reminder_type: reminder_type,
|
|
|
|
reminder_set_at: Time.zone.now
|
|
|
|
)
|
2020-04-21 19:44:04 -05:00
|
|
|
|
|
|
|
if bookmark.errors.any?
|
|
|
|
return add_errors_from(bookmark)
|
|
|
|
end
|
|
|
|
|
|
|
|
success
|
2020-04-16 20:08:07 -05:00
|
|
|
end
|
|
|
|
|
2020-03-11 19:16:00 -05:00
|
|
|
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
|
2020-04-15 20:32:21 -05:00
|
|
|
|
|
|
|
def update_topic_user_bookmarked(topic:, bookmarked:)
|
|
|
|
TopicUser.change(@user.id, topic, bookmarked: bookmarked)
|
|
|
|
end
|
2020-04-21 19:44:04 -05:00
|
|
|
|
|
|
|
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
|
2020-03-11 19:16:00 -05:00
|
|
|
end
|