2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-01 06:48:43 -05:00
|
|
|
class TopicConverter
|
|
|
|
|
|
|
|
attr_reader :topic
|
|
|
|
|
|
|
|
def initialize(topic, user)
|
|
|
|
@topic = topic
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
2017-04-11 07:43:33 -05:00
|
|
|
def convert_to_public_topic(category_id = nil)
|
2016-05-01 06:48:43 -05:00
|
|
|
Topic.transaction do
|
2019-04-16 02:16:23 -05:00
|
|
|
category_id =
|
2017-04-11 07:43:33 -05:00
|
|
|
if category_id
|
|
|
|
category_id
|
|
|
|
elsif SiteSetting.allow_uncategorized_topics
|
|
|
|
SiteSetting.uncategorized_category_id
|
|
|
|
else
|
|
|
|
Category.where(read_restricted: false)
|
|
|
|
.where.not(id: SiteSetting.uncategorized_category_id)
|
2017-07-24 08:17:42 -05:00
|
|
|
.order('id asc')
|
2019-10-21 05:32:27 -05:00
|
|
|
.pluck_first(:id)
|
2017-04-11 07:43:33 -05:00
|
|
|
end
|
|
|
|
|
2019-04-16 02:16:23 -05:00
|
|
|
PostRevisor.new(@topic.first_post, @topic).revise!(
|
|
|
|
@user,
|
2019-04-16 02:47:16 -05:00
|
|
|
category_id: category_id,
|
|
|
|
archetype: Archetype.default
|
2019-04-16 02:16:23 -05:00
|
|
|
)
|
|
|
|
|
2016-05-01 06:48:43 -05:00
|
|
|
update_user_stats
|
2019-11-17 19:25:42 -06:00
|
|
|
update_post_uploads_secure_status
|
2019-01-03 11:03:01 -06:00
|
|
|
Jobs.enqueue(:topic_action_converter, topic_id: @topic.id)
|
2019-07-22 12:02:21 -05:00
|
|
|
Jobs.enqueue(:delete_inaccessible_notifications, topic_id: @topic.id)
|
2016-05-01 06:48:43 -05:00
|
|
|
watch_topic(topic)
|
|
|
|
end
|
|
|
|
@topic
|
|
|
|
end
|
|
|
|
|
|
|
|
def convert_to_private_message
|
|
|
|
Topic.transaction do
|
2019-04-16 02:16:23 -05:00
|
|
|
@topic.update_category_topic_count_by(-1)
|
|
|
|
|
|
|
|
PostRevisor.new(@topic.first_post, @topic).revise!(
|
|
|
|
@user,
|
2019-04-16 02:47:16 -05:00
|
|
|
category_id: nil,
|
|
|
|
archetype: Archetype.private_message
|
2019-04-16 02:16:23 -05:00
|
|
|
)
|
|
|
|
|
2016-05-01 06:48:43 -05:00
|
|
|
add_allowed_users
|
2019-11-17 19:25:42 -06:00
|
|
|
update_post_uploads_secure_status
|
2019-12-09 13:15:47 -06:00
|
|
|
UserProfile.remove_featured_topic_from_all_profiles(@topic)
|
2019-04-16 02:16:23 -05:00
|
|
|
|
2019-01-03 11:03:01 -06:00
|
|
|
Jobs.enqueue(:topic_action_converter, topic_id: @topic.id)
|
2019-07-22 12:02:21 -05:00
|
|
|
Jobs.enqueue(:delete_inaccessible_notifications, topic_id: @topic.id)
|
|
|
|
|
2016-05-01 06:48:43 -05:00
|
|
|
watch_topic(topic)
|
|
|
|
end
|
|
|
|
@topic
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-11-18 08:04:21 -06:00
|
|
|
def posters
|
|
|
|
@posters ||= @topic.posts.distinct.pluck(:user_id).to_a
|
|
|
|
end
|
|
|
|
|
2016-05-01 06:48:43 -05:00
|
|
|
def update_user_stats
|
2019-11-18 08:04:21 -06:00
|
|
|
# update posts count. NOTE that DirectoryItem.refresh will overwrite this by counting UserAction records.
|
2016-05-01 06:48:43 -05:00
|
|
|
# update topics count
|
2019-11-18 08:04:21 -06:00
|
|
|
UserStat.where(user_id: posters).update_all('post_count = post_count + 1')
|
|
|
|
UserStat.where(user_id: @topic.user_id).update_all('topic_count = topic_count + 1')
|
2016-05-01 06:48:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_allowed_users
|
2019-11-18 08:04:21 -06:00
|
|
|
# update posts count. NOTE that DirectoryItem.refresh will overwrite this by counting UserAction records.
|
2016-05-01 06:48:43 -05:00
|
|
|
# update topics count
|
2019-11-18 08:04:21 -06:00
|
|
|
UserStat.where(user_id: posters).update_all('post_count = post_count - 1')
|
|
|
|
UserStat.where(user_id: @topic.user_id).update_all('topic_count = topic_count - 1')
|
|
|
|
|
|
|
|
existing_allowed_users = @topic.topic_allowed_users.pluck(:user_id)
|
|
|
|
users_to_allow = posters << @user.id
|
|
|
|
|
2020-04-03 10:42:01 -05:00
|
|
|
if (users_to_allow | existing_allowed_users).length > SiteSetting.max_allowed_message_recipients
|
|
|
|
users_to_allow = [@user.id]
|
|
|
|
end
|
|
|
|
|
2019-11-18 08:04:21 -06: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 02:16:23 -05:00
|
|
|
@topic.save!
|
2016-05-01 06:48:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def watch_topic(topic)
|
|
|
|
@topic.notifier.watch_topic!(topic.user_id)
|
|
|
|
|
2017-08-30 23:06:56 -05:00
|
|
|
@topic.reload.topic_allowed_users.each do |tau|
|
2017-03-14 01:33:06 -05:00
|
|
|
next if tau.user_id < 0 || tau.user_id == topic.user_id
|
2016-05-01 06:48:43 -05:00
|
|
|
topic.notifier.watch!(tau.user_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-17 19:25:42 -06:00
|
|
|
def update_post_uploads_secure_status
|
2020-01-22 20:01:10 -06:00
|
|
|
DB.after_commit do
|
|
|
|
Jobs.enqueue(:update_topic_upload_security, topic_id: @topic.id)
|
2019-11-17 19:25:42 -06:00
|
|
|
end
|
|
|
|
end
|
2016-05-01 06:48:43 -05:00
|
|
|
end
|