11from asyncpg import Pool
2+ from datetime import datetime
3+
4+
5+ class AcceptedSolution :
6+ """Represents an accepted solution."""
7+
8+ id : int
9+ thread_id : int
10+ post_assist_config_id : int
11+ user_id : int
12+ message_id : int
13+
14+ def __init__ (
15+ self ,
16+ thread_id : int ,
17+ post_assist_config_id : int ,
18+ user_id : int ,
19+ message_id : int ,
20+ ):
21+ self .thread_id = thread_id
22+ self .post_assist_config_id = post_assist_config_id
23+ self .user_id = user_id
24+ self .message_id = message_id
225
326
427class PostAssistDB :
@@ -14,10 +37,13 @@ async def get_config(self, id: int):
1437 async with self ._pool .acquire () as conn :
1538 conn : Pool
1639
17- config = await conn .fetchrow ("""
40+ config = await conn .fetchrow (
41+ """
1842 SELECT * FROM pph_post_assist_config
1943 WHERE id = $1;
20- """ , id )
44+ """ ,
45+ id ,
46+ )
2147
2248 return config if config is not None else None
2349
@@ -30,10 +56,13 @@ async def config_by_forum(self, forum_id: int):
3056 async with self ._pool .acquire () as conn :
3157 conn : Pool
3258
33- config = await conn .fetchrow ("""
59+ config = await conn .fetchrow (
60+ """
3461 SELECT * FROM pph_post_assist_config
3562 WHERE forum_id = $1;
36- """ , forum_id )
63+ """ ,
64+ forum_id ,
65+ )
3766
3867 return config if config is not None else None
3968
@@ -49,10 +78,13 @@ async def get_reply(self, config_id: int):
4978 async with self ._pool .acquire () as conn :
5079 conn : Pool
5180
52- config = await conn .fetchrow ("""
81+ config = await conn .fetchrow (
82+ """
5383 SELECT * FROM pph_post_assist_reply
5484 WHERE configuration_id = $1;
55- """ , config_id )
85+ """ ,
86+ config_id ,
87+ )
5688
5789 return config ["custom_message" ] if config is not None else None
5890
@@ -68,10 +100,13 @@ async def get_tags(self, config_id: int):
68100 async with self ._pool .acquire () as conn :
69101 conn : Pool
70102
71- config = await conn .fetch ("""
103+ config = await conn .fetch (
104+ """
72105 SELECT * FROM pph_post_assist_tags
73106 WHERE configuration_id = $1;
74- """ , config_id )
107+ """ ,
108+ config_id ,
109+ )
75110
76111 return config or []
77112
@@ -87,10 +122,13 @@ async def get_tag_message(self, config_id: int):
87122 async with self ._pool .acquire () as conn :
88123 conn : Pool
89124
90- config = await conn .fetchrow ("""
125+ config = await conn .fetchrow (
126+ """
91127 SELECT * FROM pph_post_assist_tag_message
92128 WHERE configuration_id = $1;
93- """ , config_id )
129+ """ ,
130+ config_id ,
131+ )
94132
95133 return config ["custom_message" ] if config is not None else None
96134
@@ -122,7 +160,8 @@ async def add_configuration(
122160 forum_id : int ,
123161 entities : list [tuple [int , str ]],
124162 entity_tag_message : str ,
125- reply : str
163+ reply : str ,
164+ enable_accept_solutions : bool ,
126165 ):
127166 """Adds a configuration to the database.
128167
@@ -135,47 +174,68 @@ async def add_configuration(
135174 async with self ._pool .acquire () as conn :
136175 conn : Pool
137176
138- await conn .execute ("""
177+ await conn .execute (
178+ """
139179 INSERT INTO pph_post_assist_config(
140- forum_id
141- ) VALUES ($1)
142- """ , forum_id )
180+ forum_id,
181+ enable_accept_solutions
182+ ) VALUES ($1, $2)
183+ """ ,
184+ forum_id ,
185+ enable_accept_solutions ,
186+ )
143187
144- config = await conn .fetchrow ("""
188+ config = await conn .fetchrow (
189+ """
145190 SELECT * FROM pph_post_assist_config
146191 WHERE forum_id = $1;
147- """ , forum_id )
192+ """ ,
193+ forum_id ,
194+ )
148195
149196 for entity_id , entity_type in entities :
150- await conn .execute ("""
197+ await conn .execute (
198+ """
151199 INSERT INTO pph_post_assist_tags(
152200 configuration_id,
153201 entity_id,
154202 entity_type
155203 ) VALUES ($1, $2, $3)
156- """ , config ["id" ], entity_id , entity_type )
157-
158- await conn .execute ("""
204+ """ ,
205+ config ["id" ],
206+ entity_id ,
207+ entity_type ,
208+ )
209+
210+ await conn .execute (
211+ """
159212 INSERT INTO pph_post_assist_tag_message(
160213 configuration_id,
161214 custom_message
162215 ) VALUES ($1, $2)
163- """ , config ["id" ], entity_tag_message )
216+ """ ,
217+ config ["id" ],
218+ entity_tag_message ,
219+ )
164220
165- await conn .execute ("""
221+ await conn .execute (
222+ """
166223 INSERT INTO pph_post_assist_reply(
167224 configuration_id,
168225 custom_message
169226 ) VALUES ($1, $2)
170- """ , config ["id" ], reply )
227+ """ ,
228+ config ["id" ],
229+ reply ,
230+ )
171231
172232 async def update_configuration (
173233 self ,
174234 id : int ,
175235 forum_id : int ,
176236 entities : list [tuple [int , str ]],
177237 entity_tag_message : str ,
178- reply : str
238+ reply : str ,
179239 ):
180240 """Updates a configuration to the database.
181241
@@ -193,37 +253,57 @@ async def update_configuration(
193253 if not config :
194254 return
195255
196- await conn .execute ("""
256+ await conn .execute (
257+ """
197258 UPDATE pph_post_assist_config SET
198259 forum_id = $1
199260 WHERE id = $2;
200- """ , forum_id , id )
261+ """ ,
262+ forum_id ,
263+ id ,
264+ )
201265
202- await conn .execute ("""
266+ await conn .execute (
267+ """
203268 DELETE FROM pph_post_assist_tags WHERE configuration_id = $1;
204- """ , id )
269+ """ ,
270+ id ,
271+ )
205272
206273 for entity_id , entity_type in entities :
207- await conn .execute ("""
274+ await conn .execute (
275+ """
208276 INSERT INTO pph_post_assist_tags(
209277 configuration_id,
210278 entity_id,
211279 entity_type
212280 ) VALUES ($1, $2, $3)
213281
214- """ , config ["id" ], entity_id , entity_type )
215-
216- await conn .execute ("""
282+ """ ,
283+ config ["id" ],
284+ entity_id ,
285+ entity_type ,
286+ )
287+
288+ await conn .execute (
289+ """
217290 UPDATE pph_post_assist_tag_message SET
218291 custom_message = $1
219292 WHERE configuration_id = $2;
220- """ , entity_tag_message , config ["id" ])
293+ """ ,
294+ entity_tag_message ,
295+ config ["id" ],
296+ )
221297
222- await conn .execute ("""
298+ await conn .execute (
299+ """
223300 UPDATE pph_post_assist_reply SET
224301 custom_message = $1
225302 WHERE configuration_id = $2;
226- """ , reply , config ["id" ])
303+ """ ,
304+ reply ,
305+ config ["id" ],
306+ )
227307
228308 async def delete_configuration (self , id : int ):
229309 """Deletes a configuration from the database.
@@ -234,7 +314,98 @@ async def delete_configuration(self, id: int):
234314 async with self ._pool .acquire () as conn :
235315 conn : Pool
236316
237- await conn .execute ("""
317+ await conn .execute (
318+ """
238319 DELETE FROM pph_post_assist_config WHERE id = $1;
239- """ , id )
320+ """ ,
321+ id ,
322+ )
240323
324+ async def is_mark_as_solution_enabled (self , forum_id : int ):
325+ """Checks if the mark as solution is enabled for a forum."""
326+
327+ async with self ._pool .acquire () as conn :
328+ conn : Pool
329+
330+ res = await conn .fetchrow (
331+ """
332+ SELECT id, enable_accept_solutions
333+ FROM pph_post_assist_config
334+ WHERE forum_id = $1;
335+ """ ,
336+ forum_id ,
337+ )
338+
339+ if not res :
340+ return (- 1 , False )
341+
342+ res = (int (res ["id" ]), bool (res ["enable_accept_solutions" ]))
343+ return res
344+
345+ async def mark_as_solution (
346+ self , post_assist_id : int , thread_id : int , message_id : int , user_id : int
347+ ):
348+ """Marks a post as a solution."""
349+
350+ async with self ._pool .acquire () as conn :
351+ conn : Pool
352+
353+ await conn .execute (
354+ """
355+ INSERT INTO pph_post_assist_config_accept_solutions(
356+ post_assist_config_id,
357+ message_id,
358+ thread_id,
359+ user_id
360+ ) VALUES (
361+ $1,
362+ $2,
363+ $3,
364+ $4
365+ )
366+ """ ,
367+ post_assist_id ,
368+ message_id ,
369+ thread_id ,
370+ user_id ,
371+ )
372+
373+ async def get_accepted_solution (self , thread_id : int ) -> AcceptedSolution | None :
374+ """Gets the accepted solution for a forum."""
375+
376+ async with self ._pool .acquire () as conn :
377+ conn : Pool
378+
379+ res = await conn .fetchrow (
380+ """
381+ SELECT * FROM pph_post_assist_config_accept_solutions
382+ WHERE thread_id = $1;
383+ """ ,
384+ thread_id ,
385+ )
386+
387+ if not res :
388+ return None
389+
390+ accepted_solution = AcceptedSolution (
391+ res ["thread_id" ],
392+ res ["post_assist_config_id" ],
393+ res ["user_id" ],
394+ res ["message_id" ],
395+ )
396+
397+ return accepted_solution
398+
399+ async def delete_solution (self , forum_id : int ):
400+ """Deletes the accepted solution for a forum."""
401+
402+ async with self ._pool .acquire () as conn :
403+ conn : Pool
404+
405+ await conn .execute (
406+ """
407+ DELETE FROM pph_post_assist_config_accept_solutions
408+ WHERE forum_id = $1;
409+ """ ,
410+ forum_id ,
411+ )
0 commit comments