|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# ScrimChatChannel — Real-time cross-organization chat for scrim sessions. |
| 4 | +# |
| 5 | +# Allows members of both participating organizations to exchange messages |
| 6 | +# during a scrim. Authorization is based on the scrim's owner org and its |
| 7 | +# linked ScrimRequest, which carries both organization IDs. |
| 8 | +# |
| 9 | +# Subscription params: |
| 10 | +# scrim_id [String] — UUID of the scrim to subscribe to |
| 11 | +# |
| 12 | +# Actions: |
| 13 | +# subscribed — validates access, opens the scrim-scoped stream |
| 14 | +# speak — persists a message; broadcast is handled by the model callback |
| 15 | +# unsubscribed — stops all streams (cleanup) |
| 16 | +# |
| 17 | +# @example Frontend subscription |
| 18 | +# consumer.subscriptions.create( |
| 19 | +# { channel: 'ScrimChatChannel', scrim_id: 'uuid' }, |
| 20 | +# { received: (data) => console.log(data) } |
| 21 | +# ) |
| 22 | +class ScrimChatChannel < ApplicationCable::Channel |
| 23 | + MAX_CONTENT_LENGTH = 1000 |
| 24 | + |
| 25 | + def subscribed |
| 26 | + scrim = find_authorized_scrim |
| 27 | + unless scrim |
| 28 | + logger.warn "[ScrimChat] Rejected subscription — user=#{current_user.id} scrim_id=#{params[:scrim_id]}" |
| 29 | + reject |
| 30 | + return |
| 31 | + end |
| 32 | + |
| 33 | + @scrim = scrim |
| 34 | + stream_name = canonical_stream_name(@scrim) |
| 35 | + stream_from stream_name |
| 36 | + logger.info "[ScrimChat] subscribed user=#{current_user.id} scrim=#{@scrim.id} stream=#{stream_name}" |
| 37 | + end |
| 38 | + |
| 39 | + def unsubscribed |
| 40 | + stop_all_streams |
| 41 | + logger.info "[ScrimChat] user=#{current_user.id} unsubscribed" |
| 42 | + end |
| 43 | + |
| 44 | + # Receives a message from the client and persists it. |
| 45 | + # Broadcasting is triggered by ScrimMessage's after_create_commit callback. |
| 46 | + # |
| 47 | + # @param data [Hash] { "content" => "message text" } |
| 48 | + def speak(data) |
| 49 | + return unless @scrim |
| 50 | + |
| 51 | + content = validate_content(data['content']) |
| 52 | + return unless content |
| 53 | + |
| 54 | + ScrimMessage.create!(scrim: @scrim, user: current_user, |
| 55 | + organization: current_user.organization, content: content) |
| 56 | + rescue ActiveRecord::RecordInvalid => e |
| 57 | + logger.error "[ScrimChat] Failed to persist message for scrim=#{@scrim.id}: #{e.message}" |
| 58 | + transmit({ error: 'Failed to send message' }) |
| 59 | + end |
| 60 | + |
| 61 | + private |
| 62 | + |
| 63 | + def validate_content(raw) |
| 64 | + content = raw.to_s.strip |
| 65 | + if content.blank? |
| 66 | + transmit({ error: 'Message content cannot be blank' }) |
| 67 | + return nil |
| 68 | + end |
| 69 | + if content.length > MAX_CONTENT_LENGTH |
| 70 | + transmit({ error: "Message exceeds #{MAX_CONTENT_LENGTH} characters" }) |
| 71 | + return nil |
| 72 | + end |
| 73 | + content |
| 74 | + end |
| 75 | + |
| 76 | + # Finds the scrim and verifies the current user's org is a participant. |
| 77 | + # |
| 78 | + # Checks owner org first, then falls back to ScrimRequest cross-org check. |
| 79 | + # Always returns nil for both "not found" and "not a participant" cases so |
| 80 | + # that foreign scrim UUIDs are not leaked via subscription rejection messages. |
| 81 | + # |
| 82 | + # @return [Scrim, nil] |
| 83 | + def find_authorized_scrim |
| 84 | + scrim_id = params[:scrim_id] |
| 85 | + return nil unless scrim_id.present? |
| 86 | + |
| 87 | + # ActionCable context doesn't go through authenticate_request!, so |
| 88 | + # Current.organization_id must be set manually for OrganizationScoped models. |
| 89 | + Current.organization_id = current_user.organization_id |
| 90 | + |
| 91 | + # Owner org — most common path |
| 92 | + scrim = current_user.organization.scrims.find_by(id: scrim_id) |
| 93 | + return scrim if scrim |
| 94 | + |
| 95 | + # Cross-org participant via ScrimRequest |
| 96 | + cross_org_scrim(scrim_id) |
| 97 | + end |
| 98 | + |
| 99 | + # Returns the scrim if the current user's org is the opposing participant |
| 100 | + # in the linked ScrimRequest. Returns nil otherwise. |
| 101 | + def cross_org_scrim(scrim_id) |
| 102 | + # Bypass OrganizationScoped — the scrim may belong to the opponent's org |
| 103 | + scrim = Scrim.unscoped_by_organization.find_by(id: scrim_id) |
| 104 | + return nil unless scrim |
| 105 | + |
| 106 | + request = scrim_request_for(scrim) |
| 107 | + return nil unless request |
| 108 | + |
| 109 | + org_id = current_user.organization_id |
| 110 | + return scrim if request.requesting_organization_id == org_id || |
| 111 | + request.target_organization_id == org_id |
| 112 | + |
| 113 | + nil |
| 114 | + end |
| 115 | + |
| 116 | + def scrim_request_for(scrim) |
| 117 | + return nil unless scrim.scrim_request_id.present? |
| 118 | + |
| 119 | + ScrimRequest.find_by(id: scrim.scrim_request_id) |
| 120 | + end |
| 121 | + |
| 122 | + # Uses ScrimRequest ID as canonical stream so both orgs share the same channel. |
| 123 | + # Falls back to per-scrim stream when no request is linked (manual scrims). |
| 124 | + def canonical_stream_name(scrim) |
| 125 | + if scrim.scrim_request_id.present? |
| 126 | + "scrim_request_chat_#{scrim.scrim_request_id}" |
| 127 | + else |
| 128 | + "scrim_chat_#{scrim.id}" |
| 129 | + end |
| 130 | + end |
| 131 | +end |
0 commit comments