|
1 | 1 | package org.togetherjava.tjbot.features.help;
|
2 | 2 |
|
3 | 3 | import net.dv8tion.jda.api.JDA;
|
| 4 | +import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
4 | 7 |
|
5 | 8 | import org.togetherjava.tjbot.db.Database;
|
| 9 | +import org.togetherjava.tjbot.db.generated.tables.records.HelpThreadsRecord; |
6 | 10 | import org.togetherjava.tjbot.features.Routine;
|
7 | 11 |
|
8 | 12 | import java.time.Instant;
|
9 | 13 | import java.time.temporal.ChronoUnit;
|
| 14 | +import java.util.List; |
10 | 15 | import java.util.concurrent.TimeUnit;
|
11 | 16 |
|
12 | 17 | import static org.togetherjava.tjbot.db.generated.tables.HelpThreads.HELP_THREADS;
|
|
16 | 21 | * closed.
|
17 | 22 | */
|
18 | 23 | public final class MarkHelpThreadCloseInDBRoutine implements Routine {
|
| 24 | + private final Logger logger = LoggerFactory.getLogger(MarkHelpThreadCloseInDBRoutine.class); |
19 | 25 | private final Database database;
|
| 26 | + private final HelpThreadLifecycleListener helpThreadLifecycleListener; |
20 | 27 |
|
21 | 28 | /**
|
22 | 29 | * Creates a new instance.
|
23 | 30 | *
|
24 | 31 | * @param database the database to store help thread metadata in
|
25 | 32 | */
|
26 |
| - public MarkHelpThreadCloseInDBRoutine(Database database) { |
| 33 | + public MarkHelpThreadCloseInDBRoutine(Database database, |
| 34 | + HelpThreadLifecycleListener helpThreadLifecycleListener) { |
27 | 35 | this.database = database;
|
| 36 | + this.helpThreadLifecycleListener = helpThreadLifecycleListener; |
28 | 37 | }
|
29 | 38 |
|
30 |
| - |
31 | 39 | @Override
|
32 | 40 | public Schedule createSchedule() {
|
33 | 41 | return new Schedule(ScheduleMode.FIXED_RATE, 0, 1, TimeUnit.HOURS);
|
34 | 42 | }
|
35 | 43 |
|
36 | 44 | @Override
|
37 | 45 | public void runRoutine(JDA jda) {
|
38 |
| - updateTicketStatus(); |
| 46 | + updateTicketStatus(jda); |
39 | 47 | }
|
40 | 48 |
|
41 |
| - private void updateTicketStatus() { |
| 49 | + private void updateTicketStatus(JDA jda) { |
42 | 50 | Instant now = Instant.now();
|
43 | 51 | Instant threeDaysAgo = now.minus(3, ChronoUnit.DAYS);
|
| 52 | + List<Long> threadIdsToClose = database.read(context -> context.selectFrom(HELP_THREADS) |
| 53 | + .where(HELP_THREADS.TICKET_STATUS.eq(HelpSystemHelper.TicketStatus.ACTIVE.val)) |
| 54 | + .and(HELP_THREADS.CREATED_AT.lessThan(threeDaysAgo)) |
| 55 | + .stream() |
| 56 | + .map(HelpThreadsRecord::getChannelId) |
| 57 | + .toList()); |
| 58 | + |
44 | 59 |
|
45 |
| - database.write(context -> context.update(HELP_THREADS) |
46 |
| - .set(HELP_THREADS.TICKET_STATUS, HelpSystemHelper.TicketStatus.ARCHIVED.val) |
47 |
| - .where(HELP_THREADS.CREATED_AT.lessOrEqual(threeDaysAgo) |
48 |
| - .and(HELP_THREADS.TICKET_STATUS.eq(HelpSystemHelper.TicketStatus.ACTIVE.val))) |
49 |
| - .execute()); |
| 60 | + threadIdsToClose.forEach(id -> { |
| 61 | + try { |
| 62 | + ThreadChannel threadChannel = jda.getThreadChannelById(id); |
| 63 | + helpThreadLifecycleListener.handleArchiveStatus(now, threadChannel); |
| 64 | + } catch (Exception exception) { |
| 65 | + logger.warn("unable to mark thread as close with id :{}", id, exception); |
| 66 | + } |
| 67 | + }); |
50 | 68 | }
|
51 | 69 | }
|
0 commit comments