Skip to content

Commit 19b570f

Browse files
committed
feat: add enable_mark_as_solved functionality and database support
1 parent c88943d commit 19b570f

File tree

3 files changed

+151
-37
lines changed

3 files changed

+151
-37
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- depends: 18_post_assist
2+
ALTER TABLE pph_post_assist_config
3+
ADD COLUMN IF NOT EXISTS enable_mark_as_solved BOOLEAN DEFAULT FALSE;

src/cogs/forum/auto_tagging.py

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,13 @@
3636

3737
AUTHOR_PLACEHOLDER = "[[@author]]"
3838

39+
3940
def get_tag_options(db: DevHelpTagDB, forum: ForumChannel) -> View | None:
4041
"""Gets all tag options for the given forum."""
4142

4243
async def select_callback(interaction: Interaction):
4344
await db.update("tag_id", int(tag_selection.values[0]))
44-
await interaction.response.edit_message(
45-
content=f"Success...",
46-
view=None
47-
)
45+
await interaction.response.edit_message(content=f"Success...", view=None)
4846
view.stop()
4947

5048
view = View()
@@ -56,12 +54,14 @@ async def select_callback(interaction: Interaction):
5654

5755
for a_tag in forum.available_tags:
5856
tag_selection.add_option(
59-
label=f"{a_tag.emoji}{a_tag.name}" if a_tag.emoji else a_tag.name, value=a_tag.id
57+
label=f"{a_tag.emoji}{a_tag.name}" if a_tag.emoji else a_tag.name,
58+
value=a_tag.id,
6059
)
6160

6261
view.add_item(tag_selection)
6362
return view
6463

64+
6565
def _getter(guild: Guild, entry: dict) -> Member | Role:
6666
"""Gets the object type and returns it."""
6767

@@ -111,7 +111,7 @@ async def load(self):
111111
staff_roles,
112112
self.logger,
113113
),
114-
message_id=view["message_id"]
114+
message_id=view["message_id"],
115115
)
116116

117117
async def cog_load(self):
@@ -149,28 +149,31 @@ async def try_send() -> Message:
149149
thread.owner_id,
150150
self.dev_help_views_db,
151151
self.dev_help_tag_db,
152-
self.forum,
152+
thread.parent,
153153
staff_roles,
154154
self.logger,
155-
)
155+
),
156156
)
157157
except Forbidden:
158158
pass
159159

160-
config = await self.config.get_config("dev_help")
160+
config_id, is_enabled = await self.db.is_mark_as_solved_enabled_for_forum(
161+
thread.parent_id
162+
)
161163

162-
if not config["config_status"]:
164+
if not is_enabled:
163165
return
164166

165-
settings = await self.dev_help_tag_db.get()
166-
167-
if not settings:
167+
global_config = await self.config.get_config("auto_tagging")
168+
if not global_config["config_status"]:
168169
return
169170

170-
if not self.forum:
171+
entry = await self.db.config_by_forum(thread.parent.id)
172+
if not entry:
171173
return
172174

173-
if thread.parent_id != self.forum.id:
175+
settings = await self.dev_help_tag_db.get()
176+
if not settings:
174177
return
175178

176179
bot_message = await try_send()
@@ -179,7 +182,9 @@ async def try_send() -> Message:
179182
return
180183

181184
await bot_message.pin()
182-
await self.dev_help_views_db.add_view(thread.id, bot_message.id, thread.owner.id)
185+
await self.dev_help_views_db.add_view(
186+
thread.id, bot_message.id, thread.owner.id
187+
)
183188

184189
async def _notify_subscribers(self, thread: Thread):
185190
config = await self.config.get_config("auto_tagging")
@@ -279,15 +284,19 @@ async def create_new(self, interaction: Interaction):
279284
entity_tag_message=tag_message,
280285
reply=reply,
281286
enable_accept_solutions=enable_accept_solutions,
287+
enable_mark_as_solved=view.state.enable_mark_as_solved,
282288
)
283289

