31
31
32
32
import static org .slf4j .LoggerFactory .getLogger ;
33
33
34
+ /**
35
+ * Class responsible for resolving context for messages and voice states.
36
+ */
34
37
public class ContextResolver {
35
38
private static final Logger log = getLogger (ContextResolver .class );
36
39
private final Voice voiceData ;
@@ -46,6 +49,12 @@ public class ContextResolver {
46
49
.expireAfterWrite (10 , TimeUnit .SECONDS )
47
50
.build ();
48
51
52
+ /**
53
+ * Constructs a ContextResolver instance with the specified voice data and configuration.
54
+ *
55
+ * @param voiceData the voice data provider
56
+ * @param configuration the configuration settings
57
+ */
49
58
public ContextResolver (Voice voiceData , Configuration configuration ) {
50
59
this .voiceData = voiceData ;
51
60
this .configuration = configuration ;
@@ -57,20 +66,29 @@ public ContextResolver(Voice voiceData, Configuration configuration) {
57
66
* Only members which have written in the last 100 messages which are not older than the max history and are not
58
67
* send before the first message of the message author are returned
59
68
*
60
- * @param message message to determine channel, author and start time
61
- * @param settings setting sof the guild.
62
- * @return list of members which have written in this channel
69
+ * @param target the target member
70
+ * @param message the message to determine channel, author, and start time
71
+ * @param settings the settings of the guild
72
+ * @return the message context
63
73
*/
64
74
@ NotNull
65
75
public MessageContext getChannelContext (Member target , Message message , @ Nullable Settings settings ) {
66
76
try {
67
77
return messageContextCache .get (message .getIdLong (), () -> retrieveChannelContext (target , message , settings ).resolve ());
68
78
} catch (ExecutionException e ) {
69
- log .error ("Could not conpute channel context." , e );
79
+ log .error ("Could not compute channel context." , e );
70
80
}
71
81
return MessageContext .byMessageAndMember (message , target );
72
82
}
73
83
84
+ /**
85
+ * Retrieves the channel context for the specified message.
86
+ *
87
+ * @param target the target member
88
+ * @param message the message to determine channel, author, and start time
89
+ * @param settings the settings of the guild
90
+ * @return the message context
91
+ */
74
92
private MessageContext retrieveChannelContext (Member target , Message message , Settings settings ) {
75
93
var history = message .getChannel ().getHistoryBefore (message , configuration .analyzerSettings ().historySize ())
76
94
.complete ();
@@ -92,12 +110,12 @@ private MessageContext retrieveChannelContext(Member target, Message message, Se
92
110
}
93
111
94
112
/**
95
- * Add the latest authors.
113
+ * Adds the latest authors to the context .
96
114
* <p>
97
- * Authors are considered latest when they have written a message in the last {@link AbuseProtection#minMessages()}
115
+ * Authors are considered latest when they have written a message in the last {@link AbuseProtection#minMessages()}.
98
116
*
99
- * @param context context to add
100
- * @param settings settings
117
+ * @param context the message context
118
+ * @param settings the settings of the guild
101
119
*/
102
120
private void addLatestAuthors (MessageContext context , @ Nullable Settings settings ) {
103
121
var maxAge = Instant .now ().minus (configuration .analyzerSettings ().latestMaxHours (), ChronoUnit .HOURS );
@@ -111,12 +129,12 @@ private void addLatestAuthors(MessageContext context, @Nullable Settings setting
111
129
}
112
130
113
131
/**
114
- * Add the recent authors.
132
+ * Adds the recent authors to the context .
115
133
* <p>
116
- * Authors are considered recent when they have written a message in the {@link AbuseProtection#maxMessageAge()}
134
+ * Authors are considered recent when they have written a message in the {@link AbuseProtection#maxMessageAge()}.
117
135
*
118
- * @param context context to add
119
- * @param settings settings
136
+ * @param context the message context
137
+ * @param settings the settings of the guild
120
138
*/
121
139
private void addRecentAuthors (MessageContext context , Settings settings ) {
122
140
var maxAge = Instant .now ().minus (settings == null ? Long .MAX_VALUE : settings .abuseProtection ()
@@ -128,11 +146,11 @@ private void addRecentAuthors(MessageContext context, Settings settings) {
128
146
}
129
147
130
148
/**
131
- * Add the ids and messages which were newer than oldest to the context
149
+ * Adds the IDs and messages which were newer than the oldest to the context.
132
150
*
133
- * @param messages messages
134
- * @param context context
135
- * @param oldest oldest allowed message
151
+ * @param messages the messages
152
+ * @param context the message context
153
+ * @param oldest the oldest allowed message
136
154
*/
137
155
private void addMembersAfter (Collection <Message > messages , MessageContext context , Instant oldest ) {
138
156
// filter message for only recent messages and after the first message of the user.
@@ -151,6 +169,13 @@ private void addMembersAfter(Collection<Message> messages, MessageContext contex
151
169
context .addIds (memberIds );
152
170
}
153
171
172
+ /**
173
+ * Finds the oldest message by the target member in the context.
174
+ *
175
+ * @param context the message context
176
+ * @param maxAge the maximum age of the message
177
+ * @return the oldest message instant
178
+ */
154
179
private Instant findOldestMessageByTarget (MessageContext context , Instant maxAge ) {
155
180
return context .rawMessages ().stream ()
156
181
.filter (mes -> Verifier .equalSnowflake (mes .getAuthor (), context .user ()))
@@ -160,6 +185,14 @@ private Instant findOldestMessageByTarget(MessageContext context, Instant maxAge
160
185
.orElse (maxAge );
161
186
}
162
187
188
+ /**
189
+ * Gets the voice context for the specified message.
190
+ *
191
+ * @param target the target member
192
+ * @param message the message to determine channel, author, and start time
193
+ * @param settings the settings of the guild
194
+ * @return the message context
195
+ */
163
196
public MessageContext getVoiceContext (Member target , Message message , @ Nullable Settings settings ) {
164
197
try {
165
198
return voiceContextCache .get (message .getIdLong (), () -> retrieveVoiceContext (target , message , settings ).resolve ()
@@ -170,6 +203,14 @@ public MessageContext getVoiceContext(Member target, Message message, @Nullable
170
203
return MessageContext .byMessageAndMember (message , target );
171
204
}
172
205
206
+ /**
207
+ * Retrieves the voice context for the specified message.
208
+ *
209
+ * @param target the target member
210
+ * @param message the message to determine channel, author, and start time
211
+ * @param settings the settings of the guild
212
+ * @return the message context
213
+ */
173
214
private MessageContext retrieveVoiceContext (Member target , Message message , @ Nullable Settings settings ) {
174
215
var context = MessageContext .byMessageAndMember (message , target );
175
216
var voiceState = target .getVoiceState ();
@@ -193,10 +234,25 @@ private MessageContext retrieveVoiceContext(Member target, Message message, @Nul
193
234
return context ;
194
235
}
195
236
237
+ /**
238
+ * Gets the combined context for the specified message.
239
+ *
240
+ * @param message the message to determine channel, author, and start time
241
+ * @param settings the settings of the guild
242
+ * @return the combined message context
243
+ */
196
244
public MessageContext getCombinedContext (Message message , @ Nullable Settings settings ) {
197
245
return getCombinedContext (message .getMember (), message , settings );
198
246
}
199
247
248
+ /**
249
+ * Gets the combined context for the specified member and message.
250
+ *
251
+ * @param target the target member
252
+ * @param message the message to determine channel, author, and start time
253
+ * @param settings the settings of the guild
254
+ * @return the combined message context
255
+ */
200
256
public MessageContext getCombinedContext (Member target , Message message , @ Nullable Settings settings ) {
201
257
return getChannelContext (target , message , settings )
202
258
.combine (getVoiceContext (target , message , settings ));
0 commit comments