2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-13 14:59:12 -05:00
|
|
|
class TopicPublisher
|
|
|
|
def initialize(topic, published_by, category_id)
|
|
|
|
@topic = topic
|
|
|
|
@published_by = published_by
|
|
|
|
@category_id = category_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def publish!
|
2018-03-26 15:06:20 -05:00
|
|
|
published_at = Time.zone.now
|
2019-04-16 01:09:51 -05:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
TopicTimestampChanger
|
|
|
|
.new(timestamp: published_at, topic: @topic)
|
|
|
|
.change! do
|
|
|
|
if @topic.private_message?
|
|
|
|
@topic = TopicConverter.new(@topic, @published_by).convert_to_public_topic(@category_id)
|
|
|
|
else
|
|
|
|
PostRevisor.new(@topic.first_post, @topic).revise!(
|
|
|
|
@published_by,
|
|
|
|
category_id: @category_id,
|
|
|
|
)
|
|
|
|
end
|
2018-03-13 14:59:12 -05:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
@topic.update_columns(visible: true)
|
2018-03-13 14:59:12 -05:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
StaffActionLogger.new(@published_by).log_topic_published(@topic)
|
2018-03-13 14:59:12 -05:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
# Clean up any publishing artifacts
|
|
|
|
SharedDraft.where(topic: @topic).delete_all
|
2019-04-16 01:09:51 -05:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
TopicTimer.where(topic: @topic).update_all(
|
|
|
|
deleted_at: DateTime.now,
|
|
|
|
deleted_by_id: @published_by.id,
|
|
|
|
)
|
2018-03-13 14:59:12 -05:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
op = @topic.first_post
|
2019-04-16 01:09:51 -05:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
if op.present?
|
|
|
|
op.revisions.destroy_all
|
2019-04-16 01:09:51 -05:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
op.update_columns(version: 1, public_version: 1, last_version_at: published_at)
|
|
|
|
end
|
2018-03-13 14:59:12 -05:00
|
|
|
end
|
|
|
|
|
2022-07-20 11:07:18 -05:00
|
|
|
Jobs.enqueue(
|
|
|
|
:notify_tag_change,
|
|
|
|
post_id: @topic.first_post.id,
|
|
|
|
notified_user_ids: [@topic.first_post.user_id, @published_by.id].uniq,
|
|
|
|
diff_tags: @topic.tags.map(&:name),
|
|
|
|
force: true,
|
|
|
|
)
|
|
|
|
|
2018-03-13 14:59:12 -05:00
|
|
|
MessageBus.publish("/topic/#{@topic.id}", reload_topic: true, refresh_stream: true)
|
|
|
|
|
|
|
|
@topic
|
|
|
|
end
|
|
|
|
end
|