forked from Together-Java/TJ-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/meta data v2 (Together-Java#990)
* alter help_thread table to add more meta data * update write operation with extra fields * listener that updates meta data based on thread status * fix error in log messages * method to update tagName on change in db * more suitable name for class and java doc * change name to avoid confusion & update java doc * minor bug that would change status back to active again and log message update * more columns added for metadata * update listener to record added metadata columns * spotless fix * replacing get(0) with getFirst() for list * add routine to settle thread status if was left open * spotless fix * 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 * document updated param in MarkHelpThreadCloseInDBRoutine constructor * use time of creation/modfication from thread and tags as csv * rely on discord for getting thread status instead of DB * changes * requested changes * update config to include tagsToIgnore * refactor tag update logic * update sql script * add tagsToIgnore list to config * change list * adding data fields that are being collected in privacy policy * updating duration of data storage in privacy policy doc * HelpThreadMetadataPurger will now purge help threads data post 180 days * instead of appending new tags, only new tags are stored now * spotless fix * tags from config sanitized * requested changes * spotless * requested changes * remove unnecessary map * increase routine schedule to 24 hrs
- Loading branch information
1 parent
9122c16
commit 7bc47d6
Showing
8 changed files
with
267 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
...ation/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadLifecycleListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package org.togetherjava.tjbot.features.help; | ||
|
||
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; | ||
import net.dv8tion.jda.api.events.channel.update.ChannelUpdateArchivedEvent; | ||
import net.dv8tion.jda.api.hooks.ListenerAdapter; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import org.togetherjava.tjbot.db.Database; | ||
import org.togetherjava.tjbot.features.EventReceiver; | ||
|
||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.togetherjava.tjbot.db.generated.tables.HelpThreads.HELP_THREADS; | ||
|
||
/** | ||
* Listens for help thread events after creation of thread. Updates metadata based on those events | ||
* in database. | ||
*/ | ||
public final class HelpThreadLifecycleListener extends ListenerAdapter implements EventReceiver { | ||
private static final Logger logger = LoggerFactory.getLogger(HelpThreadLifecycleListener.class); | ||
private final HelpSystemHelper helper; | ||
private final Database database; | ||
|
||
/** | ||
* Creates a new instance. | ||
* | ||
* @param helper to work with the help threads | ||
* @param database the database to store help thread metadata in | ||
*/ | ||
public HelpThreadLifecycleListener(HelpSystemHelper helper, Database database) { | ||
this.helper = helper; | ||
this.database = database; | ||
} | ||
|
||
@Override | ||
public void onChannelUpdateArchived(ChannelUpdateArchivedEvent event) { | ||
ThreadChannel threadChannel = event.getChannel().asThreadChannel(); | ||
|
||
if (!helper.isHelpForumName(threadChannel.getParentChannel().getName())) { | ||
return; | ||
} | ||
handleThreadStatus(threadChannel); | ||
} | ||
|
||
@Override | ||
public void onChannelUpdateAppliedTags(ChannelUpdateAppliedTagsEvent event) { | ||
ThreadChannel threadChannel = event.getChannel().asThreadChannel(); | ||
|
||
if (!helper.isHelpForumName(threadChannel.getParentChannel().getName()) | ||
|| shouldIgnoreUpdatedTagEvent(event)) { | ||
return; | ||
} | ||
|
||
|
||
String newlyAppliedTagsOnly = event.getNewTags() | ||
.stream() | ||
.filter(helper::shouldIgnoreTag) | ||
.map(ForumTag::getName) | ||
.collect(Collectors.joining(",")); | ||
|
||
|
||
long threadId = threadChannel.getIdLong(); | ||
|
||
handleTagsUpdate(threadId, newlyAppliedTagsOnly); | ||
} | ||
|
||
private void handleThreadStatus(ThreadChannel threadChannel) { | ||
Instant closedAt = threadChannel.getTimeArchiveInfoLastModified().toInstant(); | ||
long threadId = threadChannel.getIdLong(); | ||
boolean isArchived = threadChannel.isArchived(); | ||
|
||
if (isArchived) { | ||
handleArchiveStatus(closedAt, threadChannel); | ||
return; | ||
} | ||
|
||
updateThreadStatusToActive(threadId); | ||
} | ||
|
||
void handleArchiveStatus(Instant closedAt, ThreadChannel threadChannel) { | ||
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()); | ||
|
||
logger.info("Thread with id: {}, updated to archived status in database", threadId); | ||
} | ||
|
||
private void updateThreadStatusToActive(long threadId) { | ||
database.write(context -> context.update(HELP_THREADS) | ||
.set(HELP_THREADS.TICKET_STATUS, HelpSystemHelper.TicketStatus.ACTIVE.val) | ||
.where(HELP_THREADS.CHANNEL_ID.eq(threadId)) | ||
.execute()); | ||
|
||
logger.info("Thread with id: {}, updated to active status in database", threadId); | ||
} | ||
|
||
private void handleTagsUpdate(long threadId, String updatedTag) { | ||
database.write(context -> context.update(HELP_THREADS) | ||
.set(HELP_THREADS.TAGS, updatedTag) | ||
.where(HELP_THREADS.CHANNEL_ID.eq(threadId)) | ||
.execute()); | ||
|
||
logger.info("Updated tag for thread with id: {} in database", threadId); | ||
} | ||
|
||
/** | ||
* will ignore updated tag event if all new tags belong to the categories config | ||
* | ||
* @param event updated tags event | ||
* @return boolean | ||
*/ | ||
private boolean shouldIgnoreUpdatedTagEvent(ChannelUpdateAppliedTagsEvent event) { | ||
List<ForumTag> newTags = | ||
event.getNewTags().stream().filter(helper::shouldIgnoreTag).toList(); | ||
return newTags.isEmpty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.