Skip to content

Commit 152f5d8

Browse files
authored
Add blocklist (#27)
1 parent 677502a commit 152f5d8

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

lib/stream-chat/client.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def list_channel_types
202202
end
203203

204204
def update_channel_type(channel_type, **options)
205-
put("channeltypes/#{channel_type}", **options)
205+
put("channeltypes/#{channel_type}", data: options)
206206
end
207207

208208
def delete_channel_type(channel_type)
@@ -242,6 +242,26 @@ def verify_webhook(request_body, x_signature)
242242
signature == x_signature
243243
end
244244

245+
def list_blocklists
246+
get('blocklists')
247+
end
248+
249+
def get_blocklist(name)
250+
get("blocklists/#{name}")
251+
end
252+
253+
def create_blocklist(name, words)
254+
post('blocklists', data: { "name": name, "words": words })
255+
end
256+
257+
def update_blocklist(name, words)
258+
put("blocklists/#{name}", data: { "words": words })
259+
end
260+
261+
def delete_blocklist(name)
262+
delete("blocklists/#{name}")
263+
end
264+
245265
def put(relative_url, params: nil, data: nil)
246266
make_http_request(:put, relative_url, params: params, data: data)
247267
end

spec/client_spec.rb

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,93 @@
217217
resp = @client.search({ members: { '$in' => ['legolas'] } }, text)
218218
expect(resp['results'].length).to eq(1)
219219
end
220+
221+
it 'list available blocklists' do
222+
resp = @client.list_blocklists
223+
expect(resp['blocklists'].length).to eq 1
224+
end
225+
226+
it 'get blocklist profanity_en_2020_v1' do
227+
resp = @client.get_blocklist('profanity_en_2020_v1')
228+
expect(resp['blocklist']['name']).to eq 'profanity_en_2020_v1'
229+
end
230+
231+
it 'create a new blocklist' do
232+
@client.create_blocklist('no-cakes', %w[fudge cream sugar])
233+
end
234+
235+
it 'list available blocklists' do
236+
resp = @client.list_blocklists
237+
expect(resp['blocklists'].length).to eq 2
238+
end
239+
240+
it 'get blocklist info' do
241+
resp = @client.get_blocklist('no-cakes')
242+
expect(resp['blocklist']['name']).to eq 'no-cakes'
243+
expect(resp['blocklist']['words']).to eq %w[fudge cream sugar]
244+
end
245+
246+
it 'update a default blocklist should fail' do
247+
expect do
248+
@client.update_blocklist('profanity_en_2020_v1', %w[fudge cream sugar vanilla])
249+
end.to raise_error(/cannot update the builtin block list/)
250+
end
251+
252+
it 'update blocklist' do
253+
@client.update_blocklist('no-cakes', %w[fudge cream sugar vanilla])
254+
end
255+
256+
it 'get blocklist info again' do
257+
resp = @client.get_blocklist('no-cakes')
258+
expect(resp['blocklist']['name']).to eq 'no-cakes'
259+
expect(resp['blocklist']['words']).to eq %w[fudge cream sugar vanilla]
260+
end
261+
262+
it 'use the blocklist for a channel type' do
263+
@client.update_channel_type('messaging', blocklist: 'no-cakes', blocklist_behavior: 'block')
264+
end
265+
266+
it 'should block messages that match the blocklist' do
267+
channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { 'test' => true, 'language' => 'ruby' })
268+
channel.create(@random_user[:id])
269+
resp = channel.send_message({ text: 'put some sugar and fudge on that!' }, @random_user[:id])
270+
expect(resp['message']['text']).to eq 'Automod blocked your message'
271+
expect(resp['message']['type']).to eq 'error'
272+
end
273+
274+
it 'update blocklist again' do
275+
@client.update_blocklist('no-cakes', %w[fudge cream sugar vanilla jam])
276+
end
277+
278+
it 'should block messages that match the blocklist' do
279+
channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { 'test' => true, 'language' => 'ruby' })
280+
channel.create(@random_user[:id])
281+
resp = channel.send_message({ text: 'you should add more jam there ;)' }, @random_user[:id])
282+
expect(resp['message']['text']).to eq 'Automod blocked your message'
283+
expect(resp['message']['type']).to eq 'error'
284+
end
285+
286+
it 'delete a blocklist' do
287+
@client.delete_blocklist('no-cakes')
288+
end
289+
290+
it 'should not block messages anymore' do
291+
channel = @client.channel('messaging', channel_id: SecureRandom.uuid, data: { 'test' => true, 'language' => 'ruby' })
292+
channel.create(@random_user[:id])
293+
resp = channel.send_message({ text: 'put some sugar and fudge on that!' }, @random_user[:id])
294+
expect(resp['message']['text']).to eq 'put some sugar and fudge on that!'
295+
end
296+
297+
it 'list available blocklists' do
298+
resp = @client.list_blocklists
299+
expect(resp['blocklists'].length).to eq 1
300+
end
301+
302+
it 'delete a default blocklist should fail' do
303+
expect do
304+
@client.delete_blocklist('profanity_en_2020_v1')
305+
end.to raise_error(
306+
/cannot delete the builtin block list/
307+
)
308+
end
220309
end

0 commit comments

Comments
 (0)