discourse/lib/topics_bulk_action.rb
Robin Ward fb8dda7f42 FIX: We should use category_id instead of category_name to perform
operations, now that the subcategory names are not unique.
2014-07-16 15:40:35 -04:00

62 lines
1.3 KiB
Ruby

class TopicsBulkAction
def initialize(user, topic_ids, operation)
@user = user
@topic_ids = topic_ids
@operation = operation
@changed_ids = []
end
def self.operations
%w(change_category close change_notification_level reset_read)
end
def perform!
raise Discourse::InvalidParameters.new(:operation) unless TopicsBulkAction.operations.include?(@operation[:type])
send(@operation[:type])
@changed_ids
end
private
def reset_read
PostTiming.destroy_for(@user.id, @topic_ids)
end
def change_category
topics.each do |t|
if guardian.can_edit?(t)
@changed_ids << t.id if t.change_category_to_id(@operation[:category_id])
end
end
end
def change_notification_level
topics.each do |t|
if guardian.can_see?(t)
TopicUser.change(@user, t.id, notification_level: @operation[:notification_level_id].to_i)
@changed_ids << t.id
end
end
end
def close
topics.each do |t|
if guardian.can_moderate?(t)
t.update_status('closed', true, @user)
@changed_ids << t.id
end
end
end
def guardian
@guardian ||= Guardian.new(@user)
end
def topics
@topics ||= Topic.where(id: @topic_ids)
end
end