Search in sources :

Example 1 with Profile

use of com.erudika.scoold.core.Profile in project scoold by Erudika.

the class QuestionController method replyAjax.

@PostMapping({ "/{id}", "/{id}/{title}" })
public String replyAjax(@PathVariable String id, @PathVariable(required = false) String title, @RequestParam(required = false) Boolean emailme, HttpServletRequest req, HttpServletResponse res, Model model) throws IOException {
    Post showPost = pc.read(id);
    Profile authUser = utils.getAuthUser(req);
    if (showPost != null && emailme != null) {
        if (emailme) {
            showPost.addFollower(authUser.getUser());
        } else {
            showPost.removeFollower(authUser.getUser());
        }
        // update without adding revisions
        pc.update(showPost);
    } else if (showPost != null && !showPost.isClosed() && !showPost.isReply()) {
        //create new answer
        Reply answer = utils.populate(req, new Reply(), "body");
        Map<String, String> error = utils.validate(answer);
        if (!error.containsKey("body")) {
            answer.setTitle(showPost.getTitle());
            answer.setCreatorid(authUser.getId());
            answer.setParentid(showPost.getId());
            answer.create();
            showPost.setAnswercount(showPost.getAnswercount() + 1);
            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
            sendReplyNotifications(showPost, answer);
            return "reply";
        }
    }
    if (utils.isAjaxRequest(req)) {
        res.setStatus(200);
        return "base";
    } else {
        return "redirect:" + questionslink + "/" + id;
    }
}
Also used : Post(com.erudika.scoold.core.Post) Reply(com.erudika.scoold.core.Reply) HashMap(java.util.HashMap) Map(java.util.Map) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 2 with Profile

use of com.erudika.scoold.core.Profile in project scoold by Erudika.

the class QuestionController method sendReplyNotifications.

private void sendReplyNotifications(Post parentPost, Post reply) {
    // 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>();
        String name = replyAuthor.getName();
        String body = Utils.markdownToHtml(Utils.abbreviate(reply.getBody(), 500));
        String picture = Utils.formatMessage("<img src='{0}' width='25'>", replyAuthor.getPicture());
        String postURL = getServerURL() + parentPost.getPostLink(false, false);
        model.put("logourl", Config.getConfigParam("small_logo_url", "https://scoold.com/logo.png"));
        model.put("heading", Utils.formatMessage("New reply to <a href='{0}'>{1}</a>", postURL, parentPost.getTitle()));
        model.put("body", Utils.formatMessage("<h2>{0} {1}:</h2><div class='panel'>{2}</div>", picture, 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 (parentPost.hasFollowers()) {
            emailer.sendEmail(new ArrayList<String>(parentPost.getFollowers().values()), name + " replied to '" + Utils.abbreviate(reply.getTitle(), 50) + "...'", Utils.compileMustache(model, utils.loadEmailTemplate("notify")));
        }
    }
}
Also used : User(com.erudika.para.core.User) HashMap(java.util.HashMap) ParaObject(com.erudika.para.core.ParaObject) Profile(com.erudika.scoold.core.Profile)

Example 3 with Profile

use of com.erudika.scoold.core.Profile in project scoold by Erudika.

the class TranslateController method delete.

@PostMapping("/delete/{id}")
public String delete(@PathVariable String id, HttpServletRequest req, Model model) {
    if (id != null && utils.isAuthenticated(req)) {
        Translation trans = (Translation) pc.read(id);
        Profile authUser = utils.getAuthUser(req);
        if (authUser.getId().equals(trans.getCreatorid()) || utils.isAdmin(authUser)) {
            utils.getLangutils().disapproveTranslation(trans.getLocale(), trans.getId());
            pc.delete(trans);
        }
        if (!utils.isAjaxRequest(req)) {
            return "redirect:" + req.getRequestURI();
        }
    }
    return "base";
}
Also used : Translation(com.erudika.para.core.Translation) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 4 with Profile

use of com.erudika.scoold.core.Profile in project scoold by Erudika.

the class QuestionController method edit.

@PostMapping("/{id}/edit")
public String edit(@PathVariable String id, @RequestParam(required = false) String title, @RequestParam(required = false) String body, @RequestParam(required = false) String tags, HttpServletRequest req) {
    Post showPost = pc.read(id);
    Profile authUser = utils.getAuthUser(req);
    if (!utils.canEdit(showPost, authUser) || showPost == null) {
        return "redirect:" + req.getRequestURI();
    }
    Post beforeUpdate = null;
    try {
        beforeUpdate = (Post) BeanUtils.cloneBean(showPost);
    } catch (Exception ex) {
        logger.error(null, ex);
    }
    if (!StringUtils.isBlank(title) && title.length() > 10) {
        showPost.setTitle(title);
    }
    if (!StringUtils.isBlank(body)) {
        showPost.setBody(body);
    }
    if (!StringUtils.isBlank(tags) && showPost.isQuestion()) {
        showPost.setTags(Arrays.asList(StringUtils.split(tags, ",")));
    }
    showPost.setLasteditby(authUser.getId());
    //note: update only happens if something has changed
    if (!showPost.equals(beforeUpdate)) {
        showPost.update();
        utils.addBadgeOnceAndUpdate(authUser, Badge.EDITOR, true);
    }
    return "redirect:" + showPost.getPostLink(false, false);
}
Also used : Post(com.erudika.scoold.core.Post) Profile(com.erudika.scoold.core.Profile) IOException(java.io.IOException) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 5 with Profile

use of com.erudika.scoold.core.Profile in project scoold by Erudika.

the class ReportsController method create.

@PostMapping
public void create(HttpServletRequest req, HttpServletResponse res) {
    Report rep = utils.populate(req, new Report(), "link", "description", "parentid", "subType", "authorName");
    Map<String, String> error = utils.validate(rep);
    if (error.isEmpty()) {
        if (utils.isAuthenticated(req)) {
            Profile authUser = utils.getAuthUser(req);
            rep.setAuthorName(authUser.getName());
            rep.setCreatorid(authUser.getId());
            utils.addBadgeAndUpdate(authUser, REPORTER, true);
        } else {
            //allow anonymous reports
            rep.setAuthorName(utils.getLang(req).get("anonymous"));
        }
        rep.create();
        res.setStatus(200);
    } else {
        res.setStatus(400);
    }
}
Also used : Report(com.erudika.scoold.core.Report) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

Profile (com.erudika.scoold.core.Profile)85 PostMapping (org.springframework.web.bind.annotation.PostMapping)47 Post (com.erudika.scoold.core.Post)29 ParaObject (com.erudika.para.core.ParaObject)25 User (com.erudika.para.core.User)19 Pager (com.erudika.para.core.utils.Pager)19 HashMap (java.util.HashMap)15 LinkedHashMap (java.util.LinkedHashMap)15 Question (com.erudika.scoold.core.Question)13 Reply (com.erudika.scoold.core.Reply)13 Report (com.erudika.scoold.core.Report)11 IOException (java.io.IOException)11 List (java.util.List)11 Map (java.util.Map)11 UnapprovedReply (com.erudika.scoold.core.UnapprovedReply)10 GetMapping (org.springframework.web.bind.annotation.GetMapping)10 Sysprop (com.erudika.para.core.Sysprop)9 Config (com.erudika.para.core.utils.Config)9 Utils (com.erudika.para.core.utils.Utils)9 Comment (com.erudika.scoold.core.Comment)9