Skip to content

Commit 3e032ff

Browse files
authored
[CHAT-2402] Add delete users/channels support (#54)
1 parent ec8dbd9 commit 3e032ff

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

lib/stream-chat/client.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
module StreamChat
1414
DEFAULT_BLOCKLIST = 'profanity_en_2020_v1'
15+
SOFT_DELETE = 'soft'
16+
HARD_DELETE = 'hard'
1517

1618
class Client
1719
BASE_URL = 'https://chat.stream-io-api.com'
@@ -293,6 +295,18 @@ def get_export_channel_status(task_id)
293295
get("export_channels/#{task_id}")
294296
end
295297

298+
def get_task(task_id)
299+
get("tasks/#{task_id}")
300+
end
301+
302+
def delete_users(user_ids, user: SOFT_DELETE, messages: nil, conversations: nil)
303+
post('users/delete', data: { user_ids: user_ids, user: user, messages: messages, conversations: conversations })
304+
end
305+
306+
def delete_channels(cids, hard_delete: false)
307+
post('channels/delete', data: { cids: cids, hard_delete: hard_delete })
308+
end
309+
296310
def revoke_tokens(before)
297311
before = before.rfc3339 if before.instance_of?(DateTime)
298312
update_app_settings({ 'revoke_tokens_issued_before' => before })

spec/client_spec.rb

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,14 @@
390390
)
391391
end
392392

393+
it 'check status for a task that does not exist' do
394+
expect do
395+
@client.get_task(SecureRandom.uuid)
396+
end.to raise_error(
397+
/Can't find task with id/
398+
)
399+
end
400+
393401
it 'request the export for a channel that does not exist' do
394402
expect do
395403
@client.export_channels({ type: 'messaging', id: SecureRandom.uuid })
@@ -420,6 +428,74 @@
420428
end
421429
end
422430

431+
it 'request delete channels' do
432+
ch1 = @client.channel('messaging', channel_id: SecureRandom.uuid)
433+
ch1.create(@random_user[:id])
434+
ch1.send_message({ text: 'Hey Joni' }, @random_user[:id])
435+
cid1 = "#{ch1.channel_type}:#{ch1.id}"
436+
437+
ch2 = @client.channel('messaging', channel_id: SecureRandom.uuid)
438+
ch2.create(@random_user[:id])
439+
ch2.send_message({ text: 'Hey Joni' }, @random_user[:id])
440+
cid2 = "#{ch2.channel_type}:#{ch2.id}"
441+
442+
resp = @client.delete_channels([cid1, cid2], hard_delete: true)
443+
expect(resp['task_id']).not_to be_empty
444+
445+
task_id = resp['task_id']
446+
loop do
447+
resp = @client.get_task(task_id)
448+
expect(resp['status']).not_to be_empty
449+
expect(resp['created_at']).not_to be_empty
450+
expect(resp['updated_at']).not_to be_empty
451+
if resp['status'] == 'completed'
452+
result = resp['result']
453+
expect(result).not_to be_empty
454+
expect(result[cid1]).not_to be_empty
455+
expect(result[cid1]['status']).to eq 'ok'
456+
expect(result[cid2]).not_to be_empty
457+
expect(result[cid2]['status']).to eq 'ok'
458+
break
459+
end
460+
sleep(0.5)
461+
end
462+
end
463+
464+
it 'request delete users' do
465+
user_id1 = SecureRandom.uuid
466+
user_id2 = SecureRandom.uuid
467+
@client.update_users([{ id: user_id1 }, { id: user_id2 }])
468+
469+
ch1 = @client.channel('messaging', channel_id: SecureRandom.uuid)
470+
ch1.create(user_id1)
471+
ch1.send_message({ text: 'Hey Joni' }, user_id1)
472+
473+
ch2 = @client.channel('messaging', channel_id: SecureRandom.uuid)
474+
ch2.create(user_id2)
475+
ch2.send_message({ text: 'Hey Joni' }, user_id1)
476+
477+
resp = @client.delete_users([user_id1, user_id2], user: StreamChat::HARD_DELETE)
478+
expect(resp['task_id']).not_to be_empty
479+
480+
task_id = resp['task_id']
481+
loop do
482+
resp = @client.get_task(task_id)
483+
expect(resp['status']).not_to be_empty
484+
expect(resp['created_at']).not_to be_empty
485+
expect(resp['updated_at']).not_to be_empty
486+
if resp['status'] == 'completed'
487+
result = resp['result']
488+
expect(result).not_to be_empty
489+
expect(result[user_id1]).not_to be_empty
490+
expect(result[user_id1]['status']).to eq 'ok'
491+
expect(result[user_id2]).not_to be_empty
492+
expect(result[user_id2]['status']).to eq 'ok'
493+
break
494+
end
495+
sleep(0.5)
496+
end
497+
end
498+
423499
it 'check_sqs with an invalid queue url should fail' do
424500
resp = @client.check_sqs('key', 'secret', 'https://foo.com/bar')
425501
expect(resp['status']).to eq 'error'

0 commit comments

Comments
 (0)