Class: Livechat::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/livechat/configuration.rb

Overview

Host-tunable settings. Everything has a safe default, so a fresh install works with zero configuration; the hooks below let an app decide who can chat, who answers, and how everyone is named.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/livechat/configuration.rb', line 94

def initialize
  @app_name = nil
  @enabled = ->(_request) { true }
  @authorize_agent = ->(_request) { Rails.env.development? }
  @current_user = ->(_request) {}
  @visitor_label = ->(user) { user.try(:name).presence || user.try(:email).presence || user.to_s }
  @agent_label = ->(user) { user.try(:name).presence || user.try(:email).presence || user.to_s }
  @agent_display_name = ->(label) { label }
  @greeting = nil
  @reply_time_text = nil
  @launcher_label = nil
  @accent_color = nil
  @show_launcher = true
  @agent_emails = nil
  @mailer_from = nil
  @on_visitor_message = ->(_message) {}
  @on_agent_message = ->(_message) {}
  @rate_limit = { to: 30, within: 60 }
  @mount_path = '/livechat'
  @attach_files = true
  @max_attachments = 5
  @max_attachment_size = 10 * 1024 * 1024
  @allowed_attachment_types = nil
  @action_cable = false
  @action_cable_url = '/cable'
end

Instance Attribute Details

#accent_colorObject

Brand color for the widget (launcher, header, visitor bubbles, send button) as a hex value, e.g. "#7c3aed". The widget picks black or white text automatically for contrast. nil keeps the built-in blue.



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

def accent_color
  @accent_color
end

#action_cableObject

Push new messages over Action Cable instead of waiting for the next poll. Opt-in and off by default: polling stays the transport, so a host that never mounts a cable keeps working; when on (and Action Cable is present) a new message nudges the widget and the inbox to refresh at once. Requires /cable mounted in the host's routes.



84
85
86
# File 'lib/livechat/configuration.rb', line 84

def action_cable
  @action_cable
end

#action_cable_urlObject

Where the host mounts Action Cable. Only consulted when action_cable is on. Keep in sync with the mount ActionCable... => "/cable" in routes.



88
89
90
# File 'lib/livechat/configuration.rb', line 88

def action_cable_url
  @action_cable_url
end

#agent_display_nameObject

What visitors see as the sender of an agent message. Receives the stored agent_label; return it unchanged (default), a first name, or a constant like "Support team" to keep agents anonymous.



37
38
39
# File 'lib/livechat/configuration.rb', line 37

def agent_display_name
  @agent_display_name
end

#agent_emailsObject

Email addresses to notify when a visitor writes and nobody has read it — an array, or a callable returning one. nil disables the built-in email.



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

def agent_emails
  @agent_emails
end

#agent_labelObject

Turn a resolved user into the attribution stored on an agent's message. Every reply carries this, so a thread with several teammates stays legible.



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

def agent_label
  @agent_label
end

#allowed_attachment_typesObject

Cap on attachments per message, on the size of each file (bytes), and an optional content-type allowlist (nil accepts any type). Enforced on the server; a rejected upload comes back as a validation error.



77
78
79
# File 'lib/livechat/configuration.rb', line 77

def allowed_attachment_types
  @allowed_attachment_types
end

#app_nameObject

Shown in the widget header and in notification emails. nil resolves to the Rails application name.



10
11
12
# File 'lib/livechat/configuration.rb', line 10

def app_name
  @app_name
end

#attach_filesObject

Let visitors and agents attach files to messages. Requires Active Storage in the host app (rails active_storage:install); silently ignored when it isn't present, so the widget keeps working. Set false to turn attachments off even where Active Storage exists.



72
73
74
# File 'lib/livechat/configuration.rb', line 72

def attach_files
  @attach_files
end

#authorize_agentObject

Per-request gate for the inbox. Defaults to development only — override it before deploying, e.g. with an admin check. Independent of enabled, so your team can answer from production even where the widget is off.



