forked from khakers/modmail-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModMailLogDB.java
More file actions
363 lines (319 loc) · 14.7 KB
/
ModMailLogDB.java
File metadata and controls
363 lines (319 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package com.github.khakers.modmailviewer;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.github.khakers.modmailviewer.auth.Role;
import com.github.khakers.modmailviewer.auth.UserToken;
import com.github.khakers.modmailviewer.data.ModMailLogEntry;
import com.github.khakers.modmailviewer.data.ModmailConfig;
import com.github.khakers.modmailviewer.data.internal.TicketStatus;
import com.github.khakers.modmailviewer.util.DateFormatters;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.*;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Indexes;
import com.mongodb.client.model.Sorts;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bson.Document;
import org.bson.UuidRepresentation;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.codecs.pojo.PojoCodecProvider;
import org.jetbrains.annotations.Nullable;
import org.mongojack.JacksonMongoCollection;
import org.mongojack.internal.MongoJackModule;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;
public class ModMailLogDB {
public static final int DEFAULT_ITEMS_PER_PAGE = 8;
private static final Logger logger = LogManager.getLogger();
private final MongoDatabase database;
private final MongoCollection<Document> configCollection;
private final JacksonMongoCollection<ModMailLogEntry> logCollection;
private final ObjectMapper objectMapper;
private ModmailConfig cachedConfig = null;
private Instant cacheTime;
public ModMailLogDB(String connectionString) {
this.objectMapper = JsonMapper.builder()
.addModule(new JavaTimeModule())
.addModule(new Jdk8Module())
.addModule(new MongoJackModule())
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true)
.withConfigOverride(Instant.class, cfg -> cfg.setFormat(JsonFormat.Value.forPattern(DateFormatters.PYTHON_STR_ISO_OFFSET_DATE_TIME_STRING)))
.build();
CodecRegistry pojoCodecRegistry = CodecRegistries.fromProviders(PojoCodecProvider.builder().automatic(true).build());
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),
pojoCodecRegistry);
var settings = MongoClientSettings.builder()
.applyConnectionString(new ConnectionString(connectionString))
.codecRegistry(codecRegistry)
.build();
MongoClient mongoClient = MongoClients.create(settings);
this.database = mongoClient.getDatabase("modmail_bot");
database.listCollectionNames().forEach(logger::debug);
this.logCollection = JacksonMongoCollection.builder().withObjectMapper(objectMapper).build(database, "logs", ModMailLogEntry.class, UuidRepresentation.STANDARD);
this.configCollection = database.getCollection("config");
if (configCollection.countDocuments() > 1 && Config.BOT_ID == 0) {
logger.warn("Multiple configuration documents were found in your MongoDB database. " +
"You *MUST* set the BOT_ID variable to your bots ID in order for the correct modmail configuration to be used.");
}
var result = logCollection.createIndex(Indexes.descending("messages.timestamp"));
// var textIndex = logCollection.createIndex(Indexes.text("messages.content"));
logger.debug(result);
}
public int getTotalTickets(TicketStatus ticketStatus) {
return getTotalTickets(ticketStatus, "");
}
public int getTotalTickets(TicketStatus ticketStatus, @Nullable String text) {
if (text == null || text.isBlank()) {
switch (ticketStatus) {
case OPEN -> {
return Math.toIntExact(logCollection.countDocuments(Filters.eq("open", true)));
}
case CLOSED -> {
return Math.toIntExact(logCollection.countDocuments(Filters.eq("open", false)));
}
case ALL -> {
return (int) logCollection.estimatedDocumentCount();
}
default -> {
return 0;
}
}
}
switch (ticketStatus) {
case OPEN -> {
return Math.toIntExact(logCollection.countDocuments(Filters.and(Filters.eq("open", true), Filters.text(text))));
}
case CLOSED -> {
return Math.toIntExact(logCollection.countDocuments(Filters.and(Filters.eq("open", false), Filters.text(text))));
}
case ALL -> {
return Math.toIntExact(logCollection.countDocuments(Filters.text(text)));
}
default -> {
return 0;
}
}
}
public Optional<ModMailLogEntry> getModMailLogEntry(String id) {
ModMailLogEntry result = logCollection.find(Filters.eq("_id", id)).limit(1).first();
return Optional.ofNullable(result);
}
public List<ModMailLogEntry> getMostRecentEntries(int count) {
ArrayList<ModMailLogEntry> entries = new ArrayList<>();
FindIterable<ModMailLogEntry> foundLogs = logCollection
.find()
.sort(Sorts.descending("created_at"))
.limit(count);
foundLogs.forEach(entries::add);
return entries;
}
public List<ModMailLogEntry> getPaginatedMostRecentEntries(int page) {
return getPaginatedMostRecentEntries(page, DEFAULT_ITEMS_PER_PAGE);
}
public List<ModMailLogEntry> getPaginatedMostRecentEntries(int page, int itemsPerPage) {
ArrayList<ModMailLogEntry> entries = new ArrayList<>();
FindIterable<ModMailLogEntry> foundLogs = logCollection
.find()
.sort(Sorts.descending("created_at"))
.skip((page - 1) * itemsPerPage)
.limit(itemsPerPage);
foundLogs.forEach(entries::add);
return entries;
}
/**
* Returns a list of log entries based on their most recent activity
*
* @param page
* @return
*/
public List<ModMailLogEntry> getPaginatedMostRecentEntriesByMessageActivity(int page) {
return getPaginatedMostRecentEntriesByMessageActivity(page, TicketStatus.ALL);
}
/**
* Returns a list of log entries based on their most recent activity
*
* @param page
* @return
*/
public List<ModMailLogEntry> getPaginatedMostRecentEntriesByMessageActivity(int page, TicketStatus ticketStatus) {
return getPaginatedMostRecentEntriesByMessageActivity(page, DEFAULT_ITEMS_PER_PAGE, ticketStatus, null);
}
/**
* Returns a list of log entries based on their most recent activity
*
* @param page
* @param itemsPerPage
* @param searchText
* @return
*/
public List<ModMailLogEntry> getPaginatedMostRecentEntriesByMessageActivity(int page, int itemsPerPage, TicketStatus ticketStatus, String searchText) {
return searchPaginatedMostRecentEntriesByMessageActivity(page, itemsPerPage, ticketStatus, searchText, "");
// ArrayList<ModMailLogEntry> entries = new ArrayList<>(itemsPerPage);
// var ticketFilter = switch (ticketStatus) {
// case ALL -> Filters.empty();
// case CLOSED -> Filters.eq("open", false);
// case OPEN -> Filters.eq("open", true);
// };
// logger.debug("filtering by {} with {}", ticketStatus, ticketFilter);
// var foundLogs = logCollection
// .find()
// .filter(Filters.not(Filters.size("messages", 0)))
// .sort(Sorts.descending("messages.timestamp"))
// .filter(ticketFilter)
// .skip((page - 1) * itemsPerPage)
// .limit(itemsPerPage);
// foundLogs.forEach(document -> {
// try {
// entries.add(objectMapper.readValue(document.toJson(), ModMailLogEntry.class));
// } catch (JsonProcessingException e) {
// logger.error(e);
// }
// });
// logger.trace("Entries: {}", entries);
// return entries;
}
public List<ModMailLogEntry> searchPaginatedMostRecentEntriesByMessageActivity(int page, TicketStatus ticketStatus, String searchkey, String userId) {
return searchPaginatedMostRecentEntriesByMessageActivity(page, DEFAULT_ITEMS_PER_PAGE, ticketStatus, searchkey, userId);
}
public List<ModMailLogEntry> searchPaginatedMostRecentEntriesByMessageActivity(int page, int itemsPerPage, TicketStatus ticketStatus, String searchkey, String userId) {
ArrayList<ModMailLogEntry> entries = new ArrayList<>(itemsPerPage);
var ticketFilter = switch (ticketStatus) {
case ALL -> Filters.empty();
case CLOSED -> Filters.eq("open", false);
case OPEN -> Filters.eq("open", true);
};
if (!"".equals(userId)) {
logger.debug("Searching by creator ID " + userId);
ticketFilter = Filters.and(ticketFilter, Filters.eq("creator.id", userId));
}
logger.debug("filtering by {} with {} and search text '{}'", ticketStatus, ticketFilter, searchkey);
FindIterable<ModMailLogEntry> foundLogs = logCollection
.find()
.filter(Filters.not(Filters.size("messages", 0)))
.sort(Sorts.descending("messages.timestamp"))
.filter(Objects.nonNull(searchkey) && !searchkey.isBlank() ? Filters.and(ticketFilter, Filters.text(searchkey)) : ticketFilter)
.skip((page - 1) * itemsPerPage)
.limit(itemsPerPage);
try {
foundLogs.forEach(entries::add);
} catch (Exception e) {
logger.error("an exception occurred while trying to parse");
throw e;
}
logger.trace("Entries: {}", entries);
return entries;
}
public List<ModMailLogEntry> getAllLogs() {
ArrayList<ModMailLogEntry> entries = new ArrayList<>();
FindIterable<ModMailLogEntry> foundLogs = logCollection
.find();
foundLogs.forEach(entries::add);
logger.trace("Entries: {}", entries.size());
return entries;
}
/**
* Determines the number of pages required to display every modmail entry at 8 per page
*
* @param ticketStatus ticket status to filter for
* @return numbers of pages required to paginate all modmail logs
*/
public int getPaginationCount(TicketStatus ticketStatus, String search) {
return getPaginationCount(DEFAULT_ITEMS_PER_PAGE, ticketStatus, search);
}
/**
* Determines the number of pages required to display every modmail entry at the given items per page
*
* @param itemsPerPage max entries per page
* @param ticketStatus ticket status to filter for
* @return numbers of pages required to paginate all modmail logs
*/
public int getPaginationCount(int itemsPerPage, TicketStatus ticketStatus, String search) {
return (int) (Math.ceil(getTotalTickets(ticketStatus, search) / (double) (itemsPerPage)));
}
public ModmailConfig getConfig() throws Exception {
// Check to see if the config is cached
if (cacheTime != null && Instant.now().isBefore(cacheTime.plus(5, ChronoUnit.MINUTES))) {
logger.debug("grabbed cached config from {}", cacheTime.toString());
cacheTime = Instant.now();
return cachedConfig;
}
Document conf = null;
if (Config.BOT_ID == 0) {
conf = configCollection.find().first();
} else {
conf = configCollection.find(Filters.eq("bot_id", Config.BOT_ID)).first();
}
if (conf != null) {
try {
logger.trace(conf.toJson());
var config = objectMapper.readValue(conf.toJson(), ModmailConfig.class);
// set cache
cachedConfig = config;
cacheTime = Instant.now();
return config;
} catch (JsonProcessingException e) {
logger.error(e);
throw new RuntimeException(e);
}
} else {
throw new Exception("Config could not be loaded from mongodb");
}
}
public Role getUserOrGuildRole(UserToken user, long[] roles) throws Exception {
var levelPermissions = getConfig().getLevelPermissions();
var role = Role.ANYONE;
for (Map.Entry<Role, List<Long>> entry :
levelPermissions.entrySet()) {
// Check if user ID matches
if (entry.getValue().contains(user.getId())) {
var foundRole = entry.getKey();
logger.trace("Matched user by their ID to role with permission level {}", entry.getKey());
// We always want to get the user's highest possible role
if (foundRole.value > role.value) {
role = foundRole;
}
}
if (roles != null && roles.length >= 1) {
// Check if a role id matches
for (long roleID :
roles) {
if (entry.getValue().contains(roleID)) {
var foundRole = entry.getKey();
logger.trace("Matched user to role id {} with permission level {}", roleID, foundRole);
if (foundRole.value > role.value) {
role = foundRole;
}
}
}
}
}
return role;
}
public Role getUserRole(UserToken user) throws Exception {
var levelPermissions = getConfig().getLevelPermissions();
for (Map.Entry<Role, List<Long>> entry :
levelPermissions.entrySet()) {
if (entry.getValue().contains(user.getId())) {
return entry.getKey();
}
}
return Role.ANYONE;
}
public MongoCollection<Document> getConfigCollection() {
return configCollection;
}
public JacksonMongoCollection<ModMailLogEntry> getLogCollection() {
return logCollection;
}
}