Class: TopicConverter

Inherits:
Object
  • Object
show all
Defined in:
app/models/topic_converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(topic, user) ⇒ TopicConverter

Returns a new instance of TopicConverter.



6
7
8
9
# File 'app/models/topic_converter.rb', line 6

def initialize(topic, user)
  @topic = topic
  @user = user
end

Instance Attribute Details

#topicObject (readonly)

Returns the value of attribute topic.



4
5
6
# File 'app/models/topic_converter.rb', line 4

def topic
  @topic
end

Instance Method Details

#convert_to_private_messageObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/topic_converter.rb', line 45

def convert_to_private_message
  Topic.transaction do
    was_public = !@topic.category.read_restricted
    @topic.update_category_topic_count_by(-1) if @topic.visible

    PostRevisor.new(@topic.first_post, @topic).revise!(
      @user,
      category_id: nil,
      archetype: Archetype.private_message,
    )

    raise ActiveRecord::Rollback if !@topic.valid?

    add_allowed_users
    update_post_uploads_secure_status
    add_small_action("private_topic")
    Tag.update_counters(@topic.tags, { public_topic_count: -1 }) if was_public
    UserProfile.remove_featured_topic_from_all_profiles(@topic)

    Jobs.enqueue(:topic_action_converter, topic_id: @topic.id)
    Jobs.enqueue(:delete_inaccessible_notifications, topic_id: @topic.id)

    watch_topic(@topic)
  end

  @topic
end

#convert_to_public_topic(category_id = nil) ⇒ Object



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
# File 'app/models/topic_converter.rb', line 11

def convert_to_public_topic(category_id = nil)
  Topic.transaction do
    category_id ||=
      SiteSetting.uncategorized_category_id if SiteSetting.allow_uncategorized_topics

    @category = Category.find_by(id: category_id) if category_id
    @category ||=
      Category
        .where(read_restricted: false)
        .where.not(id: SiteSetting.uncategorized_category_id)
        .first

    PostRevisor.new(@topic.first_post, @topic).revise!(
      @user,
      category_id: @category.id,
      archetype: Archetype.default,
    )

    raise ActiveRecord::Rollback if !@topic.valid?

    update_user_stats
    update_post_uploads_secure_status
    add_small_action("public_topic")
    Tag.update_counters(@topic.tags, { public_topic_count: 1 }) if !@category.read_restricted

    Jobs.enqueue(:topic_action_converter, topic_id: @topic.id)
    Jobs.enqueue(:delete_inaccessible_notifications, topic_id: @topic.id)

    watch_topic(@topic)
  end

  @topic
end