Skip to content

Commit fe069de

Browse files
gumuzferhatelmas
andauthored
[CHAT-2191] Async delete_users, delete_channels & get_task (#79)
Co-authored-by: ferhat elmas <[email protected]>
1 parent 2643b70 commit fe069de

File tree

7 files changed

+113
-0
lines changed

7 files changed

+113
-0
lines changed

stream_chat/async_chat/client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ async def update_user_partial(self, update):
107107
async def delete_user(self, user_id, **options):
108108
return await self.delete(f"users/{user_id}", options)
109109

110+
async def delete_users(self, user_ids, delete_type, **options):
111+
return await self.post(
112+
"users/delete", data=dict(options, user=delete_type, user_ids=user_ids)
113+
)
114+
110115
async def deactivate_user(self, user_id, **options):
111116
return await self.post(f"users/{user_id}/deactivate", data=options)
112117

@@ -236,6 +241,9 @@ def channel(self, channel_type, channel_id=None, data=None):
236241
"""
237242
return Channel(self, channel_type, channel_id, data)
238243

244+
async def delete_channels(self, cids, **options):
245+
return await self.post(f"channels/delete", data=dict(options, cids=cids))
246+
239247
async def list_commands(self):
240248
return await self.get("commands")
241249

@@ -650,3 +658,6 @@ async def __aexit__(
650658
exc_tb: Optional[TracebackType],
651659
) -> None:
652660
await self.close()
661+
662+
async def get_task(self, task_id):
663+
return await self.get(f"tasks/{task_id}")

stream_chat/base/client.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ def update_user_partial(self, update):
9696
def delete_user(self, user_id, **options):
9797
pass
9898

99+
@abc.abstractmethod
100+
def delete_users(self, user_ids, delete_type, **options):
101+
"""
102+
Delete users asynchronously
103+
104+
:param user_ids: a list of user IDs to delete
105+
:param delete_type: type of user delete (hard|soft)
106+
:param options: additional delete options
107+
:return: task_id
108+
"""
109+
pass
110+
99111
@abc.abstractmethod
100112
def deactivate_user(self, user_id, **options):
101113
pass
@@ -222,6 +234,10 @@ def channel(self, channel_type, channel_id=None, data=None):
222234
"""
223235
pass
224236

237+
@abc.abstractmethod
238+
def delete_channels(self, cids, **options):
239+
pass
240+
225241
@abc.abstractmethod
226242
def list_commands(self):
227243
pass
@@ -570,3 +586,10 @@ def get_export_channel_status(self, task_id):
570586
Retrieves status of export
571587
"""
572588
pass
589+
590+
@abc.abstractmethod
591+
def get_task(self, task_id):
592+
"""
593+
Retrieves status of task
594+
"""
595+
pass

stream_chat/client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ def update_user_partial(self, update):
103103
def delete_user(self, user_id, **options):
104104
return self.delete(f"users/{user_id}", options)
105105

106+
def delete_users(self, user_ids, delete_type, **options):
107+
return self.post(
108+
"users/delete", data=dict(options, user=delete_type, user_ids=user_ids)
109+
)
110+
106111
def deactivate_user(self, user_id, **options):
107112
return self.post(f"users/{user_id}/deactivate", data=options)
108113

@@ -232,6 +237,9 @@ def channel(self, channel_type, channel_id=None, data=None):
232237
"""
233238
return Channel(self, channel_type, channel_id, data)
234239

240+
def delete_channels(self, cids, **options):
241+
return self.post(f"channels/delete", data=dict(options, cids=cids))
242+
235243
def list_commands(self):
236244
return self.get("commands")
237245

@@ -629,3 +637,6 @@ def get_export_channel_status(self, task_id):
629637
:type task_id: str
630638
"""
631639
return self.get(f"export_channels/{task_id}")
640+
641+
def get_task(self, task_id):
642+
return self.get(f"tasks/{task_id}")

stream_chat/tests/async_chat/test_channel.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,5 +324,6 @@ async def test_export_channel(self, client, channel, random_users):
324324
if resp["status"] == "completed":
325325
assert len(resp["result"]) != 0
326326
assert resp["result"]["url"] != ""
327+
assert "error" not in resp
327328
break
328329
time.sleep(0.5)

stream_chat/tests/async_chat/test_client.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,24 @@ async def test_delete_user(self, event_loop, client, random_user):
150150
assert "user" in response
151151
assert random_user["id"] == response["user"]["id"]
152152

153+
@pytest.mark.asyncio
154+
async def test_delete_users(self, event_loop, client, random_user):
155+
response = await client.delete_users(
156+
[random_user["id"]], "hard", conversations="hard", messages="hard"
157+
)
158+
assert "task_id" in response
159+
160+
for _ in range(10):
161+
response = await client.get_task(response["task_id"])
162+
if response["status"] == "completed" and response["result"][
163+
random_user["id"]
164+
] == {"status": "ok"}:
165+
return
166+
167+
time.sleep(1)
168+
169+
assert False, "task did not succeed"
170+
153171
@pytest.mark.asyncio
154172
async def test_deactivate_user(self, event_loop, client, random_user):
155173
response = await client.deactivate_user(random_user["id"])
@@ -504,3 +522,19 @@ def wait():
504522
wait()
505523
response = await client.list_roles()
506524
assert role not in response["roles"]
525+
526+
@pytest.mark.asyncio
527+
async def test_delete_channels(self, event_loop, client, channel):
528+
response = await client.delete_channels([channel.cid])
529+
assert "task_id" in response
530+
531+
for _ in range(10):
532+
response = await client.get_task(response["task_id"])
533+
if response["status"] == "completed" and response["result"][
534+
channel.cid
535+
] == {"status": "ok"}:
536+
return
537+
538+
time.sleep(1)
539+
540+
assert False, "task did not succeed"

stream_chat/tests/test_channel.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,5 +295,6 @@ def test_export_channel(self, client, channel, random_users):
295295
if resp["status"] == "completed":
296296
assert len(resp["result"]) != 0
297297
assert resp["result"]["url"] != ""
298+
assert "error" not in resp
298299
break
299300
time.sleep(0.5)

stream_chat/tests/test_client.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,23 @@ def test_delete_user(self, client, random_user):
129129
assert "user" in response
130130
assert random_user["id"] == response["user"]["id"]
131131

132+
def test_delete_users(self, client, random_user):
133+
response = client.delete_users(
134+
[random_user["id"]], "hard", conversations="hard", messages="hard"
135+
)
136+
assert "task_id" in response
137+
138+
for _ in range(10):
139+
response = client.get_task(response["task_id"])
140+
if response["status"] == "completed" and response["result"][
141+
random_user["id"]
142+
] == {"status": "ok"}:
143+
return
144+
145+
time.sleep(1)
146+
147+
assert False, "task did not succeed"
148+
132149
def test_deactivate_user(self, client, random_user):
133150
response = client.deactivate_user(random_user["id"])
134151
assert "user" in response
@@ -430,3 +447,18 @@ def wait():
430447
wait()
431448
response = client.list_roles()
432449
assert role not in response["roles"]
450+
451+
def test_delete_channels(self, client, channel):
452+
response = client.delete_channels([channel.cid])
453+
assert "task_id" in response
454+
455+
for _ in range(10):
456+
response = client.get_task(response["task_id"])
457+
if response["status"] == "completed" and response["result"][
458+
channel.cid
459+
] == {"status": "ok"}:
460+
return
461+
462+
time.sleep(1)
463+
464+
assert False, "task did not succeed"

0 commit comments

Comments
 (0)