|
| 1 | +package org.togetherjava.tjbot.features.help; |
| 2 | + |
| 3 | +import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; |
| 4 | +import net.dv8tion.jda.api.entities.channel.forums.ForumTag; |
| 5 | +import net.dv8tion.jda.api.events.channel.update.ChannelUpdateAppliedTagsEvent; |
| 6 | +import net.dv8tion.jda.api.events.channel.update.ChannelUpdateArchivedEvent; |
| 7 | +import net.dv8tion.jda.api.hooks.ListenerAdapter; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | + |
| 11 | +import org.togetherjava.tjbot.db.Database; |
| 12 | +import org.togetherjava.tjbot.features.EventReceiver; |
| 13 | + |
| 14 | +import java.time.Instant; |
| 15 | +import java.util.List; |
| 16 | +import java.util.stream.Collectors; |
| 17 | + |
| 18 | +import static org.togetherjava.tjbot.db.generated.tables.HelpThreads.HELP_THREADS; |
| 19 | + |
| 20 | +/** |
| 21 | + * Listens for help thread events after creation of thread. Updates metadata based on those events |
| 22 | + * in database. |
| 23 | + */ |
| 24 | +public final class HelpThreadLifecycleListener extends ListenerAdapter implements EventReceiver { |
| 25 | + private static final Logger logger = LoggerFactory.getLogger(HelpThreadLifecycleListener.class); |
| 26 | + private final HelpSystemHelper helper; |
| 27 | + private final Database database; |
| 28 | + |
| 29 | + /** |
| 30 | + * Creates a new instance. |
| 31 | + * |
| 32 | + * @param helper to work with the help threads |
| 33 | + * @param database the database to store help thread metadata in |
| 34 | + */ |
| 35 | + public HelpThreadLifecycleListener(HelpSystemHelper helper, Database database) { |
| 36 | + this.helper = helper; |
| 37 | + this.database = database; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void onChannelUpdateArchived(ChannelUpdateArchivedEvent event) { |
| 42 | + ThreadChannel threadChannel = event.getChannel().asThreadChannel(); |
| 43 | + |
| 44 | + if (!helper.isHelpForumName(threadChannel.getParentChannel().getName())) { |
| 45 | + return; |
| 46 | + } |
| 47 | + handleThreadStatus(threadChannel); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void onChannelUpdateAppliedTags(ChannelUpdateAppliedTagsEvent event) { |
| 52 | + ThreadChannel threadChannel = event.getChannel().asThreadChannel(); |
| 53 | + |
| 54 | + if (!helper.isHelpForumName(threadChannel.getParentChannel().getName()) |
| 55 | + || shouldIgnoreUpdatedTagEvent(event)) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + String newlyAppliedTagsOnly = event.getNewTags() |
| 61 | + .stream() |
| 62 | + .filter(helper::shouldIgnoreTag) |
| 63 | + .map(ForumTag::getName) |
| 64 | + .collect(Collectors.joining(",")); |
| 65 | + |
| 66 | + |
| 67 | + long threadId = threadChannel.getIdLong(); |
| 68 | + |
| 69 | + handleTagsUpdate(threadId, newlyAppliedTagsOnly); |
| 70 | + } |
| 71 | + |
| 72 | + private void handleThreadStatus(ThreadChannel threadChannel) { |
| 73 | + Instant closedAt = threadChannel.getTimeArchiveInfoLastModified().toInstant(); |
| 74 | + long threadId = threadChannel.getIdLong(); |
| 75 | + boolean isArchived = threadChannel.isArchived(); |
| 76 | + |
| 77 | + if (isArchived) { |
| 78 | + handleArchiveStatus(closedAt, threadChannel); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + updateThreadStatusToActive(threadId); |
| 83 | + } |
| 84 | + |
| 85 | + void handleArchiveStatus(Instant closedAt, ThreadChannel threadChannel) { |
| 86 | + long threadId = threadChannel.getIdLong(); |
| 87 | + int messageCount = threadChannel.getMessageCount(); |
| 88 | + int participantsExceptAuthor = threadChannel.getMemberCount() - 1; |
| 89 | + |
| 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 | + .set(HELP_THREADS.MESSAGE_COUNT, messageCount) |
| 94 | + .set(HELP_THREADS.PARTICIPANTS, participantsExceptAuthor) |
| 95 | + .where(HELP_THREADS.CHANNEL_ID.eq(threadId)) |
| 96 | + .execute()); |
| 97 | + |
| 98 | + logger.info("Thread with id: {}, updated to archived status in database", threadId); |
| 99 | + } |
| 100 | + |
| 101 | + private void updateThreadStatusToActive(long threadId) { |
| 102 | + database.write(context -> context.update(HELP_THREADS) |
| 103 | + .set(HELP_THREADS.TICKET_STATUS, HelpSystemHelper.TicketStatus.ACTIVE.val) |
| 104 | + .where(HELP_THREADS.CHANNEL_ID.eq(threadId)) |
| 105 | + .execute()); |
| 106 | + |
| 107 | + logger.info("Thread with id: {}, updated to active status in database", threadId); |
| 108 | + } |
| 109 | + |
| 110 | + private void handleTagsUpdate(long threadId, String updatedTag) { |
| 111 | + database.write(context -> context.update(HELP_THREADS) |
| 112 | + .set(HELP_THREADS.TAGS, updatedTag) |
| 113 | + .where(HELP_THREADS.CHANNEL_ID.eq(threadId)) |
| 114 | + .execute()); |
| 115 | + |
| 116 | + logger.info("Updated tag for thread with id: {} in database", threadId); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * will ignore updated tag event if all new tags belong to the categories config |
| 121 | + * |
| 122 | + * @param event updated tags event |
| 123 | + * @return boolean |
| 124 | + */ |
| 125 | + private boolean shouldIgnoreUpdatedTagEvent(ChannelUpdateAppliedTagsEvent event) { |
| 126 | + List<ForumTag> newTags = |
| 127 | + event.getNewTags().stream().filter(helper::shouldIgnoreTag).toList(); |
| 128 | + return newTags.isEmpty(); |
| 129 | + } |
| 130 | +} |
0 commit comments