Search in sources :

Example 1 with Vote

use of com.erudika.para.core.Vote in project scoold by Erudika.

the class VoteController method processVoteRequest.

boolean processVoteRequest(boolean isUpvote, ParaObject votable, HttpServletRequest req) {
    Profile author = null;
    Profile authUser = utils.getAuthUser(req);
    boolean result = false;
    boolean update = false;
    if (votable == null || authUser == null) {
        return false;
    }
    try {
        List<ParaObject> voteObjects = pc.readAll(Arrays.asList(votable.getCreatorid(), new Vote(authUser.getId(), votable.getId(), Votable.VoteValue.UP).getId(), new Vote(authUser.getId(), votable.getId(), Votable.VoteValue.DOWN).getId()));
        author = (Profile) voteObjects.stream().filter((p) -> p instanceof Profile).findFirst().orElse(null);
        Integer votes = votable.getVotes() != null ? votable.getVotes() : 0;
        boolean upvoteExists = voteObjects.stream().anyMatch((v) -> v instanceof Vote && ((Vote) v).isUpvote());
        boolean downvoteExists = voteObjects.stream().anyMatch((v) -> v instanceof Vote && ((Vote) v).isDownvote());
        boolean isVoteCorrection = (isUpvote && downvoteExists) || (!isUpvote && upvoteExists);
        if (isUpvote && voteUp(votable, authUser.getId())) {
            votes++;
            result = true;
            update = updateReputationOnUpvote(votable, votes, authUser, author, isVoteCorrection);
        } else if (!isUpvote && voteDown(votable, authUser.getId())) {
            votes--;
            result = true;
            hideCommentAndReport(votable, votes, votable.getId(), req);
            update = updateReputationOnDownvote(votable, votes, authUser, author, isVoteCorrection);
        }
    } catch (Exception ex) {
        logger.error(null, ex);
        result = false;
    }
    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 (update) {
        pc.updateAll(Arrays.asList(author, authUser));
    }
    return result;
}
Also used : Vote(com.erudika.para.core.Vote) ParaObject(com.erudika.para.core.ParaObject) Profile(com.erudika.scoold.core.Profile)

Aggregations

ParaObject (com.erudika.para.core.ParaObject)1 Vote (com.erudika.para.core.Vote)1 Profile (com.erudika.scoold.core.Profile)1