284290
if view.state.enable_mark_as_solved:
285-
forum = await self.bot.fetch_channel(view.state.forum)
286-
tag_view = get_tag_options(self.dev_help_tag_db, forum)
287-
await interaction.followup.send(view=tag_view, ephemeral=True)
288-
await tag_view.wait()
289-
290-
await interaction.followup.send("Mark as solved button configured.", ephemeral=True)
291+
forum_channel = await self.bot.fetch_channel(view.state.forum)
292+
tag_view = get_tag_options(self.dev_help_tag_db, forum_channel)
293+
if tag_view:
294+
await interaction.followup.send(view=tag_view, ephemeral=True)
295+
await tag_view.wait()
296+
297+
await interaction.followup.send(
298+
"Mark as solved button configured.", ephemeral=True
299+
)
291300

292301
await interaction.followup.send("Success!", ephemeral=True)
293302
return
@@ -364,6 +373,13 @@ async def edit(self, interaction: Interaction, config_id: int):
364373
tags = await self.db.get_tags(config_id)
365374
existing_tags: list[Role] | list[Member] = []
366375

376+
mark_as_solved_config = await self.db.get_mark_as_solved_config(config_id)
377+
current_mark_as_solved = (
378+
mark_as_solved_config["enable_mark_as_solved"]
379+
if mark_as_solved_config
380+
else False
381+
)
382+
367383
for tag in tags:
368384
tag_id = tag["entity_id"]
369385
if tag["entity_type"] == "role":
@@ -376,6 +392,7 @@ async def edit(self, interaction: Interaction, config_id: int):
376392
tag_message=tag_message,
377393
custom_msg=custom_message,
378394
existing_tags=existing_tags,
395+
enable_mark_as_solved=current_mark_as_solved,
379396
)
380397

381398
modal = PostAssistMessage(state)
@@ -400,7 +417,18 @@ async def edit(self, interaction: Interaction, config_id: int):
400417
entities=tags,
401418
entity_tag_message=tag_message,
402419
reply=reply,
420+
enable_mark_as_solved=modal.state.enable_mark_as_solved,
403421
)
422+
423+
if modal.state.enable_mark_as_solved:
424+
await interaction.followup.send(
425+
"Mark as solved button enabled for this forum.", ephemeral=True
426+
)
427+
else:
428+
await interaction.followup.send(
429+
"Mark as solved button disabled for this forum.", ephemeral=True
430+
)
431+
404432
return
405433

406434
await interaction.followup.send("Cancelled.", ephemeral=True)
@@ -416,9 +444,7 @@ async def solved(self, ctx: Context):
416444

417445
description = "This post has been marked as solved."
418446

419-
embed = Embed(
420-
description=description
421-
)
447+
embed = Embed(description=description)
422448

423449
tag = ctx.channel.parent.get_tag(tag_id)
424450

src/data/forum/post_assist.py

Lines changed: 97 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,16 @@ async def add_configuration(
139139
entity_tag_message: str,
140140
reply: str,
141141
enable_accept_solutions: bool,
142+
enable_mark_as_solved: bool = False,
142143
):
143144
"""Adds a configuration to the database.
144145
145146
:param forum_id: The forum id to add to
146147
:param entities: The entities to add
147148
:param entity_tag_message: The tag message
148149
:param reply: The reply message
150+
:param enable_accept_solutions: Whether to enable accept solutions
151+
:param enable_mark_as_solved: Whether to enable mark as solved button
149152
"""
150153

151154
async with self._pool.acquire() as conn:
@@ -155,11 +158,13 @@ async def add_configuration(
155158
"""
156159
INSERT INTO pph_post_assist_config(
157160
forum_id,
158-
enable_accept_solutions
159-
) VALUES ($1, $2)
161+
enable_accept_solutions,
162+
enable_mark_as_solved
163+
) VALUES ($1, $2, $3)
160164
""",
161165
forum_id,
162166
enable_accept_solutions,
167+
enable_mark_as_solved,
163168
)
164169

