Module: Livechat

Defined in:
lib/livechat.rb,
lib/livechat/seeds.rb,
lib/livechat/engine.rb,
lib/livechat/widget.rb,
lib/livechat/version.rb,
lib/livechat/channels.rb,
lib/livechat/configuration.rb,
lib/livechat/notifications.rb,
app/mailers/livechat/mailer.rb,
app/models/livechat/message.rb,
app/models/livechat/conversation.rb,
app/helpers/livechat/inbox_helper.rb,
app/helpers/livechat/widget_helper.rb,
app/models/livechat/application_record.rb,
app/controllers/livechat/visitor_controller.rb,
app/controllers/livechat/widgets_controller.rb,
app/controllers/livechat/messages_controller.rb,
app/controllers/livechat/application_controller.rb,
app/controllers/livechat/attachments_controller.rb,
app/controllers/livechat/conversations_controller.rb,
lib/generators/livechat/install/install_generator.rb

Overview

Loaded from the engine only when Action Cable is present (see engine.rb), so a host without a cable never pays for it and never trips Zeitwerk eager-load on a missing ActionCable constant.

Defined Under Namespace

Modules: Generators, InboxHelper, Notifications, Seeds, Widget, WidgetHelper Classes: ApplicationController, ApplicationRecord, AttachmentsController, Configuration, Conversation, ConversationsController, Engine, Mailer, Message, MessagesController, StreamChannel, VisitorController, WidgetsController

Constant Summary collapse

VERSION =
'0.7.1'

Class Method Summary collapse

Class Method Details

.action_cable_enabled?Boolean

Realtime push is opt-in and needs Action Cable loaded. Off by default: the widget and inbox poll, and only speed up when a host turns this on.

Returns:

  • (Boolean)


45
46
47
# File 'lib/livechat.rb', line 45

def action_cable_enabled?
  config.action_cable && defined?(ActionCable) ? true : false
end

.agent?(request) ⇒ Boolean

Can this request work the inbox? Checked by every inbox action.

Returns:

  • (Boolean)


32
33
34
# File 'lib/livechat.rb', line 32

def agent?(request)
  !!config.authorize_agent.call(request)
end

.app_nameObject



67
68
69
# File 'lib/livechat.rb', line 67

def app_name
  config.app_name.presence || rails_app_name
end

.attachments_enabled?Boolean

File attachments are on only when the host has Active Storage AND hasn't switched them off. Guards every attachment path so the widget degrades to text-only rather than erroring where Active Storage is absent.

Returns:

  • (Boolean)


39
40
41
# File 'lib/livechat.rb', line 39

def attachments_enabled?
  config.attach_files && Message.attachments_supported?
end

.configObject



17
18
19
# File 'lib/livechat.rb', line 17

def config
  @config ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields:



21
22
23
# File 'lib/livechat.rb', line 21

def configure
  yield config
end

.display_name_for(agent_label) ⇒ Object

The public face of an agent message. Blank display names fall back to a localized "Support", so visitors always see a sender.



73
74
75
76
# File 'lib/livechat.rb', line 73

def display_name_for(agent_label)
  name = config.agent_display_name.call(agent_label).presence
  name || I18n.t(:team, scope: :livechat, default: 'Support')
end

.enabled?(request) ⇒ Boolean

Can this request see the widget and write messages? Checked on the server for every widget endpoint and by the helper before rendering.

Returns:

  • (Boolean)


27
28
29
# File 'lib/livechat.rb', line 27

def enabled?(request)
  !!config.enabled.call(request)
end

.sign_stream(name) ⇒ Object



57
58
59
# File 'lib/livechat.rb', line 57

def sign_stream(name)
  stream_verifier.generate(name.to_s)
end

.stream_verifierObject

Signs the Action Cable stream names handed to clients, so a subscriber can only listen to a conversation the server already let it see — the widget receives its stream token through the gated /conversation endpoint, never guessing one. Same idea as Turbo's signed streams.



53
54
55
# File 'lib/livechat.rb', line 53

def stream_verifier
  @stream_verifier ||= Rails.application.message_verifier('livechat/stream')
end

.verify_stream(token) ⇒ Object



61
62
63
64
65
# File 'lib/livechat.rb', line 61

def verify_stream(token)
  stream_verifier.verify(token.to_s)
rescue StandardError
  nil
end