Skip to content

Commit e92c09d

Browse files
committed
refactor: move thread reactivation logic to HelpThreadAutoArchiver
- Make HelpThreadAutoArchiver implement UserInteractor (name: thread-inactivity) - Own ComponentIdInteractor for mark-active button ID generation - Move onButtonClick/onInactivityButton from listener to archiver - Remove mark-active routing and getComponentIdInteractor from listener - Remove unused generateMarkActiveId from HelpSystemHelper - Extract mark-active literal to constant (SonarLint S1192)
1 parent 29b7a73 commit e92c09d

File tree

4 files changed

+51
-52
lines changed

4 files changed

+51
-52
lines changed

application/src/main/java/org/togetherjava/tjbot/features/Features.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
146146
.add(new AutoPruneHelperRoutine(config, helpSystemHelper, modAuditLogWriter, database));
147147
HelpThreadCreatedListener helpThreadCreatedListener =
148148
new HelpThreadCreatedListener(helpSystemHelper);
149-
features.add(new HelpThreadAutoArchiver(helpSystemHelper,
150-
helpThreadCreatedListener.getComponentIdInteractor()));
149+
features.add(new HelpThreadAutoArchiver(helpSystemHelper));
151150
features.add(new LeftoverBookmarksCleanupRoutine(bookmarksSystem));
152151
features.add(new MarkHelpThreadCloseInDBRoutine(database, helpThreadLifecycleListener));
153152
features.add(new MemberCountDisplayRoutine(config));

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -419,17 +419,6 @@ Optional<Long> getAuthorByHelpThreadId(final long channelId) {
419419
}
420420

421421

