collab 0.1.5 → 0.2.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/lib/collab.rb +2 -1
- data/lib/collab/bridge.rb +9 -9
- data/lib/collab/channel.rb +32 -0
- data/lib/collab/config.rb +1 -11
- data/lib/collab/models/document.rb +0 -2
- data/lib/collab/models/document_transaction.rb +2 -2
- data/lib/collab/version.rb +1 -1
- data/lib/generators/collab/install/install_generator.rb +2 -0
- data/lib/generators/collab/install/templates/channel.rb +19 -0
- data/lib/generators/collab/install/templates/create_collab_tables.rb +1 -1
- data/lib/generators/collab/install/templates/initializer.rb +5 -14
- metadata +4 -3
- data/app/channels/collab/document_channel.rb +0 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae9c092826099d0d81012ba20446f93518d9f998136d110ae71141bed2a58e22
|
4
|
+
data.tar.gz: e6de0fca8c103f77bdf6677fb0ab993a8740eae2baf7292f28cf601a5946f4b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a9db9beb2c46f7b4818fd494075ddb31ee68bcb137c3a347f71acf754efc3788afbaf2b67248b9e7b1a9bd80a780015a2a1db5a5778533823d32a83134550954
|
7
|
+
data.tar.gz: d65efb8027b52bf44c14e3a36829f66a845130dca3cffaf002b0ec0fe8cdeed2a137fd6e671aff6555c52ed03705b66e72f54fab5b2872aaa30d872f4f83d476
|
data/lib/collab.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require "collab/railtie"
|
2
|
+
|
1
3
|
module Collab
|
2
4
|
def self.config
|
3
5
|
@config ||= Collab::Config.new
|
@@ -7,7 +9,6 @@ module Collab
|
|
7
9
|
|
8
10
|
autoload "Config", "collab/config"
|
9
11
|
autoload "Bridge", "collab/bridge"
|
10
|
-
autoload "Railtie", "collab/railtie"
|
11
12
|
autoload "HasCollaborativeDocument", "collab/has_collaborative_document"
|
12
13
|
|
13
14
|
module Models
|
data/lib/collab/bridge.rb
CHANGED
@@ -23,7 +23,7 @@ module Collab
|
|
23
23
|
|
24
24
|
def initialize
|
25
25
|
@node = Dir.chdir(Rails.root) do
|
26
|
-
IO.popen(["node", "-e",
|
26
|
+
IO.popen(["node", "-e", "require('rails-collab-server')"], "r+")
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
@@ -31,24 +31,24 @@ module Collab
|
|
31
31
|
@current ||= new
|
32
32
|
end
|
33
33
|
|
34
|
-
def call(name, data = nil,
|
35
|
-
req = {name: name, data: data, schemaPackage: ::Collab.config.schema_package, schemaName:
|
34
|
+
def call(name, data = nil, schema_name:)
|
35
|
+
req = {name: name, data: data, schemaPackage: ::Collab.config.schema_package, schemaName: schema_name}
|
36
36
|
@node.puts(JSON.generate(req))
|
37
37
|
res = JSON.parse(@node.gets)
|
38
38
|
raise ::Collab::Bridge::JSRuntimeError.new(res["error"]) if res["error"]
|
39
39
|
res["result"]
|
40
40
|
end
|
41
41
|
|
42
|
-
def apply_transaction(document, transaction,
|
43
|
-
call("applyTransaction", {doc: document, data: transaction},
|
42
|
+
def apply_transaction(document, transaction, schema_name:)
|
43
|
+
call("applyTransaction", {doc: document, data: transaction}, schema_name: schema_name)
|
44
44
|
end
|
45
45
|
|
46
|
-
def html_to_document(html,
|
47
|
-
call("htmlToDoc", html,
|
46
|
+
def html_to_document(html, schema_name:)
|
47
|
+
call("htmlToDoc", html, schema_name: schema_name)
|
48
48
|
end
|
49
49
|
|
50
|
-
def document_to_html(document,
|
51
|
-
call("docToHtml", document,
|
50
|
+
def document_to_html(document, schema_name:)
|
51
|
+
call("docToHtml", document, schema_name: schema_name)
|
52
52
|
end
|
53
53
|
end
|
54
54
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Collab
|
2
|
+
module Channel
|
3
|
+
def document; @document end
|
4
|
+
|
5
|
+
def subscribed
|
6
|
+
@document = find_document
|
7
|
+
|
8
|
+
starting_version = params[:startingVersion]&.to_i
|
9
|
+
raise "missing startingVersion" if starting_version.nil?
|
10
|
+
|
11
|
+
stream_for document
|
12
|
+
|
13
|
+
transactions = document.transactions
|
14
|
+
.where("document_version > ?", starting_version)
|
15
|
+
.order(document_version: :asc)
|
16
|
+
.load
|
17
|
+
|
18
|
+
raise "invalid version" unless transactions.first.document_version == (starting_version + 1) unless transactions.empty?
|
19
|
+
|
20
|
+
transactions.lazy.map(&:as_json).each(method(:transmit))
|
21
|
+
end
|
22
|
+
|
23
|
+
def submit(data)
|
24
|
+
authorize_submit!(data)
|
25
|
+
document.perform_transaction_later(data)
|
26
|
+
end
|
27
|
+
|
28
|
+
def unsubscribed
|
29
|
+
stop_all_streams # this may not be needed
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/collab/config.rb
CHANGED
@@ -1,15 +1,5 @@
|
|
1
1
|
module Collab
|
2
2
|
class Config
|
3
|
-
attr_accessor :max_transactions, :application_job, :queue_document_transaction_job_as, :schema_package, :application_record, :document_transaction_model, :document_model
|
4
|
-
|
5
|
-
def find_document_for_subscribe(&block)
|
6
|
-
@find_document_for_subscribe unless block
|
7
|
-
@find_document_for_subscribe = block
|
8
|
-
end
|
9
|
-
|
10
|
-
def authorize_update_document(&block)
|
11
|
-
@authorize_update_document unless block
|
12
|
-
@authorize_update_document = block
|
13
|
-
end
|
3
|
+
attr_accessor :max_transactions, :application_job, :queue_document_transaction_job_as, :schema_package, :application_record, :document_transaction_model, :document_model, :channel_name
|
14
4
|
end
|
15
5
|
end
|
data/lib/collab/version.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
class CollabDocumentChannel < ApplicationCable::Channel
|
2
|
+
include Collab::Channel
|
3
|
+
|
4
|
+
private
|
5
|
+
|
6
|
+
# Find the document to subscribe to based on the params passed to the channel
|
7
|
+
# Authorization may also be performed here (raise an error)
|
8
|
+
def find_document
|
9
|
+
Collab::Models::Document.find(params[:document_id]).tap do |document|
|
10
|
+
raise "authorization failed"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# Called when a client submits a transaction in order to update a document
|
15
|
+
# You should throw an error if unauthorized
|
16
|
+
def authorize_submit!
|
17
|
+
raise "authorization failed"
|
18
|
+
end
|
19
|
+
end
|
@@ -17,7 +17,7 @@ class CreateCollabTables < ActiveRecord::Migration[6.0]
|
|
17
17
|
end
|
18
18
|
|
19
19
|
create_table :collab_document_transactions, id: false do |t|
|
20
|
-
t.references :document, null: false, foreign_key: {to_table: :
|
20
|
+
t.references :document, null: false, foreign_key: {to_table: :collab_documents}, type: :uuid, index: false
|
21
21
|
|
22
22
|
t.jsonb :steps, array: true, null: false
|
23
23
|
t.integer :document_version, null: false
|
@@ -6,20 +6,11 @@ Collab.config do |c|
|
|
6
6
|
# How many old transactions to keep per document
|
7
7
|
c.max_transactions = 250
|
8
8
|
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
|
14
|
-
c.find_document_for_subscribe do
|
15
|
-
Collab::Models::Document.find params[:document_id]
|
16
|
-
end
|
17
|
-
# Called when a client submits a transaction in order to update a document
|
18
|
-
# You should throw an error if unauthorized
|
19
|
-
# The block is executed in the instance of the channel
|
20
|
-
c.authorize_update_document do |document, transaction_data|
|
21
|
-
# raise "authorization failed"
|
22
|
-
end
|
9
|
+
# ActionCable settings
|
10
|
+
# ====================
|
11
|
+
# The document channel to use for collaboration
|
12
|
+
# If you change this, you must pass {channel: "[ChannelName]"} as subscription params to the Javascript client
|
13
|
+
c.channel_name = "::CollabDocumentChannel"
|
23
14
|
|
24
15
|
# ActionJob settings
|
25
16
|
# ==================
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: collab
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Aubin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-07-
|
11
|
+
date: 2020-07-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -52,10 +52,10 @@ extensions: []
|
|
52
52
|
extra_rdoc_files: []
|
53
53
|
files:
|
54
54
|
- Rakefile
|
55
|
-
- app/channels/collab/document_channel.rb
|
56
55
|
- app/jobs/collab/document_transaction_job.rb
|
57
56
|
- lib/collab.rb
|
58
57
|
- lib/collab/bridge.rb
|
58
|
+
- lib/collab/channel.rb
|
59
59
|
- lib/collab/config.rb
|
60
60
|
- lib/collab/has_collaborative_document.rb
|
61
61
|
- lib/collab/models/base.rb
|
@@ -64,6 +64,7 @@ files:
|
|
64
64
|
- lib/collab/railtie.rb
|
65
65
|
- lib/collab/version.rb
|
66
66
|
- lib/generators/collab/install/install_generator.rb
|
67
|
+
- lib/generators/collab/install/templates/channel.rb
|
67
68
|
- lib/generators/collab/install/templates/create_collab_tables.rb
|
68
69
|
- lib/generators/collab/install/templates/initializer.rb
|
69
70
|
- lib/tasks/collab_tasks.rake
|
@@ -1,28 +0,0 @@
|
|
1
|
-
class Collab::DocumentChannel < ApplicationCable::Channel
|
2
|
-
def subscribed
|
3
|
-
@document = instance_exec(::Collab.config.find_document_for_subscribe)
|
4
|
-
|
5
|
-
starting_version = params[:startingVersion]&.to_i
|
6
|
-
raise "missing startingVersion" if starting_version.nil?
|
7
|
-
|
8
|
-
stream_for @document
|
9
|
-
|
10
|
-
transactions = @document.transactions
|
11
|
-
.where("document_version > ?", starting_version)
|
12
|
-
.order(document_version: :asc)
|
13
|
-
.load
|
14
|
-
|
15
|
-
raise "invalid version" unless transactions.first.document_version == (starting_version + 1) unless transactions.empty?
|
16
|
-
|
17
|
-
transactions.lazy.map(&:as_json).each(method(:transmit))
|
18
|
-
end
|
19
|
-
|
20
|
-
def submit(data)
|
21
|
-
instance_exec(@document, data, &::Collab.config.authorize_update_document)
|
22
|
-
@document.perform_transaction_later(data)
|
23
|
-
end
|
24
|
-
|
25
|
-
def unsubscribed
|
26
|
-
stop_all_streams # this may not be needed
|
27
|
-
end
|
28
|
-
end
|