Search in sources :

Example 1 with VotedAtSomethingEvent

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

the class VotedAtSomethingEventTest method should_calculate_karma_for_downvote.

@Test
public void should_calculate_karma_for_downvote() {
    VotedAtSomethingEvent votedAtSomething = new VotedAtSomethingEvent(new Vote(null, VoteType.DOWN), null);
    assertEquals(KarmaCalculator.DOWNVOTED_QUESTION_OR_ANSWER, votedAtSomething.reward());
}
Also used : Vote(org.mamute.model.Vote) VotedAtSomethingEvent(org.mamute.reputation.rules.VotedAtSomethingEvent) Test(org.junit.Test)

Example 2 with VotedAtSomethingEvent

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

the class VotedAtSomethingEventTest method should_calculate_karma_for_upvote.

@Test
public void should_calculate_karma_for_upvote() {
    VotedAtSomethingEvent votedAtSomething = new VotedAtSomethingEvent(new Vote(null, VoteType.UP), null);
    assertEquals(0, votedAtSomething.reward());
}
Also used : Vote(org.mamute.model.Vote) VotedAtSomethingEvent(org.mamute.reputation.rules.VotedAtSomethingEvent) Test(org.junit.Test)

Example 3 with VotedAtSomethingEvent

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

use of org.mamute.reputation.rules.VotedAtSomethingEvent 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

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