discourse/lib/topic_publisher.rb
Kane York 10ddb8a9c4 FIX: Use destroy_all instead of delete_all for shared drafts
Rails has an odd behavior for calling .delete_all on a has_many relation - the
default behavior is to nullify the foreign key fields instead of actually
'DELETE'ing the records.

Additionally, publishing a shared draft topic creates a PostRevision that the
NotifyPostRevision job picks up which is then promptly deleted.

Use destroy_all when cleaning up the revisions and have the NotifyPostRevision
job tolerate deleted PostRevision records.

This takes a small performance hit (several SQL DELETEs instead of just one)
but shouldn't be too much of an issue (high cardinalities range from 30-100).
2020-03-05 11:13:43 -08:00

55 lines
1.3 KiB
Ruby

# frozen_string_literal: true
class TopicPublisher
def initialize(topic, published_by, category_id)
@topic = topic
@published_by = published_by
@category_id = category_id
end
def publish!
published_at = Time.zone.now
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
@topic.update_columns(visible: true)
StaffActionLogger.new(@published_by).log_topic_published(@topic)
# Clean up any publishing artifacts
SharedDraft.where(topic: @topic).delete_all
TopicTimer.where(topic: @topic).update_all(
deleted_at: DateTime.now,
deleted_by_id: @published_by.id
)
op = @topic.first_post
if op.present?
op.revisions.destroy_all
op.update_columns(
version: 1,
public_version: 1,
last_version_at: published_at
)
end
end
MessageBus.publish("/topic/#{@topic.id}", reload_topic: true, refresh_stream: true)
@topic
end
end