422-
/**
423-
* Generates a component ID for the "Mark Active" button, derived from the given interactor's
424-
* namespace.
425-
*
426-
* @param interactor the component ID interactor whose namespace should be used
427-
* @return a valid component ID string for the mark-active button
428-
*/
429-
public String generateMarkActiveId(ComponentIdInteractor interactor) {
430-
return interactor.generateComponentId("mark-active");
431-
}
432-
433422
/**
434423
* will be used to filter a tag based on categories config
435424
*

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

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88
import net.dv8tion.jda.api.entities.MessageEmbed;
99
import net.dv8tion.jda.api.entities.channel.concrete.ForumChannel;
1010
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
11+
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
1112
import net.dv8tion.jda.api.interactions.components.buttons.Button;
1213
import net.dv8tion.jda.api.requests.RestAction;
1314
import net.dv8tion.jda.api.utils.TimeUtil;
1415
import org.slf4j.Logger;
1516
import org.slf4j.LoggerFactory;
1617

1718
import org.togetherjava.tjbot.features.Routine;
19+
import org.togetherjava.tjbot.features.UserInteractionType;
20+
import org.togetherjava.tjbot.features.UserInteractor;
21+
import org.togetherjava.tjbot.features.componentids.ComponentIdGenerator;
1822
import org.togetherjava.tjbot.features.componentids.ComponentIdInteractor;
1923

2024
import java.time.Duration;
@@ -29,25 +33,62 @@
2933
* Routine, which periodically checks all help threads and archives them if there has not been any
3034
* recent activity.
3135
*/
32-
public final class HelpThreadAutoArchiver implements Routine {
36+
public final class HelpThreadAutoArchiver implements Routine, UserInteractor {
3337
private static final Logger logger = LoggerFactory.getLogger(HelpThreadAutoArchiver.class);
3438
private static final int SCHEDULE_MINUTES = 60;
3539
private static final Duration ARCHIVE_AFTER_INACTIVITY_OF = Duration.ofHours(12);
3640
private static final String MARK_ACTIVE_LABEL = "Mark Active";
41+
private static final String MARK_ACTIVE_ID = "mark-active";
3742

3843
private final HelpSystemHelper helper;
39-
private final ComponentIdInteractor componentIdInteractor;
44+
private final ComponentIdInteractor inactivityInteractor =
45+
new ComponentIdInteractor(getInteractionType(), getName());
4046

4147
/**
4248
* Creates a new instance.
4349
*
4450
* @param helper the helper to use
45-
* @param componentIdInteractor the interactor used to generate component IDs for buttons
4651
*/
47-
public HelpThreadAutoArchiver(HelpSystemHelper helper,
48-
ComponentIdInteractor componentIdInteractor) {
52+
public HelpThreadAutoArchiver(HelpSystemHelper helper) {
4953
this.helper = helper;
50-
this.componentIdInteractor = componentIdInteractor;
54+
}
55+
56+
@Override
57+
public String getName() {
58+
return "thread-inactivity";
59+
}
60+
61+
@Override
62+
public UserInteractionType getInteractionType() {
63+
return UserInteractionType.OTHER;
64+
}
65+
66+
@Override
67+
public void acceptComponentIdGenerator(ComponentIdGenerator generator) {
68+
inactivityInteractor.acceptComponentIdGenerator(generator);
69+
}
70+
71+
@Override
72+
public void onButtonClick(ButtonInteractionEvent event, List<String> args) {
73+
if (args.contains(MARK_ACTIVE_ID)) {
74+
onInactivityButton(event);
75+
}
76+
}
77+
78+
private void onInactivityButton(ButtonInteractionEvent event) {
79+
event.deferEdit().queue();
80+
81+
if (event.getChannel() instanceof ThreadChannel thread) {
82+
Message botClosedThreadMessage = event.getMessage();
83+
84+
thread.getManager()
85+
.setArchived(false)
86+
.flatMap(_ -> botClosedThreadMessage.delete())
87+
.queue();
88+
89+
logger.debug("Thread {} was manually reactivated via button by user {}", thread.getId(),
90+
event.getUser().getId());
91+
}
5192
}
5293

5394
@Override
@@ -138,7 +179,7 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed)
138179
private void triggerArchiveFlow(ThreadChannel threadChannel, long authorId,
139180
MessageEmbed embed) {
140181

141-
String markActiveId = helper.generateMarkActiveId(componentIdInteractor);
182+
String markActiveId = inactivityInteractor.generateComponentId(MARK_ACTIVE_ID);
142183

143184
Function<Member, RestAction<Message>> sendEmbedWithMention =
144185
member -> threadChannel.sendMessage(member.getAsMention())
@@ -174,7 +215,7 @@ private void triggerAuthorIdNotFoundArchiveFlow(ThreadChannel threadChannel,
174215
"Was unable to find a matching thread for id: {} in DB, archiving thread without mentioning OP",
175216
threadChannel.getId());
176217

177-
String markActiveId = helper.generateMarkActiveId(componentIdInteractor);
218+
String markActiveId = inactivityInteractor.generateComponentId(MARK_ACTIVE_ID);
178219

179220
threadChannel.sendMessageEmbeds(embed)
180221
.addActionRow(Button.primary(markActiveId, MARK_ACTIVE_LABEL))

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

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,6 @@ public void acceptComponentIdGenerator(ComponentIdGenerator generator) {
166166
componentIdInteractor.acceptComponentIdGenerator(generator);
167167
}
168168

169-
/**
170-
* Returns the component ID interactor used by this listener, so that other components (e.g. the
171-
* archiver) can generate IDs within the same namespace.
172-
*
173-
* @return the component ID interactor
174-
*/
175-
public ComponentIdInteractor getComponentIdInteractor() {
176-
return componentIdInteractor;
177-
}
178-
179169
private Consumer<Throwable> handleParentMessageDeleted(Member user, ThreadChannel channel,
180170
ButtonInteractionEvent event, List<String> args) {
181171
int noOfMessages = 1; // we only care about first message from channel history
@@ -197,11 +187,7 @@ private Consumer<Throwable> handleParentMessageDeleted(Member user, ThreadChanne
197187

198188
@Override
199189
public void onButtonClick(ButtonInteractionEvent event, List<String> args) {
200-
if (args.contains("mark-active")) {
201-
onInactivityButton(event);
202-
} else {
203-
onAiHelpDismissButton(event, args);
204-
}
190+
onAiHelpDismissButton(event, args);
205191
}
206192

207193
private void onAiHelpDismissButton(ButtonInteractionEvent event, List<String> args) {
@@ -264,20 +250,4 @@ private void registerThreadDataInDB(Message message, ThreadChannel threadChannel
264250

265251
helper.writeHelpThreadToDatabase(authorId, threadChannel);
266252
}
267-
268-
private void onInactivityButton(ButtonInteractionEvent event) {
269-
event.deferEdit().queue();
270-
271-
if (event.getChannel() instanceof ThreadChannel thread) {
272-
Message botClosedThreadMessage = event.getMessage();
273-
274-
thread.getManager()
275-
.setArchived(false)
276-
.flatMap(_ -> botClosedThreadMessage.delete())
277-
.queue();
278-
279-
log.debug("Thread {} was manually reactivated via button by user {}", thread.getId(),
280-
event.getUser().getId());
281-
}
282-
}
283253
}

0 commit comments

Comments
 (0)