2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-14 03:59:12 +08: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-27 04:06:20 +08:00
|
|
|
published_at = Time.zone.now
|
2019-04-16 14:09:51 +08:00
|
|
|
|
2018-03-27 04:06:20 +08:00
|
|
|
TopicTimestampChanger.new(timestamp: published_at, topic: @topic).change! do
|
2018-03-14 03:59:12 +08:00
|
|
|
if @topic.private_message?
|
|
|
|
@topic = TopicConverter.new(@topic, @published_by)
|
|
|
|
.convert_to_public_topic(@category_id)
|
|
|
|
else
|
2019-04-16 14:09:51 +08:00
|
|
|
PostRevisor.new(@topic.first_post, @topic).revise!(@published_by,
|
|
|
|
category_id: @category_id,
|
|
|
|
)
|
2018-03-14 03:59:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
@topic.update_columns(visible: true)
|
|
|
|
|
|
|
|
StaffActionLogger.new(@published_by).log_topic_published(@topic)
|
|
|
|
|
|
|
|
# Clean up any publishing artifacts
|
|
|
|
SharedDraft.where(topic: @topic).delete_all
|
2019-04-16 14:09:51 +08:00
|
|
|
|
2018-03-14 03:59:12 +08:00
|
|
|
TopicTimer.where(topic: @topic).update_all(
|
|
|
|
deleted_at: DateTime.now,
|
|
|
|
deleted_by_id: @published_by.id
|
|
|
|
)
|
|
|
|
|
|
|
|
op = @topic.first_post
|
2019-04-16 14:09:51 +08:00
|
|
|
|
2018-03-14 03:59:12 +08:00
|
|
|
if op.present?
|
2020-03-06 02:51:51 +08:00
|
|
|
op.revisions.destroy_all
|
2019-04-16 14:09:51 +08:00
|
|
|
|
2018-03-27 04:06:20 +08:00
|
|
|
op.update_columns(
|
|
|
|
version: 1,
|
|
|
|
public_version: 1,
|
|
|
|
last_version_at: published_at
|
|
|
|
)
|
2018-03-14 03:59:12 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
MessageBus.publish("/topic/#{@topic.id}", reload_topic: true, refresh_stream: true)
|
|
|
|
|
|
|
|
@topic
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|