22import os
33import json
44import logging
5- from typing import TYPE_CHECKING , List , Optional , Tuple , Union
5+ from typing import TYPE_CHECKING , List , Optional , Tuple , Union , Literal
66from urllib .parse import urlencode
77from io import BytesIO
88from asyncio .tasks import Task
1616)
1717from swibots .utils import isUrl
1818from swibots .api .common .models import User , EmbeddedMedia , Media
19- from swibots .api .community .models import Channel , Group
19+ from swibots .api .community .models import Channel , Group , CommunityMember , SearchResultUser
2020
2121if TYPE_CHECKING :
2222 from swibots .api .chat import ChatClient
@@ -771,3 +771,45 @@ async def get_user(self, user_id: int | str = None, username: str = None) -> Use
771771 else :
772772 raise ValueError ("Either provide 'user_id' or 'username' to get user info." )
773773 return self .client .build_object (User , response .data )
774+
775+
776+ async def search_community_data (
777+ self ,
778+ query : str ,
779+ community_id : str ,
780+ filter : Literal ["MESSAGES" , "MEDIA" , "LINK" , "GROUP" , "CHANNEL" , "MEMBER" ] = "MESSAGES" ,
781+ limit : int = 10 ,
782+ page : int = 0 ,
783+ ) -> Union [List [Message ], List [Group ], List [Channel ], List [SearchResultUser ]]:
784+ """Search community data
785+
786+ Parameters:
787+ query (``str``): The search query
788+ community_id (``str``): The community id
789+ filter (``str``, *optional*): The filter. Defaults to "MESSAGES".
790+ limit (``int``, *optional*): The limit. Defaults to 10.
791+ page (``int``, *optional*): The page. Defaults to 0.
792+
793+ Returns:
794+ Union[List[Message], List[Group], List[Channel], List[SearchResultUser]]: The search results
795+
796+ """
797+ data = {
798+ "searchString" : query ,
799+ "item" : filter ,
800+ "communityId" : community_id ,
801+ "limit" : limit ,
802+ "page" : page ,
803+ }
804+ response = await self .client .get (
805+ "/v1/search/community-data?{}" .format (urlencode (data ))
806+ )
807+ if filter in ["MESSAGES" , "MEDIA" , "LINK" ]:
808+ return self .client .build_list (Message , response .data )
809+ elif filter == "GROUP" :
810+ return self .client .build_list (Group , response .data )
811+ elif filter == "CHANNEL" :
812+ return self .client .build_list (Channel , response .data )
813+ elif filter == "MEMBER" :
814+ return self .client .build_list (SearchResultUser , response .data )
815+ return response .data
0 commit comments