Search in sources :

Example 41 with Profile

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

the class TranslateController method post.

@PostMapping("/{locale}/{index}")
public String post(@PathVariable String locale, @PathVariable String index, @RequestParam String value, HttpServletRequest req, Model model) {
    Locale showLocale = utils.getLangutils().getProperLocale(locale);
    if (utils.isAuthenticated(req) && showLocale != null && !"en".equals(showLocale.getLanguage())) {
        Set<String> approved = utils.getLangutils().getApprovedTransKeys(showLocale.getLanguage());
        Profile authUser = utils.getAuthUser(req);
        String langkey = langkeys.get(getIndex(index, langkeys));
        boolean isTranslated = approved.contains(langkey);
        if (!StringUtils.isBlank(value) && (!isTranslated || utils.isAdmin(authUser))) {
            Translation trans = new Translation(showLocale.getLanguage(), langkey, StringUtils.trim(value));
            trans.setCreatorid(authUser.getId());
            trans.setAuthorName(authUser.getName());
            trans.setTimestamp(System.currentTimeMillis());
            pc.create(trans);
            model.addAttribute("newtranslation", trans);
        }
        if (!utils.isAjaxRequest(req)) {
            return "redirect:" + translatelink + "/" + showLocale.getLanguage() + "/" + getNextIndex(getIndex(index, langkeys), approved, langkeys);
        }
    }
    return "base";
}
Also used : Locale(java.util.Locale) Translation(com.erudika.para.core.Translation) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 42 with Profile

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

the class VoteController method processVoteRequest.

boolean processVoteRequest(boolean isUpvote, String type, String id, HttpServletRequest req) {
    if (StringUtils.isBlank(id) || StringUtils.isBlank(type)) {
        return false;
    }
    ParaObject votable = pc.read(id);
    Profile author = null;
    Profile authUser = utils.getAuthUser(req);
    boolean result = false;
    boolean updateAuthUser = false;
    boolean updateVoter = false;
    if (votable == null || authUser == null) {
        return false;
    }
    try {
        author = pc.read(votable.getCreatorid());
        Integer votes = votable.getVotes() != null ? votable.getVotes() : 0;
        if (isUpvote && (result = pc.voteUp(votable, authUser.getId()))) {
            votes++;
            authUser.incrementUpvotes();
            updateAuthUser = true;
            int reward;
            if (votable instanceof Post) {
                Post p = (Post) votable;
                if (p.isReply()) {
                    utils.addBadge(author, GOODANSWER, votes >= GOODANSWER_IFHAS, false);
                    reward = ANSWER_VOTEUP_REWARD_AUTHOR;
                } else if (p.isQuestion()) {
                    utils.addBadge(author, GOODQUESTION, votes >= GOODQUESTION_IFHAS, false);
                    reward = QUESTION_VOTEUP_REWARD_AUTHOR;
                } else {
                    reward = VOTEUP_REWARD_AUTHOR;
                }
            } else {
                reward = VOTEUP_REWARD_AUTHOR;
            }
            if (author != null && reward > 0) {
                author.addRep(reward);
                updateVoter = true;
            }
        } else if (!isUpvote && (result = pc.voteDown(votable, authUser.getId()))) {
            votes--;
            authUser.incrementDownvotes();
            updateAuthUser = true;
            if (votable instanceof Comment && votes <= -5) {
                //treat comment as offensive or spam - hide
                ((Comment) votable).setHidden(true);
            } else if (votable instanceof Post && votes <= -5) {
                Post p = (Post) votable;
                //mark post for closing
                Report rep = new Report();
                rep.setParentid(id);
                rep.setLink(p.getPostLink(false, false));
                rep.setDescription(utils.getLang(req).get("posts.forclosing"));
                rep.setSubType(Report.ReportType.OTHER);
                rep.setAuthorName("System");
                rep.create();
            }
            if (author != null) {
                author.removeRep(POST_VOTEDOWN_PENALTY_AUTHOR);
                updateVoter = true;
                //small penalty to voter
                authUser.removeRep(POST_VOTEDOWN_PENALTY_VOTER);
            }
        }
    } catch (Exception ex) {
        logger.error(null, ex);
    }
    utils.addBadgeOnce(authUser, SUPPORTER, authUser.getUpvotes() >= SUPPORTER_IFHAS);
    utils.addBadgeOnce(authUser, CRITIC, authUser.getDownvotes() >= CRITIC_IFHAS);
    utils.addBadgeOnce(authUser, VOTER, authUser.getTotalVotes() >= VOTER_IFHAS);
    if (updateVoter) {
        pc.update(author);
    }
    if (updateAuthUser) {
        pc.update(authUser);
    }
    if (updateAuthUser && updateVoter) {
        pc.updateAll(Arrays.asList(author, authUser));
    }
    return result;
}
Also used : Comment(com.erudika.scoold.core.Comment) Report(com.erudika.scoold.core.Report) ParaObject(com.erudika.para.core.ParaObject) Post(com.erudika.scoold.core.Post) Profile(com.erudika.scoold.core.Profile)

