livechat 1.3.0 → 1.4.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 +8 -0
- data/CHANGELOG.md +41 -0
- data/README.md +71 -2
- data/SECURITY.md +4 -1
- data/app/controllers/livechat/chat_controller.rb +41 -0
- data/app/mailers/livechat/mailer.rb +54 -0
- data/app/views/livechat/chat/show.html.erb +30 -0
- data/app/views/livechat/mailer/new_agent_reply.text.erb +2 -2
- data/config/locales/livechat.ar.yml +1 -0
- data/config/locales/livechat.bg.yml +1 -0
- data/config/locales/livechat.bn.yml +1 -0
- data/config/locales/livechat.de.yml +1 -0
- data/config/locales/livechat.el.yml +1 -0
- data/config/locales/livechat.en.yml +1 -0
- data/config/locales/livechat.es.yml +1 -0
- data/config/locales/livechat.fr.yml +1 -0
- data/config/locales/livechat.hi.yml +1 -0
- data/config/locales/livechat.hr.yml +1 -0
- data/config/locales/livechat.id.yml +1 -0
- data/config/locales/livechat.it.yml +1 -0
- data/config/locales/livechat.ja.yml +1 -0
- data/config/locales/livechat.ko.yml +1 -0
- data/config/locales/livechat.lb.yml +1 -0
- data/config/locales/livechat.nl.yml +1 -0
- data/config/locales/livechat.pl.yml +1 -0
- data/config/locales/livechat.pt.yml +1 -0
- data/config/locales/livechat.ro.yml +1 -0
- data/config/locales/livechat.ru.yml +1 -0
- data/config/locales/livechat.th.yml +1 -0
- data/config/locales/livechat.tr.yml +1 -0
- data/config/locales/livechat.uk.yml +1 -0
- data/config/locales/livechat.ur.yml +1 -0
- data/config/locales/livechat.vi.yml +1 -0
- data/config/locales/livechat.zh-CN.yml +1 -0
- data/config/routes.rb +4 -0
- data/lib/generators/livechat/foreign_key/foreign_key_generator.rb +36 -0
- data/lib/generators/livechat/foreign_key/templates/add_livechat_messages_conversation_foreign_key.rb.tt +16 -0
- data/lib/generators/livechat/install/templates/create_livechat_tables.rb.tt +2 -1
- data/lib/generators/livechat/install/templates/initializer.rb +5 -0
- data/lib/generators/livechat/migration_helpers.rb +12 -0
- data/lib/livechat/configuration.rb +14 -0
- data/lib/livechat/version.rb +1 -1
- data/lib/livechat/widget.js +135 -51
- data/lib/livechat/widget.rb +6 -4
- data/lib/livechat/widget_helper.rb +12 -0
- data/lib/livechat.rb +18 -0
- metadata +5 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 30216b783d33b09f017da6e0c611acea286f2a030dd141a8290990c22d603d34
|
|
4
|
+
data.tar.gz: 9179d65b0c2dcb0163129d1997a29ea53fb3591962f89622bd1aa09ed3203e09
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 314d2125fe8b08a5f0efe477dcbac8466d71fd5b41d967cae27c6011d50aa583e42efe62c7144c97a66a923c1a83fdb22401448742ebc93f78188c29711bbec0
|
|
7
|
+
data.tar.gz: 7c151123a05f1105800f5b446b1271ca0e7dd560e475578a363d7ded0875e360e54075c1cdafc0aa86ac84a38bcd6c3b556eb523ec068382b035ecfcfa0d4c6b
|
data/AGENTS.md
CHANGED
|
@@ -77,9 +77,12 @@ Then in the running app: load any page, confirm the bubble appears bottom-right,
|
|
|
77
77
|
| The launcher bubble | On by default. `config.show_launcher = false` to remove it |
|
|
78
78
|
| Your own element | `<%= livechat_button %>`, or any element with `data-livechat-open` |
|
|
79
79
|
| JavaScript | `window.Livechat.open()` |
|
|
80
|
+
| A page of its own | `/livechat/chat` — link it with `Livechat.chat_path(locale: I18n.locale)` |
|
|
80
81
|
|
|
81
82
|
A visitor has **one conversation**, not a queue of tickets — writing again reopens the same thread. Signed-in visitors keep it across devices (keyed by user id); guests are tracked by cookie.
|
|
82
83
|
|
|
84
|
+
**The chat as a page.** `/livechat/chat` shows the visitor's conversation filling the window; the panel's icon and the reply email link to it. It names no conversation — it resolves the visitor exactly like the widget — so do not add an id or token to that URL. If the user wants it inside their own layout, render `<%= livechat_chat style: "height: 70vh" %>` in one of their pages (it is only the element; the layout's `livechat_tag` fills it) and set `config.chat_path = "/that/page"`. `config.chat_path = false` removes the page and the icon.
|
|
85
|
+
|
|
83
86
|
### Email notifications need two settings, not one
|
|
84
87
|
|
|
85
88
|
```ruby
|
|
@@ -166,6 +169,7 @@ Everything is optional; a fresh install works with zero config. Full list with c
|
|
|
166
169
|
| `allowed_attachment_types` | `nil` | Content-type allowlist |
|
|
167
170
|
| `action_cable`, `action_cable_url` | `false`, `"/cable"` | Opt-in push |
|
|
168
171
|
| `rate_limit` | `{ to: 30, within: 60 }` | Rails 7.2+; ignored on 7.1. `nil` disables |
|
|
172
|
+
| `chat_path` | `<mount_path>/chat` | The chat as a page; a path of the host's own page rendering `livechat_chat`, or `false` |
|
|
169
173
|
| `mount_path` | `"/livechat"` | Keep in sync with `mount_livechat at:` |
|
|
170
174
|
| `on_visitor_message`, `on_agent_message` | no-ops | Run inline after save — Slack, Noticed, push |
|
|
171
175
|
|
|
@@ -177,11 +181,15 @@ Turbo Drive and strict nonce-based CSP work out of the box. 26 locales ship with
|
|
|
177
181
|
| --- | --- |
|
|
178
182
|
| `NameError` for one of your own helpers in the inbox | `isolate_namespace` scopes `helper` to the engine. Use `config.base_controller_class` so the inbox inherits your helpers, rather than `agent_layout` alone. |
|
|
179
183
|
| `NotNullViolation` attaching a file on a uuid-keyed app | The tables were generated bigint. Set `primary_key_type` in `config.generators` before installing, or migrate them to uuid. |
|
|
184
|
+
| `livechat:foreign_key` migration fails on an existing key | It is an upgrade generator for installs made before the key existed; a fresh install already has it |
|
|
185
|
+
| `livechat:foreign_key` migration fails validating the key | Messages point at a deleted conversation. Find them with `Livechat::Message.where.not(conversation_id: Livechat::Conversation.select(:id))` |
|
|
180
186
|
| `/livechat` returns 403 "Set Livechat.config.authorize_agent to grant access" | Exactly what it says: still at the development-only default |
|
|
181
187
|
| No bubble on the page | `livechat_tag` missing from the rendered layout, `config.enabled` false, or `show_launcher = false` with no opener of your own |
|
|
182
188
|
| No notification emails | `mailer_from` not set — `agent_emails` alone does nothing |
|
|
183
189
|
| Messages only appear on refresh | Expected: polling is the default. `config.action_cable = true` (with `/cable` mounted) for push |
|
|
184
190
|
| No attachment button | Active Storage not installed, or `attach_files = false` |
|
|
191
|
+
| `livechat_chat` renders an empty box | `livechat_tag` is not in the layout — the helper is only the element the widget fills |
|
|
192
|
+
| The chat in a page is stuck at 420px tall | Its element has no height, so the minimum applies. Give it one (`style: "height: 70vh"`) or a container that has one |
|
|
185
193
|
| `undefined local variable current_user` in the initializer | A gate lambda treated its argument as a controller. It is a `request` |
|
|
186
194
|
| 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
195
|
| 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 |
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,46 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.4.0 - 2026-11-21
|
|
4
|
+
|
|
5
|
+
- Add the chat as a page. `<mount_path>/chat` (default `/livechat/chat`) shows
|
|
6
|
+
the visitor their own conversation filling the window, so a visitor has a
|
|
7
|
+
URL for their chat: link it from your nav with `Livechat.chat_path`. The URL
|
|
8
|
+
names no conversation. It is resolved from the signed-in id or guest cookie,
|
|
9
|
+
like the widget, so a forwarded link shows a stranger only their own empty
|
|
10
|
+
chat. Guests keep it only in the browser that holds their cookie.
|
|
11
|
+
- Add `livechat_chat`, the same chat filling an element of your own page and
|
|
12
|
+
layout, and `config.chat_path` to point everything at that page (the
|
|
13
|
+
engine's page then redirects there) or `false` to turn the page off.
|
|
14
|
+
- The widget panel gets an "open as a page" icon. For guests, the reply
|
|
15
|
+
email's "Continue the conversation" link opens the chat page on the origin
|
|
16
|
+
they last wrote from, instead of that page itself. Signed-in visitors keep
|
|
17
|
+
the link to their last page, which can send them through the host's sign-in;
|
|
18
|
+
the engine's page cannot, and would show a signed-out reader an empty chat.
|
|
19
|
+
A host `chat_path` is linked for everyone. Messages written on the chat page
|
|
20
|
+
do not overwrite the "where they got stuck" page URL.
|
|
21
|
+
- `Livechat.chat_path(locale:)` adds the language to a nav link: the engine's
|
|
22
|
+
page does not run the host's locale switching.
|
|
23
|
+
- Costs: one new public, session-bearing route under the mount path, so a host
|
|
24
|
+
rule matching everything under it (an embed proxy, a CSP or cache rule) now
|
|
25
|
+
covers an HTML page too. One new locale key, `open_page`, in all 26
|
|
26
|
+
locales. The chat page needs no migration. The widget now removes a stale
|
|
27
|
+
copy of its root that a restored Turbo snapshot carries, before mounting a
|
|
28
|
+
fresh one.
|
|
29
|
+
- Messages get a foreign key to their conversation. Fresh installs create
|
|
30
|
+
it; existing installs add it with
|
|
31
|
+
`bin/rails generate livechat:foreign_key && bin/rails db:migrate`. Every
|
|
32
|
+
deletion in the engine already went through `destroy`, so nothing changes in
|
|
33
|
+
normal use, but a bulk `delete_all` on conversations used to leave their
|
|
34
|
+
messages behind and now raises instead. On PostgreSQL the generated
|
|
35
|
+
migration adds the key unvalidated and validates it in a second statement,
|
|
36
|
+
so it does not block writes and strong_migrations accepts it.
|
|
37
|
+
- Costs of the key: a host that deletes conversations with `delete_all` must
|
|
38
|
+
delete their messages first or use `destroy`. The migration fails if
|
|
39
|
+
messages already point at a deleted conversation; the generator prints the
|
|
40
|
+
query that finds them. Elsewhere than PostgreSQL it is a plain
|
|
41
|
+
`add_foreign_key`, which on MySQL can rebuild and lock `livechat_messages`
|
|
42
|
+
while it runs: run it at a quiet moment on a large table.
|
|
43
|
+
|
|
3
44
|
## 1.3.0 - 2026-11-14
|
|
4
45
|
|
|
5
46
|
- Add `data-livechat-badge-class` to `data-livechat-open` elements. The unread
|
data/README.md
CHANGED
|
@@ -62,6 +62,7 @@ the things it should not do. It ships inside the gem, so
|
|
|
62
62
|
| | |
|
|
63
63
|
| ------------- | ------------------------------------------------------------------------ |
|
|
64
64
|
| **Widget** | Chat bubble, expandable panel, drafts preserved, unread badges |
|
|
65
|
+
| **Chat page** | The same chat as a page with a URL — for your nav and the reply email |
|
|
65
66
|
| **Inbox** | Open / resolved tabs, search across everything, who-worked-what column |
|
|
66
67
|
| **Team** | Any teammate answers any thread; every reply signed with its author |
|
|
67
68
|
| **Email** | Both directions — one per unread stretch, not one per message |
|
|
@@ -157,6 +158,7 @@ Everything is optional — a fresh install works with zero config. In
|
|
|
157
158
|
| `action_cable` | `false` | Opt into push delivery |
|
|
158
159
|
| `action_cable_url` | `"/cable"` | Match your `mount ActionCable...` |
|
|
159
160
|
| `rate_limit` | `{ to: 30, within: 1.minute }` | Per-IP throttle (Rails 7.2+). `nil` disables |
|
|
161
|
+
| `chat_path` | `<mount_path>/chat` | The chat as a page. A path of your own, or `false` for none — see [The chat as a page](#the-chat-as-a-page) |
|
|
160
162
|
| `mount_path` | `"/livechat"` | Keep in sync with `mount` in `routes.rb` |
|
|
161
163
|
|
|
162
164
|
A typical initializer:
|
|
@@ -376,6 +378,7 @@ to a stream the server signed for it. Under a strict CSP, allow the socket with
|
|
|
376
378
|
| `<%= livechat_button %>` | A plain, unstyled opener button |
|
|
377
379
|
| `<%= livechat_button("Ask about this order", message: "I need help with order ##{@order.id}") %>` | Open and prefill in one helper |
|
|
378
380
|
| `config.show_launcher = false` | Hide the bubble entirely |
|
|
381
|
+
| `<%= livechat_chat %>` | The chat filling an element of your page — see below |
|
|
379
382
|
|
|
380
383
|
While replies are unread, every `data-livechat-open` element carries a small
|
|
381
384
|
count badge — so hiding the launcher never hides the answer.
|
|
@@ -404,6 +407,56 @@ Contextual buttons are where a Rails app beats a generic support widget:
|
|
|
404
407
|
</button>
|
|
405
408
|
```
|
|
406
409
|
|
|
410
|
+
## The chat as a page
|
|
411
|
+
|
|
412
|
+
The bubble is for asking from where you are. Some visitors want the chat
|
|
413
|
+
somewhere they can go back to — a Support link in your nav, a tab of its own,
|
|
414
|
+
the link in the reply email. That is `/livechat/chat`: the visitor's
|
|
415
|
+
conversation filling the window, the same thread the bubble opens.
|
|
416
|
+
|
|
417
|
+
```erb
|
|
418
|
+
<%= link_to "Support chat", Livechat.chat_path(locale: I18n.locale) %>
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
The engine's page doesn't run your app's locale switching, hence `locale:`.
|
|
422
|
+
The panel has an icon that opens it, passing the language along by itself.
|
|
423
|
+
|
|
424
|
+
**The URL is the same for everyone.** Like the widget, the page finds the
|
|
425
|
+
visitor's conversation from their signed-in id or guest cookie, never from
|
|
426
|
+
the URL, so a forwarded link shows a stranger their own empty chat and
|
|
427
|
+
nothing else. It also means the page shows a thread only to a browser that
|
|
428
|
+
already carries the visitor's identity: a signed-in session, or the guest
|
|
429
|
+
cookie of the browser they wrote from. The engine's page can't send anyone to
|
|
430
|
+
your sign-in, so a signed-out user who opens it sees an empty chat.
|
|
431
|
+
|
|
432
|
+
That is why the reply email's "Continue the conversation" link goes to the
|
|
433
|
+
chat page for guests, but to the last page they wrote from for signed-in
|
|
434
|
+
visitors — your page asks them to sign in, and the thread is there after. With
|
|
435
|
+
`chat_path` pointing at a page of your own (which can require sign-in), the
|
|
436
|
+
email links that page for everyone.
|
|
437
|
+
|
|
438
|
+
The engine's page is deliberately bare: your app name, the chat, nothing of
|
|
439
|
+
your layout. To have the chat inside your own layout and navigation, render
|
|
440
|
+
it in a page of yours and point `chat_path` there — the panel's icon and the
|
|
441
|
+
email follow, and `/livechat/chat` redirects to it:
|
|
442
|
+
|
|
443
|
+
```erb
|
|
444
|
+
<%# app/views/support/show.html.erb %>
|
|
445
|
+
<%= livechat_chat style: "height: 70vh" %>
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
```ruby
|
|
449
|
+
config.chat_path = "/support" # or false: no chat page, no icon
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
`livechat_chat` is only the element; the `livechat_tag` already in your layout
|
|
453
|
+
fills it. Give it a height, or put it in something that has one. While it is on
|
|
454
|
+
the page the bubble stays away, and every `data-livechat-open` element there
|
|
455
|
+
moves focus into it.
|
|
456
|
+
|
|
457
|
+
Messages written on the chat page don't count as "the page they were on" for
|
|
458
|
+
your team — the inbox keeps showing the last page where they used the bubble.
|
|
459
|
+
|
|
407
460
|
## Compatibility and public API
|
|
408
461
|
|
|
409
462
|
The following are the model and integration contracts that 1.x will keep
|
|
@@ -416,8 +469,10 @@ stable under semantic versioning:
|
|
|
416
469
|
its persisted fields, optional `files`, documented scopes, author/read
|
|
417
470
|
predicates, `public_label`, `attached_files`, and `preview`.
|
|
418
471
|
- `Livechat.configure`, `mount_livechat`, `livechat_tag`, `livechat_button`,
|
|
419
|
-
`window.Livechat.open/close`,
|
|
420
|
-
`data-livechat-message`.
|
|
472
|
+
`livechat_chat`, `Livechat.chat_path`, `window.Livechat.open/close`,
|
|
473
|
+
`data-livechat-open`, and `data-livechat-message`.
|
|
474
|
+
- The chat page under the mount path (`chat`), including its `locale`
|
|
475
|
+
parameter.
|
|
421
476
|
- The `embed.js` endpoint under the mount path, including its `locale`
|
|
422
477
|
parameter. The markup it writes is private — embed the endpoint, not a copy
|
|
423
478
|
of its output.
|
|
@@ -430,6 +485,20 @@ HTML, and widget implementation objects are private. Incompatible changes to
|
|
|
430
485
|
the public list above wait for a new major version; a deprecation normally
|
|
431
486
|
ships first.
|
|
432
487
|
|
|
488
|
+
### Upgrading to 1.4
|
|
489
|
+
|
|
490
|
+
The chat page needs nothing. One optional migration gives
|
|
491
|
+
`livechat_messages` the foreign key fresh installs now create, so a bulk
|
|
492
|
+
`delete_all` on conversations can no longer leave their messages behind:
|
|
493
|
+
|
|
494
|
+
```bash
|
|
495
|
+
bin/rails generate livechat:foreign_key && bin/rails db:migrate
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
On PostgreSQL it goes in without blocking writes. It fails if messages
|
|
499
|
+
already point at a deleted conversation; the generator prints the query that
|
|
500
|
+
finds them.
|
|
501
|
+
|
|
433
502
|
### Upgrading from 0.x
|
|
434
503
|
|
|
435
504
|
There is no migration, initializer change, constant alias, or model rename.
|
data/SECURITY.md
CHANGED
|
@@ -23,7 +23,10 @@ users have had a reasonable opportunity to upgrade.
|
|
|
23
23
|
The widget and visitor endpoints are controlled by `config.enabled`; the agent
|
|
24
24
|
inbox is separately protected by `authorize_agent` and fails closed outside
|
|
25
25
|
development until the host grants access. A visitor can read only the
|
|
26
|
-
conversation resolved from their signed-in id or opaque guest cookie.
|
|
26
|
+
conversation resolved from their signed-in id or opaque guest cookie. The chat
|
|
27
|
+
page (`<mount_path>/chat`) is a public page under the same gate and the same
|
|
28
|
+
rule: its URL names no conversation, so possessing the link grants nothing.
|
|
29
|
+
Every
|
|
27
30
|
authorized agent can read and answer every conversation in the mounted inbox.
|
|
28
31
|
Livechat 1.x does not provide tenant-isolated agent inboxes.
|
|
29
32
|
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Livechat
|
|
4
|
+
# GET <mount_path>/chat — the visitor's chat as a page of its own.
|
|
5
|
+
#
|
|
6
|
+
# Public, like the widget: it hangs off ApplicationController, never the
|
|
7
|
+
# inbox's base controller. It names no conversation. The page carries the
|
|
8
|
+
# widget in its inline mode, and the widget resolves the thread from the
|
|
9
|
+
# signed-in id or guest cookie as it does everywhere else, so the URL is the
|
|
10
|
+
# same for everyone and a leaked link shows a stranger only their own chat.
|
|
11
|
+
#
|
|
12
|
+
# Unlike the widget's endpoints this is an HTML page with a CSRF token, so
|
|
13
|
+
# it needs the session and stays out of Livechat.public_paths.
|
|
14
|
+
class ChatController < ApplicationController
|
|
15
|
+
# Explicit, or Rails' implied lookup finds layouts/livechat/application —
|
|
16
|
+
# the inbox's shell.
|
|
17
|
+
layout false
|
|
18
|
+
|
|
19
|
+
before_action :require_enabled
|
|
20
|
+
|
|
21
|
+
def show
|
|
22
|
+
return head :not_found unless Livechat.chat_path
|
|
23
|
+
# A host that put the chat on a page of its own: send people there.
|
|
24
|
+
if Livechat.chat_path != Livechat.config.engine_chat_path
|
|
25
|
+
return redirect_to(Livechat.chat_path(locale: params[:locale].presence), allow_other_host: true)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
I18n.with_locale(page_locale) { render :show }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
# This page doesn't run the host's locale switching, so a link carries the
|
|
34
|
+
# visitor's language as ?locale= (the panel's icon and the reply email
|
|
35
|
+
# both do). Anything the app doesn't have falls back.
|
|
36
|
+
def page_locale
|
|
37
|
+
requested = params[:locale].to_s
|
|
38
|
+
I18n.available_locales.map(&:to_s).include?(requested) ? requested : I18n.locale
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -26,6 +26,7 @@ module Livechat
|
|
|
26
26
|
def new_agent_reply(message)
|
|
27
27
|
@message = message
|
|
28
28
|
@conversation = message.conversation
|
|
29
|
+
@chat_url = chat_url(@conversation)
|
|
29
30
|
assign_unread_digest(message, @conversation.messages.from_agent)
|
|
30
31
|
|
|
31
32
|
mail from: Livechat.config.mailer_from,
|
|
@@ -52,6 +53,59 @@ module Livechat
|
|
|
52
53
|
nil
|
|
53
54
|
end
|
|
54
55
|
|
|
56
|
+
# Where the visitor continues: the chat page, on the origin they wrote
|
|
57
|
+
# from. That origin is where their guest cookie lives — a marketing site
|
|
58
|
+
# proxying the mount path is a different origin from the app's own host,
|
|
59
|
+
# and a link to the app's host would open an empty chat there. A page of
|
|
60
|
+
# the host's own (chat_path outside the mount) prefers the mailer host,
|
|
61
|
+
# since only the mount path is proxied. Without the chat page, it is the
|
|
62
|
+
# page they last wrote from, as before.
|
|
63
|
+
#
|
|
64
|
+
# A signed-in visitor keeps that last page when the target is the engine's
|
|
65
|
+
# page: theirs is usually behind the host's login, which takes a
|
|
66
|
+
# signed-out reader through sign-in and back to their thread. The engine's
|
|
67
|
+
# page can't ask the host to sign anyone in, and would show them an empty
|
|
68
|
+
# guest chat instead.
|
|
69
|
+
def chat_url(conversation)
|
|
70
|
+
path = Livechat.chat_path
|
|
71
|
+
return conversation.page_url unless path
|
|
72
|
+
return path if path.match?(%r{\Ahttps?://}i)
|
|
73
|
+
if conversation.visitor_id.present? && conversation.page_url.present? &&
|
|
74
|
+
path == Livechat.config.engine_chat_path
|
|
75
|
+
return conversation.page_url
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
origins = [origin_of(conversation.page_url), mailer_origin]
|
|
79
|
+
origins.reverse! unless path.start_with?(Livechat.config.engine_chat_path)
|
|
80
|
+
origin = origins.compact.first
|
|
81
|
+
return unless origin
|
|
82
|
+
|
|
83
|
+
url = "#{origin}#{path}"
|
|
84
|
+
locale = conversation.locale.presence
|
|
85
|
+
return url unless locale
|
|
86
|
+
|
|
87
|
+
separator = path.include?('?') ? '&' : '?'
|
|
88
|
+
"#{url}#{separator}locale=#{URI.encode_www_form_component(locale)}"
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def origin_of(url)
|
|
92
|
+
uri = URI.parse(url.to_s)
|
|
93
|
+
return unless uri.host && %w[http https].include?(uri.scheme)
|
|
94
|
+
|
|
95
|
+
URI::Generic.build(scheme: uri.scheme, host: uri.host, port: uri.port == uri.default_port ? nil : uri.port).to_s
|
|
96
|
+
rescue URI::InvalidURIError
|
|
97
|
+
nil
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def mailer_origin
|
|
101
|
+
options = ActionMailer::Base.default_url_options.merge(default_url_options)
|
|
102
|
+
return unless options[:host]
|
|
103
|
+
|
|
104
|
+
ActionDispatch::Http::URL.full_url_for(options.slice(:host, :protocol, :port).merge(path: '')).chomp('/')
|
|
105
|
+
rescue StandardError
|
|
106
|
+
nil
|
|
107
|
+
end
|
|
108
|
+
|
|
55
109
|
def assign_unread_digest(message, side)
|
|
56
110
|
scope = side.unread.where(id: message.id..).chronological
|
|
57
111
|
unread_count = scope.count
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="<%= I18n.locale %>"<%= ' dir="rtl"'.html_safe if Livechat::Widget.rtl?(I18n.locale) %>>
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<title><%= Livechat.app_name %></title>
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
|
7
|
+
<%# A personal page: whoever loads it sees their own chat. Nothing to index. %>
|
|
8
|
+
<meta name="robots" content="noindex">
|
|
9
|
+
<%= csrf_meta_tags %>
|
|
10
|
+
<%= csp_meta_tag %>
|
|
11
|
+
<%# Only the page frame. The chat styles itself, as it does on any page. %>
|
|
12
|
+
<style<%= %( nonce="#{content_security_policy_nonce}").html_safe if content_security_policy_nonce %>>
|
|
13
|
+
html, body { height: 100%; margin: 0; }
|
|
14
|
+
body { background: #f6f7f9; display: flex; justify-content: center; }
|
|
15
|
+
.lvc-chat-page { width: 100%; max-width: 760px; height: 100dvh; padding: 24px; box-sizing: border-box; }
|
|
16
|
+
@media (prefers-color-scheme: dark) { body { background: #111418; } }
|
|
17
|
+
/* On a phone this page is the whole screen, so the chat goes edge to edge.
|
|
18
|
+
One class more than the widget's own card rule, so it wins whatever the
|
|
19
|
+
order the two stylesheets land in. */
|
|
20
|
+
@media (max-width: 480px) {
|
|
21
|
+
.lvc-chat-page { padding: 0; }
|
|
22
|
+
.lvc-chat-page #lvc-root.lvc-inline #lvc-panel { border: none; border-radius: 0; }
|
|
23
|
+
}
|
|
24
|
+
</style>
|
|
25
|
+
</head>
|
|
26
|
+
<body>
|
|
27
|
+
<%= livechat_chat class: 'lvc-chat-page' %>
|
|
28
|
+
<%= livechat_tag %>
|
|
29
|
+
</body>
|
|
30
|
+
</html>
|
|
@@ -11,6 +11,6 @@
|
|
|
11
11
|
...and <%= @more_count %> more unread <%= @more_count == 1 ? 'reply' : 'replies' %>.
|
|
12
12
|
<% end %>
|
|
13
13
|
|
|
14
|
-
<% if @
|
|
15
|
-
<%= t('livechat.mail.continue', default: 'Continue the conversation') %>: <%= @
|
|
14
|
+
<% if @chat_url.present? %>
|
|
15
|
+
<%= t('livechat.mail.continue', default: 'Continue the conversation') %>: <%= @chat_url %>
|
|
16
16
|
<% end %>
|
|
@@ -29,6 +29,7 @@ ru:
|
|
|
29
29
|
attachment_error: "Некоторые файлы не удалось отправить."
|
|
30
30
|
expand: "Развернуть"
|
|
31
31
|
collapse: "Свернуть"
|
|
32
|
+
open_page: "Открыть на отдельной странице"
|
|
32
33
|
typing: "%{name} is typing..."
|
|
33
34
|
attachments:
|
|
34
35
|
too_many: "Слишком много файлов (макс. %{count})."
|
data/config/routes.rb
CHANGED
|
@@ -23,6 +23,10 @@ Livechat::Engine.routes.draw do
|
|
|
23
23
|
# digits-only one would have made the tables bigint-only (see below).
|
|
24
24
|
get 'attachments/:id', to: 'attachments#show', as: :attachment
|
|
25
25
|
|
|
26
|
+
# The visitor's chat as a page. No id: like the widget, it shows whoever
|
|
27
|
+
# loads it their own conversation. Above the catch-all for the same reason.
|
|
28
|
+
get 'chat', to: 'chat#show', as: :chat
|
|
29
|
+
|
|
26
30
|
# The inbox. Flat, human URLs: the mount path IS the conversation list.
|
|
27
31
|
# No `id: /\d+/` constraint: it made these routes bigint-only, which forced the
|
|
28
32
|
# tables to be bigint too, and a uuid-keyed host could then never attach a file
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'rails/generators'
|
|
4
|
+
require 'rails/generators/active_record'
|
|
5
|
+
require_relative '../migration_helpers'
|
|
6
|
+
|
|
7
|
+
module Livechat
|
|
8
|
+
module Generators
|
|
9
|
+
# For apps that installed livechat before 1.4: adds the foreign key from
|
|
10
|
+
# livechat_messages.conversation_id to livechat_conversations. A fresh
|
|
11
|
+
# install already has it. Nothing in the engine changes behavior; the
|
|
12
|
+
# database just stops accepting a message for a conversation that does
|
|
13
|
+
# not exist.
|
|
14
|
+
#
|
|
15
|
+
# bin/rails generate livechat:foreign_key && bin/rails db:migrate
|
|
16
|
+
class ForeignKeyGenerator < Rails::Generators::Base
|
|
17
|
+
include ActiveRecord::Generators::Migration
|
|
18
|
+
include MigrationHelpers
|
|
19
|
+
|
|
20
|
+
source_root File.expand_path('templates', __dir__)
|
|
21
|
+
|
|
22
|
+
desc 'Adds the messages -> conversations foreign key to an existing livechat install.'
|
|
23
|
+
|
|
24
|
+
def create_migration_file
|
|
25
|
+
migration_template 'add_livechat_messages_conversation_foreign_key.rb.tt',
|
|
26
|
+
'db/migrate/add_livechat_messages_conversation_foreign_key.rb'
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def post_install
|
|
30
|
+
say "\nForeign key queued. Run `rails db:migrate`.", :green
|
|
31
|
+
say 'It fails if a message points at a conversation that no longer exists. Find those with:'
|
|
32
|
+
say ' Livechat::Message.where.not(conversation_id: Livechat::Conversation.select(:id))'
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class AddLivechatMessagesConversationForeignKey < ActiveRecord::Migration<%= migration_version %>
|
|
4
|
+
<% if postgresql? -%>
|
|
5
|
+
disable_ddl_transaction!
|
|
6
|
+
|
|
7
|
+
def change
|
|
8
|
+
add_foreign_key :livechat_messages, :livechat_conversations, column: :conversation_id, validate: false
|
|
9
|
+
reversible { |direction| direction.up { validate_foreign_key :livechat_messages, :livechat_conversations } }
|
|
10
|
+
end
|
|
11
|
+
<% else -%>
|
|
12
|
+
def change
|
|
13
|
+
add_foreign_key :livechat_messages, :livechat_conversations, column: :conversation_id
|
|
14
|
+
end
|
|
15
|
+
<% end -%>
|
|
16
|
+
end
|
|
@@ -23,7 +23,8 @@ class CreateLivechatTables < ActiveRecord::Migration<%= migration_version %>
|
|
|
23
23
|
create_table :livechat_messages<%= primary_key_type_option %> do |t|
|
|
24
24
|
# index: false — the (conversation_id, id) index below already covers
|
|
25
25
|
# lookups by conversation_id, being a leftmost prefix of it.
|
|
26
|
-
t.references :conversation, null: false, index: false<%= foreign_key_type_option
|
|
26
|
+
t.references :conversation, null: false, index: false<%= foreign_key_type_option %>,
|
|
27
|
+
foreign_key: { to_table: :livechat_conversations }
|
|
27
28
|
t.string :author_type, null: false
|
|
28
29
|
t.string :agent_id
|
|
29
30
|
t.string :agent_label
|
|
@@ -95,6 +95,11 @@ Livechat.configure do |config|
|
|
|
95
95
|
# config.action_cable = true
|
|
96
96
|
# config.action_cable_url = "/cable"
|
|
97
97
|
|
|
98
|
+
# The chat as a page of its own, at /livechat/chat by default: a URL the
|
|
99
|
+
# visitor can bookmark, opened from the panel and the reply email. Point it
|
|
100
|
+
# at a page of yours rendering `livechat_chat`, or false to turn it off.
|
|
101
|
+
# config.chat_path = "/support"
|
|
102
|
+
|
|
98
103
|
# Default mount path used by `mount_livechat`.
|
|
99
104
|
# Override only if you mount the engine manually.
|
|
100
105
|
# config.mount_path = "/livechat"
|
|
@@ -38,6 +38,18 @@ module Livechat
|
|
|
38
38
|
config = Rails.configuration.generators
|
|
39
39
|
config.options[config.orm][:primary_key_type]
|
|
40
40
|
end
|
|
41
|
+
|
|
42
|
+
# Adding a foreign key to a table that already holds rows validates every
|
|
43
|
+
# row while it locks out writes. On PostgreSQL the key can go in
|
|
44
|
+
# unvalidated and be validated in a second statement that does not block
|
|
45
|
+
# writes, which is also the only shape strong_migrations accepts. That
|
|
46
|
+
# second statement must run outside the migration's transaction.
|
|
47
|
+
#
|
|
48
|
+
# Decided at generate time, like the key type, so the migration holds
|
|
49
|
+
# plain code rather than an adapter check.
|
|
50
|
+
def postgresql?
|
|
51
|
+
ActiveRecord::Base.connection_db_config.adapter.to_s.include?('postg')
|
|
52
|
+
end
|
|
41
53
|
end
|
|
42
54
|
end
|
|
43
55
|
end
|
|
@@ -143,6 +143,16 @@ module Livechat
|
|
|
143
143
|
# this in sync with the `mount` line in your routes.
|
|
144
144
|
attr_accessor :mount_path
|
|
145
145
|
|
|
146
|
+
# The chat as a page of its own — a URL a visitor can bookmark, that the
|
|
147
|
+
# reply email links to, and that the panel's "open as a page" icon opens.
|
|
148
|
+
# It shows whoever loads it their own conversation, resolved the same way
|
|
149
|
+
# the widget resolves it (signed-in id or guest cookie); the URL never
|
|
150
|
+
# names a conversation. nil serves the engine's page at
|
|
151
|
+
# `<mount_path>/chat`. A path or URL of your own (a page rendering
|
|
152
|
+
# `livechat_chat` in your layout) takes its place, and the engine's page
|
|
153
|
+
# redirects there. false turns the page off.
|
|
154
|
+
attr_accessor :chat_path
|
|
155
|
+
|
|
146
156
|
def initialize
|
|
147
157
|
@app_name = nil
|
|
148
158
|
@enabled = ->(_request) { true }
|
|
@@ -166,6 +176,7 @@ module Livechat
|
|
|
166
176
|
@on_agent_message = ->(_message) {}
|
|
167
177
|
@rate_limit = { to: 30, within: 60 }
|
|
168
178
|
@mount_path = '/livechat'
|
|
179
|
+
@chat_path = nil
|
|
169
180
|
@attach_files = true
|
|
170
181
|
@storage_service = nil
|
|
171
182
|
@max_attachments = 5
|
|
@@ -177,6 +188,9 @@ module Livechat
|
|
|
177
188
|
|
|
178
189
|
def widget_endpoint = "#{mount_path.chomp('/')}/widget"
|
|
179
190
|
|
|
191
|
+
# The engine's own chat page, whatever chat_path says.
|
|
192
|
+
def engine_chat_path = "#{mount_path.chomp('/')}/chat"
|
|
193
|
+
|
|
180
194
|
def agent_email_list
|
|
181
195
|
list = agent_emails
|
|
182
196
|
list = list.call if list.respond_to?(:call)
|
data/lib/livechat/version.rb
CHANGED
data/lib/livechat/widget.js
CHANGED
|
@@ -48,6 +48,11 @@
|
|
|
48
48
|
var recordingTimer = null;
|
|
49
49
|
var recordingCancelled = false;
|
|
50
50
|
var isOpen = false;
|
|
51
|
+
// Inline: the chat is part of the page (a [data-livechat-chat] element,
|
|
52
|
+
// from livechat_chat or the engine's chat page) instead of floating over it.
|
|
53
|
+
// Always open, never a modal, no launcher. Decided on every render, since a
|
|
54
|
+
// Turbo visit can move between a page with the element and one without.
|
|
55
|
+
var inline = false;
|
|
51
56
|
var lastFocused = null;
|
|
52
57
|
var expandBtnEl = null;
|
|
53
58
|
// Desktop-only: a bigger panel, remembered across Turbo visits. Never
|
|
@@ -122,7 +127,11 @@
|
|
|
122
127
|
applyExpanded(); // a Turbo swap makes a fresh root — restore the size class
|
|
123
128
|
syncOpenerBadges(); // a Turbo visit brings fresh openers — restamp them
|
|
124
129
|
|
|
125
|
-
if (
|
|
130
|
+
if (inline) {
|
|
131
|
+
// The page is the chat. The popover's remembered open state is left
|
|
132
|
+
// alone: it belongs to the floating panel on other pages.
|
|
133
|
+
showPanel();
|
|
134
|
+
} else if (sessionGet("livechat_open") === "1") {
|
|
126
135
|
openPanel();
|
|
127
136
|
} else {
|
|
128
137
|
// One initial fetch decides everything: thread or not, badge count.
|
|
@@ -144,7 +153,9 @@
|
|
|
144
153
|
// --- mounting ---------------------------------------------------------------
|
|
145
154
|
|
|
146
155
|
function mount() {
|
|
147
|
-
|
|
156
|
+
var host = document.querySelector("[data-livechat-chat]");
|
|
157
|
+
var parent = host || document.body;
|
|
158
|
+
if (root && root.parentNode === parent && document.body.contains(root)) return;
|
|
148
159
|
|
|
149
160
|
isOpen = false;
|
|
150
161
|
lastRenderedId = 0;
|
|
@@ -152,16 +163,27 @@
|
|
|
152
163
|
// A Turbo body swap removes the panel without closePanel() running; the
|
|
153
164
|
// documentElement (and any scroll lock on it) survives the swap.
|
|
154
165
|
unlockScroll();
|
|
166
|
+
unbindViewport();
|
|
167
|
+
// A restored Turbo snapshot carries a lifeless copy of the last root. Two
|
|
168
|
+
// roots would share every id, and lookups by id would find the copy.
|
|
169
|
+
var stale = document.querySelectorAll("#lvc-root");
|
|
170
|
+
for (var i = 0; i < stale.length; i++) stale[i].remove();
|
|
155
171
|
|
|
172
|
+
inline = !!host;
|
|
173
|
+
badgeEl = null;
|
|
156
174
|
root = document.createElement("div");
|
|
157
175
|
root.id = "lvc-root";
|
|
158
176
|
if (config.rtl) root.setAttribute("dir", "rtl");
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
if (!config.launcher)
|
|
177
|
+
if (inline) {
|
|
178
|
+
root.className = "lvc-inline";
|
|
179
|
+
} else if (!config.launcher) {
|
|
180
|
+
// No launcher bubble to clear — the panel sits in the corner instead of
|
|
181
|
+
// hovering 66px above nothing.
|
|
182
|
+
root.className = "lvc-no-launcher";
|
|
183
|
+
}
|
|
162
184
|
|
|
163
|
-
if (config.launcher) root.appendChild(buildLauncher());
|
|
164
|
-
|
|
185
|
+
if (config.launcher && !inline) root.appendChild(buildLauncher());
|
|
186
|
+
parent.appendChild(root);
|
|
165
187
|
}
|
|
166
188
|
|
|
167
189
|
// config.accentColor rebrands the widget. Inline style properties on the
|
|
@@ -211,7 +233,7 @@
|
|
|
211
233
|
function buildPanel() {
|
|
212
234
|
var panel = document.createElement("div");
|
|
213
235
|
panel.id = "lvc-panel";
|
|
214
|
-
panel.setAttribute("role", "dialog");
|
|
236
|
+
panel.setAttribute("role", inline ? "region" : "dialog");
|
|
215
237
|
panel.setAttribute("aria-label", config.appName);
|
|
216
238
|
panel.addEventListener("keydown", trapFocus);
|
|
217
239
|
|
|
@@ -239,25 +261,9 @@
|
|
|
239
261
|
identity.appendChild(titles);
|
|
240
262
|
header.appendChild(identity);
|
|
241
263
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
// Grow/shrink the desktop panel. Hidden on phones (already full-screen).
|
|
246
|
-
expandBtnEl = document.createElement("button");
|
|
247
|
-
expandBtnEl.type = "button";
|
|
248
|
-
expandBtnEl.id = "lvc-expand";
|
|
249
|
-
expandBtnEl.addEventListener("click", toggleExpand);
|
|
250
|
-
actions.appendChild(expandBtnEl);
|
|
251
|
-
|
|
252
|
-
var close = document.createElement("button");
|
|
253
|
-
close.type = "button";
|
|
254
|
-
close.id = "lvc-close";
|
|
255
|
-
close.setAttribute("aria-label", config.labels.close);
|
|
256
|
-
close.innerHTML = "×";
|
|
257
|
-
close.addEventListener("click", closePanel);
|
|
258
|
-
actions.appendChild(close);
|
|
259
|
-
|
|
260
|
-
header.appendChild(actions);
|
|
264
|
+
// Inline, the chat is the page: nothing to resize, open, or close.
|
|
265
|
+
expandBtnEl = null;
|
|
266
|
+
if (!inline) header.appendChild(buildActions());
|
|
261
267
|
panel.appendChild(header);
|
|
262
268
|
applyExpanded(); // set the panel size + the button's icon/label
|
|
263
269
|
|
|
@@ -403,6 +409,50 @@
|
|
|
403
409
|
return panel;
|
|
404
410
|
}
|
|
405
411
|
|
|
412
|
+
function buildActions() {
|
|
413
|
+
var actions = document.createElement("div");
|
|
414
|
+
actions.id = "lvc-actions";
|
|
415
|
+
|
|
416
|
+
// Grow/shrink the desktop panel. Hidden on phones (already full-screen).
|
|
417
|
+
expandBtnEl = document.createElement("button");
|
|
418
|
+
expandBtnEl.type = "button";
|
|
419
|
+
expandBtnEl.id = "lvc-expand";
|
|
420
|
+
expandBtnEl.addEventListener("click", toggleExpand);
|
|
421
|
+
actions.appendChild(expandBtnEl);
|
|
422
|
+
|
|
423
|
+
// The same chat as a page of its own (config.chat_path), when there is one.
|
|
424
|
+
if (config.chatUrl) {
|
|
425
|
+
var page = document.createElement("a");
|
|
426
|
+
page.id = "lvc-open-page";
|
|
427
|
+
page.href = chatPageUrl();
|
|
428
|
+
// A full load: the page has its own layout, not this one's.
|
|
429
|
+
page.setAttribute("data-turbo", "false");
|
|
430
|
+
page.setAttribute("aria-label", config.labels.openPage);
|
|
431
|
+
page.title = config.labels.openPage;
|
|
432
|
+
page.innerHTML = PAGE_ICON;
|
|
433
|
+
// The chat moves to the page; the popover shouldn't reopen on the next one.
|
|
434
|
+
page.addEventListener("click", function () { sessionSet("livechat_open", ""); });
|
|
435
|
+
actions.appendChild(page);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
var close = document.createElement("button");
|
|
439
|
+
close.type = "button";
|
|
440
|
+
close.id = "lvc-close";
|
|
441
|
+
close.setAttribute("aria-label", config.labels.close);
|
|
442
|
+
close.innerHTML = "×";
|
|
443
|
+
close.addEventListener("click", closePanel);
|
|
444
|
+
actions.appendChild(close);
|
|
445
|
+
return actions;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// The engine's chat page doesn't run the host's locale switching, so the
|
|
449
|
+
// link carries the widget's language along.
|
|
450
|
+
function chatPageUrl() {
|
|
451
|
+
if (!config.locale) return config.chatUrl;
|
|
452
|
+
var separator = config.chatUrl.indexOf("?") === -1 ? "?" : "&";
|
|
453
|
+
return config.chatUrl + separator + "locale=" + encodeURIComponent(config.locale);
|
|
454
|
+
}
|
|
455
|
+
|
|
406
456
|
// --- audio recording --------------------------------------------------------
|
|
407
457
|
|
|
408
458
|
function buildRecordingBar() {
|
|
@@ -670,26 +720,7 @@
|
|
|
670
720
|
// Livechat.open("…")) seeds the input — but never over a visitor's draft.
|
|
671
721
|
function openPanel(prefill) {
|
|
672
722
|
mount();
|
|
673
|
-
|
|
674
|
-
isOpen = true;
|
|
675
|
-
lastFocused = document.activeElement;
|
|
676
|
-
sessionSet("livechat_open", "1");
|
|
677
|
-
|
|
678
|
-
// The panel is built once and then hidden/shown — closing must never
|
|
679
|
-
// throw away the rendered thread or a half-written message.
|
|
680
|
-
var panel = document.getElementById("lvc-panel");
|
|
681
|
-
if (!panel) {
|
|
682
|
-
panel = buildPanel();
|
|
683
|
-
root.appendChild(panel);
|
|
684
|
-
}
|
|
685
|
-
panel.hidden = false;
|
|
686
|
-
panel.setAttribute("aria-modal", isMobileModal() ? "true" : "false");
|
|
687
|
-
if (isMobileModal()) lockScroll();
|
|
688
|
-
bindViewport(); // pin the panel above the keyboard on phones
|
|
689
|
-
setUnread(0);
|
|
690
|
-
poll();
|
|
691
|
-
schedulePoll();
|
|
692
|
-
}
|
|
723
|
+
showPanel();
|
|
693
724
|
if (inputEl) {
|
|
694
725
|
if (typeof prefill === "string" && prefill && !inputEl.value.trim()) {
|
|
695
726
|
inputEl.value = prefill;
|
|
@@ -699,7 +730,34 @@
|
|
|
699
730
|
}
|
|
700
731
|
}
|
|
701
732
|
|
|
733
|
+
// Inline, this runs once per page load and focuses nothing: a phone would
|
|
734
|
+
// otherwise raise its keyboard over the page the moment it loads.
|
|
735
|
+
function showPanel() {
|
|
736
|
+
if (isOpen) return;
|
|
737
|
+
isOpen = true;
|
|
738
|
+
lastFocused = document.activeElement;
|
|
739
|
+
if (!inline) sessionSet("livechat_open", "1");
|
|
740
|
+
|
|
741
|
+
// The panel is built once and then hidden/shown — closing must never
|
|
742
|
+
// throw away the rendered thread or a half-written message.
|
|
743
|
+
var panel = document.getElementById("lvc-panel");
|
|
744
|
+
if (!panel) {
|
|
745
|
+
panel = buildPanel();
|
|
746
|
+
root.appendChild(panel);
|
|
747
|
+
}
|
|
748
|
+
panel.hidden = false;
|
|
749
|
+
if (!inline) {
|
|
750
|
+
panel.setAttribute("aria-modal", isMobileModal() ? "true" : "false");
|
|
751
|
+
if (isMobileModal()) lockScroll();
|
|
752
|
+
bindViewport(); // pin the panel above the keyboard on phones
|
|
753
|
+
}
|
|
754
|
+
setUnread(0);
|
|
755
|
+
poll();
|
|
756
|
+
schedulePoll();
|
|
757
|
+
}
|
|
758
|
+
|
|
702
759
|
function closePanel() {
|
|
760
|
+
if (inline) return; // the page is the chat; there is nothing to close
|
|
703
761
|
isOpen = false;
|
|
704
762
|
sessionSet("livechat_open", "");
|
|
705
763
|
var panel = document.getElementById("lvc-panel");
|
|
@@ -710,8 +768,10 @@
|
|
|
710
768
|
schedulePoll();
|
|
711
769
|
}
|
|
712
770
|
|
|
771
|
+
// Never inline: the page owns its own scrolling, focus and keyboard there,
|
|
772
|
+
// and a scroll lock would pin a host page that merely contains the chat.
|
|
713
773
|
function isMobileModal() {
|
|
714
|
-
return !!(mobileModal && mobileModal.matches);
|
|
774
|
+
return !inline && !!(mobileModal && mobileModal.matches);
|
|
715
775
|
}
|
|
716
776
|
|
|
717
777
|
// --- expand / collapse (desktop) --------------------------------------------
|
|
@@ -726,6 +786,11 @@
|
|
|
726
786
|
'stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">' +
|
|
727
787
|
'<polyline points="4 14 10 14 10 20"/><polyline points="20 10 14 10 14 4"/>' +
|
|
728
788
|
'<line x1="14" y1="10" x2="21" y2="3"/><line x1="10" y1="14" x2="3" y2="21"/></svg>';
|
|
789
|
+
var PAGE_ICON =
|
|
790
|
+
'<svg viewBox="0 0 24 24" width="15" height="15" fill="none" stroke="currentColor" ' +
|
|
791
|
+
'stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">' +
|
|
792
|
+
'<path d="M14 4h6v6"/><path d="M20 4l-9 9"/>' +
|
|
793
|
+
'<path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/></svg>';
|
|
729
794
|
|
|
730
795
|
function toggleExpand() {
|
|
731
796
|
if (isMobileModal()) return;
|
|
@@ -737,7 +802,7 @@
|
|
|
737
802
|
// Reflect the current state onto the panel and the header button. A no-op on
|
|
738
803
|
// phones — the class is never added there, so the full-screen panel stands.
|
|
739
804
|
function applyExpanded() {
|
|
740
|
-
var on = expanded && !isMobileModal();
|
|
805
|
+
var on = expanded && !inline && !isMobileModal();
|
|
741
806
|
if (root) root.classList.toggle("lvc-expanded", on);
|
|
742
807
|
if (!expandBtnEl) return;
|
|
743
808
|
var label = on ? config.labels.collapse : config.labels.expand;
|
|
@@ -1150,13 +1215,17 @@
|
|
|
1150
1215
|
|
|
1151
1216
|
// Multipart only when there are files to carry — otherwise a plain JSON
|
|
1152
1217
|
// object, so text-only sends stay exactly as they were.
|
|
1218
|
+
//
|
|
1219
|
+
// The chat page sends no page_url: "where they got stuck" would otherwise
|
|
1220
|
+
// read "the chat page" for every message written there.
|
|
1153
1221
|
function buildMessagePayload(body) {
|
|
1222
|
+
var pageUrl = inline ? null : window.location.href;
|
|
1154
1223
|
if (!pendingFiles.length) {
|
|
1155
|
-
return { body: body, page_url:
|
|
1224
|
+
return { body: body, page_url: pageUrl, locale: config.locale };
|
|
1156
1225
|
}
|
|
1157
1226
|
var form = new FormData();
|
|
1158
1227
|
form.append("body", body);
|
|
1159
|
-
form.append("page_url",
|
|
1228
|
+
if (pageUrl) form.append("page_url", pageUrl);
|
|
1160
1229
|
form.append("locale", config.locale);
|
|
1161
1230
|
pendingFiles.forEach(function (file) { form.append("files[]", file, file.name || "voice-message.webm"); });
|
|
1162
1231
|
return form;
|
|
@@ -1245,6 +1314,9 @@
|
|
|
1245
1314
|
"#lvc-root #lvc-expand{border:none;background:none;color:var(--lvc-accent-text);cursor:pointer;" +
|
|
1246
1315
|
"padding:5px 6px;border-radius:6px;display:flex;align-items:center;justify-content:center;opacity:.9}" +
|
|
1247
1316
|
"#lvc-root #lvc-expand:hover{background:rgba(255,255,255,.15);opacity:1}" +
|
|
1317
|
+
"#lvc-root #lvc-open-page{color:var(--lvc-accent-text);padding:5px 6px;border-radius:6px;" +
|
|
1318
|
+
"display:flex;align-items:center;justify-content:center;opacity:.9}" +
|
|
1319
|
+
"#lvc-root #lvc-open-page:hover{background:rgba(255,255,255,.15);opacity:1}" +
|
|
1248
1320
|
// Desktop only: a roomier panel for long threads and image-heavy chats.
|
|
1249
1321
|
// Guarded by min-width so it can never shrink the full-screen mobile panel.
|
|
1250
1322
|
"@media(min-width:481px){#lvc-root.lvc-expanded #lvc-panel{top:20px;bottom:20px;" +
|
|
@@ -1358,6 +1430,18 @@
|
|
|
1358
1430
|
// Keep the composer box above the iPhone home indicator.
|
|
1359
1431
|
"#lvc-root #lvc-form{margin-bottom:calc(10px + env(safe-area-inset-bottom))}" +
|
|
1360
1432
|
"}" +
|
|
1433
|
+
// Inline (livechat_chat, the chat page): the panel fills its element
|
|
1434
|
+
// instead of floating. After the phone block on purpose — equal
|
|
1435
|
+
// specificity, so source order makes these win there too, including the
|
|
1436
|
+
// card's rounded border: inside a host page it is a card at any width,
|
|
1437
|
+
// not the phone popover's edge-to-edge screen. (The engine's own chat
|
|
1438
|
+
// page, which does fill the phone, squares itself off.) min-height stops
|
|
1439
|
+
// an element with no height of its own from collapsing the chat to its
|
|
1440
|
+
// header.
|
|
1441
|
+
"#lvc-root.lvc-inline{height:100%}" +
|
|
1442
|
+
"#lvc-root.lvc-inline #lvc-panel{position:relative;top:auto;right:auto;bottom:auto;left:auto;" +
|
|
1443
|
+
"z-index:auto;width:100%;max-width:none;height:100%;min-height:420px;max-height:none;box-shadow:none;" +
|
|
1444
|
+
"border:1px solid var(--lvc-border);border-radius:14px}" +
|
|
1361
1445
|
// Lives in host DOM (on data-livechat-open elements), so no #lvc-root prefix.
|
|
1362
1446
|
// A host that names its own badge classes gets none of this: unlayered
|
|
1363
1447
|
// rules would beat its layered utility classes whatever their specificity.
|
data/lib/livechat/widget.rb
CHANGED
|
@@ -105,12 +105,17 @@ module Livechat
|
|
|
105
105
|
accentColor: settings[:accent_color],
|
|
106
106
|
attachments: Livechat.attachments_enabled?,
|
|
107
107
|
maxAttachments: config.max_attachments,
|
|
108
|
+
chatUrl: Livechat.chat_path,
|
|
108
109
|
labels: labels(settings)
|
|
109
110
|
}
|
|
110
111
|
# Escape "</" so a value can't close the <script> block early.
|
|
111
112
|
payload.to_json.gsub('</', '<\/')
|
|
112
113
|
end
|
|
113
114
|
|
|
115
|
+
def rtl?(locale)
|
|
116
|
+
RTL_LANGUAGES.include?(locale.to_s.downcase.split(/[-_]/).first)
|
|
117
|
+
end
|
|
118
|
+
|
|
114
119
|
private
|
|
115
120
|
|
|
116
121
|
def script_src
|
|
@@ -153,6 +158,7 @@ module Livechat
|
|
|
153
158
|
attachmentError: t(:attachment_error, 'Some files could not be sent.'),
|
|
154
159
|
expand: t(:expand, 'Expand'),
|
|
155
160
|
collapse: t(:collapse, 'Collapse'),
|
|
161
|
+
openPage: t(:open_page, 'Open as a page'),
|
|
156
162
|
typing: t(:typing, '%{name} is typing...', name: t(:team, 'Support'))
|
|
157
163
|
}
|
|
158
164
|
end
|
|
@@ -160,10 +166,6 @@ module Livechat
|
|
|
160
166
|
def t(key, default, **args)
|
|
161
167
|
I18n.t(key, scope: :livechat, default: default, **args)
|
|
162
168
|
end
|
|
163
|
-
|
|
164
|
-
def rtl?(locale)
|
|
165
|
-
RTL_LANGUAGES.include?(locale.to_s.downcase.split(/[-_]/).first)
|
|
166
|
-
end
|
|
167
169
|
end
|
|
168
170
|
end
|
|
169
171
|
end
|
|
@@ -31,6 +31,18 @@ module Livechat
|
|
|
31
31
|
tag.button(label, type: 'button', data:, **options)
|
|
32
32
|
end
|
|
33
33
|
|
|
34
|
+
# The chat itself, in your page instead of floating over it: the visitor's
|
|
35
|
+
# own conversation, filling this element. Give it a height (or put it in a
|
|
36
|
+
# container that has one). The bubble stays away while it is on the page.
|
|
37
|
+
# Like livechat_button, it is only the element — livechat_tag in your
|
|
38
|
+
# layout brings the widget that fills it.
|
|
39
|
+
def livechat_chat(**options)
|
|
40
|
+
return unless Livechat.enabled?(request)
|
|
41
|
+
|
|
42
|
+
data = (options.delete(:data) { {} } || {}).merge(livechat_chat: '')
|
|
43
|
+
tag.div(data:, **options)
|
|
44
|
+
end
|
|
45
|
+
|
|
34
46
|
private
|
|
35
47
|
|
|
36
48
|
def livechat_visitor
|
data/lib/livechat.rb
CHANGED
|
@@ -43,6 +43,24 @@ module Livechat
|
|
|
43
43
|
["#{mount}/widget.js", "#{mount}/embed.js", "#{mount}/widget/", "#{mount}/attachments/"]
|
|
44
44
|
end
|
|
45
45
|
|
|
46
|
+
# Where the visitor's chat lives as a page, or nil when chat_path is false.
|
|
47
|
+
# Link to it from your own nav:
|
|
48
|
+
#
|
|
49
|
+
# link_to "Support chat", Livechat.chat_path(locale: I18n.locale)
|
|
50
|
+
#
|
|
51
|
+
# The engine's page doesn't run your locale switching, so pass the locale
|
|
52
|
+
# for any app that isn't single-language.
|
|
53
|
+
def chat_path(locale: nil)
|
|
54
|
+
path = case config.chat_path
|
|
55
|
+
when false then return
|
|
56
|
+
when nil then config.engine_chat_path
|
|
57
|
+
else config.chat_path.to_s
|
|
58
|
+
end
|
|
59
|
+
return path if locale.blank?
|
|
60
|
+
|
|
61
|
+
"#{path}#{path.include?('?') ? '&' : '?'}locale=#{URI.encode_www_form_component(locale.to_s)}"
|
|
62
|
+
end
|
|
63
|
+
|
|
46
64
|
# Resolve a setting that may be a plain value or a per-request callable.
|
|
47
65
|
def resolve(value, request)
|
|
48
66
|
value.respond_to?(:call) ? value.call(request) : value
|
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.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Yaroslav Shmarov
|
|
@@ -53,6 +53,7 @@ files:
|
|
|
53
53
|
- app/controllers/concerns/livechat/request_context.rb
|
|
54
54
|
- app/controllers/livechat/application_controller.rb
|
|
55
55
|
- app/controllers/livechat/attachments_controller.rb
|
|
56
|
+
- app/controllers/livechat/chat_controller.rb
|
|
56
57
|
- app/controllers/livechat/conversations_controller.rb
|
|
57
58
|
- app/controllers/livechat/dashboard_controller.rb
|
|
58
59
|
- app/controllers/livechat/messages_controller.rb
|
|
@@ -64,6 +65,7 @@ files:
|
|
|
64
65
|
- app/models/livechat/conversation.rb
|
|
65
66
|
- app/models/livechat/message.rb
|
|
66
67
|
- app/views/layouts/livechat/application.html.erb
|
|
68
|
+
- app/views/livechat/chat/show.html.erb
|
|
67
69
|
- app/views/livechat/conversations/_thread_panel.html.erb
|
|
68
70
|
- app/views/livechat/conversations/index.html.erb
|
|
69
71
|
- app/views/livechat/conversations/show.html.erb
|
|
@@ -101,6 +103,8 @@ files:
|
|
|
101
103
|
- docs/screenshots/01-widget.png
|
|
102
104
|
- docs/screenshots/03-inbox.png
|
|
103
105
|
- docs/screenshots/04-conversation.png
|
|
106
|
+
- lib/generators/livechat/foreign_key/foreign_key_generator.rb
|
|
107
|
+
- lib/generators/livechat/foreign_key/templates/add_livechat_messages_conversation_foreign_key.rb.tt
|
|
104
108
|
- lib/generators/livechat/install/install_generator.rb
|
|
105
109
|
- lib/generators/livechat/install/templates/create_livechat_tables.rb.tt
|
|
106
110
|
- lib/generators/livechat/install/templates/initializer.rb
|