Class: TopicPublisher

Inherits:
Object
  • Object
show all
Defined in:
lib/topic_publisher.rb

Instance Method Summary collapse

Constructor Details

#initialize(topic, published_by, category_id) ⇒ TopicPublisher

Returns a new instance of TopicPublisher.



4
5
6
7
8
# File 'lib/topic_publisher.rb', line 4

def initialize(topic, published_by, category_id)
  @topic = topic
  @published_by = published_by
  @category_id = category_id
end

Instance Method Details

#publish!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/topic_publisher.rb', line 10

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

  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,
  )

  MessageBus.publish("/topic/#{@topic.id}", reload_topic: true, refresh_stream: true)

  @topic
end