Skip to content

Commit

Permalink
feat: added pin, archive and partial member update functions
Browse files Browse the repository at this point in the history
  • Loading branch information
totalimmersion committed Dec 6, 2024
1 parent fa23514 commit 5500ea4
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
51 changes: 51 additions & 0 deletions stream_chat/async_chat/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
42 changes: 42 additions & 0 deletions stream_chat/base/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
51 changes: 51 additions & 0 deletions stream_chat/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 5500ea4

Please sign in to comment.