livechat 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/AGENTS.md +22 -4
- data/CHANGELOG.md +23 -0
- data/README.md +67 -1
- data/app/controllers/livechat/application_controller.rb +9 -0
- data/app/controllers/livechat/widgets_controller.rb +29 -0
- data/config/routes.rb +1 -0
- data/lib/livechat/configuration.rb +22 -2
- data/lib/livechat/version.rb +1 -1
- data/lib/livechat/widget.rb +42 -14
- data/lib/livechat/widget_helper.rb +1 -7
- data/lib/livechat.rb +43 -0
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d5968fc9db00037cc2e6330c09e1de36d1f7089bb9649972fd6371ae0349248a
|
|
4
|
+
data.tar.gz: 193702a94032278baa4d1c5162b8e5f03aa309803b760edb1036ddf1bfbe222f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a3a7a2d524aacbfe4287e89ba4fd3c4714a94df8e00731fd00d375ab1a0c658b36b5272860a9bc56d92401f15d4dff0804987f7de5d0e421825f939e0fc42661
|
|
7
|
+
data.tar.gz: 81a4123e0a17426d009c68f6f3f31199e46e9d2ed6a6b837eea2ffe4ed5d36361b9cc671989c8a4ffb6b1a6ededd7d246b92727ab656af25b6ede80779b6f284
|
data/AGENTS.md
CHANGED
|
@@ -119,9 +119,24 @@ For an account-erasure request, destroy that user's conversations with
|
|
|
119
119
|
email notifications and hook destinations separately. The content-type
|
|
120
120
|
allowlist is not malware scanning.
|
|
121
121
|
|
|
122
|
+
### Embedding on a static site
|
|
123
|
+
|
|
124
|
+
A marketing site that proxies the mount path through to Rails gets the widget from one tag:
|
|
125
|
+
|
|
126
|
+
```html
|
|
127
|
+
<script src="/livechat/embed.js" defer></script>
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Add `?locale=de` for a translated page. Two requirements, and both are load-bearing:
|
|
131
|
+
|
|
132
|
+
1. **Same origin.** The widget sends `credentials: "same-origin"`, so a cross-origin `<script src>` loads and then silently fails to carry the visitor cookie. Proxy `/livechat/*` from the static site to Rails; do not point the tag at another host.
|
|
133
|
+
2. **`config.allowed_embed_origins = ["https://example.com"]`.** A static page cannot mint a CSRF token, so a request from a listed origin is exempted. Safe because `Origin` is browser-set and unforgeable by page script, it covers only the visitor endpoints, and a request with no `Origin` is never treated as embedded.
|
|
134
|
+
|
|
135
|
+
Give that surface its own wording with a callable — `config.greeting = ->(request) { ... }` keyed on `request.origin`.
|
|
136
|
+
|
|
122
137
|
### Do not
|
|
123
138
|
|
|
124
|
-
- **Do not copy the widget JavaScript into `app/javascript`, or add a `<script>` tag for it.** `livechat_tag` renders what is needed, and the engine serves the code with a content fingerprint. There is no build step and nothing for esbuild/importmap/Tailwind to know about.
|
|
139
|
+
- **Do not copy the widget JavaScript into `app/javascript`, or add a `<script>` tag for it.** `livechat_tag` renders what is needed (on a page Rails does not render, use `embed.js` above — still not a copy), and the engine serves the code with a content fingerprint. There is no build step and nothing for esbuild/importmap/Tailwind to know about.
|
|
125
140
|
- **Do not rebuild the inbox, and do not edit views inside the gem.** To put it inside an admin you already have, set `config.base_controller_class = "Admin::BaseController"` — it inherits that controller's layout, helpers, authentication and request context. For the shell alone, `config.agent_layout = "admin/application"`. Both work with nothing else wired up: the inbox's own assets are declared by its views, not the layout.
|
|
126
141
|
- **Do not add an Action Cable mount to "make chat realtime"** unless you also set `config.action_cable = true`. Polling is the default and is not broken.
|
|
127
142
|
- **Do not set config outside the initializer.** `rate_limit` in particular is read when the controller class loads; assigning config per-request mutates it process-wide.
|
|
@@ -139,9 +154,10 @@ Everything is optional; a fresh install works with zero config. Full list with c
|
|
|
139
154
|
| `current_user` | `nil` | Receives the request; nil means guest-by-cookie |
|
|
140
155
|
| `visitor_label`, `agent_label` | name/email/to_s | Receive the user |
|
|
141
156
|
| `agent_display_name` | the label unchanged | Receives the label; return "Support team" to keep agents anonymous |
|
|
142
|
-
| `app_name`, `greeting`, `reply_time_text`, `launcher_label` | localized defaults | Widget copy |
|
|
143
|
-
| `avatar_url`, `accent_color` | `nil` | Header avatar (URL or callable) and brand hex |
|
|
144
|
-
| `show_launcher` | `true` | `false` = open only from your own elements |
|
|
157
|
+
| `app_name`, `greeting`, `reply_time_text`, `launcher_label` | localized defaults | Widget copy. All but `app_name` also take a callable receiving the request |
|
|
158
|
+
| `avatar_url`, `accent_color` | `nil` | Header avatar (URL or callable) and brand hex (also callable) |
|
|
159
|
+
| `show_launcher` | `true` | `false` = open only from your own elements. Also takes a callable |
|
|
160
|
+
| `allowed_embed_origins` | `[]` | Origins that may embed the widget on a page this app does not render |
|
|
145
161
|
| `mailer_from` | `nil` | **Required for any email at all** |
|
|
146
162
|
| `agent_emails` | `nil` | Array or callable |
|
|
147
163
|
| `attach_files` | `true` | Needs Active Storage; inert without it |
|
|
@@ -167,6 +183,8 @@ Turbo Drive and strict nonce-based CSP work out of the box. 26 locales ship with
|
|
|
167
183
|
| Messages only appear on refresh | Expected: polling is the default. `config.action_cable = true` (with `/cable` mounted) for push |
|
|
168
184
|
| No attachment button | Active Storage not installed, or `attach_files = false` |
|
|
169
185
|
| `undefined local variable current_user` in the initializer | A gate lambda treated its argument as a controller. It is a `request` |
|
|
186
|
+
| Widget loads on a static site but every message 422s | The embedding origin is not in `allowed_embed_origins`. A static page has no CSRF token |
|
|
187
|
+
| Widget on a static site never finds its thread | It is pointed at another origin. The widget sends `credentials: "same-origin"` — proxy the mount path instead of linking across hosts |
|
|
170
188
|
|
|
171
189
|
---
|
|
172
190
|
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.1.0 - 2026-09-25
|
|
4
|
+
|
|
5
|
+
- **The widget can now live on a site this app does not render.** A static
|
|
6
|
+
marketing site that proxies the mount path through to Rails loads the whole
|
|
7
|
+
widget from one tag — `<script src="/livechat/embed.js" defer>` — and never
|
|
8
|
+
reproduces the config block itself. `?locale=` picks the copy. The response is
|
|
9
|
+
never cached; the widget code it pulls in stays immutably cached as before.
|
|
10
|
+
- Added `config.allowed_embed_origins`. A page served from a listed origin
|
|
11
|
+
cannot carry a Rails CSRF token, so a matching request is exempted from
|
|
12
|
+
forgery protection. The exemption rests on the `Origin` header, which the
|
|
13
|
+
browser sets and page script cannot forge; it covers only the widget's own
|
|
14
|
+
visitor endpoints, never the inbox; and a request that sends no `Origin` at
|
|
15
|
+
all — curl, a server-side client — is never treated as embedded.
|
|
16
|
+
- Added `Livechat.public_paths`: the endpoints that answer with JavaScript, JSON
|
|
17
|
+
or a file and need no Rails session. Hosts that mint a CSP nonce (or anything
|
|
18
|
+
else) from the session per request can skip these, rather than handing a
|
|
19
|
+
cookie to every visitor who only ever loaded the page.
|
|
20
|
+
- `greeting`, `reply_time_text`, `launcher_label`, `show_launcher` and
|
|
21
|
+
`accent_color` each accept a callable receiving the request, the way
|
|
22
|
+
`avatar_url` already did. One app serving both an in-app widget and an
|
|
23
|
+
embedded one can greet each surface differently, and show the bubble on one
|
|
24
|
+
but not the other.
|
|
25
|
+
|
|
3
26
|
## 1.0.0 - 2026-08-11
|
|
4
27
|
|
|
5
28
|
- **The 1.x record names are settled.** `Livechat::Conversation` and
|
data/README.md
CHANGED
|
@@ -136,6 +136,7 @@ Everything is optional — a fresh install works with zero config. In
|
|
|
136
136
|
| `avatar_url` | `nil` | Customer-facing avatar in the widget header; URL or per-request callable |
|
|
137
137
|
| `accent_color` | `nil` | One hex restyles launcher, header, bubbles, send button |
|
|
138
138
|
| `show_launcher` | `true` | `false` hides the bubble — bring your own entry point |
|
|
139
|
+
| `allowed_embed_origins` | `[]` | Origins allowed to embed the widget on a page this app doesn't render |
|
|
139
140
|
| `visitor_label` | name, else email | How a visitor is labelled in the inbox |
|
|
140
141
|
| `agent_label` | name, else email | How an agent is labelled internally |
|
|
141
142
|
| `agent_display_name` | the full label | What **visitors** see — trim it or anonymise it |
|
|
@@ -167,7 +168,9 @@ Livechat.configure do |config|
|
|
|
167
168
|
end
|
|
168
169
|
```
|
|
169
170
|
|
|
170
|
-
`avatar_url`
|
|
171
|
+
`avatar_url`, `greeting`, `reply_time_text`, `launcher_label`, `show_launcher`
|
|
172
|
+
and `accent_color` each take a callable receiving the request, so one app can
|
|
173
|
+
brand or word the widget per tenant — or per surface:
|
|
171
174
|
|
|
172
175
|
```ruby
|
|
173
176
|
config.avatar_url = ->(request) { request.env["current_account"]&.support_avatar_url }
|
|
@@ -245,6 +248,66 @@ config.storage_service = :livechat_uploads
|
|
|
245
248
|
|
|
246
249
|
</details>
|
|
247
250
|
|
|
251
|
+
## On a site this app doesn't render
|
|
252
|
+
|
|
253
|
+
Marketing sites are usually static and live on their own host, which is exactly
|
|
254
|
+
where people ask presales questions. The widget can go there too, with one tag:
|
|
255
|
+
|
|
256
|
+
```html
|
|
257
|
+
<script src="/livechat/embed.js" defer></script>
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
`embed.js` carries the config block the layout helper would have rendered and
|
|
261
|
+
pulls in the widget, so the static page never reproduces engine markup that a
|
|
262
|
+
future release might change. Add `?locale=de` for a translated site.
|
|
263
|
+
|
|
264
|
+
Two things have to be true.
|
|
265
|
+
|
|
266
|
+
**The mount path must be same-origin with that site.** The widget sends
|
|
267
|
+
`credentials: "same-origin"`, so a cross-origin script tag would load and then
|
|
268
|
+
quietly fail to carry the visitor's cookie. Proxy `/livechat/*` from the static
|
|
269
|
+
site through to Rails — one rule on a CDN, one `location` block in nginx — and
|
|
270
|
+
the browser sees a single origin.
|
|
271
|
+
|
|
272
|
+
**The embedding origin must be allowlisted**, because a static page cannot mint
|
|
273
|
+
a CSRF token:
|
|
274
|
+
|
|
275
|
+
```ruby
|
|
276
|
+
config.allowed_embed_origins = ["https://example.com"]
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
`Livechat.public_paths` lists the endpoints that need no Rails session — the
|
|
280
|
+
widget script, the embed bootstrap, the visitor API and attachments. If your app
|
|
281
|
+
touches the session on every request (a CSP nonce generator usually does), skip
|
|
282
|
+
them there, or every visitor who merely loads a page leaves with a cookie:
|
|
283
|
+
|
|
284
|
+
```ruby
|
|
285
|
+
config.content_security_policy_nonce_generator = lambda do |request|
|
|
286
|
+
next if Livechat.public_paths.any? { |path| request.path.start_with?(path) }
|
|
287
|
+
request.session[:csp_nonce] ||= SecureRandom.hex(16)
|
|
288
|
+
end
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
A request from a listed origin skips forgery protection. What makes that safe is
|
|
292
|
+
that `Origin` is set by the browser and cannot be forged by page script, so
|
|
293
|
+
listing an origin grants nothing to any other site; the exemption covers only
|
|
294
|
+
the widget's visitor endpoints and never the inbox; and a request with no
|
|
295
|
+
`Origin` header at all is never treated as embedded, so a non-browser client
|
|
296
|
+
gains nothing.
|
|
297
|
+
|
|
298
|
+
Marketing visitors usually want a different greeting from signed-in users, and
|
|
299
|
+
the same app serves both:
|
|
300
|
+
|
|
301
|
+
```ruby
|
|
302
|
+
config.greeting = lambda do |request|
|
|
303
|
+
if request.origin == "https://example.com"
|
|
304
|
+
"Questions before you buy? Ask away."
|
|
305
|
+
else
|
|
306
|
+
"Hi! How can we help?"
|
|
307
|
+
end
|
|
308
|
+
end
|
|
309
|
+
```
|
|
310
|
+
|
|
248
311
|
## Privacy, retention, and deletion
|
|
249
312
|
|
|
250
313
|
Livechat stores conversation identity and context, message bodies and
|
|
@@ -340,6 +403,9 @@ stable under semantic versioning:
|
|
|
340
403
|
- `Livechat.configure`, `mount_livechat`, `livechat_tag`, `livechat_button`,
|
|
341
404
|
`window.Livechat.open/close`, `data-livechat-open`, and
|
|
342
405
|
`data-livechat-message`.
|
|
406
|
+
- The `embed.js` endpoint under the mount path, including its `locale`
|
|
407
|
+
parameter. The markup it writes is private — embed the endpoint, not a copy
|
|
408
|
+
of its output.
|
|
343
409
|
- `config.on_visitor_message` and `config.on_agent_message`, each called with
|
|
344
410
|
the saved `Livechat::Message` documented by the initializer.
|
|
345
411
|
|
|
@@ -13,5 +13,14 @@ module Livechat
|
|
|
13
13
|
include RequestContext
|
|
14
14
|
|
|
15
15
|
protect_from_forgery with: :exception
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
# A page on an allowlisted embed origin is static and has no Rails session
|
|
20
|
+
# to mint a token from, so the Origin header stands in for one. Only this
|
|
21
|
+
# controller's descendants are affected — the inbox verifies as usual.
|
|
22
|
+
def verified_request?
|
|
23
|
+
Livechat.embedded?(request) || super
|
|
24
|
+
end
|
|
16
25
|
end
|
|
17
26
|
end
|
|
@@ -20,6 +20,27 @@ module Livechat
|
|
|
20
20
|
serve(Widget.javascript, Widget.fingerprint)
|
|
21
21
|
end
|
|
22
22
|
|
|
23
|
+
# Bootstraps the widget on a page this app does not render. Carries the
|
|
24
|
+
# config the layout helper would have rendered, so an external page needs
|
|
25
|
+
# one <script src> and nothing that has to keep up with this engine.
|
|
26
|
+
#
|
|
27
|
+
# Never cached: it reflects the current visitor and whatever copy the host
|
|
28
|
+
# resolves per request. The heavy widget.js it pulls in stays immutable.
|
|
29
|
+
def embed
|
|
30
|
+
body = if Livechat.enabled?(request)
|
|
31
|
+
I18n.with_locale(embed_locale) do
|
|
32
|
+
Widget.embed_javascript(locale: I18n.locale,
|
|
33
|
+
authenticated: current_visitor.present?,
|
|
34
|
+
settings: Livechat.settings_for(request))
|
|
35
|
+
end
|
|
36
|
+
else
|
|
37
|
+
''
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
response.headers['Cache-Control'] = 'no-store'
|
|
41
|
+
render plain: body, content_type: 'text/javascript'
|
|
42
|
+
end
|
|
43
|
+
|
|
23
44
|
def dashboard
|
|
24
45
|
serve(Widget.dashboard_javascript, Widget.dashboard_fingerprint, 'text/javascript')
|
|
25
46
|
end
|
|
@@ -30,6 +51,14 @@ module Livechat
|
|
|
30
51
|
|
|
31
52
|
private
|
|
32
53
|
|
|
54
|
+
# An embedding site carries its own language; it asks for the matching
|
|
55
|
+
# copy with ?locale=. Anything the host does not actually have falls back
|
|
56
|
+
# rather than raising.
|
|
57
|
+
def embed_locale
|
|
58
|
+
requested = params[:locale].to_s
|
|
59
|
+
I18n.available_locales.map(&:to_s).include?(requested) ? requested : I18n.locale
|
|
60
|
+
end
|
|
61
|
+
|
|
33
62
|
def serve(source, fingerprint, content_type = 'text/javascript')
|
|
34
63
|
expires_in 1.year, public: true if params[:v] == fingerprint
|
|
35
64
|
return unless stale?(etag: [Livechat::VERSION, fingerprint])
|
data/config/routes.rb
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Livechat::Engine.routes.draw do
|
|
4
4
|
get 'widget.js', to: 'widgets#show', as: :widget
|
|
5
|
+
get 'embed.js', to: 'widgets#embed', as: :embed
|
|
5
6
|
get 'dashboard.js', to: 'widgets#dashboard', as: :dashboard_script
|
|
6
7
|
get 'dashboard.css', to: 'widgets#dashboard_stylesheet', as: :dashboard_stylesheet
|
|
7
8
|
|
|
@@ -61,6 +61,9 @@ module Livechat
|
|
|
61
61
|
|
|
62
62
|
# Widget copy. nil uses the localized defaults. reply_time_text is the
|
|
63
63
|
# honest line under the greeting — "We usually reply within a few hours."
|
|
64
|
+
# A String, or a callable receiving the request: one app serving both an
|
|
65
|
+
# in-app widget and an embedded one on a marketing site needs to greet
|
|
66
|
+
# those visitors differently.
|
|
64
67
|
attr_accessor :greeting, :reply_time_text, :launcher_label
|
|
65
68
|
|
|
66
69
|
# Optional customer-facing avatar shown in the widget header. Use a URL
|
|
@@ -70,13 +73,29 @@ module Livechat
|
|
|
70
73
|
|
|
71
74
|
# Brand color for the widget (launcher, header, visitor bubbles, send
|
|
72
75
|
# button) as a hex value, e.g. "#7c3aed". The widget picks black or white
|
|
73
|
-
# text automatically for contrast. nil keeps the built-in blue.
|
|
76
|
+
# text automatically for contrast. nil keeps the built-in blue. A callable
|
|
77
|
+
# receiving the request works too.
|
|
74
78
|
attr_accessor :accent_color
|
|
75
79
|
|
|
76
80
|
# The floating launcher bubble. Set false to open the widget only from
|
|
77
|
-
# your own elements carrying `data-livechat-open`.
|
|
81
|
+
# your own elements carrying `data-livechat-open`. Also accepts a callable
|
|
82
|
+
# receiving the request, for a host that wants the bubble on one surface
|
|
83
|
+
# and its own opener on another.
|
|
78
84
|
attr_accessor :show_launcher
|
|
79
85
|
|
|
86
|
+
# Origins allowed to embed the widget on pages this app does not render —
|
|
87
|
+
# a static marketing site that proxies `mount_path` through to this app,
|
|
88
|
+
# say. An Array of origins ("https://example.com"), or a callable
|
|
89
|
+
# receiving the request and returning one.
|
|
90
|
+
#
|
|
91
|
+
# Such a page cannot carry a Rails CSRF token, so a matching request is
|
|
92
|
+
# exempted from forgery protection. That exemption rests on the Origin
|
|
93
|
+
# header, which the browser sets and script cannot forge, and applies only
|
|
94
|
+
# to the widget's own visitor endpoints — never to the inbox. A request
|
|
95
|
+
# arriving without an Origin at all (curl, a server-side client) is never
|
|
96
|
+
# embedded and keeps the full check.
|
|
97
|
+
attr_accessor :allowed_embed_origins
|
|
98
|
+
|
|
80
99
|
# Email addresses to notify when a visitor writes and nobody has read it —
|
|
81
100
|
# an array, or a callable returning one. nil disables the built-in email.
|
|
82
101
|
attr_accessor :agent_emails
|
|
@@ -140,6 +159,7 @@ module Livechat
|
|
|
140
159
|
@avatar_url = nil
|
|
141
160
|
@accent_color = nil
|
|
142
161
|
@show_launcher = true
|
|
162
|
+
@allowed_embed_origins = []
|
|
143
163
|
@agent_emails = nil
|
|
144
164
|
@mailer_from = nil
|
|
145
165
|
@on_visitor_message = ->(_message) {}
|
data/lib/livechat/version.rb
CHANGED
data/lib/livechat/widget.rb
CHANGED
|
@@ -58,29 +58,54 @@ module Livechat
|
|
|
58
58
|
# nonce gets refused; a same-origin src is covered by `'self'` on every
|
|
59
59
|
# visit. `nonce:` is still stamped for hosts whose script-src has no
|
|
60
60
|
# 'self'; pass nil when the app has no nonce.
|
|
61
|
-
def snippet(locale:, authenticated:,
|
|
62
|
-
json = config_json(locale:, authenticated:,
|
|
61
|
+
def snippet(locale:, authenticated:, settings: nil, nonce: nil)
|
|
62
|
+
json = config_json(locale:, authenticated:, settings:)
|
|
63
63
|
nonce_attr = nonce ? %( nonce="#{nonce}") : ''
|
|
64
|
-
src = "#{Livechat.config.mount_path.chomp('/')}/widget.js?v=#{fingerprint}"
|
|
65
64
|
|
|
66
65
|
%(<script type="application/json" data-livechat-config>#{json}</script>) +
|
|
67
|
-
%(<script src="#{
|
|
66
|
+
%(<script src="#{script_src}" defer#{nonce_attr} data-livechat-widget></script>)
|
|
68
67
|
end
|
|
69
68
|
|
|
70
|
-
|
|
69
|
+
# The same two tags, assembled by the browser instead of by a Rails
|
|
70
|
+
# template. A page this app does not render — a static marketing site
|
|
71
|
+
# proxying the mount path through to Rails — gets the widget from one
|
|
72
|
+
# plain <script src>, and never reproduces the config block itself.
|
|
73
|
+
def embed_javascript(locale:, authenticated:, settings: nil)
|
|
74
|
+
<<~JS
|
|
75
|
+
/* livechat embed */
|
|
76
|
+
(function () {
|
|
77
|
+
if (window.__livechatEmbedded) return;
|
|
78
|
+
window.__livechatEmbedded = true;
|
|
79
|
+
|
|
80
|
+
var config = document.createElement("script");
|
|
81
|
+
config.type = "application/json";
|
|
82
|
+
config.setAttribute("data-livechat-config", "");
|
|
83
|
+
config.textContent = JSON.stringify(#{config_json(locale:, authenticated:, settings:)});
|
|
84
|
+
document.head.appendChild(config);
|
|
85
|
+
|
|
86
|
+
var widget = document.createElement("script");
|
|
87
|
+
widget.src = #{script_src.to_json};
|
|
88
|
+
widget.setAttribute("data-livechat-widget", "");
|
|
89
|
+
document.head.appendChild(widget);
|
|
90
|
+
})();
|
|
91
|
+
JS
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def config_json(locale:, authenticated:, settings: nil)
|
|
71
95
|
config = Livechat.config
|
|
96
|
+
settings ||= Livechat.settings_for(nil)
|
|
72
97
|
payload = {
|
|
73
98
|
endpoint: config.widget_endpoint,
|
|
74
99
|
locale: locale.to_s,
|
|
75
100
|
rtl: rtl?(locale),
|
|
76
101
|
authenticated: authenticated ? true : false,
|
|
77
|
-
launcher:
|
|
102
|
+
launcher: settings[:show_launcher] ? true : false,
|
|
78
103
|
appName: Livechat.app_name,
|
|
79
|
-
avatarUrl: avatar_url,
|
|
80
|
-
accentColor:
|
|
104
|
+
avatarUrl: settings[:avatar_url],
|
|
105
|
+
accentColor: settings[:accent_color],
|
|
81
106
|
attachments: Livechat.attachments_enabled?,
|
|
82
107
|
maxAttachments: config.max_attachments,
|
|
83
|
-
labels: labels
|
|
108
|
+
labels: labels(settings)
|
|
84
109
|
}
|
|
85
110
|
# Escape "</" so a value can't close the <script> block early.
|
|
86
111
|
payload.to_json.gsub('</', '<\/')
|
|
@@ -88,16 +113,19 @@ module Livechat
|
|
|
88
113
|
|
|
89
114
|
private
|
|
90
115
|
|
|
116
|
+
def script_src
|
|
117
|
+
"#{Livechat.config.mount_path.chomp('/')}/widget.js?v=#{fingerprint}"
|
|
118
|
+
end
|
|
119
|
+
|
|
91
120
|
# Every user-facing string in the widget, resolved through Rails I18n so
|
|
92
121
|
# the chat follows the app's current locale. Each lookup carries an
|
|
93
122
|
# English default, so the widget stays fully worded even when a key is
|
|
94
123
|
# missing for the active locale.
|
|
95
|
-
def labels
|
|
96
|
-
config = Livechat.config
|
|
124
|
+
def labels(settings)
|
|
97
125
|
{
|
|
98
|
-
launcher:
|
|
99
|
-
greeting:
|
|
100
|
-
replyTime:
|
|
126
|
+
launcher: settings[:launcher_label].presence || t(:launcher, 'Chat with us'),
|
|
127
|
+
greeting: settings[:greeting].presence || t(:greeting, 'Hi! How can we help?'),
|
|
128
|
+
replyTime: settings[:reply_time_text].presence ||
|
|
101
129
|
t(:reply_time, 'We usually reply within a few hours.'),
|
|
102
130
|
placeholder: t(:placeholder, 'Write a message…'),
|
|
103
131
|
send: t(:send, 'Send'),
|
|
@@ -12,7 +12,7 @@ module Livechat
|
|
|
12
12
|
Widget.snippet(
|
|
13
13
|
locale: I18n.locale,
|
|
14
14
|
authenticated: livechat_visitor.present?,
|
|
15
|
-
|
|
15
|
+
settings: Livechat.settings_for(request),
|
|
16
16
|
nonce: (content_security_policy_nonce if respond_to?(:content_security_policy_nonce))
|
|
17
17
|
).html_safe
|
|
18
18
|
end
|
|
@@ -33,12 +33,6 @@ module Livechat
|
|
|
33
33
|
|
|
34
34
|
private
|
|
35
35
|
|
|
36
|
-
def livechat_avatar_url
|
|
37
|
-
value = Livechat.config.avatar_url
|
|
38
|
-
value = value.call(request) if value.respond_to?(:call)
|
|
39
|
-
value.to_s.presence
|
|
40
|
-
end
|
|
41
|
-
|
|
42
36
|
def livechat_visitor
|
|
43
37
|
return @livechat_visitor if defined?(@livechat_visitor)
|
|
44
38
|
|
data/lib/livechat.rb
CHANGED
|
@@ -30,6 +30,49 @@ module Livechat
|
|
|
30
30
|
!!config.enabled.call(request)
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
+
# The engine's public surface: the widget script, the embed bootstrap, the
|
|
34
|
+
# visitor API and the attachment route. All of them answer with JavaScript,
|
|
35
|
+
# JSON or a file, and none of them need a Rails session.
|
|
36
|
+
#
|
|
37
|
+
# Exposed because a host usually has session, CSP or caching rules written
|
|
38
|
+
# for its own HTML pages, and applying those here costs every visitor a
|
|
39
|
+
# cookie for a request that never had a use for one.
|
|
40
|
+
def public_paths
|
|
41
|
+
mount = config.mount_path.chomp('/')
|
|
42
|
+
["#{mount}/widget.js", "#{mount}/embed.js", "#{mount}/widget/", "#{mount}/attachments/"]
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Resolve a setting that may be a plain value or a per-request callable.
|
|
46
|
+
def resolve(value, request)
|
|
47
|
+
value.respond_to?(:call) ? value.call(request) : value
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# The per-request widget settings a host may vary by surface. Resolved by
|
|
51
|
+
# the helper and by the embed script, so both routes see the same values.
|
|
52
|
+
def settings_for(request)
|
|
53
|
+
{
|
|
54
|
+
greeting: resolve(config.greeting, request),
|
|
55
|
+
reply_time_text: resolve(config.reply_time_text, request),
|
|
56
|
+
launcher_label: resolve(config.launcher_label, request),
|
|
57
|
+
show_launcher: resolve(config.show_launcher, request),
|
|
58
|
+
accent_color: resolve(config.accent_color, request),
|
|
59
|
+
avatar_url: resolve(config.avatar_url, request).to_s.presence
|
|
60
|
+
}
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Is this request coming from a page on an allowlisted external origin?
|
|
64
|
+
#
|
|
65
|
+
# Only ever true for a request the browser stamped with a matching Origin.
|
|
66
|
+
# A missing Origin is not a match: a non-browser client must not inherit a
|
|
67
|
+
# browser-guaranteed exemption.
|
|
68
|
+
def embedded?(request)
|
|
69
|
+
origin = request.origin.presence
|
|
70
|
+
return false unless origin
|
|
71
|
+
|
|
72
|
+
allowed = resolve(config.allowed_embed_origins, request)
|
|
73
|
+
Array(allowed).any? { |candidate| candidate.to_s.chomp('/') == origin.chomp('/') }
|
|
74
|
+
end
|
|
75
|
+
|
|
33
76
|
# Can this request work the inbox? Checked by every inbox action.
|
|
34
77
|
# The class Livechat::DashboardController inherits from. Resolved on every
|
|
35
78
|
# call rather than memoized, so a host that reassigns base_controller_class
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: livechat
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Yaroslav Shmarov
|
|
@@ -138,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
138
138
|
- !ruby/object:Gem::Version
|
|
139
139
|
version: '0'
|
|
140
140
|
requirements: []
|
|
141
|
-
rubygems_version: 4.0.
|
|
141
|
+
rubygems_version: 4.0.20
|
|
142
142
|
specification_version: 4
|
|
143
143
|
summary: 'Open-source live chat for Rails: a drop-in support widget plus a team inbox,
|
|
144
144
|
in your own database.'
|