2020-03-11 20:16:00 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BookmarkManager
|
|
|
|
include HasErrors
|
|
|
|
|
|
|
|
def initialize(user)
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
2020-05-06 23:37:39 -04:00
|
|
|
def create(post_id:, name: nil, reminder_type: nil, reminder_at: nil, options: {})
|
2020-09-07 00:52:14 -04:00
|
|
|
post = Post.find_by(id: post_id)
|
2020-04-21 20:44:04 -04:00
|
|
|
reminder_type = parse_reminder_type(reminder_type)
|
2020-03-11 20:16:00 -04:00
|
|
|
|
2020-09-07 00:52:14 -04:00
|
|
|
# no bookmarking deleted posts or topics
|
|
|
|
raise Discourse::InvalidAccess if post.blank? || post.topic.blank?
|
|
|
|
|
|
|
|
if !Guardian.new(@user).can_see_post?(post) || !Guardian.new(@user).can_see_topic?(post.topic)
|
|
|
|
raise Discourse::InvalidAccess
|
|
|
|
end
|
2020-03-23 07:04:39 -04:00
|
|
|
|
2020-03-11 20:16:00 -04:00
|
|
|
bookmark = Bookmark.create(
|
2020-05-06 23:37:39 -04:00
|
|
|
{
|
|
|
|
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
|
2020-07-20 20:00:39 -04:00
|
|
|
}.merge(options)
|
2020-03-11 20:16:00 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
if bookmark.errors.any?
|
|
|
|
return add_errors_from(bookmark)
|
|
|
|
end
|
|
|
|
|
2020-07-28 19:43:32 -04:00
|
|
|
update_topic_user_bookmarked(post.topic)
|
2020-04-15 21:32:21 -04:00
|
|
|
|
2020-03-11 20:16:00 -04:00
|
|
|
bookmark
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy(bookmark_id)
|
2021-03-21 19:50:22 -04:00
|
|
|
bookmark = find_bookmark_and_check_access(bookmark_id)
|
2020-03-11 20:16:00 -04:00
|
|
|
|
|
|
|
bookmark.destroy
|
2020-04-19 23:30:04 -04:00
|
|
|
|
2020-07-28 19:43:32 -04:00
|
|
|
bookmarks_remaining_in_topic = update_topic_user_bookmarked(bookmark.topic)
|
2020-04-28 02:19:25 -04:00
|
|
|
|
|
|
|
{ topic_bookmarked: bookmarks_remaining_in_topic }
|
2020-03-11 20:16:00 -04:00
|
|
|
end
|
|
|
|
|
2020-07-28 19:43:32 -04:00
|
|
|
def destroy_for_topic(topic, filter = {}, opts = {})
|
2020-03-11 20:16:00 -04:00
|
|
|
topic_bookmarks = Bookmark.where(user_id: @user.id, topic_id: topic.id)
|
2020-07-28 19:43:32 -04:00
|
|
|
topic_bookmarks = topic_bookmarks.where(filter)
|
2020-03-11 20:16:00 -04:00
|
|
|
|
|
|
|
Bookmark.transaction do
|
|
|
|
topic_bookmarks.each do |bookmark|
|
2020-03-11 20:52:15 -04:00
|
|
|
raise Discourse::InvalidAccess.new if !Guardian.new(@user).can_delete?(bookmark)
|
2020-03-11 20:16:00 -04:00
|
|
|
bookmark.destroy
|
|
|
|
end
|
2020-04-15 21:32:21 -04:00
|
|
|
|
2020-07-28 19:43:32 -04:00
|
|
|
update_topic_user_bookmarked(topic, opts)
|
2020-03-11 20:16:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.send_reminder_notification(id)
|
|
|
|
bookmark = Bookmark.find_by(id: id)
|
|
|
|
BookmarkReminderNotificationHandler.send_notification(bookmark)
|
|
|
|
end
|
|
|
|
|
2020-05-06 23:37:39 -04:00
|
|
|
def update(bookmark_id:, name:, reminder_type:, reminder_at:, options: {})
|
2021-03-21 19:50:22 -04:00
|
|
|
bookmark = find_bookmark_and_check_access(bookmark_id)
|
2020-04-16 21:08:07 -04:00
|
|
|
|
2020-04-21 20:44:04 -04:00
|
|
|
reminder_type = parse_reminder_type(reminder_type)
|
2020-04-16 21:08:07 -04:00
|
|
|
|
2020-04-21 20:44:04 -04:00
|
|
|
success = bookmark.update(
|
2020-05-06 23:37:39 -04:00
|
|
|
{
|
|
|
|
name: name,
|
|
|
|
reminder_at: reminder_at,
|
|
|
|
reminder_type: reminder_type,
|
|
|
|
reminder_set_at: Time.zone.now
|
2020-07-20 20:00:39 -04:00
|
|
|
}.merge(options)
|
2020-04-16 21:08:07 -04:00
|
|
|
)
|
2020-04-21 20:44:04 -04:00
|
|
|
|
|
|
|
if bookmark.errors.any?
|
|
|
|
return add_errors_from(bookmark)
|
|
|
|
end
|
|
|
|
|
|
|
|
success
|
2020-04-16 21:08:07 -04:00
|
|
|
end
|
|
|
|
|
2021-03-21 19:50:22 -04: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 20:16:00 -04:00
|
|
|
private
|
|
|
|
|
2021-03-21 19:50:22 -04:00
|
|
|
def find_bookmark_and_check_access(bookmark_id)
|
|
|
|
bookmark = Bookmark.find_by(id: bookmark_id)
|
|
|
|
raise Discourse::NotFound if !bookmark
|
|
|
|
raise Discourse::InvalidAccess.new if !Guardian.new(@user).can_edit?(bookmark)
|
|
|
|
bookmark
|
|
|
|
end
|
|
|
|
|
2020-07-28 19:43:32 -04: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
|
|
|
|
bookmarks_remaining_in_topic = Bookmark.exists?(topic_id: topic.id, user: @user)
|
|
|
|
return bookmarks_remaining_in_topic if opts.key?(:auto_track) && !opts[:auto_track]
|
|
|
|
|
|
|
|
TopicUser.change(@user.id, topic, bookmarked: bookmarks_remaining_in_topic)
|
|
|
|
bookmarks_remaining_in_topic
|
2020-04-15 21:32:21 -04:00
|
|
|
end
|
2020-04-21 20:44:04 -04: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 20:16:00 -04:00
|
|
|
end
|