Module: Collavre::Comments::ApprovalActions

Extended by:
ActiveSupport::Concern
Included in:
Collavre::CommentsController
Defined in:
app/controllers/concerns/collavre/comments/approval_actions.rb

Instance Method Summary collapse

Instance Method Details

#approveObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/concerns/collavre/comments/approval_actions.rb', line 6

def approve
  # Claude Channel permission prompts reuse the approval comment UI but the
  # tool runs inside the remote Claude Code process — never execute it
  # server-side. Approving relays an "allow" decision to the suspended
  # session instead of invoking the ActionExecutor.
  return decide_claude_channel_permission(:allow) if @comment.claude_channel_permission?

  status = @comment.approval_status(Current.user)
  if status != :ok
    render_approval_status_error(status) and return
  end

  begin
    ::Comments::ActionExecutor.new(comment: @comment, executor: Current.user).call
    @comment = Comment.with_attached_images.includes(:comment_reactions, :comment_versions, :selected_version).find(@comment.id)
    render partial: "collavre/comments/comment", locals: { comment: @comment, current_topic_id: current_topic_context }
  rescue ::Comments::ActionExecutor::ExecutionError => e
    render json: { error: e.message }, status: :unprocessable_entity
  end
end

#denyObject

Reject a Claude Channel tool-permission prompt. There is no native equivalent (the native approval UI has approve-only; an un-approved action is simply left pending), so deny is exclusive to these comments.



30
31
32
33
34
35
36
# File 'app/controllers/concerns/collavre/comments/approval_actions.rb', line 30

def deny
  unless @comment.claude_channel_permission?
    render json: { error: I18n.t("collavre.comments.approve_not_allowed") }, status: :forbidden and return
  end

  decide_claude_channel_permission(:deny)
end

#update_actionObject



38
39
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/concerns/collavre/comments/approval_actions.rb', line 38

def update_action
  # Initial checks outside the lock
  executed_error = false
  update_success = false
  approver_mismatch_error = false
  status_error_key = nil
  status_http_status = nil
  validation_error_message = nil

  @comment.with_lock do
    @comment.reload

    status_in_lock = @comment.approval_status(Current.user)
    # Allow repairing invalid format if user is approver
    if status_in_lock == :invalid_action_format
      if @comment.approver_id == Current.user&.id
        status_in_lock = :ok
      else
        status_in_lock = :not_allowed
      end
    end

    # Creative admins can also edit actions even if not the designated approver
    if status_in_lock != :ok && @creative.has_permission?(Current.user, :admin)
      status_in_lock = :ok
    end

    if status_in_lock != :ok
      approver_mismatch_error = true
      status_error_key = case status_in_lock
      when :invalid_action_format then "collavre.comments.approve_invalid_format"
      when :missing_action then "collavre.comments.approve_missing_action"
      when :missing_approver then "collavre.comments.approve_missing_approver"
      when :admin_required then "collavre.comments.approve_admin_required"
      else "collavre.comments.approve_not_allowed"
      end
      status_http_status = case status_in_lock
      when :invalid_action_format, :missing_action, :missing_approver
                      :unprocessable_entity
      else
                      :forbidden
      end
    elsif @comment.action_executed_at.present?
      executed_error = true
    else
      action_payload = params.dig(:comment, :action)
      if action_payload.blank?
        validation_error_message = I18n.t("collavre.comments.approve_missing_action")
      else
        begin
          validator = ::Comments::ActionValidator.new(comment: @comment)
          parsed_payload = validator.validate!(action_payload)
          normalized_action = JSON.pretty_generate(parsed_payload)
          update_success = @comment.update(action: normalized_action)
        rescue ::Comments::ActionValidator::ValidationError => e
          validation_error_message = e.message
        end
      end
    end
  end

  if approver_mismatch_error
    render json: { error: I18n.t(status_error_key) }, status: status_http_status
  elsif validation_error_message
    render json: { error: validation_error_message }, status: :unprocessable_entity
  elsif executed_error
    render json: { error: I18n.t("collavre.comments.approve_already_executed") }, status: :unprocessable_entity
  elsif update_success
    @comment = Comment.with_attached_images.includes(:comment_reactions, :comment_versions, :selected_version).find(@comment.id)
    render partial: "collavre/comments/comment", locals: { comment: @comment, current_topic_id: current_topic_context }
  else
    error_message = @comment.errors.full_messages.to_sentence.presence || I18n.t("collavre.comments.action_update_error")
    render json: { error: error_message }, status: :unprocessable_entity
  end
rescue ::Comments::ActionValidator::ValidationError => e
  render json: { error: e.message }, status: :unprocessable_entity
end