Class: CollavreSlack::SlackEventsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/collavre_slack/slack_events_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



6
7
8
9
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
# File 'app/controllers/collavre_slack/slack_events_controller.rb', line 6

def create
  body = request.raw_post
  payload = JSON.parse(body, symbolize_names: true)

  # Handle URL verification challenge first (needed for initial Slack app setup)
  # Slack sends a signed request, but we allow this through to complete setup
  if payload[:type] == "url_verification"
    render json: { challenge: payload[:challenge] }
    return
  end

  # For all other events, validate the signature
  unless valid_signature?(body)
    Rails.logger.warn("[SlackEvents] Invalid signature for event type: #{payload[:type]}")
    render json: { error: I18n.t("collavre_slack.errors.invalid_signature") }, status: :unauthorized
    return
  end

  handler = SlackEventHandler.new(payload: payload)
  normalized = handler.call

  if normalized
    case normalized[:type]
    when :message
      SlackInboundMessageJob.perform_later(normalized)
    when :reaction_added, :reaction_removed
      SlackInboundReactionJob.perform_later(normalized)
    when :message_updated
      SlackInboundMessageUpdateJob.perform_later(normalized)
    when :message_deleted
      SlackInboundMessageDeleteJob.perform_later(normalized)
    end
  end

  head :ok
end