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,
lib/generators/livechat/migration_helpers.rb,
app/controllers/livechat/visitor_controller.rb,
app/controllers/livechat/widgets_controller.rb,
app/controllers/livechat/messages_controller.rb,
app/controllers/livechat/dashboard_controller.rb,
app/controllers/livechat/application_controller.rb,
app/controllers/livechat/attachments_controller.rb,
app/controllers/concerns/livechat/request_context.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, RequestContext, Seeds, Widget, WidgetHelper Classes: ApplicationController, ApplicationRecord, AttachmentsController, Configuration, Conversation, ConversationsController, DashboardController, Engine, Mailer, Message, MessagesController, StreamChannel, VisitorController, WidgetsController

Constant Summary collapse

VERSION =
'0.8.2'

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)


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

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

.agent?(request) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.app_nameObject



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

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)


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

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

.base_controllerObject

Can this request work the inbox? Checked by every inbox action. The class Livechat::DashboardController inherits from. Resolved on every call rather than memoized, so a host that reassigns base_controller_class in a reloadable initializer is not pinned to a stale, unloaded constant.



35
36
37
# File 'lib/livechat.rb', line 35

def base_controller
  config.base_controller_class.to_s.constantize
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.



80
81
82
83
# File 'lib/livechat.rb', line 80

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



64
65
66
# File 'lib/livechat.rb', line 64

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.



60
61
62
# File 'lib/livechat.rb', line 60

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

.verify_stream(token) ⇒ Object



68
69
70
71
72
# File 'lib/livechat.rb', line 68

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