-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathreport.py
More file actions
246 lines (207 loc) · 8.16 KB
/
Copy pathreport.py
File metadata and controls
246 lines (207 loc) · 8.16 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
"""Reports management"""
from dataclasses import dataclass
from datetime import datetime
from telegram import Message
from .db_manager import DbManager
@dataclass()
class Report:
"""Class that represents a report
Args:
user_id: id of the user that reported
admin_group_id: id of the admin group
g_message_id: id of the post in the group
channel_id: id of the channel
c_message_id: id of the post in question in the channel
target_username: username of the reported user
date: when the report happened
"""
user_id: int
admin_group_id: int
g_message_id: int
channel_id: int | None = None
c_message_id: int | None = None
target_username: str | None = None
date: datetime | None = None
@property
def minutes_passed(self) -> float:
""":class:`float`:Amount of minutes elapsed from when the report was submitted, if applicable"""
if self.date is None:
return -1
delta_time = datetime.now() - self.date
return delta_time.total_seconds() / 60
@classmethod
def create_post_report(
cls, user_id: int, channel_id: int, c_message_id: int, admin_message: Message
) -> "Report | None":
"""Adds the report of the user on a specific post
Args:
user_id: id of the user that reported
channel_id: id of the channel
c_message_id: id of the post in question in the channel
admin_message: message received in the admin group that references the report
Returns:
instance of the class or None if the report was not created
"""
g_message_id = admin_message.message_id
admin_group_id = admin_message.chat_id
current_report = cls.get_post_report(user_id, channel_id, c_message_id)
if current_report: # there is already a report, the creation fails
return None
return cls(
user_id=user_id,
channel_id=channel_id,
c_message_id=c_message_id,
admin_group_id=admin_group_id,
g_message_id=g_message_id,
date=datetime.now(),
).save_report()
@classmethod
def create_user_report(cls, user_id: int, target_username: str, admin_message: Message) -> "Report":
"""Adds the report of the user targeting another user
Args:
user_id: id of the user that reported
target_username: username of reported user
admin_message: message received in the admin group that references the report
Returns:
instance of the class
"""
g_message_id = admin_message.message_id
admin_group_id = admin_message.chat_id
return cls(
user_id=user_id,
admin_group_id=admin_group_id,
g_message_id=g_message_id,
target_username=target_username,
date=datetime.now(),
).save_report()
@classmethod
def get_post_report(cls, user_id: int, channel_id: int, c_message_id: int) -> "Report | None":
"""Gets the report of a specific user on a published post
Args:
user_id: id of the user that reported
channel_id: id of the channel
c_message_id: id of the post in question in the channel
Returns:
instance of the class or None if the report was not present
"""
reports = DbManager.select_from(
select="*",
table_name="spot_report",
where="user_id = %s and channel_id = %s and c_message_id = %s",
where_args=(user_id, channel_id, c_message_id),
)
if len(reports) == 0: # the report is not present
return None
report = reports[0]
return cls(
user_id=report["user_id"],
channel_id=report["channel_id"],
c_message_id=report["c_message_id"],
admin_group_id=report["admin_group_id"],
g_message_id=report["g_message_id"],
date=report["message_date"],
)
@classmethod
def get_last_user_report(cls, user_id: int) -> "Report | None":
"""Gets the last user report of a specific user
Args:
user_id: id of the user that reported
Returns:
instance of the class or None if the report was not present
"""
reports = DbManager.select_from(
select="*",
table_name="user_report",
where="user_id = %s",
where_args=(user_id,),
order_by="message_date DESC",
)
if len(reports) == 0: # the vote is not present
return None
report = reports[0]
return cls(
user_id=report["user_id"],
target_username=report["target_username"],
date=report["message_date"],
admin_group_id=report["admin_group_id"],
g_message_id=report["g_message_id"],
)
@classmethod
def from_group(cls, admin_group_id: int, g_message_id: int) -> "Report | None":
"""Gets a report of any type related to the specified message in the admin group
Args:
admin_group_id: id of the admin group
g_message_id: id of the report in the group
Returns:
instance of the class or None if the report was not present
"""
reports = DbManager.select_from(
select="*",
table_name="user_report",
where="admin_group_id = %s and g_message_id = %s",
where_args=(admin_group_id, g_message_id),
)
if len(reports) > 0: # the report has been found
report = reports[0]
return cls(
user_id=report["user_id"],
target_username=report["target_username"],
date=report["message_date"],
admin_group_id=report["admin_group_id"],
g_message_id=report["g_message_id"],
)
reports = DbManager.select_from(
select="*",
table_name="spot_report",
where="admin_group_id = %s and g_message_id = %s",
where_args=(admin_group_id, g_message_id),
)
if len(reports) > 0: # the report has been found
report = reports[0]
return cls(
user_id=report["user_id"],
c_message_id=report["c_message_id"],
admin_group_id=report["admin_group_id"],
g_message_id=report["g_message_id"],
date=report["message_date"],
)
return None
def save_report(self) -> "Report":
"""Saves the report in the database"""
if self.c_message_id is not None:
DbManager.insert_into(
table_name="spot_report",
columns=("user_id", "channel_id", "message_date", "c_message_id", "admin_group_id", "g_message_id"),
values=(
self.user_id,
self.channel_id,
self.date,
self.c_message_id,
self.admin_group_id,
self.g_message_id,
),
)
else:
DbManager.insert_into(
table_name="user_report",
columns=("user_id", "target_username", "message_date", "admin_group_id", "g_message_id"),
values=(self.user_id, self.target_username, self.date, self.admin_group_id, self.g_message_id),
)
return self
def __repr__(self) -> str:
if self.c_message_id is not None:
return (
f"PostReport: [ user_id: {self.user_id}\n"
f"channel_id: {self.channel_id}\n"
f"c_message_id: {self.c_message_id}\n"
f"date: {self.date}\n"
f"admin_group_id: {self.admin_group_id}\n"
f"g_message_id: {self.g_message_id} ]"
)
return (
f"UserReport: [ user_id: {self.user_id}\n"
f"target_username: {self.target_username}\n"
f"date: {self.date}\n"
f"admin_group_id: {self.admin_group_id}\n"
f"g_message_id: {self.g_message_id} ]"
)