Module: TurboChat::ApplicationHelper::MessageRendering

Included in:
TurboChat::ApplicationHelper
Defined in:
app/helpers/turbo_chat/application_helper/message_rendering.rb

Instance Method Summary collapse

Instance Method Details

#chat_mentions_container_inline_style(chat: nil) ⇒ Object



19
20
21
22
23
24
25
26
# File 'app/helpers/turbo_chat/application_helper/message_rendering.rb', line 19

def chat_mentions_container_inline_style(chat: nil)
  hex_color = normalize_hex_color(chat_config_value(:mention_mark_hex_color, chat: chat)) ||
              normalize_hex_color(chat_config_value(:mention_highlight_hex_color, chat: chat))
  return nil if hex_color.blank?

  mention_mark_background = hex_color_with_alpha(hex_color, alpha: 0.22)
  "--chat-mention-highlight-color: #{hex_color}; --chat-mention-mark-background: #{mention_mark_background};"
end

#chat_message_css_classes(chat_message:, own_message:) ⇒ Object



4
5
6
7
8
9
10
# File 'app/helpers/turbo_chat/application_helper/message_rendering.rb', line 4

def chat_message_css_classes(chat_message:, own_message:)
  [
    "chat-bubble",
    (own_message ? "chat-bubble--own" : nil),
    *resolve_custom_message_css_classes(chat_message: chat_message, own_message: own_message)
  ].compact.uniq.join(" ")
end

#chat_message_inline_style(chat_message:, own_message:) ⇒ Object



12
13
14
15
16
17
# File 'app/helpers/turbo_chat/application_helper/message_rendering.rb', line 12

def chat_message_inline_style(chat_message:, own_message:)
  hex_color = resolve_message_hex_color(chat_message: chat_message, own_message: own_message)
  return nil if hex_color.blank?

  "--chat-bubble-bg: #{hex_color}; --chat-bubble-border: #{hex_color};"
end

#chat_message_mention_tokens(chat_message) ⇒ Object



45
46
47
48
49
50
# File 'app/helpers/turbo_chat/application_helper/message_rendering.rb', line 45

def chat_message_mention_tokens(chat_message)
  chat = chat_message.respond_to?(:chat) ? chat_message.chat : nil
  return [] unless chat_enable_mentions?(chat: chat) && chat_message.present?

  chat_message.body.to_s.scan(MENTION_PATTERN).uniq
end

#render_chat_message_body(chat_message) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/helpers/turbo_chat/application_helper/message_rendering.rb', line 28

def render_chat_message_body(chat_message)
  chat = chat_message.respond_to?(:chat) ? chat_message.chat : nil
  body = chat_message.body.to_s
  # html_safe is safe here: decorate_plain_message_text guarantees the body
  # is html_escaped before any markup injection (see method comment above).
  unless chat_render_message_html?(chat: chat)
    return (:p, decorate_plain_message_text(body, chat: chat).html_safe, class: "chat-body")
  end

  sanitized_html = sanitize(
    body,
    tags: Array(chat_config_value(:message_html_tags, default: [], chat: chat)),
    attributes: Array(chat_config_value(:message_html_attributes, default: [], chat: chat))
  )
  (:div, sanitized_html, class: "chat-body")
end