2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-01 07:48:43 -04:00
|
|
|
class TopicConverter
|
|
|
|
attr_reader :topic
|
|
|
|
|
|
|
|
def initialize(topic, user)
|
|
|
|
@topic = topic
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
2017-04-11 08:43:33 -04:00
|
|
|
def convert_to_public_topic(category_id = nil)
|
2016-05-01 07:48:43 -04:00
|
|
|
Topic.transaction do
|
2024-05-17 10:36:25 -04:00
|
|
|
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
|
2017-04-11 08:43:33 -04:00
|
|
|
|
2019-04-16 03:16:23 -04:00
|
|
|
PostRevisor.new(@topic.first_post, @topic).revise!(
|
|
|
|
@user,
|
2024-05-17 10:36:25 -04:00
|
|
|
category_id: @category.id,
|
2019-04-16 03:47:16 -04:00
|
|
|
archetype: Archetype.default,
|
2019-04-16 03:16:23 -04:00
|
|
|
)
|
|
|
|
|
2024-05-17 10:36:25 -04:00
|
|
|
raise ActiveRecord::Rollback if !@topic.valid?
|
|
|
|
|
2016-05-01 07:48:43 -04:00
|
|
|
update_user_stats
|
2019-11-17 20:25:42 -05:00
|
|
|
update_post_uploads_secure_status
|
2024-05-17 10:36:25 -04:00
|
|
|
add_small_action("public_topic")
|
|
|
|
Tag.update_counters(@topic.tags, { public_topic_count: 1 }) if !@category.read_restricted
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
Jobs.enqueue(:topic_action_converter, topic_id: @topic.id)
|
2019-07-22 13:02:21 -04:00
|
|
|
Jobs.enqueue(:delete_inaccessible_notifications, topic_id: @topic.id)
|
2024-05-17 10:36:25 -04:00
|
|
|
|
|
|
|
watch_topic(@topic)
|
2016-05-01 07:48:43 -04:00
|
|
|
end
|
2024-05-17 10:36:25 -04:00
|
|
|
|
2016-05-01 07:48:43 -04:00
|
|
|
@topic
|
|
|
|
end
|
|
|
|
|
|
|
|
def convert_to_private_message
|
|
|
|
Topic.transaction do
|
2024-05-17 10:36:25 -04:00
|
|
|
was_public = !@topic.category.read_restricted
|
2022-02-10 20:00:58 -05:00
|
|
|
@topic.update_category_topic_count_by(-1) if @topic.visible
|
2019-04-16 03:16:23 -04:00
|
|
|
|
|
|
|
PostRevisor.new(@topic.first_post, @topic).revise!(
|
|
|
|
@user,
|
2019-04-16 03:47:16 -04:00
|
|
|
category_id: nil,
|
|
|
|
archetype: Archetype.private_message,
|
2019-04-16 03:16:23 -04:00
|
|
|
)
|
|
|
|
|
2024-05-17 10:36:25 -04:00
|
|
|
raise ActiveRecord::Rollback if !@topic.valid?
|
|
|
|
|
2016-05-01 07:48:43 -04:00
|
|
|
add_allowed_users
|
2019-11-17 20:25:42 -05:00
|
|
|
update_post_uploads_secure_status
|
2024-05-17 10:36:25 -04:00
|
|
|
add_small_action("private_topic")
|
|
|
|
Tag.update_counters(@topic.tags, { public_topic_count: -1 }) if was_public
|
2019-12-09 14:15:47 -05:00
|
|
|
UserProfile.remove_featured_topic_from_all_profiles(@topic)
|
2019-04-16 03:16:23 -04:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
Jobs.enqueue(:topic_action_converter, topic_id: @topic.id)
|
2019-07-22 13:02:21 -04:00
|
|
|
Jobs.enqueue(:delete_inaccessible_notifications, topic_id: @topic.id)
|
|
|
|
|
2024-05-17 10:36:25 -04:00
|
|
|
watch_topic(@topic)
|
2016-05-01 07:48:43 -04:00
|
|
|
end
|
2024-05-17 10:36:25 -04:00
|
|
|
|
2016-05-01 07:48:43 -04:00
|
|
|
@topic
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-11-18 09:04:21 -05:00
|
|
|
def posters
|
2022-08-29 08:52:54 -04:00
|
|
|
@posters ||=
|
|
|
|
@topic
|
|
|
|
.posts
|
|
|
|
.where.not(post_type: [Post.types[:small_action], Post.types[:whisper]])
|
|
|
|
.distinct
|
|
|
|
.pluck(:user_id)
|
2019-11-18 09:04:21 -05:00
|
|
|
end
|
|
|
|
|
2022-02-10 20:00:58 -05:00
|
|
|
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.
|
2024-05-17 10:36:25 -04:00
|
|
|
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
|
2022-02-10 20:00:58 -05:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2016-05-01 07:48:43 -04:00
|
|
|
def update_user_stats
|
2022-02-10 20:00:58 -05:00
|
|
|
increment_users_post_count
|
|
|
|
UserStatCountUpdater.increment!(@topic.first_post)
|
2016-05-01 07:48:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_allowed_users
|
2022-02-10 20:00:58 -05:00
|
|
|
decrement_users_post_count
|
|
|
|
UserStatCountUpdater.decrement!(@topic.first_post)
|
2019-11-18 09:04:21 -05:00
|
|
|
|
|
|
|
existing_allowed_users = @topic.topic_allowed_users.pluck(:user_id)
|
|
|
|
users_to_allow = posters << @user.id
|
|
|
|
|
2020-04-03 11:42:01 -04:00
|
|
|
if (users_to_allow | existing_allowed_users).length > SiteSetting.max_allowed_message_recipients
|
|
|
|
users_to_allow = [@user.id]
|
|
|
|
end
|
|
|
|
|
2019-11-18 09:04:21 -05:00
|
|
|
(users_to_allow - existing_allowed_users).uniq.each do |user_id|
|
|
|
|
@topic.topic_allowed_users.build(user_id: user_id)
|
|
|
|
end
|
|
|
|
|
2019-04-16 03:16:23 -04:00
|
|
|
@topic.save!
|
2016-05-01 07:48:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def watch_topic(topic)
|
|
|
|
@topic.notifier.watch_topic!(topic.user_id)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
@topic.reload.topic_allowed_users.each do |tau|
|
2017-03-14 02:33:06 -04:00
|
|
|
next if tau.user_id < 0 || tau.user_id == topic.user_id
|
2016-05-01 07:48:43 -04:00
|
|
|
topic.notifier.watch!(tau.user_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-17 20:25:42 -05:00
|
|
|
def update_post_uploads_secure_status
|
2020-01-22 21:01:10 -05:00
|
|
|
DB.after_commit { Jobs.enqueue(:update_topic_upload_security, topic_id: @topic.id) }
|
2019-11-17 20:25:42 -05:00
|
|
|
end
|
2024-05-17 10:36:25 -04:00
|
|
|
|
|
|
|
def add_small_action(action_code)
|
|
|
|
DB.after_commit { @topic.add_small_action(@user, action_code) }
|
|
|
|
end
|
2016-05-01 07:48:43 -04:00
|
|
|
end
|