discourse/app/models/topic_converter.rb
Régis Hanol c0e9377b0e
FEATURE: Bulk convert PMs to public topics (and vice versa) (#39256)
Adds two new staff-only bulk actions in the topic-list dropdown — "Make
Public Topic…" when every selected topic is a PM, and "Make Personal
Message" when none of them are. Both are silent by default: no bump, no
post revision, no edit notifications to watchers, no small-action post
in the topic, and no forced re-watch. Admins who do want to notify
participants can tick the existing "Notify" checkbox in the bulk-actions
modal.

Today, converting PMs to public topics is a one-at-a-time operation that
fans out noisy edit notifications to everyone watching the PM, creates a
post revision on the first post, and leaves a small-action post behind.
The Support team hits this whenever they move a customer's history out
of a group inbox and into an enterprise category — the existing runbook
(Ref - t/68360) works around it with a custom console script, and doing
it by hand spams participants. A proper bulk action in the UI was the
missing piece.

The plumbing starts at TopicConverter, which now takes a `silent:`
keyword argument. When true, it passes `bypass_bump`, `skip_revision`,
and `silent` through to PostRevisor (suppressing the bump, the revision
row, and the edit notifications), skips `add_small_action`, and skips
the forced `watch_topic` call at the end. While in there,
`convert_to_private_message` now fails loudly with a base error when the
final recipient count would exceed
`max_allowed_message_recipients`, rather than silently collapsing to a
self-only PM and losing all context — this applies to both the
single-topic and bulk paths, since the check lives in the converter
itself.

TopicsBulkAction registers two new operations,
`convert_to_public_topic` and `convert_to_private_message`, both driven
by a small `bulk_convert(from_pm:)` helper. Per-topic success is
detected from the archetype flip rather than from `.valid?`, which would
re-run validations and wipe the `errors.add(:base, ...)` added by the
cap guard. Validation errors bubble into the modal's failure list
through the existing `@errors` channel.

On the frontend, `bulk-select-topics-dropdown` gets two new entries with
matching visibility rules, and `bulk-topic-actions` gains a couple of
cases that reuse the existing CategoryChooser and `allowSilent`-backed
"Notify" checkbox. `isCategoryAction` is extended so the
destination-category picker renders for the PM→public flow.

Ref - t/181539
2026-04-21 15:08:39 +02:00

174 lines
4.8 KiB
Ruby

# frozen_string_literal: true
class TopicConverter
attr_reader :topic
def initialize(topic, user, silent: false)
@topic = topic
@user = user
@silent = silent
end
def convert_to_public_topic(category_id = nil)
Topic.transaction do
category_id ||=
SiteSetting.uncategorized_category_id if SiteSetting.allow_uncategorized_topics
@category = Category.find_by(id: category_id) if category_id
@category ||=
Category
.where(read_restricted: false)
.where.not(id: SiteSetting.uncategorized_category_id)
.first
PostRevisor.new(@topic.first_post, @topic).revise!(
@user,
{ category_id: @category.id, archetype: Archetype.default },
revise_opts,
)
raise ActiveRecord::Rollback if !@topic.valid?
update_user_stats
update_post_uploads_secure_status
add_small_action("public_topic") unless @silent
Tag.update_counters(@topic.tags, { public_topic_count: 1 }) if !@category.read_restricted
Jobs.enqueue(:topic_action_converter, topic_id: @topic.id)
Jobs.enqueue(:delete_inaccessible_notifications, topic_id: @topic.id)
watch_topic(@topic) unless @silent
end
@topic
end
def convert_to_private_message
if exceeds_recipient_cap?
@topic.errors.add(
:base,
I18n.t(
"topic_converter.too_many_recipients",
max: SiteSetting.max_allowed_message_recipients,
),
)
return @topic
end
Topic.transaction do
was_public = !@topic.category.read_restricted
@topic.update_category_topic_count_by(-1) if @topic.visible
PostRevisor.new(@topic.first_post, @topic).revise!(
@user,
{ category_id: nil, archetype: Archetype.private_message },
revise_opts,
)
raise ActiveRecord::Rollback if !@topic.valid?
add_allowed_users
update_post_uploads_secure_status
add_small_action("private_topic") unless @silent
Tag.update_counters(@topic.tags, { public_topic_count: -1 }) if was_public
UserProfile.remove_featured_topic_from_all_profiles(@topic)
Jobs.enqueue(:topic_action_converter, topic_id: @topic.id)
Jobs.enqueue(:delete_inaccessible_notifications, topic_id: @topic.id)
watch_topic(@topic) unless @silent
end
@topic
end
private
def revise_opts
{ bypass_bump: @silent, skip_revision: @silent, silent: @silent }
end
def posters
@posters ||=
@topic
.posts
.where.not(post_type: [Post.types[:small_action], Post.types[:whisper]])
.distinct
.pluck(:user_id)
end
def increment_users_post_count
update_users_post_count(:increment)
end
def decrement_users_post_count
update_users_post_count(:decrement)
end
def update_users_post_count(action)
operation = action == :increment ? "+" : "-"
# NOTE that DirectoryItem.refresh will overwrite this by counting UserAction records.
#
# Changes user_stats (post_count) by the number of posts in the topic.
# First post, hidden posts and non-regular posts are ignored.
DB.exec <<~SQL
UPDATE user_stats
SET post_count = post_count #{operation} X.count
FROM (
SELECT
us.user_id,
COUNT(*) AS count
FROM user_stats us
INNER JOIN posts ON posts.topic_id = #{@topic.id.to_i} AND posts.user_id = us.user_id
WHERE posts.post_number > 1
AND NOT posts.hidden
AND posts.post_type = #{Post.types[:regular].to_i}
GROUP BY us.user_id
) X
WHERE X.user_id = user_stats.user_id
SQL
end
def update_user_stats
increment_users_post_count
UserStatCountUpdater.increment!(@topic.first_post)
end
def add_allowed_users
decrement_users_post_count
UserStatCountUpdater.decrement!(@topic.first_post)
existing_allowed_users = @topic.topic_allowed_users.pluck(:user_id)
users_to_allow = posters << @user.id
(users_to_allow - existing_allowed_users).uniq.each do |user_id|
@topic.topic_allowed_users.build(user_id: user_id)
end
@topic.save!
end
def exceeds_recipient_cap?
allowed_users = @topic.topic_allowed_users.pluck(:user_id)
total_recipients = (posters | allowed_users | [@user.id]).size
total_recipients > SiteSetting.max_allowed_message_recipients
end
def watch_topic(topic)
@topic.notifier.watch_topic!(topic.user_id)
@topic.reload.topic_allowed_users.each do |tau|
next if tau.user_id < 0 || tau.user_id == topic.user_id
topic.notifier.watch!(tau.user_id)
end
end
def update_post_uploads_secure_status
DB.after_commit { Jobs.enqueue(:update_topic_upload_security, topic_id: @topic.id) }
end
def add_small_action(action_code)
DB.after_commit { @topic.add_small_action(@user, action_code) }
end
end