Skip to content

Commit

Permalink
added support for searching users by email in the top search box
Browse files Browse the repository at this point in the history
  • Loading branch information
albogdano committed May 10, 2024
1 parent da5c821 commit 8ff3fba
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/main/java/com/erudika/scoold/controllers/SearchController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package com.erudika.scoold.controllers;

import com.erudika.para.client.ParaClient;
import com.erudika.para.core.User;
import com.erudika.para.core.annotations.Email;
import com.erudika.para.core.utils.Config;
import com.erudika.para.core.utils.Pager;
import com.erudika.para.core.utils.ParaObjectUtils;
Expand All @@ -43,6 +45,8 @@
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedOutput;
import jakarta.inject.Inject;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -54,8 +58,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import jakarta.inject.Inject;
import jakarta.servlet.http.HttpServletRequest;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.apache.hc.core5.http.HttpHeaders;
import org.slf4j.Logger;
Expand Down Expand Up @@ -112,8 +115,8 @@ public String get(@PathVariable(required = false) String type, @PathVariable(req
answerslist = pc.findQuery(Utils.type(Reply.class), qs, itemcount);
} else if ("feedback".equals(type) && utils.isFeedbackEnabled()) {
feedbacklist = pc.findQuery(Utils.type(Feedback.class), queryString, itemcount);
} else if ("people".equals(type) && usersPublic) {
userlist = pc.findQuery(Utils.type(Profile.class), getUsersSearchQuery(queryString, req), itemcount);
} else if (("people".equals(type) || isEmailQuery(queryString)) && usersPublic) {
userlist = searchUsers(queryString, req, itemcount);
} else if ("comments".equals(type)) {
commentslist = pc.findQuery(Utils.type(Comment.class), qs, itemcount);
} else {
Expand All @@ -123,7 +126,7 @@ public String get(@PathVariable(required = false) String type, @PathVariable(req
feedbacklist = pc.findQuery(Utils.type(Feedback.class), queryString);
}
if (usersPublic) {
userlist = pc.findQuery(Utils.type(Profile.class), getUsersSearchQuery(queryString, req));
userlist = searchUsers(queryString, req);
}
commentslist = pc.findQuery(Utils.type(Comment.class), qs, itemcount);
}
Expand Down Expand Up @@ -154,6 +157,21 @@ private String getUsersSearchQuery(String qs, HttpServletRequest req) {
return utils.getUsersSearchQuery(qs, spaceFilter);
}

private List<Profile> searchUsers(String queryString, HttpServletRequest req, Pager... pager) {
if (isEmailQuery(queryString)) {
List<String> uids = pc.findTerms(Utils.type(User.class),
Map.of(Config._EMAIL, StringUtils.remove(queryString, "\"")), true).
stream().map(u -> Profile.id(u.getId())).collect(Collectors.toList());
return pc.findByIds(uids);
} else {
return pc.findQuery(Utils.type(Profile.class), getUsersSearchQuery(queryString, req), pager);
}
}

private boolean isEmailQuery(String q) {
return q.matches(Email.EMAIL_PATTERN) || StringUtils.remove(q, "\"").matches(Email.EMAIL_PATTERN);
}

@ResponseBody
@GetMapping("/opensearch.xml")
public ResponseEntity<String> openSearch(HttpServletRequest req) {
Expand Down

0 comments on commit 8ff3fba

Please sign in to comment.