Search in sources :

Example 1 with UnapprovedReply

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));
        }
    }
}
Also used : UnapprovedReply(com.erudika.scoold.core.UnapprovedReply) User(com.erudika.para.core.User) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Report(com.erudika.scoold.core.Report) ParaObject(com.erudika.para.core.ParaObject) ConfigObject(com.typesafe.config.ConfigObject) Profile(com.erudika.scoold.core.Profile)

Example 2 with UnapprovedReply

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;
    }
}
Also used : UnapprovedReply(com.erudika.scoold.core.UnapprovedReply) Post(com.erudika.scoold.core.Post) UnapprovedReply(com.erudika.scoold.core.UnapprovedReply) Reply(com.erudika.scoold.core.Reply) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 3 with UnapprovedReply

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);
}
Also used : UnapprovedReply(com.erudika.scoold.core.UnapprovedReply) UnapprovedQuestion(com.erudika.scoold.core.UnapprovedQuestion) Post(com.erudika.scoold.core.Post) Question(com.erudika.scoold.core.Question) UnapprovedQuestion(com.erudika.scoold.core.UnapprovedQuestion) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

Profile (com.erudika.scoold.core.Profile)3 UnapprovedReply (com.erudika.scoold.core.UnapprovedReply)3 Post (com.erudika.scoold.core.Post)2 LinkedHashMap (java.util.LinkedHashMap)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 ParaObject (com.erudika.para.core.ParaObject)1 User (com.erudika.para.core.User)1 Question (com.erudika.scoold.core.Question)1 Reply (com.erudika.scoold.core.Reply)1 Report (com.erudika.scoold.core.Report)1 UnapprovedQuestion (com.erudika.scoold.core.UnapprovedQuestion)1 ConfigObject (com.typesafe.config.ConfigObject)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1