2014-01-31 00:15:49 +08:00
|
|
|
class TopicsBulkAction
|
|
|
|
|
|
|
|
def initialize(user, topic_ids, operation)
|
|
|
|
@user = user
|
|
|
|
@topic_ids = topic_ids
|
|
|
|
@operation = operation
|
2014-01-31 01:44:28 +08:00
|
|
|
@changed_ids = []
|
2014-01-31 00:15:49 +08:00
|
|
|
end
|
|
|
|
|
2014-01-31 00:43:01 +08:00
|
|
|
def self.operations
|
2014-02-15 06:38:55 +08:00
|
|
|
%w(change_category close change_notification_level)
|
2014-01-31 00:43:01 +08:00
|
|
|
end
|
|
|
|
|
2014-01-31 00:15:49 +08:00
|
|
|
def perform!
|
2014-01-31 00:43:01 +08:00
|
|
|
raise Discourse::InvalidParameters.new(:operation) unless TopicsBulkAction.operations.include?(@operation[:type])
|
|
|
|
send(@operation[:type])
|
2014-01-31 01:44:28 +08:00
|
|
|
@changed_ids
|
2014-01-31 00:15:49 +08:00
|
|
|
end
|
|
|
|
|
2014-01-31 00:43:01 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def change_category
|
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_edit?(t)
|
2014-01-31 01:44:28 +08:00
|
|
|
@changed_ids << t.id if t.change_category(@operation[:category_name])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-15 06:38:55 +08:00
|
|
|
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
|
|
|
|
|
2014-01-31 01:44:28 +08:00
|
|
|
def close
|
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_moderate?(t)
|
|
|
|
t.update_status('closed', true, @user)
|
|
|
|
@changed_ids << t.id
|
2014-01-31 00:43:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def guardian
|
|
|
|
@guardian ||= Guardian.new(@user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def topics
|
|
|
|
@topics ||= Topic.where(id: @topic_ids)
|
|
|
|
end
|
|
|
|
|
2014-01-31 00:15:49 +08:00
|
|
|
end
|
|
|
|
|