use of com.erudika.scoold.core.UnapprovedReply in project scoold by Erudika.
the class ScooldUtils method sendReplyNotifications.
public void sendReplyNotifications(Post parentPost, Post reply, HttpServletRequest req) {
// send email notification to author of post except when the reply is by the same person
if (parentPost != null && reply != null && !StringUtils.equals(parentPost.getCreatorid(), reply.getCreatorid())) {
// the current user - same as utils.getAuthUser(req)
Profile replyAuthor = reply.getAuthor();
Map<String, Object> model = new HashMap<String, Object>();
Map<String, String> lang = getLang(req);
String name = replyAuthor.getName();
String body = Utils.markdownToHtml(reply.getBody());
String picture = Utils.formatMessage("<img src='{0}' width='25'>", escapeHtmlAttribute(avatarRepository.getLink(replyAuthor, AvatarFormat.Square25)));
String postURL = getServerURL() + parentPost.getPostLink(false, false);
String subject = Utils.formatMessage(lang.get("notification.reply.subject"), name, Utils.abbreviate(reply.getTitle(), 255));
model.put("subject", escapeHtml(subject));
model.put("logourl", getSmallLogoUrl());
model.put("heading", Utils.formatMessage(lang.get("notification.reply.heading"), Utils.formatMessage("<a href='{0}'>{1}</a>", postURL, escapeHtml(parentPost.getTitle()))));
model.put("body", Utils.formatMessage("<h2>{0} {1}:</h2><div>{2}</div>", picture, escapeHtml(name), body));
Profile authorProfile = pc.read(parentPost.getCreatorid());
if (authorProfile != null) {
User author = authorProfile.getUser();
if (author != null) {
if (authorProfile.getReplyEmailsEnabled()) {
parentPost.addFollower(author);
}
}
}
if (postsNeedApproval() && reply instanceof UnapprovedReply) {
Report rep = new Report();
rep.setDescription("New reply awaiting approval");
rep.setSubType(Report.ReportType.OTHER);
rep.setLink(parentPost.getPostLink(false, false) + "#post-" + reply.getId());
rep.setAuthorName(reply.getAuthor().getName());
rep.create();
}
if (isReplyNotificationAllowed() && parentPost.hasFollowers()) {
emailer.sendEmail(new ArrayList<String>(parentPost.getFollowers().values()), subject, compileEmailTemplate(model));
}
}
}
use of com.erudika.scoold.core.UnapprovedReply 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.UnapprovedReply 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