From 5500ea4fcf5958606fad4f757e583f5191be431e Mon Sep 17 00:00:00 2001 From: Lennart Kuijs Date: Fri, 6 Dec 2024 15:14:02 +0100 Subject: [PATCH] feat: added pin, archive and partial member update functions --- stream_chat/async_chat/channel.py | 51 +++++++++++++++++++++++++++++++ stream_chat/base/channel.py | 42 +++++++++++++++++++++++++ stream_chat/channel.py | 51 +++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) diff --git a/stream_chat/async_chat/channel.py b/stream_chat/async_chat/channel.py index 764a19f..b019283 100644 --- a/stream_chat/async_chat/channel.py +++ b/stream_chat/async_chat/channel.py @@ -209,3 +209,54 @@ async def unmute(self, user_id: str) -> StreamResponse: "channel_cid": self.cid, } return await self.client.post("moderation/unmute/channel", data=params) + + async def pin(self, user_id: str) -> StreamResponse: + if not user_id: + raise StreamChannelException("user_id must not be empty") + + payload = { + "set": { + "pinned": True + } + } + return self.client.patch(f"{self.url}/member/{user_id}", data=payload) + + async def unpin(self, user_id: str) -> StreamResponse: + if not user_id: + raise StreamChannelException("user_id must not be empty") + + payload = { + "set": { + "pinned": False + } + } + return self.client.patch(f"{self.url}/member/{user_id}", data=payload) + + async def archive(self, user_id: str) -> StreamResponse: + if not user_id: + raise StreamChannelException("user_id must not be empty") + + payload = { + "set": { + "archived": True + } + } + return self.client.patch(f"{self.url}/member/{user_id}", data=payload) + + async def unarchive(self, user_id: str) -> StreamResponse: + if not user_id: + raise StreamChannelException("user_id must not be empty") + + payload = { + "set": { + "archived": False + } + } + return self.client.patch(f"{self.url}/member/{user_id}", data=payload) + + async def update_member_partial(self, user_id: str, to_set: Dict = None, to_unset: Iterable[str] = None) -> Union[StreamResponse, Awaitable[StreamResponse]]: + if not user_id: + raise StreamChannelException("user_id must not be empty") + + payload = {"set": to_set or {}, "unset": to_unset or []} + return self.client.patch(f"{self.url}/member/{user_id}", data=payload) diff --git a/stream_chat/base/channel.py b/stream_chat/base/channel.py index 16cc87e..f59f9f3 100644 --- a/stream_chat/base/channel.py +++ b/stream_chat/base/channel.py @@ -440,6 +440,48 @@ def unmute(self, user_id: str) -> Union[StreamResponse, Awaitable[StreamResponse """ pass + @abc.abstractmethod + def pin(self, user_id: str) -> Union[StreamResponse, Awaitable[StreamResponse]]: + """ + Pins a channel + Allows a user to pin the channel (only for theirselves) + """ + pass + + @abc.abstractmethod + def unpin(self, user_id: str) -> Union[StreamResponse, Awaitable[StreamResponse]]: + """ + Unpins a channel + Allows a user to unpin the channel (only for theirselves) + """ + pass + + @abc.abstractmethod + def archive(self, user_id: str) -> Union[StreamResponse, Awaitable[StreamResponse]]: + """ + Pins a channel + Allows a user to archive the channel (only for theirselves) + """ + pass + + @abc.abstractmethod + def unarchive(self, user_id: str) -> Union[StreamResponse, Awaitable[StreamResponse]]: + """ + Unpins a channel + Allows a user to unpin the channel (only for theirselves) + """ + pass + + @abc.abstractmethod + def update_member_partial(self, user_id: str, to_set: Dict = None, to_unset: Iterable[str] = None) -> Union[StreamResponse, Awaitable[StreamResponse]]: + """ + Update channel member partially + + :param to_set: a dictionary of key/value pairs to set or to override + :param to_unset: a list of keys to clear + """ + pass + def add_user_id(payload: Dict, user_id: str) -> Dict: return {**payload, "user": {"id": user_id}} diff --git a/stream_chat/channel.py b/stream_chat/channel.py index 776c892..b818357 100644 --- a/stream_chat/channel.py +++ b/stream_chat/channel.py @@ -210,3 +210,54 @@ def unmute(self, user_id: str) -> StreamResponse: "channel_cid": self.cid, } return self.client.post("moderation/unmute/channel", data=params) + + def pin(self, user_id: str) -> StreamResponse: + if not user_id: + raise StreamChannelException("user_id must not be empty") + + payload = { + "set": { + "pinned": True + } + } + return self.client.patch(f"{self.url}/member/{user_id}", data=payload) + + def unpin(self, user_id: str) -> StreamResponse: + if not user_id: + raise StreamChannelException("user_id must not be empty") + + payload = { + "set": { + "pinned": False + } + } + return self.client.patch(f"{self.url}/member/{user_id}", data=payload) + + def archive(self, user_id: str) -> StreamResponse: + if not user_id: + raise StreamChannelException("user_id must not be empty") + + payload = { + "set": { + "archived": True + } + } + return self.client.patch(f"{self.url}/member/{user_id}", data=payload) + + def unarchive(self, user_id: str) -> StreamResponse: + if not user_id: + raise StreamChannelException("user_id must not be empty") + + payload = { + "set": { + "archived": False + } + } + return self.client.patch(f"{self.url}/member/{user_id}", data=payload) + + def update_member_partial(self, user_id: str, to_set: Dict = None, to_unset: Iterable[str] = None) -> Union[StreamResponse, Awaitable[StreamResponse]]: + if not user_id: + raise StreamChannelException("user_id must not be empty") + + payload = {"set": to_set or {}, "unset": to_unset or []} + return self.client.patch(f"{self.url}/member/{user_id}", data=payload)