Class: Collavre::TopicsController

Inherits:
ApplicationController show all
Includes:
CreativePermissionGuard
Defined in:
app/controllers/collavre/topics_controller.rb

Instance Method Summary collapse

Instance Method Details

#archiveObject



138
139
140
141
142
143
144
# File 'app/controllers/collavre/topics_controller.rb', line 138

def archive
  topic = @creative.topics.find(params[:id])
  topic.archive!

  broadcast_topic_event("archived", topic: topic.slice(:id, :name))
  render json: { success: true }
end

#channel_chipsObject



84
85
86
87
# File 'app/controllers/collavre/topics_controller.rb', line 84

def channel_chips
  topic = @creative.topics.find(params[:id])
  render partial: "collavre/comments/channel_chips", locals: { topic: topic }
end

#createObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/collavre/topics_controller.rb', line 48

def create
  topic = @creative.topics.build(topic_params)
  topic.user = Current.user

  Topic.transaction do
    if topic.save
      agent = nil
      if params[:agent_id].present?
        agent = User.find_by(id: params[:agent_id])
        topic.set_primary_agent!(agent) if agent&.ai_user?
      end

      # Move comments to the new topic if comment_ids provided
      comment_ids = Array(params[:comment_ids]).map(&:presence).compact
      if comment_ids.any?
        CommentMoveService.new(creative: @creative, user: Current.user).call(
          comment_ids: comment_ids,
          target_topic_id: topic.id
        )
      end

      broadcast_data = agent ? topic_json_with_agent(topic, agent) : topic.slice(:id, :name)
      broadcast_topic_event("created", topic: broadcast_data, user_id: Current.user.id)
      render json: topic, status: :created
    else
      render json: { errors: topic.errors.full_messages }, status: :unprocessable_entity
    end
  end
rescue CommentMoveService::MoveError => e
  render json: { errors: [ e.message ] }, status: :unprocessable_entity
end

#destroyObject



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/collavre/topics_controller.rb', line 100

def destroy
  topic = @creative.topics.find(params[:id])

  if topic.name == Creative::MAIN_TOPIC_NAME
    render json: { error: I18n.t("collavre.topics.cannot_delete_main") }, status: :unprocessable_entity and return
  end

  topic_id = topic.id
  topic.destroy

  broadcast_topic_event("deleted", topic_id: topic_id)
  head :no_content
end

#indexObject



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
# File 'app/controllers/collavre/topics_controller.rb', line 10

def index
  is_owner = @creative.user == Current.user
  can_manage = @creative.has_permission?(Current.user, :admin) || is_owner
  can_create_topic = can_manage || @creative.has_permission?(Current.user, :write)

  # Eagerly ensure Main (and System for inboxes) exist BEFORE loading
  # active_topics. Otherwise the first inbox visit sees no System topic in
  # the sidebar, and when a later notification creates it via
  # find_or_create_by!, the unread badge appears but the user has no way to
  # open the topic.
  main_topic = @creative.main_topic(fallback_user: Current.user)
  system_topic = @creative.inbox? ? @creative.system_topic(fallback_user: Current.user) : nil

  active_topics = @creative.topics.active.includes(primary_agent: { avatar_attachment: :blob }).order(:created_at).to_a
  archived_topics = @creative.topics.archived.order(:created_at)

  last_topic_id = if Current.user
                    UserCreativePreference
                      .where(user_id: Current.user.id, creative_id: @creative.id)
                      .pick(:last_topic_id)
  end

  system_topic_id = system_topic&.id
  main_topic_id = main_topic.id

  render json: {
    topics: active_topics.map { |t| topic_json(t) },
    archived_topics: archived_topics,
    can_manage: can_manage,
    can_create_topic: can_create_topic,
    last_topic_id: last_topic_id,
    is_inbox: @creative.inbox?,
    system_topic_id: system_topic_id,
    main_topic_id: main_topic_id,
    effective_creative_id: @creative.id
  }
end

#moveObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/controllers/collavre/topics_controller.rb', line 114

def move
  topic = @creative.topics.find(params[:id])
  target_creative = Creative.find(params[:target_creative_id]).effective_origin

  unless target_creative.has_permission?(Current.user, :write) || target_creative.user == Current.user
    render json: { error: I18n.t("collavre.topics.move.no_target_permission") }, status: :forbidden and return
  end

  # Check for duplicate topic name in target creative
  if target_creative.topics.where(name: topic.name).exists?
    render json: { error: I18n.t("collavre.topics.move.duplicate_name", name: topic.name) }, status: :unprocessable_entity and return
  end

  Topic.transaction do
    topic.comments.update_all(creative_id: target_creative.id)
    topic.update!(creative: target_creative)
  end

  broadcast_topic_event("deleted", topic_id: topic.id)
  broadcast_topic_event("created", creative: target_creative, topic: topic.slice(:id, :name))

  render json: { success: true, topic: topic.slice(:id, :name), target_creative_id: target_creative.id }
end

#next_nameObject



80
81
82
# File 'app/controllers/collavre/topics_controller.rb', line 80

def next_name
  render json: { name: generate_next_topic_name }
end

#reorderObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/controllers/collavre/topics_controller.rb', line 154

def reorder
  topic_ids = params[:topic_ids]
  unless topic_ids.is_a?(Array) && topic_ids.present?
    render json: { error: "Invalid topic_ids" }, status: :unprocessable_entity and return
  end

  Topic.transaction do
    topic_ids.each_with_index do |id, index|
      @creative.topics.where(id: id).update_all(position: index)
    end
  end

  broadcast_topic_event("reordered", topic_ids: topic_ids)

  render json: { success: true }
end

#set_primary_agentObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/controllers/collavre/topics_controller.rb', line 171

def set_primary_agent
  topic = @creative.topics.find(params[:id])
  agent = User.find_by(id: params[:agent_id])

  unless agent&.ai_user?
    render json: { error: I18n.t("collavre.topics.not_ai_agent") }, status: :unprocessable_entity and return
  end

  topic.set_primary_agent!(agent)

  broadcast_topic_event("updated", topic: topic_json_with_agent(topic, agent))

  render json: { success: true, topic: topic_json_with_agent(topic, agent) }
end

#unarchiveObject



146
147
148
149
150
151
152
# File 'app/controllers/collavre/topics_controller.rb', line 146

def unarchive
  topic = @creative.topics.find(params[:id])
  topic.unarchive!

  broadcast_topic_event("unarchived", topic: topic.slice(:id, :name, :archived_at))
  render json: { success: true }
end

#updateObject



89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/collavre/topics_controller.rb', line 89

def update
  topic = @creative.topics.find(params[:id])

  if topic.update(topic_params)
    broadcast_topic_event("updated", topic: topic_json(topic))
    render json: topic_json(topic)
  else
    render json: { errors: topic.errors.full_messages }, status: :unprocessable_entity
  end
end