19
20
21
# File 'lib/livechat/configuration.rb', line 19

def authorize_agent
  @authorize_agent
end

#current_userObject

Resolve the current user (optional). Return an object responding to #id, or nil. Receives the request. Signed-in users keep one conversation across devices; guests are tracked with a cookie.



24
25
26
# File 'lib/livechat/configuration.rb', line 24

def current_user
  @current_user
end

#enabledObject

Per-request gate for the widget and its endpoints. Return false to hide the widget and reject writes for this request.



14
15
16
# File 'lib/livechat/configuration.rb', line 14

def enabled
  @enabled
end

#greetingObject

Widget copy. nil uses the localized defaults. reply_time_text is the honest line under the greeting — "We usually reply within a few hours."



41
42
43
# File 'lib/livechat/configuration.rb', line 41

def greeting
  @greeting
end

#launcher_labelObject

Widget copy. nil uses the localized defaults. reply_time_text is the honest line under the greeting — "We usually reply within a few hours."



41
42
43
# File 'lib/livechat/configuration.rb', line 41

def launcher_label
  @launcher_label
end

#mailer_fromObject

From-address for the built-in notification emails. Required for any email to be sent.



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

def mailer_from
  @mailer_from
end

#max_attachment_sizeObject

Cap on attachments per message, on the size of each file (bytes), and an optional content-type allowlist (nil accepts any type). Enforced on the server; a rejected upload comes back as a validation error.



77
78
79
# File 'lib/livechat/configuration.rb', line 77

def max_attachment_size
  @max_attachment_size
end

#max_attachmentsObject

Cap on attachments per message, on the size of each file (bytes), and an optional content-type allowlist (nil accepts any type). Enforced on the server; a rejected upload comes back as a validation error.



77
78
79
# File 'lib/livechat/configuration.rb', line 77

def max_attachments
  @max_attachments
end

#mount_pathObject

Where the engine is mounted. The widget calls paths under it, so keep this in sync with the mount line in your routes.



92
93
94
# File 'lib/livechat/configuration.rb', line 92

def mount_path
  @mount_path
end

#on_agent_messageObject

Called with each saved Livechat::Message — wire up Slack, Noticed, push… Runs inline after save; keep it fast or hand off to a job.



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

def on_agent_message
  @on_agent_message
end

#on_visitor_messageObject

Called with each saved Livechat::Message — wire up Slack, Noticed, push… Runs inline after save; keep it fast or hand off to a job.



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

def on_visitor_message
  @on_visitor_message
end

#rate_limitObject

Per-IP throttle for the public endpoints, as keyword arguments for Rails' rate limiter (Rails 7.2+; ignored on 7.1). nil disables it.



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

def rate_limit
  @rate_limit
end

#reply_time_textObject

Widget copy. nil uses the localized defaults. reply_time_text is the honest line under the greeting — "We usually reply within a few hours."



41
42
43
# File 'lib/livechat/configuration.rb', line 41

def reply_time_text
  @reply_time_text
end

#show_launcherObject

The floating launcher bubble. Set false to open the widget only from your own elements carrying data-livechat-open.



50
51
52
# File 'lib/livechat/configuration.rb', line 50

def show_launcher
  @show_launcher
end

#visitor_labelObject

Turn a resolved user into the visitor name shown in the inbox.



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

def visitor_label
  @visitor_label
end

Instance Method Details

#agent_email_listObject



123
124
125
126
127
# File 'lib/livechat/configuration.rb', line 123

def agent_email_list
  list = agent_emails
  list = list.call if list.respond_to?(:call)
  Array(list).compact_blank
end

#emails_enabled?Boolean

Returns:

  • (Boolean)


129
# File 'lib/livechat/configuration.rb', line 129

def emails_enabled? = mailer_from.present?

#widget_endpointObject



121
# File 'lib/livechat/configuration.rb', line 121

def widget_endpoint = "#{mount_path.chomp('/')}/widget"