use of com.erudika.scoold.core.UnapprovedQuestion in project scoold by Erudika.
the class ScooldUtils method sendNewPostNotifications.
@SuppressWarnings("unchecked")
public void sendNewPostNotifications(Post question, HttpServletRequest req) {
if (question == null) {
return;
}
// the current user - same as utils.getAuthUser(req)
Profile postAuthor = question.getAuthor() != null ? question.getAuthor() : pc.read(question.getCreatorid());
if (!question.getType().equals(Utils.type(UnapprovedQuestion.class))) {
if (!isNewPostNotificationAllowed()) {
return;
}
Map<String, Object> model = new HashMap<String, Object>();
Map<String, String> lang = getLang(req);
String name = postAuthor.getName();
String body = Utils.markdownToHtml(question.getBody());
String picture = Utils.formatMessage("<img src='{0}' width='25'>", escapeHtmlAttribute(avatarRepository.getLink(postAuthor, AvatarFormat.Square25)));
String postURL = getServerURL() + question.getPostLink(false, false);
String tagsString = Optional.ofNullable(question.getTags()).orElse(Collections.emptyList()).stream().map(t -> "<span class=\"tag\">" + escapeHtml(t) + "</span>").collect(Collectors.joining(" "));
String subject = Utils.formatMessage(lang.get("notification.newposts.subject"), name, Utils.abbreviate(question.getTitle(), 255));
model.put("subject", escapeHtml(subject));
model.put("logourl", getSmallLogoUrl());
model.put("heading", Utils.formatMessage(lang.get("notification.newposts.heading"), picture, escapeHtml(name)));
model.put("body", Utils.formatMessage("<h2><a href='{0}'>{1}</a></h2><div>{2}</div><br>{3}", postURL, escapeHtml(question.getTitle()), body, tagsString));
Set<String> emails = new HashSet<String>(getNotificationSubscribers(EMAIL_ALERTS_PREFIX + "new_post_subscribers"));
emails.addAll(getFavTagsSubscribers(question.getTags()));
sendEmailsToSubscribersInSpace(emails, question.getSpace(), subject, compileEmailTemplate(model));
} else if (postsNeedApproval() && question instanceof UnapprovedQuestion) {
Report rep = new Report();
rep.setDescription("New question awaiting approval");
rep.setSubType(Report.ReportType.OTHER);
rep.setLink(question.getPostLink(false, false));
rep.setAuthorName(postAuthor.getName());
rep.create();
}
}
use of com.erudika.scoold.core.UnapprovedQuestion in project scoold by Erudika.
the class QuestionsController method getQuestions.
public List<Question> getQuestions(String sortby, String filter, HttpServletRequest req, Model model) {
Pager itemcount = getPagerFromCookie(req, utils.getPager("page", req));
List<Question> questionslist = Collections.emptyList();
String type = Utils.type(Question.class);
Profile authUser = utils.getAuthUser(req);
String currentSpace = utils.getSpaceIdFromCookie(authUser, req);
String query = getQuestionsQuery(req, authUser, sortby, currentSpace, itemcount);
if (!StringUtils.isBlank(filter) && authUser != null) {
if ("favtags".equals(filter)) {
if (!authUser.hasFavtags() && req.getParameterValues("favtags") != null) {
// API override
authUser.setFavtags(Arrays.asList(req.getParameterValues("favtags")));
}
if (isSpaceFilteredRequest(authUser, currentSpace) && authUser.hasFavtags()) {
questionslist = pc.findQuery(type, getSpaceFilteredFavtagsQuery(currentSpace, authUser), itemcount);
} else {
questionslist = pc.findTermInList(type, Config._TAGS, authUser.getFavtags(), itemcount);
}
} else if ("local".equals(filter)) {
String latlng = Optional.ofNullable(authUser.getLatlng()).orElse(req.getParameter("latlng"));
String[] ll = latlng == null ? new String[0] : latlng.split(",");
if (ll.length == 2) {
double lat = NumberUtils.toDouble(ll[0]);
double lng = NumberUtils.toDouble(ll[1]);
questionslist = pc.findNearby(type, query, 25, lat, lng, itemcount);
}
}
model.addAttribute("localFilterOn", "local".equals(filter));
model.addAttribute("tagFilterOn", "favtags".equals(filter));
model.addAttribute("filter", "/" + Utils.stripAndTrim(filter));
} else {
questionslist = pc.findQuery(type, query, itemcount);
}
if (utils.postsNeedApproval() && utils.isMod(authUser)) {
Pager p = new Pager(itemcount.getPage(), itemcount.getLimit());
List<UnapprovedQuestion> uquestionslist = pc.findQuery(Utils.type(UnapprovedQuestion.class), query, p);
List<Question> qlist = new LinkedList<>(uquestionslist);
itemcount.setCount(itemcount.getCount() + p.getCount());
qlist.addAll(questionslist);
questionslist = qlist;
}
utils.fetchProfiles(questionslist);
model.addAttribute("itemcount", itemcount);
model.addAttribute("questionslist", questionslist);
return questionslist;
}
use of com.erudika.scoold.core.UnapprovedQuestion in project scoold by Erudika.
the class QuestionsController method post.
@PostMapping("/questions/ask")
public String post(@RequestParam(required = false) String location, @RequestParam(required = false) String latlng, @RequestParam(required = false) String address, String space, HttpServletRequest req, HttpServletResponse res, Model model) {
if (utils.isAuthenticated(req)) {
Profile authUser = utils.getAuthUser(req);
String currentSpace = utils.getValidSpaceIdExcludingAll(authUser, space, req);
boolean needsApproval = utils.postNeedsApproval(authUser);
Question q = utils.populate(req, needsApproval ? new UnapprovedQuestion() : new Question(), "title", "body", "tags|,", "location");
q.setCreatorid(authUser.getId());
q.setSpace(currentSpace);
if (StringUtils.isBlank(q.getTagsString())) {
q.setTags(Arrays.asList(Config.getConfigParam("default_question_tag", "question")));
}
Map<String, String> error = utils.validate(q);
if (error.isEmpty()) {
q.setLocation(location);
q.setAuthor(authUser);
String qid = q.create();
utils.sendNewPostNotifications(q, req);
if (!StringUtils.isBlank(latlng)) {
Address addr = new Address(qid + Config.SEPARATOR + Utils.type(Address.class));
addr.setAddress(address);
addr.setCountry(location);
addr.setLatlng(latlng);
addr.setParentid(qid);
addr.setCreatorid(authUser.getId());
pc.create(addr);
}
authUser.setLastseen(System.currentTimeMillis());
model.addAttribute("newpost", getNewQuestionPayload(q));
} else {
model.addAttribute("error", error);
model.addAttribute("draftQuestion", q);
model.addAttribute("defaultTag", "");
model.addAttribute("path", "questions.vm");
model.addAttribute("includeGMapsScripts", utils.isNearMeFeatureEnabled());
model.addAttribute("askSelected", "navbtn-hover");
return "base";
}
if (utils.isAjaxRequest(req)) {
res.setStatus(200);
res.setContentType("application/json");
try {
res.getWriter().println("{\"url\":\"" + q.getPostLink(false, false) + "\"}");
} catch (IOException ex) {
}
return "blank";
} else {
return "redirect:" + q.getPostLink(false, false);
}
}
if (utils.isAjaxRequest(req)) {
res.setStatus(400);
return "blank";
} else {
return "redirect:" + SIGNINLINK + "?returnto=" + QUESTIONSLINK + "/ask";
}
}
use of com.erudika.scoold.core.UnapprovedQuestion 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";
}
use of com.erudika.scoold.core.UnapprovedQuestion in project scoold by Erudika.
the class QuestionController method modApprove.
@PostMapping("/{id}/approve")
public String modApprove(@PathVariable String id, HttpServletRequest req) {
Post showPost = pc.read(id);
Profile authUser = utils.getAuthUser(req);
if (utils.isMod(authUser)) {
if (showPost instanceof UnapprovedQuestion) {
showPost.setType(Utils.type(Question.class));
pc.create(showPost);
utils.sendNewPostNotifications(showPost, req);
} else if (showPost instanceof UnapprovedReply) {
showPost.setType(Utils.type(Reply.class));
pc.create(showPost);
}
}
return "redirect:" + showPost.getPostLink(false, false);
}
Aggregations