Search in sources :

Example 1 with ReceivedVoteEvent

use of org.mamute.reputation.rules.ReceivedVoteEvent in project mamute by caelum.

the class KarmaCalculatorTest method should_calculate_karma_for_votes.

@Test
public void should_calculate_karma_for_votes() {
    assertEquals(MY_QUESTION_VOTED_UP, karmaCalculator.karmaFor(new ReceivedVoteEvent(VoteType.UP, question, null, true).reputationEvent()));
    assertEquals(MY_QUESTION_VOTED_DOWN, karmaCalculator.karmaFor(new ReceivedVoteEvent(VoteType.DOWN, answer, null, true).reputationEvent()));
    assertEquals(MY_ANSWER_VOTED_UP, karmaCalculator.karmaFor(new ReceivedVoteEvent(VoteType.UP, answer, null, true).reputationEvent()));
    assertEquals(MY_ANSWER_VOTED_DOWN, karmaCalculator.karmaFor(new ReceivedVoteEvent(VoteType.DOWN, question, null, true).reputationEvent()));
}
Also used : ReceivedVoteEvent(org.mamute.reputation.rules.ReceivedVoteEvent) Test(org.junit.Test)

Example 2 with ReceivedVoteEvent

use of org.mamute.reputation.rules.ReceivedVoteEvent 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 3 with ReceivedVoteEvent

use of org.mamute.reputation.rules.ReceivedVoteEvent in project mamute by caelum.

the class VotingMachine method register.

public void register(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 vote its own votable");
    }
    Vote previous = votes.previousVoteFor(votable.getId(), voter, votableType);
    boolean shouldCountKarma = voteChecker.shouldCountKarma(voter, votableAuthor, current);
    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.substitute(previous, current);
    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)

Aggregations

ReceivedVoteEvent (org.mamute.reputation.rules.ReceivedVoteEvent)3 ReputationEvent (org.mamute.model.ReputationEvent)2 ReputationEventContext (org.mamute.model.ReputationEventContext)2 User (org.mamute.model.User)2 Vote (org.mamute.model.Vote)2 VotedAtSomethingEvent (org.mamute.reputation.rules.VotedAtSomethingEvent)2 Test (org.junit.Test)1