Search in sources :

Example 16 with Profile

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

the class CommentController method createAjax.

@PostMapping
public String createAjax(@RequestParam String comment, @RequestParam String parentid, HttpServletRequest req, Model model) {
    Profile authUser = utils.getAuthUser(req);
    if (utils.canComment(authUser, req) && !StringUtils.isBlank(comment) && !StringUtils.isBlank(parentid)) {
        Comment showComment = utils.populate(req, new Comment(), "comment");
        showComment.setCreatorid(authUser.getId());
        Map<String, String> error = utils.validate(showComment);
        if (error.isEmpty()) {
            showComment.setComment(comment);
            showComment.setParentid(parentid);
            showComment.setAuthorName(authUser.getName());
            if (showComment.create() != null) {
                long commentCount = authUser.getComments();
                utils.addBadgeOnce(authUser, COMMENTATOR, commentCount >= COMMENTATOR_IFHAS);
                authUser.setComments(commentCount + 1);
                authUser.update();
                model.addAttribute("showComment", showComment);
                // send email to the author of parent post
                Post parentPost = pc.read(parentid);
                if (parentPost != null) {
                    parentPost.addCommentId(showComment.getId());
                    parentPost.update();
                }
                sendCommentNotification(parentPost, showComment, authUser);
            }
        }
    }
    return "comment";
}
Also used : Comment(com.erudika.scoold.core.Comment) Post(com.erudika.scoold.core.Post) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 17 with Profile

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

the class SettingsController method post.

@PostMapping
public String post(@RequestParam(required = false) String tags, @RequestParam(required = false) String latlng, @RequestParam(required = false) String replyEmailsOn, @RequestParam(required = false) String commentEmailsOn, HttpServletRequest req) {
    if (utils.isAuthenticated(req)) {
        Profile authUser = utils.getAuthUser(req);
        if (!StringUtils.isBlank(tags)) {
            Set<String> ts = new LinkedHashSet<String>();
            for (String tag : tags.split(",")) {
                if (!StringUtils.isBlank(tag) && ts.size() <= MAX_FAV_TAGS) {
                    ts.add(tag);
                }
            }
            authUser.setFavtags(new LinkedList<String>(ts));
        }
        if (!StringUtils.isBlank(latlng)) {
            authUser.setLatlng(latlng);
        }
        authUser.setReplyEmailsEnabled(Boolean.valueOf(replyEmailsOn));
        authUser.setCommentEmailsEnabled(Boolean.valueOf(commentEmailsOn));
        authUser.update();
    }
    return "redirect:" + settingslink;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 18 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 19 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 20 with Profile

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

the class ScooldUtils method fetchProfiles.

public void fetchProfiles(List<? extends ParaObject> objects) {
    if (objects == null || objects.isEmpty()) {
        return;
    }
    Map<String, String> authorids = new HashMap<String, String>(objects.size());
    Map<String, Profile> authors = new HashMap<String, Profile>(objects.size());
    for (ParaObject obj : objects) {
        if (obj.getCreatorid() != null) {
            authorids.put(obj.getId(), obj.getCreatorid());
        }
    }
    List<String> ids = new ArrayList<String>(new HashSet<String>(authorids.values()));
    if (ids.isEmpty()) {
        return;
    }
    // read all post authors in batch
    for (ParaObject author : pc.readAll(ids)) {
        authors.put(author.getId(), (Profile) author);
    }
    // set author object for each post
    for (ParaObject obj : objects) {
        if (obj instanceof Post) {
            ((Post) obj).setAuthor(authors.get(authorids.get(obj.getId())));
        } else if (obj instanceof Revision) {
            ((Revision) obj).setAuthor(authors.get(authorids.get(obj.getId())));
        }
    }
}
Also used : Revision(com.erudika.scoold.core.Revision) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ParaObject(com.erudika.para.core.ParaObject) Post(com.erudika.scoold.core.Post) ArrayList(java.util.ArrayList) Profile(com.erudika.scoold.core.Profile)

Aggregations

Profile (com.erudika.scoold.core.Profile)30 PostMapping (org.springframework.web.bind.annotation.PostMapping)20 Post (com.erudika.scoold.core.Post)12 ParaObject (com.erudika.para.core.ParaObject)4 Pager (com.erudika.para.utils.Pager)4 Reply (com.erudika.scoold.core.Reply)4 Report (com.erudika.scoold.core.Report)4 HashMap (java.util.HashMap)4 User (com.erudika.para.core.User)3 Comment (com.erudika.scoold.core.Comment)3 Question (com.erudika.scoold.core.Question)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 Translation (com.erudika.para.core.Translation)2 Feedback (com.erudika.scoold.core.Feedback)2 ArrayList (java.util.ArrayList)2 Locale (java.util.Locale)2 Address (com.erudika.para.core.Address)1 Revision (com.erudika.scoold.core.Revision)1 IOException (java.io.IOException)1 LinkedHashSet (java.util.LinkedHashSet)1