Skip to content

Commit 4173da4

Browse files
authored
Bugfix/npe help thread close (#1160)
* handle thread channel no longer exists * apply spotless * remove logger, no longer needed * handle unexpected errors while marking threads archived in database * spotless formatting * suppress warning from sonar for usage of throwable * catch base exception instead of throwable
1 parent 6ee7eba commit 4173da4

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

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

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.togetherjava.tjbot.features.help;
22

3+
import net.dv8tion.jda.api.JDA;
34
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
45
import net.dv8tion.jda.api.entities.channel.forums.ForumTag;
56
import net.dv8tion.jda.api.events.channel.update.ChannelUpdateAppliedTagsEvent;
@@ -75,14 +76,25 @@ private void handleThreadStatus(ThreadChannel threadChannel) {
7576
boolean isArchived = threadChannel.isArchived();
7677

7778
if (isArchived) {
78-
handleArchiveStatus(closedAt, threadChannel);
79+
handleArchiveStatus(closedAt, threadId, threadChannel.getJDA());
7980
return;
8081
}
8182

8283
updateThreadStatusToActive(threadId);
8384
}
8485

85-
void handleArchiveStatus(Instant closedAt, ThreadChannel threadChannel) {
86+
void handleArchiveStatus(Instant closedAt, long id, JDA jda) {
87+
ThreadChannel threadChannel = jda.getThreadChannelById(id);
88+
if (threadChannel == null) {
89+
logger.info("thread with id: {} no longer exists, marking archived in records", id);
90+
database.write(context -> context.update(HELP_THREADS)
91+
.set(HELP_THREADS.CLOSED_AT, closedAt)
92+
.set(HELP_THREADS.TICKET_STATUS, HelpSystemHelper.TicketStatus.ARCHIVED.val)
93+
.where(HELP_THREADS.CHANNEL_ID.eq(id))
94+
.execute());
95+
return;
96+
}
97+
8698
long threadId = threadChannel.getIdLong();
8799
int messageCount = threadChannel.getMessageCount();
88100
int participantsExceptAuthor = threadChannel.getMemberCount() - 1;
@@ -96,6 +108,7 @@ void handleArchiveStatus(Instant closedAt, ThreadChannel threadChannel) {
96108
.execute());
97109

98110
logger.info("Thread with id: {}, updated to archived status in database", threadId);
111+
99112
}
100113

101114
private void updateThreadStatusToActive(long threadId) {

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
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;
54
import org.slf4j.Logger;
65
import org.slf4j.LoggerFactory;
76

@@ -59,13 +58,12 @@ private void updateTicketStatus(JDA jda) {
5958
.map(HelpThreadsRecord::getChannelId)
6059
.toList());
6160

62-
6361
threadIdsToClose.forEach(id -> {
6462
try {
65-
ThreadChannel threadChannel = jda.getThreadChannelById(id);
66-
helpThreadLifecycleListener.handleArchiveStatus(now, threadChannel);
63+
helpThreadLifecycleListener.handleArchiveStatus(now, id, jda);
6764
} catch (Exception exception) {
68-
logger.warn("unable to mark thread as close with id :{}", id, exception);
65+
logger.warn("Failed to update status of thread with id: {} to archived", id,
66+
exception);
6967
}
7068
});
7169
}

0 commit comments

Comments
 (0)