165170
config = await conn.fetchrow(
@@ -213,13 +218,15 @@ async def update_configuration(
213218
entities: list[tuple[int, str]],
214219
entity_tag_message: str,
215220
reply: str,
221+
enable_mark_as_solved: bool | None = None,
216222
):
217223
"""Updates a configuration to the database.
218224
219225
:param id: The configuration id to update
220226
:param entities: The entities to add
221227
:param entity_tag_message: The tag message
222228
:param reply: The reply message
229+
:param enable_mark_as_solved: Whether to enable mark as solved button
223230
"""
224231

225232
async with self._pool.acquire() as conn:
@@ -230,15 +237,29 @@ async def update_configuration(
230237
if not config:
231238
return
232239

233-
await conn.execute(
234-
"""
235-
UPDATE pph_post_assist_config SET
236-
forum_id = $1
237-
WHERE id = $2;
238-
""",
239-
forum_id,
240-
id,
241-
)
240+
# Update the main configuration
241+
if enable_mark_as_solved is not None:
242+
await conn.execute(
243+
"""
244+
UPDATE pph_post_assist_config SET
245+
forum_id = $1,
246+
enable_mark_as_solved = $3
247+
WHERE id = $2;
248+
""",
249+
forum_id,
250+
id,
251+
enable_mark_as_solved,
252+
)
253+
else:
254+
await conn.execute(
255+
"""
256+
UPDATE pph_post_assist_config SET
257+
forum_id = $1
258+
WHERE id = $2;
259+
""",
260+
forum_id,
261+
id,
262+
)
242263

243264
await conn.execute(
244265
"""
@@ -255,7 +276,6 @@ async def update_configuration(
255276
entity_id,
256277
entity_type
257278
) VALUES ($1, $2, $3)
258-
259279
""",
260280
config["id"],
261281
entity_id,
@@ -318,3 +338,68 @@ async def is_mark_as_solution_enabled(self, forum_id: int):
318338

319339
res = (int(res["id"]), bool(res["enable_accept_solutions"]))
320340
return res
341+
342+
async def get_mark_as_solved_config(self, config_id: int):
343+
"""Get the mark as solved configuration for a specific config ID.
344+
345+
:param config_id: The configuration ID
346+
:return: Dictionary with mark as solved settings or None
347+
"""
348+
async with self._pool.acquire() as conn:
349+
conn: Pool
350+
351+
config = await conn.fetchrow(
352+
"""
353+
SELECT enable_mark_as_solved FROM pph_post_assist_config
354+
WHERE id = $1;
355+
""",
356+
config_id,
357+
)
358+
359+
return config if config is not None else None
360+
361+
async def set_mark_as_solved_config(
362+
self, config_id: int, enable_mark_as_solved: bool
363+
):
364+
"""Update the mark as solved configuration for a config ID.
365+
366+
:param config_id: The configuration ID
367+
:param enable_mark_as_solved: Whether to enable mark as solved button
368+
"""
369+
async with self._pool.acquire() as conn:
370+
conn: Pool
371+
372+
await conn.execute(
373+
"""
374+
UPDATE pph_post_assist_config
375+
SET enable_mark_as_solved = $2
376+
WHERE id = $1;
377+
""",
378+
config_id,
379+
enable_mark_as_solved,
380+
)
381+
382+
async def is_mark_as_solved_enabled_for_forum(self, forum_id: int):
383+
"""Check if mark as solved button is enabled for a specific forum.
384+
385+
:param forum_id: The forum ID
386+
:return: Tuple of (config_id, is_enabled)
387+
"""
388+
async with self._pool.acquire() as conn:
389+
conn: Pool
390+
391+
res = await conn.fetchrow(
392+
"""
393+
SELECT id,
394+
COALESCE(enable_mark_as_solved, FALSE)
395+
as enable_mark_as_solved
396+
FROM pph_post_assist_config
397+
WHERE forum_id = $1;
398+
""",
399+
forum_id,
400+
)
401+
402+
if not res:
403+
return (-1, False)
404+
405+
return (int(res["id"]), bool(res["enable_mark_as_solved"]))

0 commit comments

Comments
 (0)