-
-
Notifications
You must be signed in to change notification settings - Fork 91
Automatically pin first message in projects forum #1167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Zabuzard
merged 5 commits into
Together-Java:develop
from
andrew-09:feature/pin_first_message_project_forum
Oct 30, 2024
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3e802e3
Automatically pin first message in project forum
80412cb
Fixes: 1. used config file for "projects" channel name
87c0e84
Fix comments in projects package package-info.java file
f0f9135
Suggested changes - More reliable way to verify parent message of cha…
8963457
pass the event and thread channel to this method isPostMessage, get t…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
60 changes: 60 additions & 0 deletions
60
...src/main/java/org/togetherjava/tjbot/features/projects/ProjectsThreadCreatedListener.java
This file contains hidden or 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,60 @@ | ||
package org.togetherjava.tjbot.features.projects; | ||
|
||
import com.github.benmanes.caffeine.cache.Cache; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import net.dv8tion.jda.api.entities.channel.Channel; | ||
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; | ||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; | ||
import net.dv8tion.jda.api.hooks.ListenerAdapter; | ||
|
||
import org.togetherjava.tjbot.config.Config; | ||
import org.togetherjava.tjbot.features.EventReceiver; | ||
|
||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Listens for new threads being created in the "projects" forum and pins the first message. * | ||
* {@link Config#getProjectsChannelPattern()}. | ||
*/ | ||
public final class ProjectsThreadCreatedListener extends ListenerAdapter implements EventReceiver { | ||
private final String configProjectsChannelPattern; | ||
private final Cache<Long, Instant> threadIdToCreatedAtCache = Caffeine.newBuilder() | ||
.maximumSize(1_000) | ||
.expireAfterAccess(2, TimeUnit.of(ChronoUnit.MINUTES)) | ||
.build(); | ||
|
||
public ProjectsThreadCreatedListener(Config config) { | ||
configProjectsChannelPattern = config.getProjectsChannelPattern(); | ||
} | ||
|
||
@Override | ||
public void onMessageReceived(MessageReceivedEvent event) { | ||
if (event.isFromThread()) { | ||
ThreadChannel threadChannel = event.getChannel().asThreadChannel(); | ||
Channel parentChannel = threadChannel.getParentChannel(); | ||
boolean isPost = isPostMessage(threadChannel); | ||
|
||
if (parentChannel.getName().equals(configProjectsChannelPattern) && isPost) { | ||
handleProjectThread(event); | ||
} | ||
} | ||
} | ||
|
||
private boolean wasThreadAlreadyHandled(long threadChannelId) { | ||
Instant now = Instant.now(); | ||
Instant createdAt = threadIdToCreatedAtCache.get(threadChannelId, any -> now); | ||
return createdAt != now; | ||
} | ||
|
||
private boolean isPostMessage(ThreadChannel threadChannel) { | ||
int messageCount = threadChannel.getMessageCount(); | ||
return messageCount <= 1 && !wasThreadAlreadyHandled(threadChannel.getIdLong()); | ||
} | ||
|
||
private void handleProjectThread(MessageReceivedEvent event) { | ||
// Pin the first message in the thread | ||
event.getMessage().pin().queue(); | ||
} | ||
andrew-09 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
11 changes: 11 additions & 0 deletions
11
application/src/main/java/org/togetherjava/tjbot/features/projects/package-info.java
This file contains hidden or 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,11 @@ | ||
/** | ||
* This packages offers all the functionality for the projects channel. The core class is | ||
* {@link org.togetherjava.tjbot.features.projects.ProjectsThreadCreatedListener}. | ||
*/ | ||
@MethodsReturnNonnullByDefault | ||
@ParametersAreNonnullByDefault | ||
package org.togetherjava.tjbot.features.projects; | ||
|
||
import org.togetherjava.tjbot.annotations.MethodsReturnNonnullByDefault; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.