Example 43 with Profile

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

the class QuestionController method delete.

@PostMapping("/{id}/delete")
public String delete(@PathVariable String id, HttpServletRequest req) {
    Post showPost = pc.read(id);
    Profile authUser = utils.getAuthUser(req);
    if (!utils.canEdit(showPost, authUser) || showPost == null) {
        return "redirect:" + req.getRequestURI();
    }
    if (!showPost.isReply()) {
        if ((utils.isMine(showPost, authUser) || utils.isMod(authUser))) {
            showPost.delete();
            return "redirect:" + questionslink + "?success=true&code=16";
        }
    } else if (showPost.isReply()) {
        if (utils.isMine(showPost, authUser) || utils.isMod(authUser)) {
            Post parent = pc.read(showPost.getParentid());
            parent.setAnswercount(parent.getAnswercount() - 1);
            parent.update();
            showPost.delete();
        }
    }
    return "redirect:" + showPost.getPostLink(false, false);
}
Also used : Post(com.erudika.scoold.core.Post) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 44 with Profile

use of com.erudika.scoold.core.Profile 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, HttpServletRequest req, Model model) {
    if (utils.isAuthenticated(req)) {
        Profile authUser = utils.getAuthUser(req);
        Question q = utils.populate(req, new Question(), "title", "body", "tags|,", "location");
        q.setCreatorid(authUser.getId());
        Map<String, String> error = utils.validate(q);
        if (error.isEmpty()) {
            q.setLocation(location);
            String qid = q.create();
            if (!StringUtils.isBlank(latlng)) {
                Address addr = new Address();
                addr.setAddress(address);
                addr.setCountry(location);
                addr.setLatlng(latlng);
                addr.setParentid(qid);
                addr.setCreatorid(authUser.getId());
                pc.create(addr);
            }
            authUser.setLastseen(System.currentTimeMillis());
        } else {
            model.addAttribute("error", error);
            model.addAttribute("path", "questions.vm");
            model.addAttribute("includeGMapsScripts", true);
            model.addAttribute("askSelected", "navbtn-hover");
            return "base";
        }
        return "redirect:" + q.getPostLink(false, false);
    }
    return "redirect:" + signinlink + "?returnto=" + questionslink + "/ask";
}
Also used : Address(com.erudika.para.core.Address) Question(com.erudika.scoold.core.Question) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 45 with Profile

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

the class CommentController method sendCommentNotification.

private void sendCommentNotification(Post parentPost, Comment comment, Profile commentAuthor) {
    // send email notification to author of post except when the comment is by the same person
    if (parentPost != null && comment != null && !StringUtils.equals(parentPost.getCreatorid(), comment.getCreatorid())) {
        Profile authorProfile = pc.read(parentPost.getCreatorid());
        if (authorProfile != null && authorProfile.getCommentEmailsEnabled()) {
            User author = authorProfile.getUser();
            if (author != null) {
                Map<String, Object> model = new HashMap<String, Object>();
                String name = commentAuthor.getName();
                String body = Utils.markdownToHtml(Utils.abbreviate(comment.getComment(), 255));
                String pic = Utils.formatMessage("<img src='{0}' width='25'>", commentAuthor.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 comment on <a href='{0}'>{1}</a>", postURL, parentPost.getTitle()));
                model.put("body", Utils.formatMessage("<h2>{0} {1}:</h2><div class='panel'>{2}</div>", pic, name, body));
                emailer.sendEmail(Arrays.asList(author.getEmail()), name + " commented on your post", Utils.compileMustache(model, utils.loadEmailTemplate("notify")));
            }
        }
    }
}
Also used : User(com.erudika.para.core.User) HashMap(java.util.HashMap) Profile(com.erudika.scoold.core.Profile)

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