Class: Collavre::TopicsController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- ApplicationController
- Collavre::TopicsController
- Includes:
- CreativePermissionGuard
- Defined in:
- app/controllers/collavre/topics_controller.rb
Instance Method Summary collapse
- #archive ⇒ Object
- #create ⇒ Object
- #destroy ⇒ Object
- #index ⇒ Object
- #move ⇒ Object
- #next_name ⇒ Object
- #reorder ⇒ Object
- #set_primary_agent ⇒ Object
- #unarchive ⇒ Object
- #update ⇒ Object
Instance Method Details
#archive ⇒ Object
125 126 127 128 129 130 131 |
# File 'app/controllers/collavre/topics_controller.rb', line 125 def archive topic = @creative.topics.find(params[:id]) topic.archive! broadcast_topic_event("archived", topic: topic.slice(:id, :name)) render json: { success: true } end |
#create ⇒ Object
40 41 42 43 44 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 |
# File 'app/controllers/collavre/topics_controller.rb', line 40 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. }, status: :unprocessable_entity end end rescue CommentMoveService::MoveError => e render json: { errors: [ e. ] }, status: :unprocessable_entity end |
#destroy ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'app/controllers/collavre/topics_controller.rb', line 87 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 |
#index ⇒ 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 |
# File 'app/controllers/collavre/topics_controller.rb', line 10 def index is_owner = @creative.user == Current.user can_manage = @creative.(Current.user, :admin) || is_owner can_create_topic = can_manage || @creative.(Current.user, :write) active_topics = @creative.topics.active.order(:created_at).to_a preload_primary_agents(active_topics) 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 = @creative.inbox? ? @creative.topics.find_by(name: Creative::SYSTEM_TOPIC_NAME)&.id : nil main_topic_id = @creative.main_topic(fallback_user: Current.user).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 } end |
#move ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/controllers/collavre/topics_controller.rb', line 101 def move topic = @creative.topics.find(params[:id]) target_creative = Creative.find(params[:target_creative_id]).effective_origin unless target_creative.(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_name ⇒ Object
72 73 74 |
# File 'app/controllers/collavre/topics_controller.rb', line 72 def next_name render json: { name: generate_next_topic_name } end |
#reorder ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'app/controllers/collavre/topics_controller.rb', line 141 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_agent ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'app/controllers/collavre/topics_controller.rb', line 158 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 |
#unarchive ⇒ Object
133 134 135 136 137 138 139 |
# File 'app/controllers/collavre/topics_controller.rb', line 133 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 |
#update ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'app/controllers/collavre/topics_controller.rb', line 76 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. }, status: :unprocessable_entity end end |