Module: TurboChat::Messages

Defined in:
lib/turbo_chat/messages.rb

Defined Under Namespace

Classes: AuthorizationError, InvalidMessageError

Constant Summary collapse

SEND_MESSAGE_KIND =
:message
VALID_SENT_AT_TYPES =
[String, Numeric, Date, Time, DateTime].freeze

Class Method Summary collapse

Class Method Details

.ingest_external!(chat:, participant:, body:, source:, external_id:, sent_at: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/turbo_chat/messages.rb', line 33

def ingest_external!(chat:, participant:, body:, source:, external_id:, sent_at: nil)
  validate_external_id!(external_id)

  send_message_as(
    participant,
    chat,
    body: body,
    source: source,
    external_id: external_id,
    sent_at: sent_at
  )
end

.send_message_as(participant, chat, body:, source: TurboChat::ChatMessage::DEFAULT_SOURCE, external_id: nil, sent_at: nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/turbo_chat/messages.rb', line 10

def send_message_as(participant, chat, body:, source: TurboChat::ChatMessage::DEFAULT_SOURCE, external_id: nil, sent_at: nil)
  validate_message_inputs!(chat: chat, participant: participant, body: body)
  attributes = normalized_message_attributes(
    body: body,
    source: source,
    external_id: external_id,
    sent_at: sent_at
  )
  authorize_post_message!(participant: participant, chat: chat)

  find_existing_message(chat: chat, source: attributes[:source], external_id: attributes[:external_id]) ||
    create_message!(
      chat: chat,
      participant: participant,
      body: attributes[:body],
      source: attributes[:source],
      external_id: attributes[:external_id],
      sent_at: attributes[:sent_at]
    )
rescue ActiveRecord::RecordNotUnique
  handle_record_not_unique!(chat: chat, source: attributes[:source], external_id: attributes[:external_id])
end