Class: TurboChat::ChatMessage

Inherits:
ApplicationRecord show all
Includes:
BlockedWordsModeration, BodyLengthValidation, Broadcasting, Formatting, MentionValidation, Signals
Defined in:
app/models/turbo_chat/chat_message.rb,
app/models/turbo_chat/chat_message/signals.rb,
app/models/turbo_chat/chat_message/formatting.rb,
app/models/turbo_chat/chat_message/broadcasting.rb,
app/models/turbo_chat/chat_message/mention_validation.rb,
app/models/turbo_chat/chat_message/body_length_validation.rb,
app/models/turbo_chat/chat_message/blocked_words_moderation.rb

Defined Under Namespace

Modules: BlockedWordsModeration, BodyLengthValidation, Broadcasting, Formatting, MentionValidation, Signals

Constant Summary collapse

MENTION_PATTERN =
/(?<![[:alnum:]_])@[[:alpha:]][[:alnum:]_]{0,31}/.freeze
ROLE_MENTION_PATTERN =
/\A@[A-Z][A-Z0-9_]{0,31}\z/.freeze
SOURCE_KEY_PATTERN =
/\A[a-z0-9][a-z0-9_-]{0,31}\z/.freeze
STREAM_NAME =
:messages
MESSAGE_PARTIAL =
"turbo_chat/chat_messages/message"
CHAT_MESSAGE_PARTIAL =
"turbo_chat/chat_messages/chat_message"
SIGNALS_PARTIAL =
"turbo_chat/chat_messages/signals"
MEMBERSHIP_SYSTEM_MESSAGE_TEMPLATES =
{
  invited: "%{actor} invited %{subject}.",
  accepted: "%{actor} accepted the invitation.",
  declined: "%{actor} declined the invitation.",
  left: "%{actor} left the chat.",
  muted: "%{actor} muted %{subject}.",
  unmuted: "%{actor} unmuted %{subject}.",
  timed_out: "%{actor} timed out %{subject}.",
  timeout_cleared: "%{actor} cleared timeout for %{subject}.",
  banned: "%{actor} removed %{subject} from the chat."
}.freeze
MEMBERSHIP_SYSTEM_EVENTS_WITH_SUBJECT =
i[invited muted unmuted timed_out timeout_cleared banned].freeze
MEMBERSHIP_SYSTEM_EVENT_TYPES =
MEMBERSHIP_SYSTEM_MESSAGE_TEMPLATES.keys.freeze
DEFAULT_SOURCE =
"app".freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Formatting

#edited?, #formatted_participant_role, #formatted_timestamp, #formatted_updated_timestamp, #participant_display_name, #participant_membership_role

Class Method Details

.create_membership_system_message!(chat:, actor:, event:, subject: nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/turbo_chat/chat_message.rb', line 83

def create_membership_system_message!(chat:, actor:, event:, subject: nil)
  return nil unless system_messages_enabled?(chat)
  return nil if chat.nil? || actor.nil?

  normalized_event = event.to_s.to_sym
  return nil unless MEMBERSHIP_SYSTEM_EVENT_TYPES.include?(normalized_event)

  body = membership_system_message_body(actor: actor, event: normalized_event, subject: subject)
  return nil if body.blank?

  create!(chat: chat, participant: actor, kind: :system, body: body)
end

.normalize_external_id(value) ⇒ Object



79
80
81
# File 'app/models/turbo_chat/chat_message.rb', line 79

def normalize_external_id(value)
  value.to_s.strip.presence
end

.normalize_source_key(value) ⇒ Object



72
73
74
75
76
77
# File 'app/models/turbo_chat/chat_message.rb', line 72

def normalize_source_key(value)
  normalized = value.to_s.strip.downcase
  return DEFAULT_SOURCE if normalized.blank?

  normalized
end

.system_messages_enabled?(chat = nil) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'app/models/turbo_chat/chat_message.rb', line 96

def system_messages_enabled?(chat = nil)
  TurboChat::Configuration.config_boolean(:system_messages, default: true, chat: chat)
end

Instance Method Details

#signal_textObject



61
62
63
64
65
# File 'app/models/turbo_chat/chat_message.rb', line 61

def signal_text
  return nil unless signal?

  body.presence
end

#signal_text=(value) ⇒ Object



67
68
69
# File 'app/models/turbo_chat/chat_message.rb', line 67

def signal_text=(value)
  self.body = value
end