Skip to content

Commit 58cce30

Browse files
committed
feat: add search_community_data
1 parent 16eb279 commit 58cce30

File tree

3 files changed

+112
-3
lines changed

3 files changed

+112
-3
lines changed

swibots/api/chat/controllers/message_controller.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import json
44
import logging
5-
from typing import TYPE_CHECKING, List, Optional, Tuple, Union
5+
from typing import TYPE_CHECKING, List, Optional, Tuple, Union, Literal
66
from urllib.parse import urlencode
77
from io import BytesIO
88
from asyncio.tasks import Task
@@ -16,7 +16,7 @@
1616
)
1717
from swibots.utils import isUrl
1818
from 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

2121
if 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

swibots/api/chat/methods/get_community_media_files.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from typing import List
22
import swibots
33
from swibots.api.chat.models import Message
4-
4+
from typing import Literal, Union
5+
from swibots.api.community.models import Channel, Group, SearchResultUser
56

67
class GetCommunityMediaFiles:
78
async def get_community_media_files(
@@ -21,3 +22,29 @@ async def get_community_media_files(
2122
This function does the same as :meth:`~switch.api.chat.controllers.MessageController.get_messages`.
2223
"""
2324
return await self.chat_service.messages.get_community_media_files(community_id)
25+
26+
27+
async def search_community_data(
28+
self: "swibots.ApiClient",
29+
query: str,
30+
community_id: str,
31+
filter: Literal["MESSAGES", "MEDIA", "LINK", "GROUP", "CHANNEL", "MEMBER"] = "MESSAGES",
32+
limit: int = 10,
33+
page: int = 0,
34+
) -> Union[List[Message], List[Group], List[Channel], List[SearchResultUser]]:
35+
"""Search community data
36+
37+
Parameters:
38+
query (``str``): The search query
39+
community_id (``str``): The community id
40+
filter (``str``, *optional*): The filter. Defaults to "MESSAGES".
41+
limit (``int``, *optional*): The limit. Defaults to 10.
42+
page (``int``, *optional*): The page. Defaults to 0.
43+
44+
Returns:
45+
Union[List[Message], List[Group], List[Channel], List[SearchResultUser]]: The search results
46+
47+
Raises:
48+
``~switch.error.SwitchError``: If the search results could not be retrieved
49+
"""
50+
return await self.chat_service.messages.search_community_data(query, community_id, filter, limit, page)

swibots/api/community/models/community_member.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,43 @@ def from_json(self, data: JSONDict = None) -> "CommunityMember":
8080
self.xp = data.get("xp")
8181
self.xp_spend = data.get("xp_spend")
8282
return self
83+
84+
85+
class SearchResultUser(SwitchObject):
86+
def __init__(self, app: "swibots.App" = None, id: str = None, member_id: str = None, name: str = None, username: str = None, image_url: str = None, active: bool = None, deleted: bool = None, profile_colour: str = None, bot: bool = None):
87+
super().__init__(app)
88+
self.id = id
89+
self.member_id = member_id
90+
self.name = name
91+
self.username = username
92+
self.image_url = image_url
93+
self.active = active
94+
self.deleted = deleted
95+
self.profile_colour = profile_colour
96+
self.bot = bot
97+
98+
def to_json(self) -> JSONDict:
99+
return {
100+
"id": self.id,
101+
"memberId": self.member_id,
102+
"name": self.name,
103+
"username": self.username,
104+
"imageUrl": self.image_url,
105+
"active": self.active,
106+
"deleted": self.deleted,
107+
"profileColour": self.profile_colour,
108+
"bot": self.bot,
109+
}
110+
111+
def from_json(self, data: JSONDict = None) -> "SearchResultUser":
112+
if data is not None:
113+
self.id = data.get("id")
114+
self.member_id = data.get("memberId")
115+
self.name = data.get("name")
116+
self.username = data.get("username")
117+
self.image_url = data.get("imageUrl")
118+
self.active = data.get("active")
119+
self.deleted = data.get("deleted")
120+
self.profile_colour = data.get("profileColour")
121+
self.bot = data.get("bot")
122+
return self

0 commit comments

Comments
 (0)