Skip to content

Commit 8ce255a

Browse files
committed
feat: add headings methods
1 parent 58cce30 commit 8ce255a

File tree

6 files changed

+152
-5
lines changed

6 files changed

+152
-5
lines changed

swibots/api/chat/chat_client.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
MediaController,
1010
StickerController,
1111
OrganizationController,
12+
HeadingsController,
1213
)
1314
from swibots.api.chat.events import (
1415
ChatEvent,
@@ -62,6 +63,7 @@ def __init__(
6263
self._ads: AdvertisingController = None
6364
self._stickers: StickerController = None
6465
self._organization: OrganizationController = None
66+
self._headings: HeadingsController = None
6567
self._ws: SwitchWSAsyncClient = None
6668
self._started = False
6769

@@ -77,6 +79,12 @@ def ads(self) -> AdvertisingController:
7779
self._ads = AdvertisingController(self)
7880
return self._ads
7981

82+
@property
83+
def headings(self) -> HeadingsController:
84+
if self._headings is None:
85+
self._headings = HeadingsController(self)
86+
return self._headings
87+
8088
@property
8189
def organization(self) -> OrganizationController:
8290
if self._organization is None:
@@ -185,7 +193,7 @@ async def _parse_event(self, raw_message: WsMessage, callback) -> ChatEvent:
185193
evt = self.build_object(InlineQueryEvent, json_data)
186194
else:
187195
logging.error(f"Received unknown event type: {messagetype}")
188-
# evt = self.build_object(ChatEvent, json_data)
196+
# evt = self.build_object(ChatEvent, json_data)
189197
return
190198
except Exception as e:
191199
logger.exception(e)

swibots/api/chat/controllers/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
from .sticker_controller import StickerController
55
from .organization_controller import OrganizationController
66
from .ads_controller import AdvertisingController
7+
from .heading_controller import HeadingsController
78

89
__all__ = [
910
"AdvertisingController",
1011
"MessageController",
1112
"ChatController",
1213
"MediaController",
1314
"StickerController",
14-
"OrganizationController"
15+
"OrganizationController",
16+
"HeadingsController",
1517
]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import logging
2+
from typing import TYPE_CHECKING, Optional, List, Literal
3+
from swibots.api.community.models import CommunityHeading
4+
from swibots.api.common.models import User
5+
from urllib.parse import urlencode
6+
7+
if TYPE_CHECKING:
8+
from swibots.api.community import CommunityClient
9+
10+
log = logging.getLogger(__name__)
11+
12+
class HeadingsController:
13+
def __init__(self, client: "CommunityClient"):
14+
self.client = client
15+
16+
async def get_headings(self, community_id: str, additional: bool = False) -> List[CommunityHeading]:
17+
"""
18+
:param community_id: The ID of the community
19+
:return: The Heading object
20+
"""
21+
query = urlencode({"additional": additional,
22+
"communityId": community_id,
23+
})
24+
response = await self.client.get(f"/headings?{query}")
25+
return self.client.build_list(CommunityHeading, response.data)
26+
27+
28+
async def create_heading(self, community_id: str, name: str,
29+
chat_id: str,
30+
heading_for: Literal["CHANNEL", "GROUP"] = "CHANNEL",
31+
heading_type: Literal['BLANK', "VALUE"] = "VALUE",
32+
):
33+
query = urlencode({
34+
"communityId": community_id,
35+
"heading": name,
36+
"id": chat_id,
37+
"headingFor": heading_for,
38+
"headingType": heading_type,
39+
})
40+
response = await self.client.post(f"/headings?{query}")
41+
return response.data
42+
43+
async def delete_heading(self, community_id: str, heading_name: str):
44+
query = urlencode({
45+
"communityId": community_id,
46+
"headingName": heading_name,
47+
})
48+
response = await self.client.post(f"/headings/delete-headings?{query}")
49+
return response.data
50+
51+
async def edit_heading(self, community_id: str, heading_name: str, new_heading_name: str,
52+
heading_type: Literal['BLANK', "VALUE"] = "VALUE"):
53+
query = urlencode({
54+
"communityId": community_id,
55+
"headingName": heading_name,
56+
"newHeadingName": new_heading_name,
57+
"headingType": heading_type,
58+
})
59+
response = await self.client.post(f"/headings/edit-headings?{query}")
60+
return response.data
61+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from typing import Type, TypeVar, Optional, List, Literal
2+
import swibots
3+
from swibots.api.community.models import CommunityHeading
4+
5+
6+
class HeadingMethods:
7+
8+
async def get_headings(self: "swibots.ApiClient", community_id: str, additional: bool = False) -> List[CommunityHeading]:
9+
"""
10+
Get the headings of a community
11+
12+
:param community_id: The ID of the community
13+
:param additional: Whether to get additional information about the headings
14+
"""
15+
return await self.chat_service.headings.get_headings(community_id, additional)
16+
17+
async def create_heading(self: "swibots.ApiClient", community_id: str, name: str,
18+
chat_id: str,
19+
heading_for: Literal["CHANNEL", "GROUP"] = "CHANNEL",
20+
heading_type: Literal['BLANK', "VALUE"] = "VALUE",
21+
):
22+
"""
23+
Create a heading in a community
24+
25+
:param community_id: The ID of the community
26+
:param name: The name of the heading
27+
:param chat_id: The ID of the chat
28+
:param heading_for: The type of the heading
29+
:param heading_type: The type of the heading
30+
"""
31+
return await self.chat_service.headings.create_heading(community_id, name, chat_id, heading_for, heading_type)
32+
33+
async def delete_heading(self, community_id: str, heading_name: str):
34+
"""
35+
Delete a heading from a community
36+
37+
:param community_id: The ID of the community
38+
:param heading_name: The name of the heading to delete
39+
"""
40+
return await self.chat_service.headings.delete_heading(community_id, heading_name)
41+
42+
async def edit_heading(self, community_id: str, heading_name: str, new_heading_name: str,
43+
heading_type: Literal['BLANK', "VALUE"] = "VALUE"):
44+
"""
45+
Edit a heading in a community
46+
47+
:param community_id: The ID of the community
48+
:param heading_name: The name of the heading to edit
49+
:param new_heading_name: The new name of the heading
50+
:param heading_type: The type of the heading
51+
"""
52+
return await self.chat_service.headings.edit_heading(community_id, heading_name, new_heading_name, heading_type)

swibots/api/community/models/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from .channel import Channel
2-
from .community import Community
2+
from .community import Community, CommunityHeading
33
from .group import Group
44
from .role import Role
55
from .rolepermission import RolePermission
66
from .rolemember import RoleMember
77
from .baninfo import BanInfo
8-
from .community_member import CommunityMember
8+
from .community_member import CommunityMember, SearchResultUser
99
from .restricteduser import RestrictedUser
1010
from .quest import Quest, QuestCategory
1111
from .instantmessaging import InstantMessaging
@@ -18,9 +18,11 @@
1818
"RolePermission",
1919
"RoleMember",
2020
"BanInfo",
21+
"CommunityHeading",
2122
"CommunityMember",
2223
"RestrictedUser",
2324
"Quest",
2425
"QuestCategory",
2526
"InstantMessaging",
27+
"SearchResultUser",
2628
]

swibots/api/community/models/community.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from typing import Optional
1+
from typing import Optional, Union, List
22
from swibots.utils.types import JSONDict
33
from swibots.base.switch_object import SwitchObject
44
from swibots.api.community.models.channel import Channel
5+
from swibots.api.community.models.group import Group
56
import swibots
67

78

@@ -108,3 +109,24 @@ def from_json(self, data: Optional[JSONDict]) -> Optional["Community"]:
108109
self.default_channel = Channel().from_json(data.get("defaultChannel"))
109110

110111
return self
112+
113+
114+
class CommunityHeading(SwitchObject):
115+
def __init__(self, app: "swibots.Client" = None,
116+
name: str = None,
117+
heading_type: str = None,
118+
public: bool = None,
119+
details: List[Union[Channel, Group]] = None):
120+
super().__init__(app)
121+
self.name = name
122+
self.details = details
123+
self.heading_type = heading_type
124+
self.public = public
125+
126+
def from_json(self, data):
127+
self.name = data.get("headingName")
128+
self.details = [(Group if detail['isGroup'] else Channel)().from_json(detail) for detail in data.get("details")]
129+
self.public = data.get("enabledPublic")
130+
self.heading_type = data.get("headingType")
131+
132+
return self

0 commit comments

Comments
 (0)