1414import static org .togetherjava .tjbot .db .generated .tables .ScamHistory .SCAM_HISTORY ;
1515
1616/**
17- * Store for history of detected scam messages. Can be used to retrieve information about
18- * past events and further processing and handling of scam. For example, to delete a group of
19- * duplicate scam messages after a moderator confirmed that it actually is scam and decided for an action.
17+ * Store for history of detected scam messages. Can be used to retrieve information about past
18+ * events and further processing and handling of scam. For example, to delete a group of duplicate
19+ * scam messages after a moderator confirmed that it actually is scam and decided for an action.
2020 * <p>
21- * Scam has to be added to the store using
22- * {@link #addScam (Message, boolean )} and can then be used to determine {@link #hasRecentScamDuplicate(Message)}
23- * or for further processing, such as {@link #markScamDuplicatesDeleted(Message)}.
21+ * Scam has to be added to the store using {@link #addScam(Message, boolean)} and can then be used
22+ * to determine {@link #hasRecentScamDuplicate (Message)} or for further processing, such as
23+ * {@link #markScamDuplicatesDeleted(Message)}.
2424 * <p>
25- * Entries are only kept for a certain amount of time and will be purged regularly by {@link ScamHistoryPurgeRoutine}.
25+ * Entries are only kept for a certain amount of time and will be purged regularly by
26+ * {@link ScamHistoryPurgeRoutine}.
2627 * <p>
2728 * The store persists the actions and is thread safe.
2829 */
@@ -42,48 +43,52 @@ public ScamHistoryStore(@NotNull Database database) {
4243 /**
4344 * Adds the given scam message to the store.
4445 *
45- * @param scam the message to add
46+ * @param scam the message to add
4647 * @param isDeleted whether the message is already, or about to get, deleted
4748 */
4849 public void addScam (@ NotNull Message scam , boolean isDeleted ) {
4950 Objects .requireNonNull (scam );
5051
5152 database .write (context -> context .newRecord (SCAM_HISTORY )
52- .setSentAt (scam .getTimeCreated ().toInstant ())
53- .setGuildId (scam .getGuild ().getIdLong ())
54- .setChannelId (scam .getChannel ().getIdLong ())
55- .setMessageId (scam .getIdLong ())
56- .setAuthorId (scam .getAuthor ().getIdLong ())
57- .setContent (scam .getContentRaw ())
58- .setIsDeleted (isDeleted )
59- .insert ());
53+ .setSentAt (scam .getTimeCreated ().toInstant ())
54+ .setGuildId (scam .getGuild ().getIdLong ())
55+ .setChannelId (scam .getChannel ().getIdLong ())
56+ .setMessageId (scam .getIdLong ())
57+ .setAuthorId (scam .getAuthor ().getIdLong ())
58+ .setContent (scam .getContentRaw ())
59+ .setIsDeleted (isDeleted )
60+ .insert ());
6061 }
6162
6263 /**
63- * Marks all duplicates to the given scam message (i.e. same guild, author, content, ...) as deleted.
64+ * Marks all duplicates to the given scam message (i.e. same guild, author, content, ...) as
65+ * deleted.
6466 *
6567 * @param scam the scam message to mark duplicates for
66- * @return identifications of all scam messages that have just been marked deleted, which previously have not been marked accordingly yet
68+ * @return identifications of all scam messages that have just been marked deleted, which
69+ * previously have not been marked accordingly yet
6770 */
6871 public @ NotNull Collection <ScamIdentification > markScamDuplicatesDeleted (
6972 @ NotNull Message scam ) {
7073 return database .writeAndProvide (context -> {
7174 Result <ScamHistoryRecord > undeletedDuplicates = context .selectFrom (SCAM_HISTORY )
72- .where (SCAM_HISTORY .GUILD_ID .eq (scam .getGuild ().getIdLong ())
73- .and (SCAM_HISTORY .AUTHOR_ID .eq (scam .getAuthor ().getIdLong ()))
74- .and (SCAM_HISTORY .CONTENT .eq (scam .getContentRaw ()))
75- .and (SCAM_HISTORY .IS_DELETED .isFalse ()))
76- .fetch ();
75+ .where (SCAM_HISTORY .GUILD_ID .eq (scam .getGuild ().getIdLong ())
76+ .and (SCAM_HISTORY .AUTHOR_ID .eq (scam .getAuthor ().getIdLong ()))
77+ .and (SCAM_HISTORY .CONTENT .eq (scam .getContentRaw ()))
78+ .and (SCAM_HISTORY .IS_DELETED .isFalse ()))
79+ .fetch ();
7780
78- undeletedDuplicates . forEach (
79- undeletedDuplicate -> undeletedDuplicate .setIsDeleted (true ).update ());
81+ undeletedDuplicates
82+ . forEach ( undeletedDuplicate -> undeletedDuplicate .setIsDeleted (true ).update ());
8083
8184 return undeletedDuplicates .stream ().map (ScamIdentification ::ofDatabaseRecord ).toList ();
8285 });
8386 }
8487
8588 /**
86- * Whether there are recent (a few minutes) duplicates to the given scam message (i.e. same guild, author, content, ...).
89+ * Whether there are recent (a few minutes) duplicates to the given scam message (i.e. same
90+ * guild, author, content, ...).
91+ *
8792 * @param scam the scam message to look for duplicates
8893 * @return whether there are recent duplicates
8994 */
@@ -92,23 +97,25 @@ public boolean hasRecentScamDuplicate(@NotNull Message scam) {
9297
9398 return database .read (context -> context .fetchCount (SCAM_HISTORY ,
9499 SCAM_HISTORY .SENT_AT .greaterOrEqual (recentScamThreshold )
95- .and (SCAM_HISTORY .GUILD_ID .eq (scam .getGuild ().getIdLong ()))
96- .and (SCAM_HISTORY .AUTHOR_ID .eq (scam .getAuthor ().getIdLong ()))
97- .and (SCAM_HISTORY .CONTENT .eq (scam .getContentRaw ())))) != 0 ;
100+ .and (SCAM_HISTORY .GUILD_ID .eq (scam .getGuild ().getIdLong ()))
101+ .and (SCAM_HISTORY .AUTHOR_ID .eq (scam .getAuthor ().getIdLong ()))
102+ .and (SCAM_HISTORY .CONTENT .eq (scam .getContentRaw ())))) != 0 ;
98103 }
99104
100105 /**
101106 * Deletes all scam records from the history, which have been sent earlier than the given time.
107+ *
102108 * @param olderThan all records older than this will be deleted
103109 */
104110 public void deleteHistoryOlderThan (Instant olderThan ) {
105111 database .write (context -> context .deleteFrom (SCAM_HISTORY )
106- .where (SCAM_HISTORY .SENT_AT .lessOrEqual (olderThan ))
107- .execute ());
112+ .where (SCAM_HISTORY .SENT_AT .lessOrEqual (olderThan ))
113+ .execute ());
108114 }
109115
110116 /**
111117 * Identification of a scam message, consisting mostly of IDs that uniquely identify it.
118+ *
112119 * @param guildId the id of the guild the message was sent in
113120 * @param channelId the id of the channel the message was sent in
114121 * @param messageId the id of the message itself
0 commit comments