Skip to content

Commit

Permalink
added 3 new standard webhook events - question.view, user.signin, use…
Browse files Browse the repository at this point in the history
…r.search, closes #418
  • Loading branch information
albogdano committed Nov 30, 2023
1 parent 41b5d8b commit 7256630
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
Expand All @@ -56,6 +57,7 @@
import javax.ws.rs.Produces;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.hc.core5.http.HttpHeaders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -136,6 +138,7 @@ public String get(@PathVariable String id, @PathVariable(required = false) Strin
if (showPost.getAuthor() != null) {
model.addAttribute("ogimage", utils.getFullAvatarURL(showPost.getAuthor(), AvatarFormat.Profile));
}
triggerQuestionViewEvent(showPost, req);
return "base";
}

Expand Down Expand Up @@ -604,4 +607,23 @@ private void unApproveAnswer(Profile authUser, Profile author, Post showPost) {
pc.updateAll(Arrays.asList(author, authUser));
}
}

private void triggerQuestionViewEvent(Post question, HttpServletRequest req) {
if (req != null) {
Profile authUser = utils.getAuthUser(req);
Map<String, Object> payload = new LinkedHashMap<>(ParaObjectUtils.getAnnotatedFields(authUser, false));
if (authUser != null) {
payload.put("visitor", ParaObjectUtils.getAnnotatedFields(authUser, false));
} else {
payload.put("visitor", Collections.emptyMap());
}
Map<String, String> headers = new HashMap<>();
headers.put(HttpHeaders.REFERER, req.getHeader(HttpHeaders.REFERER));
headers.put(HttpHeaders.USER_AGENT, req.getHeader(HttpHeaders.USER_AGENT));
headers.put("User-IP", req.getRemoteAddr());
payload.put("headers", headers);
payload.put("question", question);
utils.triggerHookEvent("question.view", payload);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.erudika.para.client.ParaClient;
import com.erudika.para.core.utils.Config;
import com.erudika.para.core.utils.Pager;
import com.erudika.para.core.utils.ParaObjectUtils;
import com.erudika.para.core.utils.Utils;
import com.erudika.scoold.ScooldConfig;
import static com.erudika.scoold.ScooldServer.SEARCHLINK;
Expand All @@ -43,11 +44,20 @@
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedOutput;
import java.io.IOException;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.hc.core5.http.HttpHeaders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.CacheControl;
Expand Down Expand Up @@ -135,6 +145,7 @@ public String get(@PathVariable(required = false) String type, @PathVariable(req
model.addAttribute("feedbacklist", feedbacklist);
model.addAttribute("commentslist", commentslist);

triggerSearchEvent(type, qs, userlist.size() + questionslist.size() + answerslist.size() + commentslist.size(), req);
return "base";
}

Expand Down Expand Up @@ -299,4 +310,25 @@ private String getSitemap(HttpServletRequest req) throws IOException, FeedExcept
logger.debug("Sitemap generation skipped - public={} auth={}", utils.isDefaultSpacePublic(), utils.isAuthenticated(req));
return "<_/>";
}

private void triggerSearchEvent(String type, String query, int results, HttpServletRequest req) {
if (req != null) {
Profile authUser = utils.getAuthUser(req);
Map<String, Object> payload = new LinkedHashMap<>(ParaObjectUtils.getAnnotatedFields(authUser, false));
if (authUser != null) {
payload.put("visitor", ParaObjectUtils.getAnnotatedFields(authUser, false));
} else {
payload.put("visitor", Collections.emptyMap());
}
Map<String, String> headers = new HashMap<>();
headers.put(HttpHeaders.REFERER, req.getHeader(HttpHeaders.REFERER));
headers.put(HttpHeaders.USER_AGENT, req.getHeader(HttpHeaders.USER_AGENT));
headers.put("User-IP", req.getRemoteAddr());
payload.put("headers", headers);
payload.put("category", type);
payload.put("query", query);
payload.put("results", results);
utils.triggerHookEvent("user.search", payload);
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/erudika/scoold/controllers/SigninController.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,27 @@
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.ParaObjectUtils;
import com.erudika.para.core.utils.Utils;
import com.erudika.scoold.ScooldConfig;
import static com.erudika.scoold.ScooldServer.HOMEPAGE;
import static com.erudika.scoold.ScooldServer.SIGNINLINK;
import com.erudika.scoold.core.Profile;
import com.erudika.scoold.utils.HttpUtils;
import static com.erudika.scoold.utils.HttpUtils.getBackToUrl;
import static com.erudika.scoold.utils.HttpUtils.setAuthCookie;
import com.erudika.scoold.utils.ScooldUtils;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.hc.core5.http.HttpHeaders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
Expand Down Expand Up @@ -159,6 +163,7 @@ public String signup(@RequestParam String name, @RequestParam String email, @Req
User u = pc.signIn("password", email + ":" + name + ":" + passw, false);
if (u != null && u.getActive()) {
setAuthCookie(u.getPassword(), req, res);
triggerLoginEvent(u, req);
return "redirect:" + getBackToUrl(req);
} else {
verifyEmailIfNecessary(name, email, req);
Expand Down Expand Up @@ -305,6 +310,7 @@ private String onAuthSuccess(User u, HttpServletRequest req, HttpServletResponse
if (u != null && utils.isEmailDomainApproved(u.getEmail())) {
// the user password in this case is a Bearer token (JWT)
setAuthCookie(u.getPassword(), req, res);
triggerLoginEvent(u, req);
return "redirect:" + getBackToUrl(req);
} else if (u != null && !utils.isEmailDomainApproved(u.getEmail())) {
logger.warn("Signin failed for {} because that domain is not in the whitelist.", u.getEmail());
Expand Down Expand Up @@ -421,4 +427,18 @@ private boolean isValidResetToken(Sysprop s, String key, String token) {
}
return false;
}

private void triggerLoginEvent(User u, HttpServletRequest req) {
if (req != null && u != null) {
Profile authUser = utils.getAuthUser(req);
Map<String, Object> payload = new LinkedHashMap<>(ParaObjectUtils.getAnnotatedFields(authUser, false));
Map<String, String> headers = new HashMap<>();
headers.put(HttpHeaders.REFERER, req.getHeader(HttpHeaders.REFERER));
headers.put(HttpHeaders.USER_AGENT, req.getHeader(HttpHeaders.USER_AGENT));
headers.put("User-IP", req.getRemoteAddr());
payload.put("user", u);
payload.put("headers", headers);
utils.triggerHookEvent("user.signin", payload);
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/erudika/scoold/utils/ScooldUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,14 @@ public final class ScooldUtils {
HOOK_EVENTS = new HashSet<>(Arrays.asList(
"question.create",
"question.close",
"question.view",
"answer.create",
"answer.accept",
"report.create",
"comment.create",
"user.signin",
"user.signup",
"user.search",
"revision.restore"));

WHITELISTED_MACROS = new HashMap<String, String>();
Expand Down

0 comments on commit 7256630

Please sign in to comment.