-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcommunity.py
237 lines (192 loc) · 7.86 KB
/
community.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
from typing import Any, List, Optional, Union
from datetime import datetime
from pythorhead.requestor import Request, Requestor
from pythorhead.types import LanguageType, ListingType, SortType
class Community:
def __init__(self, _requestor: Requestor):
self._requestor = _requestor
def create(
self,
name: str,
title: str,
description: Optional[str] = None,
icon: Optional[str] = None,
banner: Optional[str] = None,
nsfw: Optional[bool] = None,
posting_restricted_to_mods: Optional[bool] = None,
discussion_languages: Optional[List[Union[int, LanguageType]]] = None,
) -> Optional[dict]:
"""
Create a community
Args:
name (str)
title (str)
description (str, optional): Defaults to None
icon (str, optional): Defaults to None
nsfw (bool, optional): Defaults to None
posting_restricted_to_mods (bool, optional): Defaults to None
discussion_languages: (List[Union[int, LanguageType]], optional): Defaults to None
Returns:
Optional[dict]: post data if successful
"""
new_community: dict[str, Any] = {
"name": name,
"title": title,
}
if description is not None:
new_community["description"] = description
if icon is not None:
new_community["icon"] = icon
if nsfw is not None:
new_community["nsfw"] = nsfw
if [posting_restricted_to_mods] is not None:
new_community["[posting_restricted_to_mods]"] = [posting_restricted_to_mods]
if discussion_languages is not None:
new_community["discussion_languages"] = [
language.value for language in discussion_languages if isinstance(language, LanguageType)
]
return self._requestor.api(Request.POST, "/community", json=new_community)
def edit(
self,
community_id: int,
title: Optional[str] = None,
description: Optional[str] = None,
icon: Optional[str] = None,
banner: Optional[str] = None,
nsfw: Optional[bool] = None,
posting_restricted_to_mods: Optional[bool] = None,
discussion_languages: Optional[List[Union[int, LanguageType]]] = None,
) -> Optional[dict]:
"""
Edit a community
Args:
name (str)
title (str)
description (str, optional): Defaults to None
icon (str, optional): Defaults to None
nsfw (bool, optional): Defaults to None
posting_restricted_to_mods (bool, optional): Defaults to None
discussion_languages: (List[Union[int, LanguageType]], optional): Defaults to None
Returns:
Optional[dict]: post data if successful
"""
params: dict[str, Any] = {key: value for key, value in locals().items() if value is not None and key != "self"}
if discussion_languages is not None:
params["discussion_languages"] = [
language.value for language in discussion_languages if isinstance(language, LanguageType)
]
return self._requestor.api(Request.PUT, "/community", json=params)
def get(self, id: Optional[int] = None, name: Optional[str] = None) -> Optional[dict]:
"""
Get a community by id or name
Args:
id (Optional[int]): Defaults to None
name (Optional[str]): Defaults to None
Returns:
Optional[dict]: community data if successful
"""
get_community: dict = {}
if id is not None:
get_community["id"] = id
if name is not None:
get_community["name"] = name
return self._requestor.api(Request.GET, "/community", params=get_community)
def list( # noqa: A003
self,
limit: Optional[int] = None,
page: Optional[int] = None,
sort: Optional[SortType] = None,
type_: Optional[ListingType] = None,
) -> List[dict]:
"""
Get communities, with various filters.
Args:
limit (Optional[int], optional): Defaults to None.
page (Optional[int], optional): Defaults to None.
sort (Optional[SortType], optional): Defaults to None.
type_ (Optional[ListingType], optional): Defaults to None.
Returns:
List[dict]: list of communities
"""
params: list[str, Any] = {key: value for key, value in locals().items() if value is not None and key != "self"}
if data := self._requestor.api(Request.GET, "/community/list", params=params):
return data["communities"]
return []
def follow(self, id: int, follow: Optional[bool] = True):
"""
Subscribe to the community with supplied id.
Args:
id (int)
follow (Optional[bool], optional): Defaults to True
Returns:
Optional[dict]: community info if successful
"""
follow_community: dict = {}
follow_community["community_id"] = id
follow_community["follow"] = follow
if data := self._requestor.api(Request.POST, "/community/follow", json=follow_community):
return data["community_view"]
return None
def purge(self, id: int, reason: Optional[str] = None) -> Optional[dict]:
"""
Admin purge / delete a community from the database
Args:
id (int)
reason (Optional[str]): Defaults to None
Returns:
Optional[dict]: purge result if successful
"""
purge_community: dict[str, Any] = {
"community_id": id,
}
if reason is not None:
purge_community["reason"] = reason
return self._requestor.api(Request.POST, "/admin/purge/community", json=purge_community)
def add_mod_to_community(self, added: bool, community_id: int, person_id: int) -> Optional[dict]:
"""
Add a mod to community
Args:
added (bool)
community_id (int)
person_id (bool)
Returns:
Optional[dict]:
"""
addmodtocommunity = {
"added": added,
"community_id": community_id,
"person_id": person_id
}
return self._requestor.api(Request.POST, "/community/mod", json=addmodtocommunity)
def ban_user(
self,
ban: bool = True,
expires: Optional[Union[datetime, int]] = None,
person_id: int = None,
community_id: int = None,
reason: Optional[str] = None,
remove_data: Optional[bool] = None
) -> Optional[dict]:
"""
Ban user from community
Args:
ban (bool): Defaults to True. False Unbans the user
expires (Optional[Union[datetime, int]]): Unix time of ban expiration as an integer or a datetime object. Defaults to None for permanent ban.
person_id (int): Defaults to None
community_id (int): Defaults to None
reason (Optional[str]): Defaults to None
remove_data (Optional[bool]): Defaults to None
Returns:
Optional[dict]:
"""
banFromCommunity: dict[str, Any] = {"ban": ban, "person_id": person_id, "community_id": community_id}
if expires is not None:
if isinstance(expires, datetime):
# Convert the datetime to Unix time
expires = int(expires.timestamp())
banFromCommunity["expires"] = expires
if reason is not None:
banFromCommunity["reason"] = reason
if remove_data is not None:
banFromCommunity["remove_data"] = remove_data
return self._requestor.api(Request.POST, "/community/ban_user", json=banFromCommunity)