use of com.erudika.scoold.core.Profile in project scoold by Erudika.
the class QuestionsController method setSpace.
@GetMapping({ "/questions/space/{space}", "/questions/space" })
public String setSpace(@PathVariable(required = false) String space, HttpServletRequest req, HttpServletResponse res, Model model) {
if ("all".equals(space) || utils.isAllSpaces(space)) {
space = Post.ALL_MY_SPACES + ":" + utils.getLang(req).get("allspaces");
} else {
Sysprop spaceObj = pc.read(utils.getSpaceId(space));
if (!StringUtils.isBlank(space) && spaceObj == null) {
Profile authUser = utils.getAuthUser(req);
if (authUser != null && utils.canAccessSpace(authUser, space)) {
authUser.removeSpace(space);
authUser.update();
}
}
if (spaceObj != null) {
space = spaceObj.getId().concat(Config.SEPARATOR).concat(spaceObj.getName());
} else {
space = Post.DEFAULT_SPACE;
}
}
utils.storeSpaceIdInCookie(space, req, res);
String backTo = HttpUtils.getBackToUrl(req);
if (StringUtils.isBlank(backTo)) {
return get(req.getParameter("sortby"), req, model);
} else {
return "redirect:" + backTo;
}
}
use of com.erudika.scoold.core.Profile in project scoold by Erudika.
the class QuestionsController method getSimilarAjax.
@GetMapping("/questions/similar/{like}")
public void getSimilarAjax(@PathVariable String like, HttpServletRequest req, HttpServletResponse res) throws IOException {
if (!utils.isDefaultSpacePublic() && !utils.isAuthenticated(req)) {
res.setStatus(401);
return;
}
Pager pager = new Pager(1, "votes", true, Config.getConfigInt("max_similar_posts", 7));
Profile authUser = utils.getAuthUser(req);
StringBuilder sb = new StringBuilder();
Question q = new Question();
q.setTitle(like);
q.setBody("");
q.setTags(Arrays.asList(""));
for (Post similarPost : utils.getSimilarPosts(q, pager)) {
if (utils.isMod(authUser) || utils.canAccessSpace(authUser, similarPost.getSpace())) {
boolean hasAnswer = !StringUtils.isBlank(similarPost.getAnswerid());
sb.append("<span class=\"lightborder phm").append(hasAnswer ? " light-green white-text" : "").append("\">");
sb.append(similarPost.getVotes());
sb.append("</span> <a href=\"").append(similarPost.getPostLink(false, false)).append("\">");
sb.append(HtmlUtils.htmlEscape(similarPost.getTitle())).append("</a><br>");
}
}
res.setCharacterEncoding("UTF-8");
res.getWriter().print(sb.toString());
res.setStatus(200);
}
use of com.erudika.scoold.core.Profile in project scoold by Erudika.
the class QuestionController method close.
@PostMapping("/{id}/close")
public String close(@PathVariable String id, HttpServletRequest req) {
Post showPost = pc.read(id);
Profile authUser = utils.getAuthUser(req);
if (showPost == null) {
return "redirect:" + req.getRequestURI();
}
if (utils.isMod(authUser) && !showPost.isReply()) {
if (showPost.isClosed()) {
showPost.setCloserid("");
} else {
showPost.setCloserid(authUser.getId());
utils.triggerHookEvent("question.close", showPost);
}
showPost.update();
}
return "redirect:" + showPost.getPostLink(false, false);
}
use of com.erudika.scoold.core.Profile in project scoold by Erudika.
the class QuestionController method reply.
@PostMapping({ "/{id}", "/{id}/{title}", "/{id}/{title}/write" })
public String reply(@PathVariable String id, @PathVariable(required = false) String title, @RequestParam(required = false) Boolean emailme, HttpServletRequest req, HttpServletResponse res, Model model) {
Post showPost = pc.read(id);
Profile authUser = utils.getAuthUser(req);
if (authUser == null || showPost == null) {
if (utils.isAjaxRequest(req)) {
res.setStatus(400);
return "base";
} else {
return "redirect:" + QUESTIONSLINK + "/" + id;
}
}
if (emailme != null) {
followPost(showPost, authUser, emailme);
} else if (!showPost.isClosed() && !showPost.isReply()) {
// create new answer
boolean needsApproval = utils.postNeedsApproval(authUser);
Reply answer = utils.populate(req, needsApproval ? new UnapprovedReply() : new Reply(), "body");
Map<String, String> error = utils.validate(answer);
if (!error.containsKey("body") && !StringUtils.isBlank(answer.getBody())) {
answer.setTitle(showPost.getTitle());
answer.setCreatorid(authUser.getId());
answer.setParentid(showPost.getId());
answer.setSpace(showPost.getSpace());
answer.create();
showPost.setAnswercount(showPost.getAnswercount() + 1);
showPost.setLastactivity(System.currentTimeMillis());
if (showPost.getAnswercount() >= MAX_REPLIES_PER_POST) {
showPost.setCloserid("0");
}
// update without adding revisions
pc.update(showPost);
utils.addBadgeAndUpdate(authUser, Badge.EUREKA, answer.getCreatorid().equals(showPost.getCreatorid()));
answer.setAuthor(authUser);
model.addAttribute("showPost", showPost);
model.addAttribute("answerslist", Collections.singletonList(answer));
// send email to the question author
utils.sendReplyNotifications(showPost, answer, req);
model.addAttribute("newpost", getNewAnswerPayload(answer));
} else {
model.addAttribute("error", error);
model.addAttribute("path", "question.vm");
res.setStatus(400);
}
return "reply";
} else {
model.addAttribute("error", "Parent post doesn't exist or cannot have children.");
}
if (utils.isAjaxRequest(req)) {
res.setStatus(200);
return "reply";
} else {
return "redirect:" + QUESTIONSLINK + "/" + id;
}
}
use of com.erudika.scoold.core.Profile in project scoold by Erudika.
the class QuestionController method get.
@GetMapping({ "/{id}", "/{id}/{title}", "/{id}/{title}/*" })
public String get(@PathVariable String id, @PathVariable(required = false) String title, @RequestParam(required = false) String sortby, HttpServletRequest req, HttpServletResponse res, Model model) {
Post showPost = pc.read(id);
if (showPost == null || !ParaObjectUtils.typesMatch(showPost)) {
return "redirect:" + QUESTIONSLINK;
}
Profile authUser = utils.getAuthUser(req);
if (!utils.canAccessSpace(authUser, showPost.getSpace())) {
return "redirect:" + (utils.isDefaultSpacePublic() || utils.isAuthenticated(req) ? QUESTIONSLINK : SIGNINLINK + "?returnto=" + req.getRequestURI());
} else if (!utils.isDefaultSpace(showPost.getSpace()) && pc.read(utils.getSpaceId(showPost.getSpace())) == null) {
showPost.setSpace(Post.DEFAULT_SPACE);
pc.update(showPost);
}
if (showPost instanceof UnapprovedQuestion && !(utils.isMine(showPost, authUser) || utils.isMod(authUser))) {
return "redirect:" + QUESTIONSLINK;
}
Pager itemcount = utils.getPager("page", req);
itemcount.setSortby("newest".equals(sortby) ? "timestamp" : "votes");
List<Reply> answerslist = getAllAnswers(authUser, showPost, itemcount);
LinkedList<Post> allPosts = new LinkedList<Post>();
allPosts.add(showPost);
allPosts.addAll(answerslist);
utils.fetchProfiles(allPosts);
utils.getComments(allPosts);
utils.updateViewCount(showPost, req, res);
model.addAttribute("path", "question.vm");
model.addAttribute("title", utils.getLang(req).get("questions.title") + " - " + showPost.getTitle());
model.addAttribute("description", Utils.abbreviate(Utils.stripAndTrim(showPost.getBody(), " "), 195));
model.addAttribute("itemcount", itemcount);
model.addAttribute("showPost", allPosts.removeFirst());
model.addAttribute("answerslist", allPosts);
model.addAttribute("similarquestions", utils.getSimilarPosts(showPost, new Pager(10)));
model.addAttribute("maxCommentLength", Comment.MAX_COMMENT_LENGTH);
model.addAttribute("includeGMapsScripts", utils.isNearMeFeatureEnabled());
model.addAttribute("maxCommentLengthError", Utils.formatMessage(utils.getLang(req).get("maxlength"), Comment.MAX_COMMENT_LENGTH));
return "base";
}
Aggregations