message_bus 2.2.4 → 3.0.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.
Potentially problematic release.
This version of message_bus might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.rubocop.yml +9 -1
- data/.travis.yml +1 -1
- data/CHANGELOG +7 -0
- data/Gemfile +6 -3
- data/README.md +32 -8
- data/lib/message_bus/client.rb +19 -8
- data/lib/message_bus/diagnostics.rb +0 -1
- data/lib/message_bus/rack/diagnostics.rb +3 -2
- data/lib/message_bus/version.rb +1 -1
- data/spec/lib/message_bus/client_spec.rb +173 -23
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: caef3d7766089e6196e8a42f2829edf6aebbd7bf70ef38210db050007edcd865
|
4
|
+
data.tar.gz: da3fd2dd2e2111802ddeffb4d8a0981f62fb4fdd8fd3fe249a36acbbd66298a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a3e6b63fe2448577ab4e099ba0135130b14731f017f669f28fa720e00412e14997dca92091ab5b84faaefa3443aa87f8cc0159d21b454199d287156e267c998
|
7
|
+
data.tar.gz: fb65941d890beb25a2a0a2e743d9d2176a2ec7f7d26609ccab4aacc3aefc6f783c5de5b498c69fedb4c774e329506d638038dc21cde77107b6bade4efd90aa76
|
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
data/CHANGELOG
CHANGED
data/Gemfile
CHANGED
@@ -20,8 +20,11 @@ group :test, :development do
|
|
20
20
|
gem 'byebug'
|
21
21
|
end
|
22
22
|
|
23
|
+
group :development do
|
24
|
+
gem 'yard'
|
25
|
+
gem 'rubocop-discourse', require: false
|
26
|
+
gem 'rubocop-rspec', require: false
|
27
|
+
end
|
28
|
+
|
23
29
|
gem 'rack'
|
24
30
|
gem 'concurrent-ruby' # for distributed-cache
|
25
|
-
|
26
|
-
gem 'rubocop'
|
27
|
-
gem 'yard'
|
data/README.md
CHANGED
@@ -74,32 +74,40 @@ id = MessageBus.last_id("/channel")
|
|
74
74
|
MessageBus.backlog "/channel", id
|
75
75
|
```
|
76
76
|
|
77
|
-
|
78
|
-
# messages can be targetted at particular users or groups
|
79
|
-
MessageBus.publish "/channel", "hello", user_ids: [1,2,3], group_ids: [4,5,6]
|
77
|
+
### Targetted messages
|
80
78
|
|
81
|
-
|
82
|
-
MessageBus.publish "/channel", "hello", client_ids: ["XXX","YYY"]
|
79
|
+
Messages can be targetted to particular clients by supplying the `client_ids` option when publishing a message.
|
83
80
|
|
84
|
-
|
81
|
+
```ruby
|
82
|
+
MessageBus.publish "/channel", "hello", client_ids: ["XXX", "YYY"] # (using MessageBus.clientId)
|
83
|
+
```
|
85
84
|
|
85
|
+
By configuring the `user_id_lookup` and `group_ids_lookup` options with a Proc or Lambda which will be called with a [Rack specification environment](https://github.com/rack/rack/blob/master/SPEC.rdoc#the-environment-), messages can be targetted to particular clients users or groups by supplying either the `user_ids` or `group_ids` options when publishing a message.
|
86
|
+
|
87
|
+
```ruby
|
86
88
|
MessageBus.configure(user_id_lookup: proc do |env|
|
87
89
|
# this lookup occurs on JS-client poolings, so that server can retrieve backlog
|
88
90
|
# for the client considering/matching/filtering user_ids set on published messages
|
89
91
|
# if user_id is not set on publish time, any user_id returned here will receive the message
|
90
|
-
|
91
92
|
# return the user id here
|
92
93
|
end)
|
93
94
|
|
95
|
+
# Target user_ids when publishing a message
|
96
|
+
MessageBus.publish "/channel", "hello", user_ids: [1, 2, 3]
|
97
|
+
|
94
98
|
MessageBus.configure(group_ids_lookup: proc do |env|
|
95
99
|
# return the group ids the user belongs to
|
96
100
|
# can be nil or []
|
97
101
|
end)
|
98
102
|
|
99
|
-
#
|
103
|
+
# Target group_ids when publishing a message
|
104
|
+
MessageBus.publish "/channel", "hello", group_ids: [1, 2, 3]
|
105
|
+
|
106
|
+
# example of MessageBus to set user_ids from an initializer in Rails and Devise:
|
100
107
|
# config/inializers/message_bus.rb
|
101
108
|
MessageBus.user_id_lookup do |env|
|
102
109
|
req = Rack::Request.new(env)
|
110
|
+
|
103
111
|
if req.session && req.session["warden.user.user.key"] && req.session["warden.user.user.key"][0][0]
|
104
112
|
user = User.find(req.session["warden.user.user.key"][0][0])
|
105
113
|
user.id
|
@@ -107,6 +115,22 @@ MessageBus.user_id_lookup do |env|
|
|
107
115
|
end
|
108
116
|
```
|
109
117
|
|
118
|
+
If both `user_ids` and `group_ids` options are supplied when publishing a message, the message will be targetted at clients with lookup return values that matches on either the `user_ids` **or** the `group_ids` options.
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
MessageBus.publish "/channel", "hello", user_ids: [1, 2, 3], group_ids: [1, 2, 3]
|
122
|
+
```
|
123
|
+
|
124
|
+
If the `client_ids` option is supplied with either the `user_ids` or `group_ids` options when publising a message, the `client_ids` option will be applied unconditionally and messages will be filtered further using `user_id` or `group_id` clauses.
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
MessageBus.publish "/channel", "hello", client_ids: ["XXX", "YYY"], user_ids: [1, 2, 3], group_ids: [1, 2, 3]
|
128
|
+
```
|
129
|
+
|
130
|
+
Passing `nil` or `[]` to either `client_ids`, `user_ids` or `group_ids` is equivalent to allowing all values on each option.
|
131
|
+
|
132
|
+
### Error handling
|
133
|
+
|
110
134
|
```ruby
|
111
135
|
MessageBus.configure(on_middleware_error: proc do |env, e|
|
112
136
|
# If you wish to add special handling based on error
|
data/lib/message_bus/client.rb
CHANGED
@@ -128,15 +128,26 @@ class MessageBus::Client
|
|
128
128
|
# @return [Boolean] whether or not the client has permission to receive the
|
129
129
|
# passed message
|
130
130
|
def allowed?(msg)
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
131
|
+
client_allowed = !msg.client_ids || msg.client_ids.length == 0 || msg.client_ids.include?(self.client_id)
|
132
|
+
|
133
|
+
user_allowed = false
|
134
|
+
group_allowed = false
|
135
|
+
|
136
|
+
# this is an inconsistency we should fix anyway, publishing `user_ids: nil` should work same as groups
|
137
|
+
has_users = msg.user_ids && msg.user_ids.length > 0
|
138
|
+
has_groups = msg.group_ids && msg.group_ids.length > 0
|
139
|
+
|
140
|
+
if has_users
|
141
|
+
user_allowed = msg.user_ids.include?(self.user_id)
|
142
|
+
end
|
143
|
+
|
144
|
+
if has_groups
|
145
|
+
group_allowed = (
|
146
|
+
msg.group_ids - (self.group_ids || [])
|
138
147
|
).length < msg.group_ids.length
|
139
|
-
|
148
|
+
end
|
149
|
+
|
150
|
+
client_allowed && (user_allowed || group_allowed || (!has_users && !has_groups))
|
140
151
|
end
|
141
152
|
|
142
153
|
# @return [Array<MessageBus::Message>] the set of messages the client is due
|
@@ -45,7 +45,7 @@ class MessageBus::Rack::Diagnostics
|
|
45
45
|
return [200, { 'Content-Type' => 'application/javascript;charset=UTF-8' }, [content]]
|
46
46
|
end
|
47
47
|
|
48
|
-
|
48
|
+
[404, {}, ['not found']]
|
49
49
|
end
|
50
50
|
|
51
51
|
private
|
@@ -92,6 +92,7 @@ class MessageBus::Rack::Diagnostics
|
|
92
92
|
</body>
|
93
93
|
</html>
|
94
94
|
HTML
|
95
|
-
|
95
|
+
|
96
|
+
[200, { "content-type" => "text/html;" }, [html]]
|
96
97
|
end
|
97
98
|
end
|
data/lib/message_bus/version.rb
CHANGED
@@ -169,37 +169,187 @@ describe MessageBus::Client do
|
|
169
169
|
log[0].data.must_equal 'world'
|
170
170
|
end
|
171
171
|
|
172
|
-
|
173
|
-
|
174
|
-
@message.client_ids = ["1", "2"]
|
175
|
-
@client.client_id = "2"
|
176
|
-
@client.allowed?(@message).must_equal true
|
177
|
-
|
178
|
-
@client.client_id = "3"
|
179
|
-
@client.allowed?(@message).must_equal false
|
180
|
-
end
|
181
|
-
|
182
|
-
describe "targetted at group" do
|
183
|
-
before do
|
172
|
+
describe '#allowed?' do
|
173
|
+
it "allows only client_id in list if message contains client_ids" do
|
184
174
|
@message = MessageBus::Message.new(1, 2, '/test', 'hello')
|
185
|
-
@message.
|
186
|
-
|
175
|
+
@message.client_ids = ["1", "2"]
|
176
|
+
@client.client_id = "2"
|
177
|
+
@client.allowed?(@message).must_equal true
|
187
178
|
|
188
|
-
|
189
|
-
@client.group_ids = [77, 0, 10]
|
179
|
+
@client.client_id = "3"
|
190
180
|
@client.allowed?(@message).must_equal false
|
191
|
-
end
|
192
181
|
|
193
|
-
|
194
|
-
|
182
|
+
@message.client_ids = []
|
183
|
+
|
184
|
+
@client.client_id = "3"
|
195
185
|
@client.allowed?(@message).must_equal true
|
196
|
-
end
|
197
186
|
|
198
|
-
|
199
|
-
|
200
|
-
@client.
|
187
|
+
@message.client_ids = nil
|
188
|
+
|
189
|
+
@client.client_id = "3"
|
201
190
|
@client.allowed?(@message).must_equal true
|
202
191
|
end
|
192
|
+
|
193
|
+
describe 'targetted at user' do
|
194
|
+
before do
|
195
|
+
@message = MessageBus::Message.new(1, 2, '/test', 'hello')
|
196
|
+
@message.user_ids = [1, 2, 3]
|
197
|
+
end
|
198
|
+
|
199
|
+
it "allows client with user_id that is included in message's user_ids" do
|
200
|
+
@client.user_id = 1
|
201
|
+
@client.allowed?(@message).must_equal(true)
|
202
|
+
end
|
203
|
+
|
204
|
+
it "denies client with user_id that is not included in message's user_ids" do
|
205
|
+
@client.user_id = 4
|
206
|
+
@client.allowed?(@message).must_equal(false)
|
207
|
+
end
|
208
|
+
|
209
|
+
it "denies client with nil user_id" do
|
210
|
+
@client.user_id = nil
|
211
|
+
|
212
|
+
@client.allowed?(@message).must_equal(false)
|
213
|
+
end
|
214
|
+
|
215
|
+
it "allows client if message's user_ids is not set" do
|
216
|
+
@message.user_ids = nil
|
217
|
+
@client.user_id = 4
|
218
|
+
@client.allowed?(@message).must_equal(true)
|
219
|
+
end
|
220
|
+
|
221
|
+
it "allows client if message's user_ids is empty" do
|
222
|
+
@message.user_ids = []
|
223
|
+
@client.user_id = 4
|
224
|
+
@client.allowed?(@message).must_equal(true)
|
225
|
+
end
|
226
|
+
|
227
|
+
it "allows client with client_id that is included in message's client_ids" do
|
228
|
+
@message.client_ids = ["1", "2"]
|
229
|
+
@client.client_id = "1"
|
230
|
+
@client.user_id = 1
|
231
|
+
|
232
|
+
@client.allowed?(@message).must_equal(true)
|
233
|
+
end
|
234
|
+
|
235
|
+
it "denies client with client_id that is not included in message's client_ids" do
|
236
|
+
@message.client_ids = ["1", "2"]
|
237
|
+
@client.client_id = "3"
|
238
|
+
@client.user_id = 1
|
239
|
+
|
240
|
+
@client.allowed?(@message).must_equal(false)
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
describe "targetted at group" do
|
245
|
+
before do
|
246
|
+
@message = MessageBus::Message.new(1, 2, '/test', 'hello')
|
247
|
+
@message.group_ids = [1, 2, 3]
|
248
|
+
end
|
249
|
+
|
250
|
+
it "denies client that are not members of group" do
|
251
|
+
@client.group_ids = [77, 0, 10]
|
252
|
+
@client.allowed?(@message).must_equal false
|
253
|
+
end
|
254
|
+
|
255
|
+
it 'denies client with nil group_ids' do
|
256
|
+
@client.group_ids = nil
|
257
|
+
@client.allowed?(@message).must_equal false
|
258
|
+
end
|
259
|
+
|
260
|
+
it "allows client that are members of group" do
|
261
|
+
@client.group_ids = [1, 2, 3]
|
262
|
+
@client.allowed?(@message).must_equal true
|
263
|
+
end
|
264
|
+
|
265
|
+
it "allows any client if message's group_ids is not set" do
|
266
|
+
@message.group_ids = nil
|
267
|
+
@client.group_ids = [77, 0, 10]
|
268
|
+
@client.allowed?(@message).must_equal true
|
269
|
+
end
|
270
|
+
|
271
|
+
it "allows any client if message's group_ids is empty" do
|
272
|
+
@message.group_ids = []
|
273
|
+
@client.group_ids = [77, 0, 10]
|
274
|
+
@client.allowed?(@message).must_equal true
|
275
|
+
end
|
276
|
+
|
277
|
+
it "allows client with client_id that is included in message's client_ids" do
|
278
|
+
@message.client_ids = ["1", "2"]
|
279
|
+
@client.client_id = "1"
|
280
|
+
@client.group_ids = [1]
|
281
|
+
|
282
|
+
@client.allowed?(@message).must_equal(true)
|
283
|
+
end
|
284
|
+
|
285
|
+
it "denies client with client_id that is not included in message's client_ids" do
|
286
|
+
@message.client_ids = ["1", "2"]
|
287
|
+
@client.client_id = "3"
|
288
|
+
@client.group_ids = [1]
|
289
|
+
|
290
|
+
@client.allowed?(@message).must_equal(false)
|
291
|
+
end
|
292
|
+
end
|
293
|
+
|
294
|
+
describe 'targetted at group and user' do
|
295
|
+
before do
|
296
|
+
@message = MessageBus::Message.new(1, 2, '/test', 'hello')
|
297
|
+
@message.group_ids = [1, 2, 3]
|
298
|
+
@message.user_ids = [4, 5, 6]
|
299
|
+
end
|
300
|
+
|
301
|
+
it "allows client with user_id that is included in message's user_ids" do
|
302
|
+
@client.user_id = 4
|
303
|
+
@client.allowed?(@message).must_equal(true)
|
304
|
+
end
|
305
|
+
|
306
|
+
it "denies client with user_id that is not included in message's user_ids" do
|
307
|
+
@client.user_id = 1
|
308
|
+
@client.allowed?(@message).must_equal(false)
|
309
|
+
end
|
310
|
+
|
311
|
+
it "allows client with group_ids that is included in message's group_ids" do
|
312
|
+
@client.group_ids = [1, 0, 3]
|
313
|
+
@client.allowed?(@message).must_equal(true)
|
314
|
+
end
|
315
|
+
|
316
|
+
it "denies client with group_ids that is not included in message's group_ids" do
|
317
|
+
@client.group_ids = [8, 9, 10]
|
318
|
+
@client.allowed?(@message).must_equal(false)
|
319
|
+
end
|
320
|
+
|
321
|
+
it "allows client with allowed client_id and user_id" do
|
322
|
+
@message.client_ids = ["1", "2"]
|
323
|
+
@client.user_id = 4
|
324
|
+
@client.client_id = "2"
|
325
|
+
|
326
|
+
@client.allowed?(@message).must_equal(true)
|
327
|
+
end
|
328
|
+
|
329
|
+
it "denies client with allowed client_id but disallowed user_id" do
|
330
|
+
@message.client_ids = ["1", "2"]
|
331
|
+
@client.user_id = 99
|
332
|
+
@client.client_id = "2"
|
333
|
+
|
334
|
+
@client.allowed?(@message).must_equal(false)
|
335
|
+
end
|
336
|
+
|
337
|
+
it "allows client with allowed client_id and group_id" do
|
338
|
+
@message.client_ids = ["1", "2"]
|
339
|
+
@client.group_ids = [1]
|
340
|
+
@client.client_id = "2"
|
341
|
+
|
342
|
+
@client.allowed?(@message).must_equal(true)
|
343
|
+
end
|
344
|
+
|
345
|
+
it "denies client with allowed client_id but disallowed group_id" do
|
346
|
+
@message.client_ids = ["1", "2"]
|
347
|
+
@client.group_ids = [99]
|
348
|
+
@client.client_id = "2"
|
349
|
+
|
350
|
+
@client.allowed?(@message).must_equal(false)
|
351
|
+
end
|
352
|
+
end
|
203
353
|
end
|
204
354
|
end
|
205
355
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: message_bus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Saffron
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-04-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|