2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-01-30 11:15:49 -05:00
|
|
|
class TopicsBulkAction
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def initialize(user, topic_ids, operation, options = {})
|
2014-01-30 11:15:49 -05:00
|
|
|
@user = user
|
|
|
|
@topic_ids = topic_ids
|
|
|
|
@operation = operation
|
2014-01-30 12:44:28 -05:00
|
|
|
@changed_ids = []
|
2015-12-22 19:09:17 -05:00
|
|
|
@options = options
|
2014-01-30 11:15:49 -05:00
|
|
|
end
|
|
|
|
|
2014-01-30 11:43:01 -05:00
|
|
|
def self.operations
|
2015-12-22 19:09:17 -05:00
|
|
|
@operations ||= %w(change_category close archive change_notification_level
|
|
|
|
reset_read dismiss_posts delete unlist archive_messages
|
2017-05-10 01:11:52 -04:00
|
|
|
move_messages_to_inbox change_tags append_tags relist)
|
2015-03-06 20:36:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.register_operation(name, &block)
|
|
|
|
operations << name
|
|
|
|
define_method(name, &block)
|
2014-01-30 11:43:01 -05:00
|
|
|
end
|
|
|
|
|
2014-01-30 11:15:49 -05:00
|
|
|
def perform!
|
2014-01-30 11:43:01 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:operation) unless TopicsBulkAction.operations.include?(@operation[:type])
|
2019-05-06 22:22:37 -04:00
|
|
|
# careful these are private methods, we need send
|
2014-01-30 11:43:01 -05:00
|
|
|
send(@operation[:type])
|
2014-01-30 12:44:28 -05:00
|
|
|
@changed_ids
|
2014-01-30 11:15:49 -05:00
|
|
|
end
|
|
|
|
|
2014-01-30 11:43:01 -05:00
|
|
|
private
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def find_group
|
|
|
|
return unless @options[:group]
|
2015-12-22 19:09:17 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
group = Group.where('name ilike ?', @options[:group]).first
|
|
|
|
raise Discourse::InvalidParameters.new(:group) unless group
|
|
|
|
unless group.group_users.where(user_id: @user.id).exists?
|
|
|
|
raise Discourse::InvalidParameters.new(:group)
|
2015-12-22 19:09:17 -05:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
group
|
|
|
|
end
|
2015-12-22 19:09:17 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def move_messages_to_inbox
|
|
|
|
group = find_group
|
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_see?(t) && t.private_message?
|
|
|
|
if group
|
|
|
|
GroupArchivedMessage.move_to_inbox!(group.id, t)
|
|
|
|
else
|
|
|
|
UserArchivedMessage.move_to_inbox!(@user.id, t)
|
2015-12-22 19:09:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2015-12-22 19:09:17 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def archive_messages
|
|
|
|
group = find_group
|
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_see?(t) && t.private_message?
|
|
|
|
if group
|
|
|
|
GroupArchivedMessage.archive!(group.id, t)
|
|
|
|
else
|
|
|
|
UserArchivedMessage.archive!(@user.id, t)
|
2015-12-22 19:09:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2015-12-22 19:09:17 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def dismiss_posts
|
2019-10-31 19:19:43 -04:00
|
|
|
highest_number_source_column = @user.staff? ? 'highest_staff_post_number' : 'highest_post_number'
|
2019-06-26 23:26:07 -04:00
|
|
|
sql = <<~SQL
|
|
|
|
UPDATE topic_users tu
|
2019-10-31 19:19:43 -04:00
|
|
|
SET highest_seen_post_number = t.#{highest_number_source_column} , last_read_post_number = t.#{highest_number_source_column}
|
2019-06-26 23:26:07 -04:00
|
|
|
FROM topics t
|
|
|
|
WHERE t.id = tu.topic_id AND tu.user_id = :user_id AND t.id IN (:topic_ids)
|
|
|
|
SQL
|
2014-08-08 02:29:51 -04:00
|
|
|
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec(sql, user_id: @user.id, topic_ids: @topic_ids)
|
2018-06-07 01:28:18 -04:00
|
|
|
@changed_ids.concat @topic_ids
|
|
|
|
end
|
2014-08-08 02:29:51 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def reset_read
|
|
|
|
PostTiming.destroy_for(@user.id, @topic_ids)
|
|
|
|
end
|
2014-02-21 13:03:50 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
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])
|
2014-01-30 12:44:28 -05:00
|
|
|
end
|
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2014-01-30 12:44:28 -05:00
|
|
|
|
2018-06-07 01:28:18 -04: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
|
2014-02-14 17:38:55 -05:00
|
|
|
end
|
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2014-02-14 17:38:55 -05:00
|
|
|
|
2018-06-07 01:28:18 -04: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 11:43:01 -05:00
|
|
|
end
|
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2014-01-30 11:43:01 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def unlist
|
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_moderate?(t)
|
|
|
|
t.update_status('visible', false, @user)
|
|
|
|
@changed_ids << t.id
|
2015-10-27 16:57:40 -04:00
|
|
|
end
|
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2015-10-27 16:57:40 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def relist
|
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_moderate?(t)
|
|
|
|
t.update_status('visible', true, @user)
|
|
|
|
@changed_ids << t.id
|
2017-05-10 01:11:52 -04:00
|
|
|
end
|
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2017-05-10 01:11:52 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def archive
|
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_moderate?(t)
|
|
|
|
t.update_status('archived', true, @user)
|
|
|
|
@changed_ids << t.id
|
2014-09-22 14:56:48 -04:00
|
|
|
end
|
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2014-09-22 14:56:48 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def delete
|
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_delete?(t)
|
2018-08-14 10:15:46 -04:00
|
|
|
post = t.ordered_posts.first
|
|
|
|
PostDestroyer.new(@user, post).destroy if post
|
2014-08-11 15:13:44 -04:00
|
|
|
end
|
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2014-08-11 15:13:44 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def change_tags
|
|
|
|
tags = @operation[:tags]
|
|
|
|
tags = DiscourseTagging.tags_for_saving(tags, guardian) if tags.present?
|
|
|
|
|
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_edit?(t)
|
|
|
|
if tags.present?
|
|
|
|
DiscourseTagging.tag_topic_by_names(t, guardian, tags)
|
|
|
|
else
|
|
|
|
t.tags = []
|
2016-04-25 15:55:15 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
@changed_ids << t.id
|
2016-04-25 15:55:15 -04:00
|
|
|
end
|
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2016-04-25 15:55:15 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def append_tags
|
|
|
|
tags = @operation[:tags]
|
|
|
|
tags = DiscourseTagging.tags_for_saving(tags, guardian) if tags.present?
|
2016-12-23 18:45:21 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_edit?(t)
|
|
|
|
if tags.present?
|
|
|
|
DiscourseTagging.tag_topic_by_names(t, guardian, tags, append: true)
|
2016-12-23 18:45:21 -05:00
|
|
|
end
|
2018-08-14 10:15:46 -04:00
|
|
|
@changed_ids << t.id
|
2016-12-23 18:45:21 -05:00
|
|
|
end
|
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2016-12-23 18:45:21 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def guardian
|
|
|
|
@guardian ||= Guardian.new(@user)
|
|
|
|
end
|
2014-01-30 11:43:01 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def topics
|
|
|
|
@topics ||= Topic.where(id: @topic_ids)
|
|
|
|
end
|
2014-01-30 11:43:01 -05:00
|
|
|
|
2014-01-30 11:15:49 -05:00
|
|
|
end
|