2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-01-31 00:15:49 +08:00
|
|
|
class TopicsBulkAction
|
2015-12-23 08:09:17 +08:00
|
|
|
def initialize(user, topic_ids, operation, options = {})
|
2014-01-31 00:15:49 +08:00
|
|
|
@user = user
|
|
|
|
@topic_ids = topic_ids
|
|
|
|
@operation = operation
|
2014-01-31 01:44:28 +08:00
|
|
|
@changed_ids = []
|
2015-12-23 08:09:17 +08:00
|
|
|
@options = options
|
2014-01-31 00:15:49 +08:00
|
|
|
end
|
|
|
|
|
2014-01-31 00:43:01 +08:00
|
|
|
def self.operations
|
2015-12-23 08:09:17 +08:00
|
|
|
@operations ||= %w[
|
|
|
|
change_category
|
|
|
|
close
|
2024-03-07 05:08:49 +08:00
|
|
|
silent_close
|
2015-12-23 08:09:17 +08:00
|
|
|
archive
|
|
|
|
change_notification_level
|
2022-02-22 04:45:01 +08:00
|
|
|
destroy_post_timing
|
|
|
|
dismiss_posts
|
|
|
|
delete
|
|
|
|
unlist
|
|
|
|
archive_messages
|
2020-10-07 01:11:55 +08:00
|
|
|
move_messages_to_inbox
|
|
|
|
change_tags
|
|
|
|
append_tags
|
|
|
|
remove_tags
|
2022-05-23 00:32:55 +08:00
|
|
|
relist
|
|
|
|
dismiss_topics
|
|
|
|
reset_bump_dates
|
2023-01-09 20:10:19 +08:00
|
|
|
]
|
2015-03-07 09:36:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.register_operation(name, &block)
|
|
|
|
operations << name
|
|
|
|
define_method(name, &block)
|
2014-01-31 00:43:01 +08:00
|
|
|
end
|
|
|
|
|
2014-01-31 00:15:49 +08:00
|
|
|
def perform!
|
2024-05-27 18:27:13 +08:00
|
|
|
if TopicsBulkAction.operations.exclude?(@operation[:type])
|
2014-01-31 00:43:01 +08:00
|
|
|
raise Discourse::InvalidParameters.new(:operation)
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2019-05-07 10:22:37 +08:00
|
|
|
# careful these are private methods, we need send
|
2014-01-31 00:43:01 +08:00
|
|
|
send(@operation[:type])
|
2021-05-26 07:38:46 +08:00
|
|
|
@changed_ids.sort
|
2014-01-31 00:15:49 +08:00
|
|
|
end
|
|
|
|
|
2014-01-31 00:43:01 +08:00
|
|
|
private
|
|
|
|
|
2015-12-23 08:09:17 +08:00
|
|
|
def find_group
|
|
|
|
return unless @options[:group]
|
|
|
|
|
|
|
|
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)
|
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
group
|
|
|
|
end
|
2015-12-23 08:09:17 +08:00
|
|
|
|
|
|
|
def move_messages_to_inbox
|
|
|
|
group = find_group
|
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_see?(t) && t.private_message?
|
|
|
|
if group
|
2021-09-10 09:20:50 +08:00
|
|
|
GroupArchivedMessage.move_to_inbox!(group.id, t, acting_user_id: @user.id)
|
2015-12-23 08:09:17 +08:00
|
|
|
else
|
2018-03-06 14:38:43 +08:00
|
|
|
UserArchivedMessage.move_to_inbox!(@user.id, t)
|
2015-12-23 08:09:17 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2015-12-23 08:09:17 +08:00
|
|
|
|
|
|
|
def archive_messages
|
|
|
|
group = find_group
|
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_see?(t) && t.private_message?
|
|
|
|
if group
|
2021-09-10 09:20:50 +08:00
|
|
|
GroupArchivedMessage.archive!(group.id, t, acting_user_id: @user.id)
|
2015-12-23 08:09:17 +08:00
|
|
|
else
|
2018-03-06 14:38:43 +08:00
|
|
|
UserArchivedMessage.archive!(@user.id, t)
|
2015-12-23 08:09:17 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2015-12-23 08:09:17 +08:00
|
|
|
|
2014-08-08 14:29:51 +08:00
|
|
|
def dismiss_posts
|
2022-06-30 08:18:12 +08:00
|
|
|
highest_number_source_column =
|
|
|
|
@user.whisperer? ? "highest_staff_post_number" : "highest_post_number"
|
2023-07-13 18:05:56 +08:00
|
|
|
|
2019-06-27 11:26:07 +08:00
|
|
|
sql = <<~SQL
|
|
|
|
UPDATE topic_users tu
|
2021-07-05 14:17:31 +08:00
|
|
|
SET last_read_post_number = t.#{highest_number_source_column}
|
2019-06-27 11:26:07 +08: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 14:29:51 +08:00
|
|
|
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec(sql, user_id: @user.id, topic_ids: @topic_ids)
|
2023-07-13 18:05:56 +08:00
|
|
|
TopicTrackingState.publish_dismiss_new_posts(@user.id, topic_ids: @topic_ids.sort)
|
|
|
|
|
2014-08-08 14:29:51 +08:00
|
|
|
@changed_ids.concat @topic_ids
|
|
|
|
end
|
|
|
|
|
2021-05-26 07:38:46 +08:00
|
|
|
def dismiss_topics
|
2023-06-14 01:08:55 +08:00
|
|
|
ids =
|
2021-05-26 07:38:46 +08:00
|
|
|
Topic
|
|
|
|
.where(id: @topic_ids)
|
|
|
|
.joins(
|
|
|
|
"LEFT JOIN topic_users ON topic_users.topic_id = topics.id AND topic_users.user_id = #{@user.id}",
|
|
|
|
)
|
|
|
|
.where("topics.created_at >= ?", dismiss_topics_since_date)
|
|
|
|
.where("topic_users.last_read_post_number IS NULL")
|
|
|
|
.order("topics.created_at DESC")
|
|
|
|
.limit(SiteSetting.max_new_topics)
|
2023-06-14 01:08:55 +08:00
|
|
|
.filter { |t| guardian.can_see?(t) }
|
|
|
|
.map(&:id)
|
|
|
|
|
|
|
|
if ids.present?
|
|
|
|
now = Time.zone.now
|
|
|
|
rows = ids.map { |id| { topic_id: id, user_id: @user.id, created_at: now } }
|
|
|
|
DismissedTopicUser.insert_all(rows)
|
2023-07-13 18:05:56 +08:00
|
|
|
TopicTrackingState.publish_dismiss_new(@user.id, topic_ids: ids.sort)
|
2023-06-14 01:08:55 +08:00
|
|
|
end
|
2023-07-13 18:05:56 +08:00
|
|
|
|
2023-06-14 01:08:55 +08:00
|
|
|
@changed_ids = ids
|
2021-05-26 07:38:46 +08:00
|
|
|
end
|
|
|
|
|
2022-02-22 04:45:01 +08:00
|
|
|
def destroy_post_timing
|
|
|
|
topics.each do |t|
|
|
|
|
PostTiming.destroy_last_for(@user, topic: t)
|
|
|
|
@changed_ids << t.id
|
|
|
|
end
|
2014-02-22 02:03:50 +08:00
|
|
|
end
|
|
|
|
|
2014-01-31 00:43:01 +08:00
|
|
|
def change_category
|
2020-11-12 20:57:12 +08:00
|
|
|
updatable_topics = topics.where.not(category_id: @operation[:category_id])
|
|
|
|
|
|
|
|
if SiteSetting.create_revision_on_bulk_topic_moves
|
|
|
|
opts = { bypass_bump: true, validate_post: false, bypass_rate_limiter: true }
|
|
|
|
|
|
|
|
updatable_topics.each do |t|
|
|
|
|
if guardian.can_edit?(t)
|
|
|
|
changes = { category_id: @operation[:category_id] }
|
|
|
|
@changed_ids << t.id if t.first_post.revise(@user, changes, opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
updatable_topics.each do |t|
|
|
|
|
if guardian.can_edit?(t)
|
|
|
|
@changed_ids << t.id if t.change_category_to_id(@operation[:category_id])
|
|
|
|
end
|
2014-01-31 01:44:28 +08:00
|
|
|
end
|
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2014-01-31 01:44:28 +08:00
|
|
|
|
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
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2014-02-15 06:38:55 +08:00
|
|
|
|
2014-01-31 01:44:28 +08:00
|
|
|
def close
|
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_moderate?(t)
|
2024-04-06 04:56:52 +08:00
|
|
|
t.update_status("closed", true, @user, { message: @operation[:message] })
|
2014-01-31 01:44:28 +08:00
|
|
|
@changed_ids << t.id
|
2014-01-31 00:43:01 +08:00
|
|
|
end
|
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2014-01-31 00:43:01 +08:00
|
|
|
|
2024-03-07 05:08:49 +08:00
|
|
|
def silent_close
|
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_moderate?(t)
|
2024-06-11 07:36:54 +08:00
|
|
|
t.update_status(
|
|
|
|
"closed",
|
|
|
|
true,
|
|
|
|
@user,
|
|
|
|
{ message: @operation[:message], silent_tracking: true },
|
|
|
|
)
|
2024-03-07 05:08:49 +08:00
|
|
|
@changed_ids << t.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-28 04:57:40 +08:00
|
|
|
def unlist
|
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_moderate?(t)
|
2024-04-29 08:34:46 +08:00
|
|
|
t.update_status(
|
|
|
|
"visible",
|
|
|
|
false,
|
|
|
|
@user,
|
|
|
|
{ visibility_reason_id: Topic.visibility_reasons[:bulk_action] },
|
|
|
|
)
|
2015-10-28 04:57:40 +08:00
|
|
|
@changed_ids << t.id
|
|
|
|
end
|
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2015-10-28 04:57:40 +08:00
|
|
|
|
2017-05-10 13:11:52 +08:00
|
|
|
def relist
|
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_moderate?(t)
|
2024-04-29 08:34:46 +08:00
|
|
|
t.update_status(
|
|
|
|
"visible",
|
|
|
|
true,
|
|
|
|
@user,
|
|
|
|
{ visibility_reason_id: Topic.visibility_reasons[:bulk_action] },
|
|
|
|
)
|
2017-05-10 13:11:52 +08:00
|
|
|
@changed_ids << t.id
|
|
|
|
end
|
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2017-05-10 13:11:52 +08:00
|
|
|
|
2022-05-23 00:32:55 +08:00
|
|
|
def reset_bump_dates
|
|
|
|
if guardian.can_update_bumped_at?
|
|
|
|
topics.each do |t|
|
|
|
|
t.reset_bumped_at
|
|
|
|
@changed_ids << t.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-23 02:56:48 +08:00
|
|
|
def archive
|
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_moderate?(t)
|
|
|
|
t.update_status("archived", true, @user)
|
|
|
|
@changed_ids << t.id
|
|
|
|
end
|
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2014-09-23 02:56:48 +08:00
|
|
|
|
2014-08-12 03:13:44 +08:00
|
|
|
def delete
|
|
|
|
topics.each do |t|
|
2017-09-28 20:22:49 +08:00
|
|
|
if guardian.can_delete?(t)
|
2018-08-14 22:15:46 +08:00
|
|
|
post = t.ordered_posts.first
|
|
|
|
PostDestroyer.new(@user, post).destroy if post
|
2014-08-12 03:13:44 +08:00
|
|
|
end
|
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2014-08-12 03:13:44 +08:00
|
|
|
|
2016-04-26 03:55:15 +08:00
|
|
|
def change_tags
|
|
|
|
tags = @operation[:tags]
|
|
|
|
tags = DiscourseTagging.tags_for_saving(tags, guardian) if tags.present?
|
2018-06-07 13:28:18 +08:00
|
|
|
|
2016-04-26 03:55:15 +08:00
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_edit?(t)
|
|
|
|
if tags.present?
|
2016-05-05 02:02:47 +08:00
|
|
|
DiscourseTagging.tag_topic_by_names(t, guardian, tags)
|
2016-04-26 03:55:15 +08:00
|
|
|
else
|
2016-05-05 02:02:47 +08:00
|
|
|
t.tags = []
|
2016-04-26 03:55:15 +08:00
|
|
|
end
|
2016-05-05 02:02:47 +08:00
|
|
|
@changed_ids << t.id
|
2016-04-26 03:55:15 +08:00
|
|
|
end
|
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2016-04-26 03:55:15 +08:00
|
|
|
|
2016-12-24 07:45:21 +08:00
|
|
|
def append_tags
|
|
|
|
tags = @operation[:tags]
|
|
|
|
tags = DiscourseTagging.tags_for_saving(tags, guardian) if tags.present?
|
|
|
|
|
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_edit?(t)
|
2017-03-01 01:08:06 +08:00
|
|
|
DiscourseTagging.tag_topic_by_names(t, guardian, tags, append: true) if tags.present?
|
2018-08-14 22:15:46 +08:00
|
|
|
@changed_ids << t.id
|
2016-12-24 07:45:21 +08:00
|
|
|
end
|
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2016-12-24 07:45:21 +08:00
|
|
|
|
2020-10-07 01:11:55 +08:00
|
|
|
def remove_tags
|
|
|
|
topics.each do |t|
|
|
|
|
if guardian.can_edit?(t)
|
|
|
|
TopicTag.where(topic_id: t.id).delete_all
|
|
|
|
@changed_ids << t.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-31 00:43:01 +08:00
|
|
|
def guardian
|
|
|
|
@guardian ||= Guardian.new(@user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def topics
|
|
|
|
@topics ||= Topic.where(id: @topic_ids)
|
|
|
|
end
|
|
|
|
|
2021-05-26 07:38:46 +08:00
|
|
|
def dismiss_topics_since_date
|
|
|
|
new_topic_duration_minutes =
|
|
|
|
@user.user_option&.new_topic_duration_minutes ||
|
|
|
|
SiteSetting.default_other_new_topic_duration_minutes
|
|
|
|
setting_date =
|
|
|
|
case new_topic_duration_minutes
|
|
|
|
when User::NewTopicDuration::LAST_VISIT
|
|
|
|
@user.previous_visit_at || @user.created_at
|
|
|
|
when User::NewTopicDuration::ALWAYS
|
|
|
|
@user.created_at
|
|
|
|
else
|
|
|
|
new_topic_duration_minutes.minutes.ago
|
|
|
|
end
|
|
|
|
[setting_date, @user.created_at, Time.at(SiteSetting.min_new_topics_time).to_datetime].max
|
|
|
|
end
|
2014-01-31 00:15:49 +08:00
|
|
|
end
|