From abf763945b970196ba52a76716dcd8adca1184af Mon Sep 17 00:00:00 2001 From: nullishamy Date: Tue, 7 Jan 2025 21:39:18 +0000 Subject: [PATCH] feat: allow filtering by user --- .../github/khakers/modmailviewer/Main.java | 11 ++++++-- .../khakers/modmailviewer/ModMailLogDB.java | 14 +++++++--- src/main/jte/pages/homepage.jte | 16 +++++++++-- .../static/js/TicketFilterHandler.js | 28 +++++++++++++++++++ src/main/resources/static/js/base.js | 2 +- 5 files changed, 61 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/github/khakers/modmailviewer/Main.java b/src/main/java/com/github/khakers/modmailviewer/Main.java index 1a077197..2a9996d0 100644 --- a/src/main/java/com/github/khakers/modmailviewer/Main.java +++ b/src/main/java/com/github/khakers/modmailviewer/Main.java @@ -155,20 +155,25 @@ public static void main(String[] args) { .check(s -> s.length() > 0 && s.length() < 120 , "search text cannot be greater than 50 characters") .getOrDefault(""); + String userIdSearch = ctx.queryParamAsClass("userId", String.class) + .check(s -> s.length() > 0 && s.length() < 120 + , "userId query cannot be greater than 50 characters") + .getOrDefault(""); var ticketFilter = TicketStatus.valueOf(statusFilter.toUpperCase()); var pageCount = db.getPaginationCount(ticketFilter, search); page = Math.min(pageCount, page); ctx.render("pages/homepage.jte", model( "ctx", ctx, - "logEntries", db.searchPaginatedMostRecentEntriesByMessageActivity(page, ticketFilter, search), + "logEntries", db.searchPaginatedMostRecentEntriesByMessageActivity(page, ticketFilter, search, userIdSearch), "page", page, "pageCount", pageCount, "user", authHandler != null ? AuthHandler.getUser(ctx) : new UserToken(0L, "anonymous", "0000", "", new long[]{}, false), "modMailLogDB", db, "ticketStatusFilter", ticketFilter, "showNSFW", showNSFW, - "search", search)); + "search", search, + "userId", userIdSearch)); }, RoleUtils.atLeastSupporter()) .get("/logs/{id}", ctx -> { var entry = db.getModMailLogEntry(ctx.pathParam("id")); @@ -214,4 +219,4 @@ public static void main(String[] args) { logger.info("You are running Modmail-Viewer {} built on {}", ModmailViewer.VERSION, ModmailViewer.BUILD_TIMESTAMP); } -} \ No newline at end of file +} diff --git a/src/main/java/com/github/khakers/modmailviewer/ModMailLogDB.java b/src/main/java/com/github/khakers/modmailviewer/ModMailLogDB.java index 565538fc..27085cd3 100644 --- a/src/main/java/com/github/khakers/modmailviewer/ModMailLogDB.java +++ b/src/main/java/com/github/khakers/modmailviewer/ModMailLogDB.java @@ -181,7 +181,7 @@ public List getPaginatedMostRecentEntriesByMessageActivity(int * @return */ public List getPaginatedMostRecentEntriesByMessageActivity(int page, int itemsPerPage, TicketStatus ticketStatus, String searchText) { - return searchPaginatedMostRecentEntriesByMessageActivity(page, itemsPerPage, ticketStatus, searchText); + return searchPaginatedMostRecentEntriesByMessageActivity(page, itemsPerPage, ticketStatus, searchText, ""); // ArrayList entries = new ArrayList<>(itemsPerPage); // var ticketFilter = switch (ticketStatus) { // case ALL -> Filters.empty(); @@ -207,17 +207,23 @@ public List getPaginatedMostRecentEntriesByMessageActivity(int // return entries; } - public List searchPaginatedMostRecentEntriesByMessageActivity(int page, TicketStatus ticketStatus, String searchkey) { - return searchPaginatedMostRecentEntriesByMessageActivity(page, DEFAULT_ITEMS_PER_PAGE, ticketStatus, searchkey); + public List searchPaginatedMostRecentEntriesByMessageActivity(int page, TicketStatus ticketStatus, String searchkey, String userId) { + return searchPaginatedMostRecentEntriesByMessageActivity(page, DEFAULT_ITEMS_PER_PAGE, ticketStatus, searchkey, userId); } - public List searchPaginatedMostRecentEntriesByMessageActivity(int page, int itemsPerPage, TicketStatus ticketStatus, String searchkey) { + public List searchPaginatedMostRecentEntriesByMessageActivity(int page, int itemsPerPage, TicketStatus ticketStatus, String searchkey, String userId) { ArrayList 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 foundLogs = logCollection .find() diff --git a/src/main/jte/pages/homepage.jte b/src/main/jte/pages/homepage.jte index 979f9f4d..ae2e21be 100644 --- a/src/main/jte/pages/homepage.jte +++ b/src/main/jte/pages/homepage.jte @@ -10,7 +10,6 @@ @param Context ctx - @param java.util.List logEntries @param int page @@ -20,6 +19,7 @@ @param TicketStatus ticketStatusFilter @param boolean showNSFW @param String search +@param String userId @@ -67,6 +67,18 @@
+
+
+
@@ -210,4 +222,4 @@
@template.macros.footer() - \ No newline at end of file + diff --git a/src/main/resources/static/js/TicketFilterHandler.js b/src/main/resources/static/js/TicketFilterHandler.js index c85f7437..f8869d95 100644 --- a/src/main/resources/static/js/TicketFilterHandler.js +++ b/src/main/resources/static/js/TicketFilterHandler.js @@ -18,12 +18,40 @@ up.compiler("#ClearSearchButton", function (element) { }) }) +up.compiler("#ClearUserQueryButton", function (element) { + element.addEventListener('click', () => { + const url = window.location.href; + console.log(url); + const params = up.Params.fromURL(url); + params.delete('userId'); + + up.navigate({params: params, url: window.location.pathname}); + }) +}) + up.compiler('#SearchButton', function (element) { element.addEventListener("click", (event) => { search(); }) }) +up.compiler('#UserSearchButton', function (element) { + element.addEventListener("click", (event) => { + userSearch(); + }) +}) + +function userSearch() { + const search = document.getElementById("UserSearchBoxInput").value; + + const url = window.location.href; + console.log(url); + const params = up.Params.fromURL(url); + params.set('userId', search); + + up.navigate({params: params, url: window.location.pathname}); +} + function search() { const search = document.getElementById("SearchBoxInput").value; diff --git a/src/main/resources/static/js/base.js b/src/main/resources/static/js/base.js index 0d01d174..5ceee528 100644 --- a/src/main/resources/static/js/base.js +++ b/src/main/resources/static/js/base.js @@ -123,4 +123,4 @@ up.compiler('.content', (element, data, meta) => { bsModal.show(); } -}); \ No newline at end of file +});