Skip to content

Commit

Permalink
feat: allow filtering by user
Browse files Browse the repository at this point in the history
  • Loading branch information
nullishamy committed Jan 7, 2025
1 parent a77b72a commit abf7639
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
11 changes: 8 additions & 3 deletions src/main/java/com/github/khakers/modmailviewer/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -214,4 +219,4 @@ public static void main(String[] args) {
logger.info("You are running Modmail-Viewer {} built on {}", ModmailViewer.VERSION, ModmailViewer.BUILD_TIMESTAMP);

}
}
}
14 changes: 10 additions & 4 deletions src/main/java/com/github/khakers/modmailviewer/ModMailLogDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public List<ModMailLogEntry> getPaginatedMostRecentEntriesByMessageActivity(int
* @return
*/
public List<ModMailLogEntry> getPaginatedMostRecentEntriesByMessageActivity(int page, int itemsPerPage, TicketStatus ticketStatus, String searchText) {
return searchPaginatedMostRecentEntriesByMessageActivity(page, itemsPerPage, ticketStatus, searchText);
return searchPaginatedMostRecentEntriesByMessageActivity(page, itemsPerPage, ticketStatus, searchText, "");
// ArrayList<ModMailLogEntry> entries = new ArrayList<>(itemsPerPage);
// var ticketFilter = switch (ticketStatus) {
// case ALL -> Filters.empty();
Expand All @@ -207,17 +207,23 @@ public List<ModMailLogEntry> getPaginatedMostRecentEntriesByMessageActivity(int
// return entries;
}

public List<ModMailLogEntry> searchPaginatedMostRecentEntriesByMessageActivity(int page, TicketStatus ticketStatus, String searchkey) {
return searchPaginatedMostRecentEntriesByMessageActivity(page, DEFAULT_ITEMS_PER_PAGE, ticketStatus, searchkey);
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) {
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()
Expand Down
16 changes: 14 additions & 2 deletions src/main/jte/pages/homepage.jte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

@param Context ctx


@param java.util.List<com.github.khakers.modmailviewer.data.ModMailLogEntry> logEntries

@param int page
Expand All @@ -20,6 +19,7 @@
@param TicketStatus ticketStatusFilter
@param boolean showNSFW
@param String search
@param String userId

<!doctype html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
Expand Down Expand Up @@ -67,6 +67,18 @@
</div>
<div class="col-xl-8 col-sm-12 pt-2 py-xl-0">
<div id="SearchBox" class="input-group px-xl-0 px-sm-2">
<button class="btn btn-outline-danger" type="button" id="ClearUserQueryButton"><i
class="bi bi-x-lg"></i> Reset
</button>
<input id="UserSearchBoxInput" type="search" class="form-control" placeholder="Search by user"
aria-label="Search Text" aria-describedby="SearchButton" value="${userId}">
<button class="btn btn-outline-primary" type="button" id="UserSearchButton"><i
class="bi bi-search"></i> Search
</button>
</div>
</div>
<div class="col-xl-8 col-sm-12 pt-2 py-xl-0">
<div id="UserSearchBox" class="input-group px-xl-0 px-sm-2">
<button class="btn btn-outline-danger" type="button" id="ClearSearchButton"><i
class="bi bi-x-lg"></i> Reset
</button>
Expand Down Expand Up @@ -210,4 +222,4 @@
</div>
@template.macros.footer()
</body>
</html>
</html>
28 changes: 28 additions & 0 deletions src/main/resources/static/js/TicketFilterHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/static/js/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,4 @@ up.compiler('.content', (element, data, meta) => {
bsModal.show();

}
});
});

0 comments on commit abf7639

Please sign in to comment.