2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-01 19:48:43 +08:00
|
|
|
class TopicConverter
|
|
|
|
|
|
|
|
attr_reader :topic
|
|
|
|
|
|
|
|
def initialize(topic, user)
|
|
|
|
@topic = topic
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
2017-04-11 20:43:33 +08:00
|
|
|
def convert_to_public_topic(category_id = nil)
|
2016-05-01 19:48:43 +08:00
|
|
|
Topic.transaction do
|
2019-04-16 15:16:23 +08:00
|
|
|
category_id =
|
2017-04-11 20:43:33 +08: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 21:17:42 +08:00
|
|
|
.order('id asc')
|
|
|
|
.pluck(:id).first
|
2017-04-11 20:43:33 +08:00
|
|
|
end
|
|
|
|
|
2019-04-16 15:16:23 +08:00
|
|
|
PostRevisor.new(@topic.first_post, @topic).revise!(
|
|
|
|
@user,
|
2019-04-16 15:47:16 +08:00
|
|
|
category_id: category_id,
|
|
|
|
archetype: Archetype.default
|
2019-04-16 15:16:23 +08:00
|
|
|
)
|
|
|
|
|
2016-05-01 19:48:43 +08:00
|
|
|
update_user_stats
|
2019-01-04 01:03:01 +08:00
|
|
|
Jobs.enqueue(:topic_action_converter, topic_id: @topic.id)
|
2019-07-23 01:02:21 +08:00
|
|
|
Jobs.enqueue(:delete_inaccessible_notifications, topic_id: @topic.id)
|
2017-11-10 04:33:26 +08:00
|
|
|
|
2016-05-01 19:48:43 +08:00
|
|
|
watch_topic(topic)
|
|
|
|
end
|
|
|
|
@topic
|
|
|
|
end
|
|
|
|
|
|
|
|
def convert_to_private_message
|
|
|
|
Topic.transaction do
|
2019-04-16 15:16:23 +08:00
|
|
|
@topic.update_category_topic_count_by(-1)
|
|
|
|
|
|
|
|
PostRevisor.new(@topic.first_post, @topic).revise!(
|
|
|
|
@user,
|
2019-04-16 15:47:16 +08:00
|
|
|
category_id: nil,
|
|
|
|
archetype: Archetype.private_message
|
2019-04-16 15:16:23 +08:00
|
|
|
)
|
|
|
|
|
2016-05-01 19:48:43 +08:00
|
|
|
add_allowed_users
|
2019-04-16 15:16:23 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
Jobs.enqueue(:topic_action_converter, topic_id: @topic.id)
|
2019-07-23 01:02:21 +08:00
|
|
|
Jobs.enqueue(:delete_inaccessible_notifications, topic_id: @topic.id)
|
|
|
|
|
2016-05-01 19:48:43 +08:00
|
|
|
watch_topic(topic)
|
|
|
|
end
|
|
|
|
@topic
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def update_user_stats
|
|
|
|
@topic.posts.where(deleted_at: nil).each do |p|
|
|
|
|
user = User.find(p.user_id)
|
2017-11-10 04:33:26 +08:00
|
|
|
# update posts count. NOTE that DirectoryItem.refresh will overwrite this by counting UserAction records.
|
2016-05-01 19:48:43 +08:00
|
|
|
user.user_stat.post_count += 1
|
|
|
|
user.user_stat.save!
|
|
|
|
end
|
|
|
|
# update topics count
|
|
|
|
@topic.user.user_stat.topic_count += 1
|
|
|
|
@topic.user.user_stat.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_allowed_users
|
|
|
|
@topic.posts.where(deleted_at: nil).each do |p|
|
|
|
|
user = User.find(p.user_id)
|
|
|
|
@topic.topic_allowed_users.build(user_id: user.id) unless @topic.topic_allowed_users.where(user_id: user.id).exists?
|
2017-11-10 04:33:26 +08:00
|
|
|
# update posts count. NOTE that DirectoryItem.refresh will overwrite this by counting UserAction records.
|
2016-05-01 19:48:43 +08:00
|
|
|
user.user_stat.post_count -= 1
|
|
|
|
user.user_stat.save!
|
|
|
|
end
|
2018-03-08 20:29:04 +08:00
|
|
|
@topic.topic_allowed_users.build(user_id: @user.id) unless @topic.topic_allowed_users.where(user_id: @user.id).exists?
|
2016-05-01 19:48:43 +08:00
|
|
|
# update topics count
|
|
|
|
@topic.user.user_stat.topic_count -= 1
|
|
|
|
@topic.user.user_stat.save!
|
2019-04-16 15:16:23 +08:00
|
|
|
@topic.save!
|
2016-05-01 19:48:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def watch_topic(topic)
|
|
|
|
@topic.notifier.watch_topic!(topic.user_id)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
@topic.reload.topic_allowed_users.each do |tau|
|
2017-03-14 14:33:06 +08:00
|
|
|
next if tau.user_id < 0 || tau.user_id == topic.user_id
|
2016-05-01 19:48:43 +08:00
|
|
|
topic.notifier.watch!(tau.user_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|