From f2623e8b9d77676f1eed23bcb1d24f892c7c8972 Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Sat, 31 Aug 2024 22:44:56 +0530 Subject: [PATCH 1/7] handle thread channel no longer exists --- .../help/HelpThreadLifecycleListener.java | 29 ++++++++++++++----- .../help/MarkHelpThreadCloseInDBRoutine.java | 10 +------ 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadLifecycleListener.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadLifecycleListener.java index 3c2e778ba1..29f35dbcc5 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadLifecycleListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadLifecycleListener.java @@ -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; @@ -75,27 +76,39 @@ 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; database.write(context -> context.update(HELP_THREADS) - .set(HELP_THREADS.CLOSED_AT, closedAt) - .set(HELP_THREADS.TICKET_STATUS, HelpSystemHelper.TicketStatus.ARCHIVED.val) - .set(HELP_THREADS.MESSAGE_COUNT, messageCount) - .set(HELP_THREADS.PARTICIPANTS, participantsExceptAuthor) - .where(HELP_THREADS.CHANNEL_ID.eq(threadId)) - .execute()); + .set(HELP_THREADS.CLOSED_AT, closedAt) + .set(HELP_THREADS.TICKET_STATUS, HelpSystemHelper.TicketStatus.ARCHIVED.val) + .set(HELP_THREADS.MESSAGE_COUNT, messageCount) + .set(HELP_THREADS.PARTICIPANTS, participantsExceptAuthor) + .where(HELP_THREADS.CHANNEL_ID.eq(threadId)) + .execute()); logger.info("Thread with id: {}, updated to archived status in database", threadId); + } private void updateThreadStatusToActive(long threadId) { diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java b/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java index ec96d77c72..90d250289d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java @@ -59,14 +59,6 @@ private void updateTicketStatus(JDA jda) { .map(HelpThreadsRecord::getChannelId) .toList()); - - threadIdsToClose.forEach(id -> { - try { - ThreadChannel threadChannel = jda.getThreadChannelById(id); - helpThreadLifecycleListener.handleArchiveStatus(now, threadChannel); - } catch (Exception exception) { - logger.warn("unable to mark thread as close with id :{}", id, exception); - } - }); + threadIdsToClose.forEach(id -> helpThreadLifecycleListener.handleArchiveStatus(now, id, jda)); } } From 28349d4087d1dfdc0a279421c3a609ee1d463a9e Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Sat, 31 Aug 2024 22:57:19 +0530 Subject: [PATCH 2/7] apply spotless --- .../help/HelpThreadLifecycleListener.java | 20 +++++++++---------- .../help/MarkHelpThreadCloseInDBRoutine.java | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadLifecycleListener.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadLifecycleListener.java index 29f35dbcc5..3669ed2517 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadLifecycleListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadLifecycleListener.java @@ -88,10 +88,10 @@ void handleArchiveStatus(Instant closedAt, long id, JDA jda) { 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()); + .set(HELP_THREADS.CLOSED_AT, closedAt) + .set(HELP_THREADS.TICKET_STATUS, HelpSystemHelper.TicketStatus.ARCHIVED.val) + .where(HELP_THREADS.CHANNEL_ID.eq(id)) + .execute()); return; } @@ -100,12 +100,12 @@ void handleArchiveStatus(Instant closedAt, long id, JDA jda) { int participantsExceptAuthor = threadChannel.getMemberCount() - 1; database.write(context -> context.update(HELP_THREADS) - .set(HELP_THREADS.CLOSED_AT, closedAt) - .set(HELP_THREADS.TICKET_STATUS, HelpSystemHelper.TicketStatus.ARCHIVED.val) - .set(HELP_THREADS.MESSAGE_COUNT, messageCount) - .set(HELP_THREADS.PARTICIPANTS, participantsExceptAuthor) - .where(HELP_THREADS.CHANNEL_ID.eq(threadId)) - .execute()); + .set(HELP_THREADS.CLOSED_AT, closedAt) + .set(HELP_THREADS.TICKET_STATUS, HelpSystemHelper.TicketStatus.ARCHIVED.val) + .set(HELP_THREADS.MESSAGE_COUNT, messageCount) + .set(HELP_THREADS.PARTICIPANTS, participantsExceptAuthor) + .where(HELP_THREADS.CHANNEL_ID.eq(threadId)) + .execute()); logger.info("Thread with id: {}, updated to archived status in database", threadId); diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java b/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java index 90d250289d..543233e3dd 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java @@ -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; @@ -59,6 +58,7 @@ private void updateTicketStatus(JDA jda) { .map(HelpThreadsRecord::getChannelId) .toList()); - threadIdsToClose.forEach(id -> helpThreadLifecycleListener.handleArchiveStatus(now, id, jda)); + threadIdsToClose + .forEach(id -> helpThreadLifecycleListener.handleArchiveStatus(now, id, jda)); } } From 286607f21012607aac98c8ff0261adc6abe418a8 Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Sat, 31 Aug 2024 23:25:05 +0530 Subject: [PATCH 3/7] remove logger, no longer needed --- .../tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java b/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java index 543233e3dd..54488788e8 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java @@ -1,8 +1,6 @@ package org.togetherjava.tjbot.features.help; import net.dv8tion.jda.api.JDA; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.db.Database; import org.togetherjava.tjbot.db.generated.tables.records.HelpThreadsRecord; @@ -20,8 +18,6 @@ * closed. */ public final class MarkHelpThreadCloseInDBRoutine implements Routine { - private static final Logger logger = - LoggerFactory.getLogger(MarkHelpThreadCloseInDBRoutine.class); private final Database database; private final HelpThreadLifecycleListener helpThreadLifecycleListener; From f4880cbd4faa04c8efea96798494e63b32683bbd Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Mon, 2 Sep 2024 21:10:13 +0530 Subject: [PATCH 4/7] handle unexpected errors while marking threads archived in database --- .../help/MarkHelpThreadCloseInDBRoutine.java | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java b/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java index 54488788e8..4b5b401d2b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java @@ -2,6 +2,8 @@ import net.dv8tion.jda.api.JDA; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.db.Database; import org.togetherjava.tjbot.db.generated.tables.records.HelpThreadsRecord; import org.togetherjava.tjbot.features.Routine; @@ -18,18 +20,20 @@ * closed. */ public final class MarkHelpThreadCloseInDBRoutine implements Routine { + private static final Logger logger = + LoggerFactory.getLogger(MarkHelpThreadCloseInDBRoutine.class); private final Database database; private final HelpThreadLifecycleListener helpThreadLifecycleListener; /** * Creates a new instance. * - * @param database the database to store help thread metadata in + * @param database the database to store help thread metadata in * @param helpThreadLifecycleListener class which offers method to update thread status in - * database + * database */ public MarkHelpThreadCloseInDBRoutine(Database database, - HelpThreadLifecycleListener helpThreadLifecycleListener) { + HelpThreadLifecycleListener helpThreadLifecycleListener) { this.database = database; this.helpThreadLifecycleListener = helpThreadLifecycleListener; } @@ -48,13 +52,20 @@ private void updateTicketStatus(JDA jda) { Instant now = Instant.now(); Instant threeDaysAgo = now.minus(3, ChronoUnit.DAYS); List threadIdsToClose = database.read(context -> context.selectFrom(HELP_THREADS) - .where(HELP_THREADS.TICKET_STATUS.eq(HelpSystemHelper.TicketStatus.ACTIVE.val)) - .and(HELP_THREADS.CREATED_AT.lessThan(threeDaysAgo)) - .stream() - .map(HelpThreadsRecord::getChannelId) - .toList()); + .where(HELP_THREADS.TICKET_STATUS.eq(HelpSystemHelper.TicketStatus.ACTIVE.val)) + .and(HELP_THREADS.CREATED_AT.lessThan(threeDaysAgo)) + .stream() + .map(HelpThreadsRecord::getChannelId) + .toList()); threadIdsToClose - .forEach(id -> helpThreadLifecycleListener.handleArchiveStatus(now, id, jda)); + .forEach(id -> { + try { + helpThreadLifecycleListener.handleArchiveStatus(now, id, jda); + } catch (Throwable throwable) { + logger.warn("Failed to update status of thread with id: {} to archived",id, throwable); + } + } + ); } } From 49f7503660fabbd3efb7be4634cb5b49d6d5b2ab Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Mon, 2 Sep 2024 21:12:31 +0530 Subject: [PATCH 5/7] spotless formatting --- .../help/MarkHelpThreadCloseInDBRoutine.java | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java b/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java index 4b5b401d2b..f0a231a43f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java @@ -1,9 +1,9 @@ package org.togetherjava.tjbot.features.help; import net.dv8tion.jda.api.JDA; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.togetherjava.tjbot.db.Database; import org.togetherjava.tjbot.db.generated.tables.records.HelpThreadsRecord; import org.togetherjava.tjbot.features.Routine; @@ -28,12 +28,12 @@ public final class MarkHelpThreadCloseInDBRoutine implements Routine { /** * Creates a new instance. * - * @param database the database to store help thread metadata in + * @param database the database to store help thread metadata in * @param helpThreadLifecycleListener class which offers method to update thread status in - * database + * database */ public MarkHelpThreadCloseInDBRoutine(Database database, - HelpThreadLifecycleListener helpThreadLifecycleListener) { + HelpThreadLifecycleListener helpThreadLifecycleListener) { this.database = database; this.helpThreadLifecycleListener = helpThreadLifecycleListener; } @@ -52,20 +52,19 @@ private void updateTicketStatus(JDA jda) { Instant now = Instant.now(); Instant threeDaysAgo = now.minus(3, ChronoUnit.DAYS); List threadIdsToClose = database.read(context -> context.selectFrom(HELP_THREADS) - .where(HELP_THREADS.TICKET_STATUS.eq(HelpSystemHelper.TicketStatus.ACTIVE.val)) - .and(HELP_THREADS.CREATED_AT.lessThan(threeDaysAgo)) - .stream() - .map(HelpThreadsRecord::getChannelId) - .toList()); + .where(HELP_THREADS.TICKET_STATUS.eq(HelpSystemHelper.TicketStatus.ACTIVE.val)) + .and(HELP_THREADS.CREATED_AT.lessThan(threeDaysAgo)) + .stream() + .map(HelpThreadsRecord::getChannelId) + .toList()); - threadIdsToClose - .forEach(id -> { - try { - helpThreadLifecycleListener.handleArchiveStatus(now, id, jda); - } catch (Throwable throwable) { - logger.warn("Failed to update status of thread with id: {} to archived",id, throwable); - } - } - ); + threadIdsToClose.forEach(id -> { + try { + helpThreadLifecycleListener.handleArchiveStatus(now, id, jda); + } catch (Throwable throwable) { + logger.warn("Failed to update status of thread with id: {} to archived", id, + throwable); + } + }); } } From 15cb3046d7b3bbdbb1c7c4c8aaf45a2959d56b6f Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Mon, 2 Sep 2024 21:33:56 +0530 Subject: [PATCH 6/7] suppress warning from sonar for usage of throwable --- .../tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java | 1 + 1 file changed, 1 insertion(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java b/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java index f0a231a43f..b7111e3f74 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java @@ -48,6 +48,7 @@ public void runRoutine(JDA jda) { updateTicketStatus(jda); } + @SuppressWarnings("java:S1181") private void updateTicketStatus(JDA jda) { Instant now = Instant.now(); Instant threeDaysAgo = now.minus(3, ChronoUnit.DAYS); From 03b1e5e7757fafe7c1926e0ef13f23efcb4b7371 Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Wed, 4 Sep 2024 12:45:53 +0530 Subject: [PATCH 7/7] catch base exception instead of throwable --- .../tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java b/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java index b7111e3f74..65cca49c6b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java @@ -48,7 +48,6 @@ public void runRoutine(JDA jda) { updateTicketStatus(jda); } - @SuppressWarnings("java:S1181") private void updateTicketStatus(JDA jda) { Instant now = Instant.now(); Instant threeDaysAgo = now.minus(3, ChronoUnit.DAYS); @@ -62,9 +61,9 @@ private void updateTicketStatus(JDA jda) { threadIdsToClose.forEach(id -> { try { helpThreadLifecycleListener.handleArchiveStatus(now, id, jda); - } catch (Throwable throwable) { + } catch (Exception exception) { logger.warn("Failed to update status of thread with id: {} to archived", id, - throwable); + exception); } }); }