Skip to content

Commit 768096e

Browse files
slavabobikSlava Bobik
andauthored
added deactivate users API (#189)
Co-authored-by: Slava Bobik <[email protected]>
1 parent 514a795 commit 768096e

File tree

6 files changed

+66
-1
lines changed

6 files changed

+66
-1
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* @10printhello @totalimmersion
1+
* @10printhello @totalimmersion @slavabobik

stream_chat/async_chat/client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ async def restore_users(self, user_ids: Iterable[str]) -> StreamResponse:
190190
async def deactivate_user(self, user_id: str, **options: Any) -> StreamResponse:
191191
return await self.post(f"users/{user_id}/deactivate", data=options)
192192

193+
async def deactivate_users(
194+
self, user_ids: Iterable[str], **options: Any
195+
) -> StreamResponse:
196+
return await self.post(
197+
"users/deactivate", data=dict(options, user_ids=user_ids)
198+
)
199+
193200
async def reactivate_user(self, user_id: str, **options: Any) -> StreamResponse:
194201
return await self.post(f"users/{user_id}/reactivate", data=options)
195202

stream_chat/base/client.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,21 @@ def deactivate_user(
256256
"""
257257
pass
258258

259+
@abc.abstractmethod
260+
def deactivate_users(
261+
self, user_ids: Iterable[str], **options: Any
262+
) -> Union[StreamResponse, Awaitable[StreamResponse]]:
263+
"""
264+
Deactivates multiple users in a batch operation.
265+
Deactivated users cannot connect to Stream Chat, and can't send or receive messages.
266+
To reactivate users, use `reactivate_user` method for each user.
267+
268+
:param user_ids: a list of user IDs to deactivate
269+
:param options: additional options
270+
:return: task_id
271+
"""
272+
pass
273+
259274
@abc.abstractmethod
260275
def reactivate_user(
261276
self, user_id: str, **options: Any

stream_chat/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ def restore_users(self, user_ids: Iterable[str]) -> StreamResponse:
178178
def deactivate_user(self, user_id: str, **options: Any) -> StreamResponse:
179179
return self.post(f"users/{user_id}/deactivate", data=options)
180180

181+
def deactivate_users(
182+
self, user_ids: Iterable[str], **options: Any
183+
) -> StreamResponse:
184+
return self.post("users/deactivate", data=dict(options, user_ids=user_ids))
185+
181186
def reactivate_user(self, user_id: str, **options: Any) -> StreamResponse:
182187
return self.post(f"users/{user_id}/reactivate", data=options)
183188

stream_chat/tests/async_chat/test_client.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,27 @@ async def test_deactivate_user(self, client: StreamChatAsync, random_user: Dict)
194194
assert "user" in response
195195
assert random_user["id"] == response["user"]["id"]
196196

197+
async def test_deactivate_users(self, client: StreamChatAsync, random_users: Dict):
198+
user_ids = [user["id"] for user in random_users]
199+
response = await client.deactivate_users(user_ids)
200+
assert "task_id" in response
201+
assert len(response["task_id"]) == 36
202+
203+
async def f():
204+
r = await client.get_task(response["task_id"])
205+
return r["status"] == "completed"
206+
207+
await wait_for_async(f)
208+
209+
response = await client.get_task(response["task_id"])
210+
assert response["status"] == "completed"
211+
assert "result" in response
212+
assert "users" in response["result"]
213+
# Verify that all users in the response have deactivated_at field
214+
for user in response["result"]["users"]:
215+
assert "deactivated_at" in user
216+
assert user["id"] in user_ids
217+
197218
async def test_reactivate_user(self, client: StreamChatAsync, random_user: Dict):
198219
response = await client.deactivate_user(random_user["id"])
199220
assert "user" in response

stream_chat/tests/test_client.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,23 @@ def test_deactivate_user(self, client: StreamChat, random_user: Dict):
197197
assert "user" in response
198198
assert random_user["id"] == response["user"]["id"]
199199

200+
def test_deactivate_users(self, client: StreamChat, random_users: Dict):
201+
user_ids = [user["id"] for user in random_users]
202+
response = client.deactivate_users(user_ids)
203+
assert "task_id" in response
204+
assert len(response["task_id"]) == 36 # task_id is a UUID
205+
206+
wait_for(lambda: client.get_task(response["task_id"])["status"] == "completed")
207+
208+
response = client.get_task(response["task_id"])
209+
assert response["status"] == "completed"
210+
assert "result" in response
211+
assert "users" in response["result"]
212+
# Verify that all users in the response have deactivated_at field
213+
for user in response["result"]["users"]:
214+
assert "deactivated_at" in user
215+
assert user["id"] in user_ids
216+
200217
def test_reactivate_user(self, client: StreamChat, random_user: Dict):
201218
response = client.deactivate_user(random_user["id"])
202219
assert "user" in response

0 commit comments

Comments
 (0)