-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathpending_post.py
More file actions
291 lines (244 loc) · 10.2 KB
/
Copy pathpending_post.py
File metadata and controls
291 lines (244 loc) · 10.2 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""Pending post management"""
from dataclasses import dataclass
from datetime import datetime, timezone
from telegram import Message
from .db_manager import DbManager
@dataclass()
class PendingPost:
"""Class that represents a pending post
Args:
user_id: id of the user that sent the post
u_message_id: id of the original message of the post
g_message_id: id of the post in the group
admin_group_id: id of the admin group
credit_username: username of the user that sent the post if it's a credit post
date: when the post was sent
"""
user_id: int
u_message_id: int
g_message_id: int
admin_group_id: int
date: datetime
credit_username: str | None = None
@classmethod
def create(
cls, user_message: Message, g_message_id: int, admin_group_id: int, credit_username: str | None = None
) -> "PendingPost":
"""Creates a new post and inserts it in the table of pending posts
Args:
user_message: message sent by the user that contains the post
g_message_id: id of the post in the group
admin_group_id: id of the admin group
credit_username: username of the user that sent the post if it's a credit post
Returns:
instance of the class
"""
user_id = user_message.from_user.id
u_message_id = user_message.message_id
date = datetime.now(tz=timezone.utc)
return cls(
user_id=user_id,
u_message_id=u_message_id,
g_message_id=g_message_id,
admin_group_id=admin_group_id,
credit_username=credit_username,
date=date,
).save_post()
@classmethod
def from_group(cls, g_message_id: int, admin_group_id: int) -> "PendingPost | None":
"""Retrieves a pending post from the info related to the admin group
Args:
g_message_id: id of the post in the group
admin_group_id: id of the admin group
Returns:
instance of the class
"""
pending_post_arr = DbManager.select_from(
select="*",
table_name="pending_post",
where="admin_group_id = %s and g_message_id = %s",
where_args=(admin_group_id, g_message_id),
)
if not pending_post_arr:
return None
pending_post = pending_post_arr[0]
return cls(
user_id=pending_post["user_id"],
u_message_id=pending_post["u_message_id"],
admin_group_id=pending_post["admin_group_id"],
g_message_id=pending_post["g_message_id"],
credit_username=pending_post["credit_username"],
date=pending_post["message_date"],
)
@classmethod
def from_user(cls, user_id: int) -> "PendingPost | None":
"""Retrieves a pending post from the user_id
Args:
user_id: id of the author of the post
Returns:
instance of the class
"""
pending_post_arr = DbManager.select_from(
select="*", table_name="pending_post", where="user_id = %s", where_args=(user_id,)
)
if not pending_post_arr:
return None
pending_post = pending_post_arr[0]
return cls(
user_id=pending_post["user_id"],
u_message_id=pending_post["u_message_id"],
admin_group_id=pending_post["admin_group_id"],
g_message_id=pending_post["g_message_id"],
credit_username=pending_post["credit_username"],
date=pending_post["message_date"],
)
@staticmethod
def get_all(admin_group_id: int, before: datetime | None = None) -> list["PendingPost"]:
"""Gets the list of pending posts in the specified admin group.
If before is specified, returns only the one sent before that timestamp
Args:
admin_group_id: id of the admin group
before: timestamp before which messages will be considered
Returns:
list of ids of pending posts
"""
if before:
pending_posts_id = DbManager.select_from(
select="g_message_id",
table_name="pending_post",
where="admin_group_id = %s and (message_date < %s or message_date IS NULL)",
where_args=(admin_group_id, before),
)
else:
pending_posts_id = DbManager.select_from(
select="g_message_id",
table_name="pending_post",
where="admin_group_id = %s",
where_args=(admin_group_id,),
)
pending_posts = []
for post in pending_posts_id:
g_message_id = int(post["g_message_id"])
pending_post = PendingPost.from_group(admin_group_id=admin_group_id, g_message_id=g_message_id)
if pending_post is not None:
pending_posts.append(pending_post)
return pending_posts
def save_post(self) -> "PendingPost":
"""Saves the pending_post in the database"""
columns: tuple[str, ...] = ("user_id", "u_message_id", "g_message_id", "admin_group_id", "message_date")
values: tuple[int | datetime | str, ...] = (
self.user_id,
self.u_message_id,
self.g_message_id,
self.admin_group_id,
self.date,
)
if self.credit_username is not None:
columns += ("credit_username",)
values += (self.credit_username,)
DbManager.insert_into(
table_name="pending_post",
columns=columns,
values=values,
)
return self
def get_votes(self, vote: bool) -> int:
"""Gets all the votes of a specific kind (approve or reject)
Args:
vote: whether you look for the approve or reject votes
Returns:
number of votes
"""
return DbManager.count_from(
table_name="admin_votes",
where="g_message_id = %s and admin_group_id = %s and is_upvote = %s",
where_args=(self.g_message_id, self.admin_group_id, vote),
)
def get_credit_username(self) -> str | None:
"""Gets the username of the user that credited the post
Returns:
username of the user that credited the post, or None if the post is not credited
"""
return self.credit_username
def get_list_admin_votes(self, vote: "bool | None" = None) -> "list[int] | list[tuple[int, bool]]":
"""Gets the list of admins that approved or rejected the post
Args:
vote: whether you look for the approve or reject votes, or None if you want all the votes
Returns:
list of admins that approved or rejected a pending post
"""
where = "g_message_id = %s and admin_group_id = %s"
where_args: tuple[int | bool, ...] = (self.g_message_id, self.admin_group_id)
if vote is not None:
where += " and is_upvote = %s"
where_args = (self.g_message_id, self.admin_group_id, vote)
votes = DbManager.select_from(
select="admin_id, is_upvote", table_name="admin_votes", where=where, where_args=where_args
)
if vote is None:
return [(vote["admin_id"], vote["is_upvote"]) for vote in votes]
return [vote["admin_id"] for vote in votes]
def __get_admin_vote(self, admin_id: int) -> bool | None:
"""Gets the vote of a specific admin on a pending post
Args:
admin_id: id of the admin that voted
Returns:
a bool representing the vote or None if a vote was not yet made
"""
vote = DbManager.select_from(
select="is_upvote",
table_name="admin_votes",
where="admin_id = %s and g_message_id = %s and admin_group_id = %s",
where_args=(admin_id, self.g_message_id, self.admin_group_id),
)
if len(vote) == 0: # the vote is not present
return None
return vote[0]["is_upvote"]
def set_admin_vote(self, admin_id: int, approval: bool) -> int:
"""Adds the vote of the admin on a specific post, or update the existing vote, if needed
Args:
admin_id: id of the admin that voted
approval: whether the vote is approval or reject
Returns:
number of similar votes (all the approve or the reject), or -1 if the vote wasn't updated
"""
vote = self.__get_admin_vote(admin_id)
if vote is None: # there isn't a vote yet
DbManager.insert_into(
table_name="admin_votes",
columns=("admin_id", "g_message_id", "admin_group_id", "is_upvote"),
values=(admin_id, self.g_message_id, self.admin_group_id, approval),
)
number_of_votes = self.get_votes(vote=approval)
elif bool(vote) != approval: # the vote was different from the approval
DbManager.update_from(
table_name="admin_votes",
set_clause="is_upvote = %s",
where="admin_id = %s and g_message_id = %s and admin_group_id = %s",
args=(approval, admin_id, self.g_message_id, self.admin_group_id),
)
number_of_votes = self.get_votes(vote=approval)
else:
return -1
return number_of_votes
def delete_post(self):
"""Removes all entries on a post that is no longer pending"""
DbManager.delete_from(
table_name="pending_post",
where="g_message_id = %s and admin_group_id = %s",
where_args=(self.g_message_id, self.admin_group_id),
)
DbManager.delete_from(
table_name="admin_votes",
where="g_message_id = %s and admin_group_id = %s",
where_args=(self.g_message_id, self.admin_group_id),
)
def __repr__(self) -> str:
return (
f"PendingPost: [ user_id: {self.user_id}\n"
f"u_message_id: {self.u_message_id}\n"
f"admin_group_id: {self.admin_group_id}\n"
f"g_message_id: {self.g_message_id}\n"
f"credit_username: {self.credit_username}\n"
f"date : {self.date} ]"
)