Search in sources :

Example 11 with Vote

use of org.mamute.model.Vote in project mamute by caelum.

the class MassiveVoteTest method should_not_increase_karma_for_massive_votes.

@Test
public void should_not_increase_karma_for_massive_votes() {
    for (int i = 0; i < massiveVote.getMaxVotesAllowed(); i++) {
        Vote currentVote = vote(massiveVoter, VoteType.DOWN, 1l);
        assertTrue(massiveVote.shouldCountKarma(massiveVoter, author, currentVote));
    }
    Vote deniedVote = vote(massiveVoter, VoteType.DOWN, 1l);
    assertFalse(massiveVote.shouldCountKarma(massiveVoter, author, deniedVote));
}
Also used : MassiveVote(org.mamute.model.vote.MassiveVote) Vote(org.mamute.model.Vote) Test(org.junit.Test)

Example 12 with Vote

use of org.mamute.model.Vote in project mamute by caelum.

the class MassiveVote method shouldCountKarma.

public boolean shouldCountKarma(User author, User target, Vote current) {
    List<Vote> votes = getVotesWith(current.getType()).to(target).from(author);
    if (votes.size() == MAX_VOTE_ALLOWED) {
        Vote oldestVote = votes.get(0);
        if (isExpired(oldestVote)) {
            votes.remove(0);
            votes.add(current);
            return true;
        } else {
            return false;
        }
    } else if (votes.size() < MAX_VOTE_ALLOWED) {
        votes.add(current);
        getVotesWith(current.getType()).put(target, votes);
        return true;
    }
    throw new IllegalArgumentException("massive vote size is bigger than " + MAX_VOTE_ALLOWED);
}
Also used : Vote(org.mamute.model.Vote)

Example 13 with Vote

use of org.mamute.model.Vote in project mamute by caelum.

the class VotingMachine method unRegister.

public void unRegister(Votable votable, Vote current, Class<?> votableType) {
    User voter = current.getAuthor();
    User votableAuthor = votable.getAuthor();
    ReputationEventContext eventContext = votes.contextOf(votable);
    if (votable.getAuthor().getId().equals(voter.getId())) {
        throw new IllegalArgumentException("an author can't unvote its own votable since it can't even vote on it");
    }
    Vote previous = votes.previousVoteFor(votable.getId(), voter, votableType);
    boolean shouldCountKarma = voteChecker.shouldCountKarma(voter, votableAuthor, current);
    /* O previous vai sempre existir nessa caso !! ( o ideal :]  ) */
    if (previous != null) {
        ReputationEvent receivedVote = new ReceivedVoteEvent(previous.getType(), votable, eventContext, shouldCountKarma).reputationEvent();
        votableAuthor.descreaseKarma(karmaCalculator.karmaFor(receivedVote));
        ReputationEvent votedAtSomething = new VotedAtSomethingEvent(previous, eventContext).reputationEvent();
        voter.descreaseKarma(karmaCalculator.karmaFor(votedAtSomething));
        reputationEvents.delete(receivedVote);
        reputationEvents.delete(votedAtSomething);
        votable.remove(previous);
    }
// ReputationEvent receivedVote = new ReceivedVoteEvent(current.getType(), votable, eventContext, shouldCountKarma).reputationEvent();
// votableAuthor.increaseKarma(karmaCalculator.karmaFor(receivedVote));
// ReputationEvent votedAtSomething = new VotedAtSomethingEvent(current, eventContext).reputationEvent();
// voter.increaseKarma(karmaCalculator.karmaFor(votedAtSomething));
// reputationEvents.save(receivedVote);
// reputationEvents.save(votedAtSomething);
// if (votable.getVoteCount() <= -5) {
// votable.getQuestion().remove();
// retrieveDownvote.retrieveKarma(votable.getVotes());
// }
}
Also used : Vote(org.mamute.model.Vote) User(org.mamute.model.User) ReceivedVoteEvent(org.mamute.reputation.rules.ReceivedVoteEvent) VotedAtSomethingEvent(org.mamute.reputation.rules.VotedAtSomethingEvent) ReputationEventContext(org.mamute.model.ReputationEventContext) ReputationEvent(org.mamute.model.ReputationEvent)

Example 14 with Vote

use of org.mamute.model.Vote in project mamute by caelum.

the class VoteController method tryToVoteVotable.

@SuppressWarnings("rawtypes")
private void tryToVoteVotable(Long id, VoteType voteType, Class votableType) {
    try {
        Votable votable = votes.loadVotable(votableType, id);
        Vote current = new Vote(currentUser.getCurrent(), voteType);
        votingMachine.register(votable, current, votableType);
        votes.save(current);
        result.use(Results.json()).withoutRoot().from(votable.getVoteCount()).serialize();
    } catch (IllegalArgumentException e) {
        result.use(Results.http()).sendError(409);
        return;
    }
}
Also used : Vote(org.mamute.model.Vote) Votable(org.mamute.model.interfaces.Votable)

Example 15 with Vote

use of org.mamute.model.Vote in project mamute by caelum.

the class VoteController method tryToRemoveVoteVotable.

@SuppressWarnings("rawtypes")
private void tryToRemoveVoteVotable(Long id, VoteType voteType, Class votableType) {
    try {
        Votable votable = votes.loadVotable(votableType, id);
        Vote current = new Vote(currentUser.getCurrent(), voteType);
        votingMachine.unRegister(votable, current, votableType);
        // votes.save(current);
        result.use(Results.json()).withoutRoot().from(votable.getVoteCount()).serialize();
    } catch (IllegalArgumentException e) {
        result.use(Results.http()).sendError(409);
        return;
    }
}
Also used : Vote(org.mamute.model.Vote) Votable(org.mamute.model.interfaces.Votable)

Aggregations

Vote (org.mamute.model.Vote)21 Test (org.junit.Test)13 MassiveVote (org.mamute.model.vote.MassiveVote)10 VotedAtSomethingEvent (org.mamute.reputation.rules.VotedAtSomethingEvent)4 SuspectMassiveVote (org.mamute.dto.SuspectMassiveVote)3 Question (org.mamute.model.Question)3 Answer (org.mamute.model.Answer)2 ReputationEvent (org.mamute.model.ReputationEvent)2 ReputationEventContext (org.mamute.model.ReputationEventContext)2 User (org.mamute.model.User)2 Votable (org.mamute.model.interfaces.Votable)2 ReceivedVoteEvent (org.mamute.reputation.rules.ReceivedVoteEvent)2 ArrayList (java.util.ArrayList)1 Query (org.hibernate.Query)1 DateTime (org.joda.time.DateTime)1 AnswerAndVotes (org.mamute.model.AnswerAndVotes)1 Comment (org.mamute.model.Comment)1 CommentsAndVotes (org.mamute.model.CommentsAndVotes)1