message_bus 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of message_bus might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: caef3d7766089e6196e8a42f2829edf6aebbd7bf70ef38210db050007edcd865
4
- data.tar.gz: da3fd2dd2e2111802ddeffb4d8a0981f62fb4fdd8fd3fe249a36acbbd66298a1
3
+ metadata.gz: fe33cef07b7712b1bbcc2ab444e123fd1121665a5ecf1db7fb24dba190e48b38
4
+ data.tar.gz: 325300ad20aeb221d468326247c7755022dc8e594e8de1311601dec89b4a2c2b
5
5
  SHA512:
6
- metadata.gz: 6a3e6b63fe2448577ab4e099ba0135130b14731f017f669f28fa720e00412e14997dca92091ab5b84faaefa3443aa87f8cc0159d21b454199d287156e267c998
7
- data.tar.gz: fb65941d890beb25a2a0a2e743d9d2176a2ec7f7d26609ccab4aacc3aefc6f783c5de5b498c69fedb4c774e329506d638038dc21cde77107b6bade4efd90aa76
6
+ metadata.gz: 216a6156aad68865beb14a519bda4f709e10d96bcc8c58fa4bc042ef596fd7563926517f5b5c82d547f4ff26ce7bd3ac0a74998ce9cf7b4b2d66c792f049c602
7
+ data.tar.gz: fb521cf6b6928586f7ba1f2da19a8def8d3f77acc0d6cb1c417fe03d8a36f0af8a470c29cc26ce400c12ba8648223ded2de43109a4616b5c89b967ed4e6a67aa
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ 28-04-2020
2
+
3
+ - Version 3.1.0
4
+
5
+ - FEATURE: `MessageBus#register_client_message_filter` to register a custom filter so that messages can be inspected and filtered away from clients.
6
+
1
7
  27-04-2020
2
8
 
3
9
  - Version 3.0.0
data/README.md CHANGED
@@ -129,6 +129,20 @@ MessageBus.publish "/channel", "hello", client_ids: ["XXX", "YYY"], user_ids: [1
129
129
 
130
130
  Passing `nil` or `[]` to either `client_ids`, `user_ids` or `group_ids` is equivalent to allowing all values on each option.
131
131
 
132
+ ### Filtering Client Messages
133
+
134
+ Custom client message filters can be registered via `MessageBus#register_client_message_filter`. This can be useful for filtering away messages from the client based on the message's payload.
135
+
136
+ For example, ensuring that only messages seen by the server in the last 20 seconds are published to the client:
137
+
138
+ ```
139
+ MessageBus.register_client_message_filter('/test') do |message|
140
+ (Time.now.to_i - message.data[:published_at]) <= 20
141
+ end
142
+
143
+ MessageBus.publish('/test/5', { data: "somedata", published_at: Time.now.to_i })
144
+ ```
145
+
132
146
  ### Error handling
133
147
 
134
148
  ```ruby
@@ -554,6 +554,26 @@ module MessageBus::Implementation
554
554
  @config[:keepalive_interval] || 60
555
555
  end
556
556
 
557
+ # Registers a client message filter that allows messages to be filtered from the client.
558
+ #
559
+ # @param [String,Regexp] channel_prefix channel prefix to match against a message's channel
560
+ #
561
+ # @yieldparam [MessageBus::Message] message published to the channel that matched the prefix provided
562
+ # @yieldreturn [Boolean] whether the message should be published to the client
563
+ # @return [void]
564
+ def register_client_message_filter(channel_prefix, &blk)
565
+ if blk
566
+ configure(client_message_filters: []) if !@config[:client_message_filters]
567
+ @config[:client_message_filters] << [channel_prefix, blk]
568
+ end
569
+ end
570
+
571
+ # @return [Array] returns a hash of message filters that have been registered
572
+ def client_message_filters
573
+ configure(client_message_filters: []) if !@config[:client_message_filters]
574
+ @config[:client_message_filters]
575
+ end
576
+
557
577
  private
558
578
 
559
579
  ENCODE_SITE_TOKEN = "$|$"
@@ -147,7 +147,24 @@ class MessageBus::Client
147
147
  ).length < msg.group_ids.length
148
148
  end
149
149
 
150
- client_allowed && (user_allowed || group_allowed || (!has_users && !has_groups))
150
+ has_permission = client_allowed && (user_allowed || group_allowed || (!has_users && !has_groups))
151
+
152
+ return has_permission if !has_permission
153
+
154
+ filters_allowed = true
155
+
156
+ len = @bus.client_message_filters.length
157
+ while len > 0
158
+ len -= 1
159
+ channel_prefix, blk = @bus.client_message_filters[len]
160
+
161
+ if msg.channel.start_with?(channel_prefix)
162
+ filters_allowed = blk.call(msg)
163
+ break if !filters_allowed
164
+ end
165
+ end
166
+
167
+ filters_allowed
151
168
  end
152
169
 
153
170
  # @return [Array<MessageBus::Message>] the set of messages the client is due
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MessageBus
4
- VERSION = "3.0.0"
4
+ VERSION = "3.1.0"
5
5
  end
@@ -350,6 +350,39 @@ describe MessageBus::Client do
350
350
  @client.allowed?(@message).must_equal(false)
351
351
  end
352
352
  end
353
+
354
+ describe 'when MessageBus#client_message_filters has been configured' do
355
+
356
+ it 'filters messages correctly' do
357
+ message = MessageBus::Message.new(1, 2, '/test/5', 'hello')
358
+ @client.allowed?(message).must_equal(true)
359
+
360
+ @bus.register_client_message_filter('/test') do |m|
361
+ m.data != 'hello'
362
+ end
363
+
364
+ @client.allowed?(message).must_equal(false)
365
+ end
366
+
367
+ it 'filters messages correctly when multiple filters have been configured' do
368
+
369
+ bob_message = MessageBus::Message.new(1, 2, '/test/5', 'bob')
370
+ fred_message = MessageBus::Message.new(1, 2, '/test/5', 'fred')
371
+ random_message = MessageBus::Message.new(1, 2, '/test/77', 'random')
372
+
373
+ @bus.register_client_message_filter('/test') do |message|
374
+ message.data == 'bob' || message.data == 'fred'
375
+ end
376
+
377
+ @bus.register_client_message_filter('/test') do |message|
378
+ message.data == 'fred'
379
+ end
380
+
381
+ @client.allowed?(fred_message).must_equal(true)
382
+ @client.allowed?(bob_message).must_equal(false)
383
+ @client.allowed?(random_message).must_equal(false)
384
+ end
385
+ end
353
386
  end
354
387
  end
355
388
  end
@@ -292,4 +292,19 @@ describe MessageBus do
292
292
 
293
293
  data.must_equal(["pre-fork", "from-fork", "continuation"])
294
294
  end
295
+
296
+ describe '#register_client_message_filter' do
297
+ it 'should register the message filter correctly' do
298
+ @bus.register_client_message_filter('/test')
299
+
300
+ @bus.client_message_filters.must_equal([])
301
+
302
+ @bus.register_client_message_filter('/test') { puts "hello world" }
303
+
304
+ channel, blk = @bus.client_message_filters[0]
305
+
306
+ blk.must_respond_to(:call)
307
+ channel.must_equal('/test')
308
+ end
309
+ end
295
310
  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: 3.0.0
4
+ version: 3.1.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-04-27 00:00:00.000000000 Z
11
+ date: 2020-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack