2014-01-30 10:15:49 -06:00
|
|
|
class TopicsBulkAction
|
|
|
|
|
|
|
|
def initialize(user, topic_ids, operation)
|
|
|
|
@user = user
|
|
|
|
@topic_ids = topic_ids
|
|
|
|
@operation = operation
|
2014-01-30 11:44:28 -06:00
|
|
|
@changed_ids = []
|
2014-01-30 10:15:49 -06:00
|
|
|
end
|
|
|
|
|
2014-01-30 10:43:01 -06:00
|
|
|
def self.operations
|
2014-08-11 14:13:44 -05:00
|
|
|
%w(change_category close change_notification_level reset_read dismiss_posts delete)
|
2014-01-30 10:43:01 -06:00
|
|
|
end
|
|
|
|
|
2014-01-30 10:15:49 -06:00
|
|
|
def perform!
|
2014-01-30 10:43:01 -06:00
|
|
|
raise Discourse::InvalidParameters.new(:operation) unless TopicsBulkAction.operations.include?(@operation[:type])
|
|
|
|
send(@operation[:type])
|
2014-01-30 11:44:28 -06:00
|
|
|
@changed_ids
|
2014-01-30 10:15:49 -06:00
|
|
|
end
|
|
|
|
|
2014-01-30 10:43:01 -06:00
|
|
|
private
|
|
|
|
|
2014-08-08 01:29:51 -05:00
|
|
|
def dismiss_posts
|
|
|
|
sql = "
|
|
|
|
UPDATE topic_users tu
|
|
|
|
SET seen_post_count = t.posts_count , last_read_post_number = highest_post_number
|
|
|
|
FROM topics t
|
|
|
|
WHERE t.id = tu.topic_id AND tu.user_id = :user_id AND t.id IN (:topic_ids)
|
|
|
|
"
|
|
|
|
|
|
|
|
Topic.exec_sql(sql, user_id: @user.id, topic_ids: @topic_ids)
|
|
|
|
@changed_ids.concat @topic_ids
|
|
|
|
end
|
|
|
|
|
2014-02-21 12:03:50 -06:00
|
|
|
def reset_read
|
|
|
|
PostTiming.destroy_for(@user.id, @topic_ids)
|
|
|
|
end
|
|
|
|
|
2014-01-30 10:43:01 -06:00
|
|
|
def change_category
|
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_edit?(t)
|
2014-07-16 14:39:39 -05:00
|
|
|
@changed_ids << t.id if t.change_category_to_id(@operation[:category_id])
|
2014-01-30 11:44:28 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-14 16:38:55 -06: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-30 11:44:28 -06:00
|
|
|
def close
|
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_moderate?(t)
|
|
|
|
t.update_status('closed', true, @user)
|
|
|
|
@changed_ids << t.id
|
2014-01-30 10:43:01 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-11 14:13:44 -05:00
|
|
|
def delete
|
|
|
|
topics.each do |t|
|
|
|
|
t.trash! if guardian.can_delete?(t)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-30 10:43:01 -06:00
|
|
|
def guardian
|
|
|
|
@guardian ||= Guardian.new(@user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def topics
|
|
|
|
@topics ||= Topic.where(id: @topic_ids)
|
|
|
|
end
|
|
|
|
|
2014-08-11 14:13:44 -05:00
|
|
|
|
2014-01-30 10:15:49 -06:00
|
|
|
end
|
|
|
|
|