Skip to content

Commit 58b1292

Browse files
committed
refactor MarkHelpThreadCloseInDBRoutine and changes
* update constructor with HelpThreadLifecycleListener instance * using HelpThreadLifecycleListener method to clean up left over threads * increasing scope of handleArchiveStatus to package level * adding logger to MarkHelpThreadCloseInDBRoutine class
1 parent ab0fe61 commit 58b1292

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

application/src/main/java/org/togetherjava/tjbot/features/Features.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
8888
new CodeMessageHandler(blacklistConfig.special(), jshellEval);
8989
ChatGptService chatGptService = new ChatGptService(config);
9090
HelpSystemHelper helpSystemHelper = new HelpSystemHelper(config, database, chatGptService);
91+
HelpThreadLifecycleListener helpThreadLifecycleListener =
92+
new HelpThreadLifecycleListener(helpSystemHelper, database);
9193

9294
// NOTE The system can add special system relevant commands also by itself,
9395
// hence this list may not necessarily represent the full list of all commands actually
@@ -106,7 +108,7 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
106108
.add(new AutoPruneHelperRoutine(config, helpSystemHelper, modAuditLogWriter, database));
107109
features.add(new HelpThreadAutoArchiver(helpSystemHelper));
108110
features.add(new LeftoverBookmarksCleanupRoutine(bookmarksSystem));
109-
features.add(new MarkHelpThreadCloseInDBRoutine(database));
111+
features.add(new MarkHelpThreadCloseInDBRoutine(database, helpThreadLifecycleListener));
110112

111113
// Message receivers
112114
features.add(new TopHelpersMessageListener(database, config));

application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadLifecycleListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private void handleThreadStatus(ThreadChannel threadChannel) {
7979
changeStatusToActive(threadId);
8080
}
8181

82-
private void handleArchiveStatus(Instant closedAt, ThreadChannel threadChannel) {
82+
void handleArchiveStatus(Instant closedAt, ThreadChannel threadChannel) {
8383
long threadId = threadChannel.getIdLong();
8484
int messageCount = threadChannel.getMessageCount();
8585
int participantsExceptAuthor = threadChannel.getMemberCount() - 1;
Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package org.togetherjava.tjbot.features.help;
22

33
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;
47

58
import org.togetherjava.tjbot.db.Database;
9+
import org.togetherjava.tjbot.db.generated.tables.records.HelpThreadsRecord;
610
import org.togetherjava.tjbot.features.Routine;
711

812
import java.time.Instant;
913
import java.time.temporal.ChronoUnit;
14+
import java.util.List;
1015
import java.util.concurrent.TimeUnit;
1116

1217
import static org.togetherjava.tjbot.db.generated.tables.HelpThreads.HELP_THREADS;
@@ -16,36 +21,49 @@
1621
* closed.
1722
*/
1823
public final class MarkHelpThreadCloseInDBRoutine implements Routine {
24+
private final Logger logger = LoggerFactory.getLogger(MarkHelpThreadCloseInDBRoutine.class);
1925
private final Database database;
26+
private final HelpThreadLifecycleListener helpThreadLifecycleListener;
2027

2128
/**
2229
* Creates a new instance.
2330
*
2431
* @param database the database to store help thread metadata in
2532
*/
26-
public MarkHelpThreadCloseInDBRoutine(Database database) {
33+
public MarkHelpThreadCloseInDBRoutine(Database database,
34+
HelpThreadLifecycleListener helpThreadLifecycleListener) {
2735
this.database = database;
36+
this.helpThreadLifecycleListener = helpThreadLifecycleListener;
2837
}
2938

30-
3139
@Override
3240
public Schedule createSchedule() {
3341
return new Schedule(ScheduleMode.FIXED_RATE, 0, 1, TimeUnit.HOURS);
3442
}
3543

3644
@Override
3745
public void runRoutine(JDA jda) {
38-
updateTicketStatus();
46+
updateTicketStatus(jda);
3947
}
4048

41-
private void updateTicketStatus() {
49+
private void updateTicketStatus(JDA jda) {
4250
Instant now = Instant.now();
4351
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+
4459

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+
});
5068
}
5169
}

0 commit comments

Comments
 (0)