livechat 1.1.0 → 1.3.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/CHANGELOG.md +45 -0
- data/README.md +25 -0
- data/Rakefile +9 -1
- data/app/controllers/livechat/visitor_controller.rb +1 -1
- data/app/models/livechat/conversation.rb +2 -3
- data/app/models/livechat/message.rb +5 -5
- data/app/views/livechat/conversations/_thread_panel.html.erb +7 -6
- data/config/locales/livechat.ar.yml +1 -1
- data/config/locales/livechat.bg.yml +1 -1
- data/config/locales/livechat.bn.yml +1 -1
- data/config/locales/livechat.de.yml +1 -1
- data/config/locales/livechat.el.yml +1 -1
- data/config/locales/livechat.en.yml +1 -1
- data/config/locales/livechat.es.yml +1 -1
- data/config/locales/livechat.fr.yml +1 -1
- data/config/locales/livechat.hi.yml +1 -1
- data/config/locales/livechat.hr.yml +1 -1
- data/config/locales/livechat.id.yml +1 -1
- data/config/locales/livechat.it.yml +1 -1
- data/config/locales/livechat.ja.yml +1 -1
- data/config/locales/livechat.ko.yml +1 -1
- data/config/locales/livechat.lb.yml +1 -1
- data/config/locales/livechat.nl.yml +1 -1
- data/config/locales/livechat.pl.yml +1 -1
- data/config/locales/livechat.pt.yml +1 -1
- data/config/locales/livechat.ro.yml +1 -1
- data/config/locales/livechat.ru.yml +1 -1
- data/config/locales/livechat.th.yml +1 -1
- data/config/locales/livechat.tr.yml +1 -1
- data/config/locales/livechat.uk.yml +1 -1
- data/config/locales/livechat.ur.yml +1 -1
- data/config/locales/livechat.vi.yml +1 -1
- data/config/locales/livechat.zh-CN.yml +1 -1
- data/docs/ractors.md +123 -0
- data/docs/screenshots/01-widget.png +0 -0
- data/docs/screenshots/03-inbox.png +0 -0
- data/docs/screenshots/04-conversation.png +0 -0
- data/lib/livechat/dashboard.js +15 -4
- data/lib/livechat/engine.rb +11 -0
- data/lib/livechat/ractor_support.rb +534 -0
- data/lib/livechat/version.rb +1 -1
- data/lib/livechat/widget.js +6 -2
- data/lib/livechat/widget.rb +4 -4
- data/lib/livechat.rb +1 -0
- metadata +6 -1
data/lib/livechat/dashboard.js
CHANGED
|
@@ -2,10 +2,9 @@
|
|
|
2
2
|
* livechat dashboard — the little JavaScript the inbox needs, served as a
|
|
3
3
|
* same-origin script so it works under strict CSPs (no inline handlers).
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* eat a half-written reply. Cmd/Ctrl+Enter sends.
|
|
5
|
+
* Thread polling appends new messages without disturbing a reply draft.
|
|
6
|
+
* The inbox list reloads only while the agent is not typing or searching.
|
|
7
|
+
* Cmd/Ctrl+Enter sends; submitting a reply restores focus after navigation.
|
|
9
8
|
*/
|
|
10
9
|
(function () {
|
|
11
10
|
"use strict";
|
|
@@ -359,6 +358,18 @@
|
|
|
359
358
|
thread.appendChild(typingEl);
|
|
360
359
|
|
|
361
360
|
if (replyBox) {
|
|
361
|
+
// Remember only the form destination for one navigation, not a draft.
|
|
362
|
+
// Ordinary visits to the split-pane inbox keep their existing focus.
|
|
363
|
+
var focusKey = "livechat:reply-focus";
|
|
364
|
+
try {
|
|
365
|
+
var focusAction = window.sessionStorage.getItem(focusKey);
|
|
366
|
+
window.sessionStorage.removeItem(focusKey);
|
|
367
|
+
if (focusAction === replyBox.form.action) replyBox.focus();
|
|
368
|
+
} catch (e) { /* storage may be disabled; the reply still submits */ }
|
|
369
|
+
replyBox.form.addEventListener("submit", function () {
|
|
370
|
+
try { window.sessionStorage.setItem(focusKey, replyBox.form.action); }
|
|
371
|
+
catch (e) { /* storage may be disabled */ }
|
|
372
|
+
});
|
|
362
373
|
replyBox.addEventListener("keydown", function (event) {
|
|
363
374
|
if (event.key === "Enter" && (event.metaKey || event.ctrlKey)) {
|
|
364
375
|
event.preventDefault();
|
data/lib/livechat/engine.rb
CHANGED
|
@@ -2,8 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
module Livechat
|
|
4
4
|
class Engine < ::Rails::Engine
|
|
5
|
+
initializer 'livechat.ractor_middleware' do |app|
|
|
6
|
+
app.middleware.insert_before 0, RactorSupport::Middleware
|
|
7
|
+
end
|
|
8
|
+
|
|
5
9
|
isolate_namespace Livechat
|
|
6
10
|
|
|
11
|
+
initializer 'livechat.ractorize', before: 'action_controller.ractorize' do
|
|
12
|
+
if Rails::Application.method_defined?(:ractorize!)
|
|
13
|
+
Engine.routes.singleton_class.prepend(RactorSupport::MountedRoutes)
|
|
14
|
+
end
|
|
15
|
+
ActiveSupport.on_load(:before_ractorize) { Livechat::RactorSupport.prepare! }
|
|
16
|
+
end
|
|
17
|
+
|
|
7
18
|
rake_tasks do
|
|
8
19
|
load File.expand_path('../tasks/livechat_tasks.rake', __dir__)
|
|
9
20
|
end
|
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Livechat
|
|
4
|
+
# Used only by Rails' opt-in before_ractorize hook. Normal Rails boots keep
|
|
5
|
+
# mutable configuration and lazy assets, including on older Ruby versions.
|
|
6
|
+
module RactorSupport
|
|
7
|
+
# Small bridges for framework paths that Rails main does not prepare yet.
|
|
8
|
+
# They are installed only when the host explicitly calls ractorize!.
|
|
9
|
+
# Rails' built-in JSON renderer is still defined from an unisolated Proc.
|
|
10
|
+
# Keep its JSON/JSONP and escaping behavior in a regular Ruby method.
|
|
11
|
+
module JsonRendering
|
|
12
|
+
def _render_with_renderer_json(json, options)
|
|
13
|
+
json_options = options.except(:callback, :content_type, :status)
|
|
14
|
+
json_options[:escape] ||= false if !self.class.escape_json_responses? && options[:callback].blank?
|
|
15
|
+
json = json.to_json(json_options) unless json.is_a?(String)
|
|
16
|
+
if options[:callback].present?
|
|
17
|
+
self.content_type = :js if media_type.nil? || media_type == Mime[:json]
|
|
18
|
+
"/**/#{options[:callback]}(#{json})"
|
|
19
|
+
else
|
|
20
|
+
self.content_type = :json if media_type.nil?
|
|
21
|
+
json
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
module DependencyInterlock
|
|
27
|
+
def interlock
|
|
28
|
+
return super if Ractor.main?
|
|
29
|
+
|
|
30
|
+
Ractor[:engine_dependency_interlock] ||= ActiveSupport::Dependencies::Interlock.new
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
module LiveThreads
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def new_controller_thread
|
|
38
|
+
return super if Ractor.main?
|
|
39
|
+
|
|
40
|
+
Thread.new do
|
|
41
|
+
Thread.current.abort_on_exception = true
|
|
42
|
+
yield
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
module UniquenessCoverage
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
def covered_by_unique_index?(klass, record, attribute, scope)
|
|
51
|
+
return super unless frozen?
|
|
52
|
+
|
|
53
|
+
copies = Ractor[:engine_uniqueness_validators] ||= {}
|
|
54
|
+
copy = copies[[self, klass]] ||= dup
|
|
55
|
+
copy.send(:covered_by_unique_index?, klass, record, attribute, scope)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# GlobalID calls CGI.unescape without an encoding, whose default reads a
|
|
60
|
+
# process-wide class variable. Preserve CGI's configured default on main.
|
|
61
|
+
module UriUnescaping
|
|
62
|
+
def unescape(string, *arguments)
|
|
63
|
+
return super if Ractor.main?
|
|
64
|
+
|
|
65
|
+
shared_arguments = Ractor.make_shareable([string, *arguments], copy: true)
|
|
66
|
+
ActiveSupport::Ractors.on_main(self) { unescape(*shared_arguments) }
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
module CacheFreshness
|
|
71
|
+
def strict_freshness
|
|
72
|
+
return super if Ractor.main?
|
|
73
|
+
|
|
74
|
+
ActiveSupport::Ractors.on_main(self) { strict_freshness }
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Attribute decorators (including Rails enums and serialized attributes)
|
|
79
|
+
# are boot-time factories. Keep their original receivers and captures on
|
|
80
|
+
# the main Ractor, rather than changing the semantics of Rails' closures.
|
|
81
|
+
class Decorator
|
|
82
|
+
def initialize(callback)
|
|
83
|
+
@key = RactorSupport.register_decorator(callback)
|
|
84
|
+
freeze
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def call(*arguments)
|
|
88
|
+
return RactorSupport.call_decorator(@key, arguments) if Ractor.main?
|
|
89
|
+
|
|
90
|
+
shared_arguments = Ractor.make_shareable(arguments, copy: true)
|
|
91
|
+
ActiveSupport::Ractors.on_main(self) { RactorSupport.call_decorator(@key, shared_arguments) }
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def self.register_decorator(callback)
|
|
96
|
+
@decorators ||= []
|
|
97
|
+
@decorators << callback
|
|
98
|
+
@decorators.length - 1
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def self.call_decorator(key, arguments)
|
|
102
|
+
@decorators.fetch(key).call(*arguments)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def self.prepare_attributes(model)
|
|
106
|
+
prepare_attributes(model.superclass) if model.superclass.respond_to?(:pending_attribute_modifications, true)
|
|
107
|
+
model.send(:pending_attribute_modifications)&.each do |modification|
|
|
108
|
+
next unless modification.respond_to?(:decorator) && modification.decorator.is_a?(Proc)
|
|
109
|
+
|
|
110
|
+
modification.decorator = Decorator.new(modification.decorator)
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
module AdapterQuoting
|
|
115
|
+
def quote_column_name(name)
|
|
116
|
+
return super if Ractor.main?
|
|
117
|
+
|
|
118
|
+
shared_name = Ractor.make_shareable(name, copy: true)
|
|
119
|
+
ActiveSupport::Ractors.on_main(self) { quote_column_name(shared_name) }
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def quote_table_name(name)
|
|
123
|
+
return super if Ractor.main?
|
|
124
|
+
|
|
125
|
+
shared_name = Ractor.make_shareable(name, copy: true)
|
|
126
|
+
ActiveSupport::Ractors.on_main(self) { quote_table_name(shared_name) }
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Route arguments can contain records with native Set caches. Only their
|
|
131
|
+
# to_param value is needed to generate a mount prefix; keep the caller's
|
|
132
|
+
# original record objects in the options passed to the actual route.
|
|
133
|
+
def self.routing_snapshot(value)
|
|
134
|
+
return value if Ractor.shareable?(value)
|
|
135
|
+
|
|
136
|
+
copy = case value
|
|
137
|
+
when ActiveRecord::Base then value.to_param
|
|
138
|
+
when Hash then value.to_h { |key, item| [routing_snapshot(key), routing_snapshot(item)] }
|
|
139
|
+
when Array then value.map { |item| routing_snapshot(item) }
|
|
140
|
+
when Set then Set.new(value.map { |item| routing_snapshot(item) })
|
|
141
|
+
else return Ractor.make_shareable(value, copy: true)
|
|
142
|
+
end
|
|
143
|
+
Ractor.make_shareable(copy)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
module MountedRoutes
|
|
147
|
+
def find_script_name(options)
|
|
148
|
+
return super unless defined?(Ractor.shareable_proc) && !Ractor.main?
|
|
149
|
+
|
|
150
|
+
shared_options = RactorSupport.routing_snapshot(options)
|
|
151
|
+
prefix, updated = ActiveSupport::Ractors.on_main(self) do
|
|
152
|
+
local_options = shared_options.dup
|
|
153
|
+
[find_script_name(local_options), local_options]
|
|
154
|
+
end
|
|
155
|
+
(shared_options.keys - updated.keys).each { |key| options.delete(key) }
|
|
156
|
+
updated.each do |key, value|
|
|
157
|
+
options[key] = value unless shared_options.key?(key) && shared_options[key] == value
|
|
158
|
+
end
|
|
159
|
+
prefix
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
module Digesting
|
|
164
|
+
def hexdigest(value)
|
|
165
|
+
return super if Ractor.main?
|
|
166
|
+
|
|
167
|
+
shared_value = Ractor.make_shareable(value, copy: true)
|
|
168
|
+
ActiveSupport::Ractors.on_main(self) { hexdigest(shared_value) }
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
module KeyGeneration
|
|
173
|
+
def generate_key(salt, key_size = 64)
|
|
174
|
+
return super if Ractor.main?
|
|
175
|
+
|
|
176
|
+
shared_salt = Ractor.make_shareable(salt, copy: true)
|
|
177
|
+
ActiveSupport::Ractors.on_main(self) { generate_key(shared_salt, key_size) }
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
module UploadCleanup
|
|
182
|
+
def open
|
|
183
|
+
reopen(path, 'rb+')
|
|
184
|
+
self
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# Tempfile#close takes a positional unlink flag.
|
|
188
|
+
def close(unlink_now = false) # rubocop:disable Style/OptionalBooleanParameter
|
|
189
|
+
super() unless closed?
|
|
190
|
+
unlink if unlink_now
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def unlink
|
|
194
|
+
File.unlink(path)
|
|
195
|
+
rescue Errno::ENOENT
|
|
196
|
+
nil
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def close!
|
|
200
|
+
close(true)
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def self.upload_tempfile(filename, _content_type)
|
|
205
|
+
extension = File.extname(filename.gsub("\0", '%00'))[0, 129]
|
|
206
|
+
Tempfile.create(['RackMultipart', extension]).extend(UploadCleanup)
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
class Middleware
|
|
210
|
+
def initialize(app)
|
|
211
|
+
@app = app
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def call(env)
|
|
215
|
+
if (fallbacks = RactorSupport.fallbacks)
|
|
216
|
+
Fiber[:i18n_fallbacks] ||= fallbacks
|
|
217
|
+
env['rack.multipart.tempfile_factory'] ||= RactorSupport.method(:upload_tempfile) unless Ractor.main?
|
|
218
|
+
end
|
|
219
|
+
@app.call(env)
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def self.fallbacks
|
|
224
|
+
@fallbacks
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
module StorageMetadata
|
|
228
|
+
def analyzed = read_store_attribute(:metadata, :analyzed)
|
|
229
|
+
def identified = read_store_attribute(:metadata, :identified)
|
|
230
|
+
def composed = read_store_attribute(:metadata, :composed)
|
|
231
|
+
|
|
232
|
+
def analyzed=(value)
|
|
233
|
+
write_store_attribute(:metadata, :analyzed, value)
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def identified=(value)
|
|
237
|
+
write_store_attribute(:metadata, :identified, value)
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
def composed=(value)
|
|
241
|
+
write_store_attribute(:metadata, :composed, value)
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
class StorageServices
|
|
246
|
+
def initialize(configurations)
|
|
247
|
+
@configurations = Ractor.make_shareable(configurations, copy: true)
|
|
248
|
+
freeze
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def fetch(name, &)
|
|
252
|
+
registry = if Ractor.main?
|
|
253
|
+
RactorSupport.storage_registry
|
|
254
|
+
else
|
|
255
|
+
Ractor[:engine_storage_services] ||= ActiveStorage::Service::Registry.new(@configurations)
|
|
256
|
+
end
|
|
257
|
+
registry.fetch(name, &)
|
|
258
|
+
end
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
def self.storage_registry
|
|
262
|
+
@storage_registry
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
module StorageChecksums
|
|
266
|
+
def checksum_implementation(**options)
|
|
267
|
+
implementation = super
|
|
268
|
+
if Ractor.main? || !implementation.respond_to?(:name) || !implementation.name&.start_with?('OpenSSL::Digest::')
|
|
269
|
+
return implementation
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
DigestFactory.new(implementation.name.delete_prefix('OpenSSL::Digest::'))
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
class DigestFactory
|
|
277
|
+
def initialize(algorithm)
|
|
278
|
+
@algorithm = algorithm.freeze
|
|
279
|
+
freeze
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
def new
|
|
283
|
+
OpenSSL::Digest.new(@algorithm)
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
def file(io)
|
|
287
|
+
new.file(io)
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
def base64digest(value)
|
|
291
|
+
new.base64digest(value)
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
def self.prepare_redirects!
|
|
296
|
+
base = ActionController::Base
|
|
297
|
+
readers = Module.new
|
|
298
|
+
%i[raise_on_open_redirects action_on_open_redirect action_on_path_relative_redirect].each do |key|
|
|
299
|
+
source = base.method(key).source_location&.first
|
|
300
|
+
next unless source&.end_with?('/action_controller/metal/redirecting.rb')
|
|
301
|
+
|
|
302
|
+
value = Ractor.make_shareable(base.public_send(key), copy: true)
|
|
303
|
+
readers.define_method(key, Ractor.shareable_proc { Ractor.main? ? super() : value })
|
|
304
|
+
end
|
|
305
|
+
return if readers.instance_methods(false).empty?
|
|
306
|
+
|
|
307
|
+
base.prepend(readers)
|
|
308
|
+
base.singleton_class.prepend(readers)
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
def self.prepare_previewers!
|
|
312
|
+
ActiveStorage.previewers.each do |previewer|
|
|
313
|
+
readers = Module.new
|
|
314
|
+
previewer.methods.grep(/_exists\?\z/).each do |capability|
|
|
315
|
+
method = previewer.method(capability)
|
|
316
|
+
next unless method.arity.zero? && method.source_location&.first&.include?('/active_storage/previewer/')
|
|
317
|
+
|
|
318
|
+
readers.define_method(capability, Ractor.shareable_proc do
|
|
319
|
+
if Ractor.main?
|
|
320
|
+
super()
|
|
321
|
+
else
|
|
322
|
+
ActiveSupport::Ractors.on_main(self) { public_send(capability) }
|
|
323
|
+
end
|
|
324
|
+
end)
|
|
325
|
+
end
|
|
326
|
+
previewer.singleton_class.prepend(readers) unless readers.instance_methods(false).empty?
|
|
327
|
+
end
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
def self.prepare_storage!
|
|
331
|
+
return unless defined?(ActiveStorage::Blob)
|
|
332
|
+
|
|
333
|
+
prepare_previewers!
|
|
334
|
+
ActiveStorage::Blob.prepend(StorageMetadata) unless ActiveStorage::Blob < StorageMetadata
|
|
335
|
+
%i[EXTENSIONS TYPE_EXTS TYPE_ALIASES TYPE_PARENTS ROOT_XML MAGIC].each do |name|
|
|
336
|
+
Ractor.make_shareable(Marcel.const_get(name)) if Marcel.const_defined?(name)
|
|
337
|
+
end
|
|
338
|
+
ActiveStorage::Service.prepend(StorageChecksums) unless ActiveStorage::Service < StorageChecksums
|
|
339
|
+
unless Ractor.shareable?(ActiveStorage::Blob.services)
|
|
340
|
+
@storage_registry = ActiveStorage::Blob.services
|
|
341
|
+
configurations = @storage_registry.instance_variable_get(:@configurations)
|
|
342
|
+
default_name = ActiveStorage::Blob.service.name
|
|
343
|
+
default_reader = Module.new
|
|
344
|
+
default_reader.define_method(:service, Ractor.shareable_proc do
|
|
345
|
+
Ractor.main? ? super() : services.fetch(default_name)
|
|
346
|
+
end)
|
|
347
|
+
ActiveStorage::Blob.singleton_class.prepend(default_reader)
|
|
348
|
+
ActiveStorage::Blob.services = StorageServices.new(configurations)
|
|
349
|
+
end
|
|
350
|
+
settings = ActiveStorage.class_variables.map { |name| name.to_s.delete_prefix('@@').to_sym }
|
|
351
|
+
settings.select! { |key| ActiveStorage.respond_to?(key) }
|
|
352
|
+
snapshot = settings.to_h { |key| [key, ActiveStorage.public_send(key)] }
|
|
353
|
+
checksum = snapshot[:checksum_implementation]
|
|
354
|
+
if checksum.is_a?(Class) && checksum.name&.start_with?('OpenSSL::Digest::')
|
|
355
|
+
snapshot[:checksum_implementation] = DigestFactory.new(checksum.name.delete_prefix('OpenSSL::Digest::'))
|
|
356
|
+
end
|
|
357
|
+
Ractor.make_shareable(snapshot)
|
|
358
|
+
readers = Module.new
|
|
359
|
+
settings.each do |key|
|
|
360
|
+
readers.define_method(key, Ractor.shareable_proc { Ractor.main? ? super() : snapshot.fetch(key) })
|
|
361
|
+
end
|
|
362
|
+
ActiveStorage.singleton_class.prepend(readers)
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
def self.prepare_framework!
|
|
366
|
+
CGI.singleton_class.prepend(UriUnescaping) unless CGI.singleton_class < UriUnescaping
|
|
367
|
+
Ractor.make_shareable(GlobalID.app) if defined?(GlobalID) && GlobalID.respond_to?(:app)
|
|
368
|
+
I18n.available_locales.each { |locale| I18n.transliterate('ractor', locale: locale) }
|
|
369
|
+
require 'i18n/ractorize'
|
|
370
|
+
require 'tempfile'
|
|
371
|
+
prepare_redirects!
|
|
372
|
+
%i[BASE36_ALPHABET BASE58_ALPHABET].each do |name|
|
|
373
|
+
Ractor.make_shareable(SecureRandom.const_get(name)) if SecureRandom.const_defined?(name)
|
|
374
|
+
end
|
|
375
|
+
uniqueness = ActiveRecord::Validations::UniquenessValidator
|
|
376
|
+
uniqueness.prepend(UniquenessCoverage) unless uniqueness < UniquenessCoverage
|
|
377
|
+
dependencies = ActiveSupport::Dependencies.singleton_class
|
|
378
|
+
dependencies.prepend(DependencyInterlock) unless dependencies < DependencyInterlock
|
|
379
|
+
prepare_storage!
|
|
380
|
+
Ractor.make_shareable(ActiveRecord::Associations::AssociationScope::INSTANCE)
|
|
381
|
+
Ractor.make_shareable(ActionDispatch::Http::Cache::Response::SPECIAL_KEYS)
|
|
382
|
+
freshness = ActionDispatch::Http::Cache::Request.singleton_class
|
|
383
|
+
freshness.prepend(CacheFreshness) unless freshness < CacheFreshness
|
|
384
|
+
fallbacks = I18n.fallbacks
|
|
385
|
+
I18n.available_locales.each { |locale| fallbacks[locale] }
|
|
386
|
+
@fallbacks = Ractor.make_shareable(fallbacks)
|
|
387
|
+
ActiveSupport::Digest.singleton_class.prepend(Digesting) unless ActiveSupport::Digest.singleton_class < Digesting
|
|
388
|
+
ActiveSupport::KeyGenerator.prepend(KeyGeneration) unless ActiveSupport::KeyGenerator < KeyGeneration
|
|
389
|
+
ActiveRecord::Base.connection_handler.connection_pool_list.each do |pool|
|
|
390
|
+
adapter = pool.db_config.adapter_class.singleton_class
|
|
391
|
+
adapter.prepend(AdapterQuoting) unless adapter < AdapterQuoting
|
|
392
|
+
end
|
|
393
|
+
rescue LoadError => e
|
|
394
|
+
raise LoadError,
|
|
395
|
+
"Ractor requests need the upstream I18n Ractor support (see gemfiles/rails_ractor.gemfile): #{e.message}"
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
def self.broadcast(name, payload)
|
|
399
|
+
if @prepared && !Ractor.main?
|
|
400
|
+
shared_name = Ractor.make_shareable(name, copy: true)
|
|
401
|
+
shared_payload = Ractor.make_shareable(payload, copy: true)
|
|
402
|
+
ActiveSupport::Ractors.on_main { ActionCable.server.broadcast(shared_name, shared_payload) }
|
|
403
|
+
else
|
|
404
|
+
ActionCable.server.broadcast(name, payload)
|
|
405
|
+
end
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
def self.prepare!
|
|
409
|
+
# A cache store can reference the whole middleware stack and freeze
|
|
410
|
+
# other mounted engines before their own hooks run. Warm every engine's
|
|
411
|
+
# request environment before making any of that graph shareable.
|
|
412
|
+
Rails.application.railties.each do |railtie|
|
|
413
|
+
railtie.env_config if railtie.is_a?(Rails::Engine)
|
|
414
|
+
end
|
|
415
|
+
Engine.instance.env_config
|
|
416
|
+
prepare_framework!
|
|
417
|
+
ActiveRecord::Base.descendants.each do |model|
|
|
418
|
+
next if model.abstract_class? || !model.table_exists?
|
|
419
|
+
|
|
420
|
+
model.reflect_on_all_associations.each { |reflection| reflection.send(:inverse_name) }
|
|
421
|
+
prepare_attributes(model)
|
|
422
|
+
Ractor.make_shareable(model.defined_enums)
|
|
423
|
+
model.defined_enums.each do |attribute, mapping|
|
|
424
|
+
method_name = attribute.pluralize
|
|
425
|
+
source = model.method(method_name).source_location&.first
|
|
426
|
+
next unless source&.end_with?('/active_record/enum.rb')
|
|
427
|
+
|
|
428
|
+
model.define_singleton_method(method_name, Ractor.shareable_proc { mapping })
|
|
429
|
+
end
|
|
430
|
+
model.define_attribute_methods
|
|
431
|
+
if model.respond_to?(:attachment_reflections)
|
|
432
|
+
model.attachment_reflections.each_value(&:named_variants)
|
|
433
|
+
Ractor.make_shareable(model.attachment_reflections)
|
|
434
|
+
end
|
|
435
|
+
end
|
|
436
|
+
ActionController::Base.descendants.each do |controller|
|
|
437
|
+
next unless controller.name&.start_with?('Livechat::')
|
|
438
|
+
|
|
439
|
+
controller.prepend(LiveThreads) if defined?(ActionController::Live) && controller < ActionController::Live
|
|
440
|
+
renderer_source = controller.instance_method(:_render_with_renderer_json).source_location&.first
|
|
441
|
+
if renderer_source&.end_with?('/action_controller/metal/renderers.rb') && !(controller < JsonRendering)
|
|
442
|
+
controller.prepend(JsonRendering)
|
|
443
|
+
end
|
|
444
|
+
Ractor.make_shareable(controller.controller_name)
|
|
445
|
+
controller.etaggers = controller.etaggers.map { |callback| Ractor.shareable_proc(&callback) }.freeze
|
|
446
|
+
end
|
|
447
|
+
Livechat.instance_variable_set(:@config, configuration(Livechat.config))
|
|
448
|
+
%i[javascript dashboard_javascript dashboard_stylesheet fingerprint
|
|
449
|
+
dashboard_fingerprint dashboard_stylesheet_fingerprint].each do |method|
|
|
450
|
+
next unless Widget.respond_to?(method)
|
|
451
|
+
|
|
452
|
+
Widget.public_send(method)
|
|
453
|
+
end
|
|
454
|
+
ActionController::Base.descendants.each do |controller|
|
|
455
|
+
next unless controller.respond_to?(:livechat_rate_limit_options)
|
|
456
|
+
|
|
457
|
+
options = controller.livechat_rate_limit_options
|
|
458
|
+
controller.livechat_rate_limit_options = share_options(options)
|
|
459
|
+
end
|
|
460
|
+
if Livechat.config.action_cable && defined?(ActionCable)
|
|
461
|
+
verifier = Livechat.stream_verifier
|
|
462
|
+
verifier.verify(verifier.generate('livechat:ractor:warmup'))
|
|
463
|
+
Livechat.instance_variable_set(:@stream_verifier, Ractor.make_shareable(verifier))
|
|
464
|
+
end
|
|
465
|
+
@prepared = true
|
|
466
|
+
Widget.instance_variables.each do |variable|
|
|
467
|
+
Widget.instance_variable_set(variable, Ractor.make_shareable(Widget.instance_variable_get(variable)))
|
|
468
|
+
end
|
|
469
|
+
end
|
|
470
|
+
|
|
471
|
+
# Rails edge's rate_limit currently closes over reassigned keyword
|
|
472
|
+
# arguments. A named filter avoids that unshareable closure; the actual
|
|
473
|
+
# counter, cache key and rejection still use Rails' rate_limiting method.
|
|
474
|
+
def self.rate_limit(controller, to:, within:, only:, with:, **options)
|
|
475
|
+
unless Rails::Application.method_defined?(:ractorize!)
|
|
476
|
+
return controller.rate_limit(to: to, within: within, only: only, with: with, **options)
|
|
477
|
+
end
|
|
478
|
+
|
|
479
|
+
controller.class_attribute :livechat_rate_limit_options, instance_accessor: false
|
|
480
|
+
controller.livechat_rate_limit_options = {
|
|
481
|
+
to: to, within: within, by: -> { request.remote_ip }, with: with,
|
|
482
|
+
store: controller.cache_store, name: nil, scope: controller.controller_path
|
|
483
|
+
}.merge(options.slice(:by, :store, :name, :scope))
|
|
484
|
+
controller.livechat_rate_limit_options[:scope] ||= controller.controller_path
|
|
485
|
+
filters = options.except(:by, :store, :name, :scope)
|
|
486
|
+
controller.before_action :livechat_rate_limit, only: only, **filters
|
|
487
|
+
controller.class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
|
488
|
+
private
|
|
489
|
+
def livechat_rate_limit
|
|
490
|
+
rate_limiting(**self.class.livechat_rate_limit_options)
|
|
491
|
+
end
|
|
492
|
+
RUBY
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
def self.share_options(options)
|
|
496
|
+
options.to_h do |key, value|
|
|
497
|
+
value = configuration_value(value) if value.is_a?(Proc)
|
|
498
|
+
[key, Ractor.make_shareable(value)]
|
|
499
|
+
end.freeze
|
|
500
|
+
end
|
|
501
|
+
|
|
502
|
+
def self.configuration_value(value)
|
|
503
|
+
case value
|
|
504
|
+
when Proc
|
|
505
|
+
return value if Ractor.shareable?(value)
|
|
506
|
+
|
|
507
|
+
# Like Rails callbacks, isolated callbacks use arguments and constants,
|
|
508
|
+
# not their initializer's implicit self. Keep lambda/proc arity intact.
|
|
509
|
+
value.lambda? ? Ractor.shareable_lambda(&value) : Ractor.shareable_proc(&value)
|
|
510
|
+
when Hash
|
|
511
|
+
value.to_h { |key, item| [configuration_value(key), configuration_value(item)] }.freeze
|
|
512
|
+
when Array
|
|
513
|
+
value.map { |item| configuration_value(item) }.freeze
|
|
514
|
+
else
|
|
515
|
+
Ractor.make_shareable(value, copy: true)
|
|
516
|
+
end
|
|
517
|
+
end
|
|
518
|
+
|
|
519
|
+
def self.configuration(original)
|
|
520
|
+
prepared = original.dup
|
|
521
|
+
original.instance_variables.each do |variable|
|
|
522
|
+
value = original.instance_variable_get(variable)
|
|
523
|
+
begin
|
|
524
|
+
shared = configuration_value(value)
|
|
525
|
+
prepared.instance_variable_set(variable, shared)
|
|
526
|
+
rescue Ractor::Error, TypeError => e
|
|
527
|
+
setting = variable.to_s.delete_prefix('@')
|
|
528
|
+
raise ArgumentError, "Livechat.config.#{setting} cannot be shared with Ractors: #{e.message}"
|
|
529
|
+
end
|
|
530
|
+
end
|
|
531
|
+
Ractor.make_shareable(prepared)
|
|
532
|
+
end
|
|
533
|
+
end
|
|
534
|
+
end
|
data/lib/livechat/version.rb
CHANGED
data/lib/livechat/widget.js
CHANGED
|
@@ -937,7 +937,8 @@
|
|
|
937
937
|
if (unreadCount > 0) {
|
|
938
938
|
if (!badge) {
|
|
939
939
|
badge = document.createElement("span");
|
|
940
|
-
|
|
940
|
+
var hostClass = openers[i].getAttribute("data-livechat-badge-class");
|
|
941
|
+
badge.className = "lvc-opener-badge" + (hostClass ? " " + hostClass : "");
|
|
941
942
|
openers[i].appendChild(badge);
|
|
942
943
|
}
|
|
943
944
|
badge.textContent = unreadCount > 9 ? "9+" : String(unreadCount);
|
|
@@ -1358,7 +1359,10 @@
|
|
|
1358
1359
|
"#lvc-root #lvc-form{margin-bottom:calc(10px + env(safe-area-inset-bottom))}" +
|
|
1359
1360
|
"}" +
|
|
1360
1361
|
// Lives in host DOM (on data-livechat-open elements), so no #lvc-root prefix.
|
|
1361
|
-
|
|
1362
|
+
// A host that names its own badge classes gets none of this: unlayered
|
|
1363
|
+
// rules would beat its layered utility classes whatever their specificity.
|
|
1364
|
+
// :where() keeps the old single-class weight for hosts that override it.
|
|
1365
|
+
":where([data-livechat-open]:not([data-livechat-badge-class]))>.lvc-opener-badge{display:inline-flex;min-width:18px;height:18px;padding:0 5px;" +
|
|
1362
1366
|
"margin-inline-start:6px;border-radius:999px;background:#dc2626;color:#fff;" +
|
|
1363
1367
|
"font-size:11px;font-weight:700;line-height:18px;align-items:center;" +
|
|
1364
1368
|
"justify-content:center;vertical-align:middle}";
|
data/lib/livechat/widget.rb
CHANGED
|
@@ -9,9 +9,9 @@ module Livechat
|
|
|
9
9
|
# Rails app regardless of its CSS or JS setup. It lives under lib/ (not
|
|
10
10
|
# app/assets/) so a host that runs an asset pipeline never ingests it.
|
|
11
11
|
module Widget
|
|
12
|
-
SOURCE = File.expand_path('widget.js', __dir__)
|
|
13
|
-
DASHBOARD_SOURCE = File.expand_path('dashboard.js', __dir__)
|
|
14
|
-
DASHBOARD_STYLESHEET_SOURCE = File.expand_path('dashboard.css', __dir__)
|
|
12
|
+
SOURCE = File.expand_path('widget.js', __dir__).freeze
|
|
13
|
+
DASHBOARD_SOURCE = File.expand_path('dashboard.js', __dir__).freeze
|
|
14
|
+
DASHBOARD_STYLESHEET_SOURCE = File.expand_path('dashboard.css', __dir__).freeze
|
|
15
15
|
|
|
16
16
|
# Right-to-left scripts, so the chat renders mirrored for those locales.
|
|
17
17
|
# Matched on the language subtag, so region variants ("ar-EG") count too.
|
|
@@ -136,7 +136,7 @@ module Livechat
|
|
|
136
136
|
emailPlaceholder: t(:email_placeholder, 'you@example.com'),
|
|
137
137
|
emailSave: t(:email_save, 'Save'),
|
|
138
138
|
emailSaved: t(:email_saved, 'We will also reply by email.'),
|
|
139
|
-
eventResolved: t(:event_resolved, '
|
|
139
|
+
eventResolved: t(:event_resolved, 'Resolved for now. Send another message whenever you need help.'),
|
|
140
140
|
eventReopened: t(:event_reopened, 'Conversation reopened'),
|
|
141
141
|
errorSend: t(:error_send, 'Could not send. Please try again.'),
|
|
142
142
|
unreadAria: t(:unread_aria, 'unread messages'),
|
data/lib/livechat.rb
CHANGED
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.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Yaroslav Shmarov
|
|
@@ -97,6 +97,10 @@ files:
|
|
|
97
97
|
- config/locales/livechat.vi.yml
|
|
98
98
|
- config/locales/livechat.zh-CN.yml
|
|
99
99
|
- config/routes.rb
|
|
100
|
+
- docs/ractors.md
|
|
101
|
+
- docs/screenshots/01-widget.png
|
|
102
|
+
- docs/screenshots/03-inbox.png
|
|
103
|
+
- docs/screenshots/04-conversation.png
|
|
100
104
|
- lib/generators/livechat/install/install_generator.rb
|
|
101
105
|
- lib/generators/livechat/install/templates/create_livechat_tables.rb.tt
|
|
102
106
|
- lib/generators/livechat/install/templates/initializer.rb
|
|
@@ -109,6 +113,7 @@ files:
|
|
|
109
113
|
- lib/livechat/dashboard.js
|
|
110
114
|
- lib/livechat/engine.rb
|
|
111
115
|
- lib/livechat/notifications.rb
|
|
116
|
+
- lib/livechat/ractor_support.rb
|
|
112
117
|
- lib/livechat/seeds.rb
|
|
113
118
|
- lib/livechat/version.rb
|
|
114
119
|
- lib/livechat/widget.js
|