-
-
Notifications
You must be signed in to change notification settings - Fork 89
Add moderation actions table #298
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
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3555029
Added ModerationActionsStore
Zabuzard 5d214eb
Integrated store into moderation commands
Zabuzard f0e4d9e
Javadoc and Linter
Zabuzard 127af26
Note about timestamps being slightly off by design
Zabuzard df4b87c
helper method to get rid of code duplication (CR by Zabuzard)
Zabuzard 834de32
added not-null checks for fail-fast
Zabuzard 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
40 changes: 40 additions & 0 deletions
40
application/src/main/java/org/togetherjava/tjbot/commands/moderation/ActionRecord.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,40 @@ | ||
package org.togetherjava.tjbot.commands.moderation; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.togetherjava.tjbot.db.generated.tables.records.ModerationActionsRecord; | ||
|
||
import java.time.Instant; | ||
|
||
/** | ||
* Record for actions as maintained by {@link ModerationActionsStore}. Each action has a unique | ||
* caseId. | ||
* | ||
* @param caseId the unique case id associated with this action | ||
* @param issuedAt the instant at which this action was issued | ||
* @param guildId the id of the guild in which context this action happened | ||
* @param authorId the id of the user who issued the action | ||
* @param targetId the id of the user who was the target of the action | ||
* @param actionType the type of the action | ||
* @param actionExpiresAt the instant at which this action expires, for temporary actions; otherwise | ||
* {@code null} | ||
* @param reason the reason why this action was executed | ||
*/ | ||
public record ActionRecord(int caseId, @NotNull Instant issuedAt, long guildId, long authorId, | ||
long targetId, @NotNull ModerationUtils.Action actionType, | ||
@Nullable Instant actionExpiresAt, @NotNull String reason) { | ||
|
||
/** | ||
* Creates the action record that corresponds to the given action entry from the database table. | ||
* | ||
* @param action the action to convert | ||
* @return the corresponding action record | ||
*/ | ||
@SuppressWarnings("StaticMethodOnlyUsedInOneClass") | ||
static @NotNull ActionRecord of(@NotNull ModerationActionsRecord action) { | ||
return new ActionRecord(action.getCaseId(), action.getIssuedAt(), action.getGuildId(), | ||
action.getAuthorId(), action.getTargetId(), | ||
ModerationUtils.Action.valueOf(action.getActionType()), action.getActionExpiresAt(), | ||
action.getReason()); | ||
} | ||
} |
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
145 changes: 145 additions & 0 deletions
145
...tion/src/main/java/org/togetherjava/tjbot/commands/moderation/ModerationActionsStore.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,145 @@ | ||
package org.togetherjava.tjbot.commands.moderation; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.jooq.Condition; | ||
import org.togetherjava.tjbot.db.Database; | ||
import org.togetherjava.tjbot.db.generated.tables.ModerationActions; | ||
import org.togetherjava.tjbot.db.generated.tables.records.ModerationActionsRecord; | ||
|
||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Store for moderation actions, e.g. as banning users. Can be used to retrieve information about | ||
* past events, such as when a user has been banned the last time. | ||
* | ||
* Actions have to be added to the store using | ||
* {@link #addAction(long, long, long, ModerationUtils.Action, Instant, String)} at the time they | ||
* are executed and can then be retrieved by methods such as | ||
* {@link #getActionsByTypeAscending(long, ModerationUtils.Action)} or | ||
* {@link #findActionByCaseId(int)}. | ||
* | ||
* Be aware that timestamps associated with actions, such as {@link ActionRecord#issuedAt()} are | ||
* slightly off the timestamps used by Discord. | ||
* | ||
* The store persists the actions and is thread safe. | ||
*/ | ||
@SuppressWarnings("ClassCanBeRecord") | ||
public final class ModerationActionsStore { | ||
private final Database database; | ||
|
||
/** | ||
* Creates a new instance which writes and retrieves actions from a given database. | ||
* | ||
* @param database the database to write and retrieve actions from | ||
*/ | ||
public ModerationActionsStore(@NotNull Database database) { | ||
this.database = database; | ||
Zabuzard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* Gets all actions of a given type that have been written to the store, chronologically | ||
* ascending with the earliest action first. | ||
* | ||
* @param guildId the id of the guild, only actions that happened in the context of that guild | ||
* will be retrieved | ||
* @param actionType the type of action to filter for | ||
* @return a list of all actions with the given type, chronologically ascending | ||
*/ | ||
public @NotNull List<ActionRecord> getActionsByTypeAscending(long guildId, | ||
@NotNull ModerationUtils.Action actionType) { | ||
return getActionsFromGuildAscending(guildId, | ||
ModerationActions.MODERATION_ACTIONS.ACTION_TYPE.eq(actionType.name())); | ||
} | ||
|
||
/** | ||
* Gets all actions executed against a given target that have been written to the store, | ||
* chronologically ascending with the earliest action first. | ||
* | ||
* @param guildId the id of the guild, only actions that happened in the context of that guild | ||
* will be retrieved | ||
* @param targetId the id of the target user to filter for | ||
* @return a list of all actions executed against the target, chronologically ascending | ||
*/ | ||
public @NotNull List<ActionRecord> getActionsByTargetAscending(long guildId, long targetId) { | ||
return getActionsFromGuildAscending(guildId, | ||
ModerationActions.MODERATION_ACTIONS.TARGET_ID.eq(targetId)); | ||
} | ||
|
||
/** | ||
* Gets all actions executed by a given author that have been written to the store, | ||
* chronologically ascending with the earliest action first. | ||
* | ||
* @param guildId the id of the guild, only actions that happened in the context of that guild | ||
* will be retrieved | ||
* @param authorId the id of the author user to filter for | ||
* @return a list of all actions executed by the author, chronologically ascending | ||
*/ | ||
public @NotNull List<ActionRecord> getActionsByAuthorAscending(long guildId, long authorId) { | ||
return getActionsFromGuildAscending(guildId, | ||
ModerationActions.MODERATION_ACTIONS.AUTHOR_ID.eq(authorId)); | ||
} | ||
|
||
/** | ||
* Gets the action with the given case id from the store, if present. | ||
* | ||
* @param caseId the actions' case id to search for | ||
* @return the action with the given case id, if present | ||
*/ | ||
public @NotNull Optional<ActionRecord> findActionByCaseId(int caseId) { | ||
return Optional | ||
.of(database.read(context -> context.selectFrom(ModerationActions.MODERATION_ACTIONS) | ||
.where(ModerationActions.MODERATION_ACTIONS.CASE_ID.eq(caseId)) | ||
.fetchOne())) | ||
.map(ActionRecord::of); | ||
} | ||
|
||
/** | ||
* Adds the given action to the store. A unique case id will be associated to the action and | ||
* returned. | ||
* | ||
* It is assumed that the action is issued at the point in time this method is called. It is not | ||
* possible to assign a different timestamp, especially not an earlier point in time. | ||
* Consequently, this causes the timestamps to be slightly off from the timestamps recorded by | ||
* Discord itself. | ||
* | ||
* @param guildId the id of the guild in which context this action happened | ||
* @param authorId the id of the user who issued the action | ||
* @param targetId the id of the user who was the target of the action | ||
* @param actionType the type of the action | ||
* @param actionExpiresAt the instant at which this action expires, for temporary actions; | ||
* otherwise {@code null} | ||
* @param reason the reason why this action was executed | ||
* @return the unique case id associated with the action | ||
*/ | ||
@SuppressWarnings("MethodWithTooManyParameters") | ||
public int addAction(long guildId, long authorId, long targetId, | ||
@NotNull ModerationUtils.Action actionType, @Nullable Instant actionExpiresAt, | ||
@NotNull String reason) { | ||
return database.writeAndProvide(context -> { | ||
ModerationActionsRecord actionRecord = | ||
context.newRecord(ModerationActions.MODERATION_ACTIONS) | ||
.setIssuedAt(Instant.now()) | ||
.setGuildId(guildId) | ||
.setAuthorId(authorId) | ||
.setTargetId(targetId) | ||
.setActionType(actionType.name()) | ||
.setActionExpiresAt(actionExpiresAt) | ||
.setReason(reason); | ||
actionRecord.insert(); | ||
return actionRecord.getCaseId(); | ||
}); | ||
} | ||
|
||
private @NotNull List<ActionRecord> getActionsFromGuildAscending(long guildId, | ||
@NotNull Condition condition) { | ||
return database.read(context -> context.selectFrom(ModerationActions.MODERATION_ACTIONS) | ||
.where(ModerationActions.MODERATION_ACTIONS.GUILD_ID.eq(guildId).and(condition)) | ||
Zabuzard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.orderBy(ModerationActions.MODERATION_ACTIONS.ISSUED_AT.asc()) | ||
.stream() | ||
.map(ActionRecord::of) | ||
.toList()); | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
application/src/main/resources/db/V5__Add_Moderation_Actions.sql
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 @@ | ||
CREATE TABLE moderation_actions | ||
( | ||
case_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, | ||
issued_at TIMESTAMP NOT NULL, | ||
guild_id BIGINT NOT NULL, | ||
author_id BIGINT NOT NULL, | ||
target_id BIGINT NOT NULL, | ||
action_type TEXT NOT NULL, | ||
action_expires_at TIMESTAMP, | ||
reason TEXT NOT NULL | ||
) |
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.