2024-05-22 11:50:21 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BookmarksBulkAction
|
|
|
|
def initialize(user, bookmark_ids, operation, options = {})
|
|
|
|
@user = user
|
|
|
|
@bookmark_ids = bookmark_ids
|
|
|
|
@operation = operation
|
|
|
|
@changed_ids = []
|
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.operations
|
|
|
|
@operations ||= %w[clear_reminder delete]
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform!
|
2024-05-27 06:27:13 -04:00
|
|
|
if BookmarksBulkAction.operations.exclude?(@operation[:type])
|
2024-05-22 11:50:21 -04:00
|
|
|
raise Discourse::InvalidParameters.new(:operation)
|
|
|
|
end
|
2024-08-25 19:17:39 -04:00
|
|
|
|
|
|
|
case @operation[:type]
|
|
|
|
when "clear_reminder"
|
|
|
|
clear_reminder
|
|
|
|
when "delete"
|
|
|
|
delete
|
|
|
|
end
|
|
|
|
|
2024-05-22 11:50:21 -04:00
|
|
|
@changed_ids.sort
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def delete
|
2024-08-25 19:17:39 -04:00
|
|
|
@bookmark_ids.each do |bookmark_id|
|
|
|
|
if guardian.can_delete?(bookmark_id)
|
|
|
|
BookmarkManager.new(@user).destroy(bookmark_id)
|
|
|
|
@changed_ids << bookmark_id
|
2024-05-22 11:50:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_reminder
|
2024-08-25 19:17:39 -04:00
|
|
|
bookmarks.each do |bookmark|
|
|
|
|
if guardian.can_edit?(bookmark)
|
|
|
|
bookmark.clear_reminder!(force_clear_reminder_at: true)
|
|
|
|
@changed_ids << bookmark.id
|
2024-05-22 11:50:21 -04:00
|
|
|
else
|
|
|
|
raise Discourse::InvalidAccess.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def guardian
|
|
|
|
@guardian ||= Guardian.new(@user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def bookmarks
|
|
|
|
@bookmarks ||= Bookmark.where(id: @bookmark_ids)
|
|
|
|
end
|
|
|
|
end
|