Search in sources :

Example 16 with Vote

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

the class VoteDAO method previousVoteFor.

public Vote previousVoteFor(Serializable id, User currentUser, Class type) {
    String hql = "select v from " + type.getSimpleName() + " q join q.votes as v where v.author = :author and q.id = :votable";
    Query query = session.createQuery(hql);
    return (Vote) query.setParameter("author", currentUser).setParameter("votable", id).setMaxResults(1).uniqueResult();
}
Also used : Vote(org.mamute.model.Vote) SuspectMassiveVote(org.mamute.dto.SuspectMassiveVote) Query(org.hibernate.Query)

Example 17 with Vote

use of org.mamute.model.Vote 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)

Example 18 with Vote

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

the class VoteDAOTest method upvote.

private Vote upvote(Votable votable, User user) {
    Vote vote = new Vote(user, VoteType.UP);
    session.save(votable);
    session.save(vote);
    votingMachine.register(votable, vote, Comment.class);
    return vote;
}
Also used : Vote(org.mamute.model.Vote) MassiveVote(org.mamute.model.vote.MassiveVote) SuspectMassiveVote(org.mamute.dto.SuspectMassiveVote)

Example 19 with Vote

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

the class QuestionTest method should_remove_vote_values_and_update_vote_count.

@Test
public void should_remove_vote_values_and_update_vote_count() {
    Question myQuestion = question.build();
    assertEquals(0l, myQuestion.getVoteCount());
    Vote firstVote = new Vote(null, VoteType.UP);
    myQuestion.substitute(null, firstVote);
    assertEquals(1l, myQuestion.getVoteCount());
    myQuestion.substitute(firstVote, new Vote(null, VoteType.DOWN));
    assertEquals(-1l, myQuestion.getVoteCount());
    myQuestion.substitute(null, new Vote(null, VoteType.DOWN));
    assertEquals(-2l, myQuestion.getVoteCount());
}
Also used : Vote(org.mamute.model.Vote) Question(org.mamute.model.Question) Test(org.junit.Test)

Example 20 with Vote

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

the class VotingMachineTest method should_decrese_karma_of_downvoter.

@Test
public void should_decrese_karma_of_downvoter() throws Exception {
    Vote newVote = new Vote(voter, VoteType.DOWN);
    votingMachine.register(votable, newVote, Question.class);
    assertEquals(-2l, voter.getKarma());
}
Also used : Vote(org.mamute.model.Vote) MassiveVote(org.mamute.model.vote.MassiveVote) Test(org.junit.Test)

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