Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions stream_chat/async_chat/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ async def update_partial(
payload = {"set": to_set or {}, "unset": to_unset or []}
return await self.client.patch(self.url, data=payload)

async def delete(self) -> StreamResponse:
return await self.client.delete(self.url)
async def delete(self, hard: bool = False) -> StreamResponse:
return await self.client.delete(self.url, {"hard_delete": hard})

async def truncate(self, **options: Any) -> StreamResponse:
return await self.client.post(f"{self.url}/truncate", data=options)
Expand Down
4 changes: 3 additions & 1 deletion stream_chat/base/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ def update_partial(
pass

@abc.abstractmethod
def delete(self) -> Union[StreamResponse, Awaitable[StreamResponse]]:
def delete(
self, hard: bool = False
) -> Union[StreamResponse, Awaitable[StreamResponse]]:
"""
Delete the channel. Messages are permanently removed.

Expand Down
4 changes: 2 additions & 2 deletions stream_chat/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ def update_partial(
payload = {"set": to_set or {}, "unset": to_unset or []}
return self.client.patch(self.url, data=payload)

def delete(self) -> StreamResponse:
return self.client.delete(self.url)
def delete(self, hard: bool = False) -> StreamResponse:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a breaking change, or you can call delete without passing the hard flag?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not a breaking change. You can. It has a default.

return self.client.delete(self.url, params={"hard_delete": hard})

def truncate(self, **options: Any) -> StreamResponse:
return self.client.post(f"{self.url}/truncate", data=options)
Expand Down
6 changes: 3 additions & 3 deletions stream_chat/tests/async_chat/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async def channel(client: StreamChatAsync, random_user: Dict):
yield channel

try:
await channel.delete()
await client.delete_channels([channel.cid], hard_delete=True)
except Exception:
pass

Expand Down Expand Up @@ -128,10 +128,10 @@ async def fellowship_of_the_ring(client: StreamChatAsync):
await channel.create("gandalf")
yield
try:
await channel.delete()
await channel.delete(hard=True)
await hard_delete_users(client, [m["id"] for m in members])
except Exception:
pass
await hard_delete_users(client, [m["id"] for m in members])


async def hard_delete_users(client: StreamChatAsync, user_ids: List[str]):
Expand Down
61 changes: 34 additions & 27 deletions stream_chat/tests/async_chat/test_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from stream_chat.async_chat.channel import Channel
from stream_chat.async_chat.client import StreamChatAsync
from stream_chat.base.exceptions import StreamAPIException
from stream_chat.tests.async_chat.conftest import hard_delete_users


@pytest.mark.incremental
Expand All @@ -34,6 +35,7 @@ async def test_create_without_id(

await channel.create(random_users[0]["id"])
assert channel.id is not None
await channel.delete(hard=True)

async def test_create_with_options(
self, client: StreamChatAsync, random_users: List[Dict]
Expand All @@ -45,6 +47,7 @@ async def test_create_with_options(

await channel.create(random_users[0]["id"], hide_for_creator=True)
assert channel.id is not None
await channel.delete(hard=True)

async def test_send_message_with_options(self, channel: Channel, random_user: Dict):
response = await channel.send_message(
Expand All @@ -54,26 +57,23 @@ async def test_send_message_with_options(self, channel: Channel, random_user: Di
assert response["message"]["text"] == "hi"

async def test_send_message_with_restricted_visibility(
self, client: StreamChatAsync, channel: Channel, random_user: Dict
self, channel: Channel, random_users: List[Dict]
):
# Create test users first
restricted_users = [
{"id": "amy", "name": "Amy"},
{"id": "paul", "name": "Paul"},
]
await client.upsert_users(restricted_users)
amy = random_users[0]["id"]
paul = random_users[1]["id"]
user = random_users[2]["id"]

# Add users to channel
await channel.add_members([u["id"] for u in restricted_users])
await channel.add_members([amy, paul])

# Send message with restricted visibility
response = await channel.send_message(
{"text": "hi", "restricted_visibility": ["amy", "paul"]}, random_user["id"]
{"text": "hi", "restricted_visibility": [amy, paul]}, user
)

assert "message" in response
assert response["message"]["text"] == "hi"
assert response["message"]["restricted_visibility"] == ["amy", "paul"]
assert response["message"]["restricted_visibility"] == [amy, paul]

async def test_send_event(self, channel: Channel, random_user: Dict):
response = await channel.send_event({"type": "typing.start"}, random_user["id"])
Expand Down Expand Up @@ -303,39 +303,44 @@ async def test_channel_hide_show(
)
assert len(response["channels"]) == 1

async def test_invites(self, client: StreamChatAsync, channel: Channel):
members = ["john", "paul", "george", "pete", "ringo", "eric"]
await client.upsert_users([{"id": m} for m in members])
async def test_invites(self, client: StreamChatAsync, random_users: List[Dict]):
john = random_users[0]["id"]
ringo = random_users[1]["id"]
eric = random_users[2]["id"]

channel = client.channel(
"team",
"beatles-" + str(uuid.uuid4()),
{"members": members, "invites": ["ringo", "eric"]},
{"members": [john], "invites": [ringo, eric]},
)
await channel.create("john")
await channel.create(john)
# accept the invite when not a member
with pytest.raises(StreamAPIException):
await channel.accept_invite("brian")
await channel.accept_invite("brian" + str(uuid.uuid4()))
# accept the invite when a member
accept = await channel.accept_invite("ringo")
accept = await channel.accept_invite(ringo)
for m in accept["members"]:
if m["user_id"] == "ringo":
if m["user_id"] == ringo:
assert m["invited"] is True
assert "invite_accepted_at" in m
# can accept again, noop
await channel.accept_invite("ringo")
await channel.accept_invite(ringo)

reject = await channel.reject_invite("eric")
reject = await channel.reject_invite(eric)
for m in reject["members"]:
if m["user_id"] == "eric":
if m["user_id"] == eric:
assert m["invited"] is True
assert "invite_rejected_at" in m
# can reject again, noop
await channel.reject_invite("eric")
await channel.reject_invite(eric)
await channel.delete(hard=True)

async def test_query_members(self, client: StreamChatAsync, channel: Channel):
members = ["paul", "george", "john", "jessica", "john2"]
await client.upsert_users([{"id": m, "name": m} for m in members])
for member in members:
rand = str(uuid.uuid4())
user_ids = ["paul", "george", "john", "jessica", "john2"]
user_ids = [f"{n}-{rand}" for n in user_ids]
await client.upsert_users([{"id": m, "name": m} for m in user_ids])
for member in user_ids:
await channel.add_members([member])

response = await channel.query_members(
Expand All @@ -346,8 +351,10 @@ async def test_query_members(self, client: StreamChatAsync, channel: Channel):
)

assert len(response) == 2
assert response[0]["user"]["id"] == "jessica"
assert response[1]["user"]["id"] == "john2"
assert response[0]["user"]["id"] == f"jessica-{rand}"
assert response[1]["user"]["id"] == f"john2-{rand}"

await hard_delete_users(client, user_ids)

async def test_mute_unmute(
self, client: StreamChatAsync, channel: Channel, random_users: List[Dict]
Expand Down
Loading
Loading