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));
}
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);
}
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());
// }
}
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;
}
}
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;
}
}
Aggregations