Skip to content

Commit

Permalink
Bugfix/npe help thread close (#1160)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ankitsmt211 authored Sep 27, 2024
1 parent 6ee7eba commit 4173da4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.togetherjava.tjbot.features.help;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
import net.dv8tion.jda.api.entities.channel.forums.ForumTag;
import net.dv8tion.jda.api.events.channel.update.ChannelUpdateAppliedTagsEvent;
Expand Down Expand Up @@ -75,14 +76,25 @@ private void handleThreadStatus(ThreadChannel threadChannel) {
boolean isArchived = threadChannel.isArchived();

if (isArchived) {
handleArchiveStatus(closedAt, threadChannel);
handleArchiveStatus(closedAt, threadId, threadChannel.getJDA());
return;
}

updateThreadStatusToActive(threadId);
}

void handleArchiveStatus(Instant closedAt, ThreadChannel threadChannel) {
void handleArchiveStatus(Instant closedAt, long id, JDA jda) {
ThreadChannel threadChannel = jda.getThreadChannelById(id);
if (threadChannel == null) {
logger.info("thread with id: {} no longer exists, marking archived in records", id);
database.write(context -> context.update(HELP_THREADS)
.set(HELP_THREADS.CLOSED_AT, closedAt)
.set(HELP_THREADS.TICKET_STATUS, HelpSystemHelper.TicketStatus.ARCHIVED.val)
.where(HELP_THREADS.CHANNEL_ID.eq(id))
.execute());
return;
}

long threadId = threadChannel.getIdLong();
int messageCount = threadChannel.getMessageCount();
int participantsExceptAuthor = threadChannel.getMemberCount() - 1;
Expand All @@ -96,6 +108,7 @@ void handleArchiveStatus(Instant closedAt, ThreadChannel threadChannel) {
.execute());

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

}

private void updateThreadStatusToActive(long threadId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.togetherjava.tjbot.features.help;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -59,13 +58,12 @@ private void updateTicketStatus(JDA jda) {
.map(HelpThreadsRecord::getChannelId)
.toList());


threadIdsToClose.forEach(id -> {
try {
ThreadChannel threadChannel = jda.getThreadChannelById(id);
helpThreadLifecycleListener.handleArchiveStatus(now, threadChannel);
helpThreadLifecycleListener.handleArchiveStatus(now, id, jda);
} catch (Exception exception) {
logger.warn("unable to mark thread as close with id :{}", id, exception);
logger.warn("Failed to update status of thread with id: {} to archived", id,
exception);
}
});
}
Expand Down

0 comments on commit 4173da4

Please sign in to comment.