Search in sources :

Example 1 with ReputationEvent

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

the class ReputationEventDAOTest method should_return_tag_asker_summary.

@Test
public void should_return_tag_asker_summary() {
    User question1Author = user("solutionAuthor", "solution@x.com");
    User question3Author = user("answerAuthor", "other@x.com");
    session.save(question3Author);
    session.save(question1Author);
    Tag tag = tag("teste");
    session.save(tag);
    Question question1 = question(question1Author, tag);
    Question question2 = question(question1Author, tag);
    Question question3 = question(question3Author, tag);
    session.save(question1);
    session.save(question2);
    session.save(question3);
    for (int i = 0; i < 30; i++) {
        session.save(new ReputationEvent(EventType.QUESTION_UPVOTE, question1, question1Author));
    }
    session.save(new ReputationEvent(EventType.QUESTION_UPVOTE, question2, question1Author));
    long karmaRewardForQuestion1 = EventType.QUESTION_UPVOTE.reward().longValue() * 31;
    for (int i = 0; i < 10; i++) {
        session.save(new ReputationEvent(EventType.QUESTION_UPVOTE, question3, question3Author));
    }
    long karmaRewardForQuestion2 = EventType.QUESTION_UPVOTE.reward().longValue() * 10;
    List<UserSummaryForTag> summaryForTag = reputationEvents.getTopAskersSummaryAllTime(tag);
    assertEquals(2, summaryForTag.size());
    assertEquals(karmaRewardForQuestion1, summaryForTag.get(0).getKarmaReward().longValue());
    assertEquals(question1Author, summaryForTag.get(0).getUser());
    assertEquals(2l, summaryForTag.get(0).getCount().longValue());
    assertEquals(karmaRewardForQuestion2, summaryForTag.get(1).getKarmaReward().longValue());
    assertEquals(question3Author, summaryForTag.get(1).getUser());
    assertEquals(1l, summaryForTag.get(1).getCount().longValue());
}
Also used : UserSummaryForTag(org.mamute.dto.UserSummaryForTag) User(org.mamute.model.User) LoggedUser(org.mamute.model.LoggedUser) Question(org.mamute.model.Question) Tag(org.mamute.model.Tag) UserSummaryForTag(org.mamute.dto.UserSummaryForTag) ReputationEvent(org.mamute.model.ReputationEvent) Test(org.junit.Test)

Example 2 with ReputationEvent

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

the class ReputationEventDAOTest method should_ignore_events_of_deleted_questions.

@Test
public void should_ignore_events_of_deleted_questions() {
    session.save(new ReputationEvent(EventType.QUESTION_UPVOTE, questionInvolved1, author));
    session.delete(questionInvolved1);
    KarmaByContextHistory karmaByQuestion = reputationEvents.karmaWonByQuestion(author, new DateTime(0));
    assertNotNull(karmaByQuestion);
    assertNotNull(karmaByQuestion.getHistory());
    assertTrue(karmaByQuestion.getHistory().isEmpty());
}
Also used : KarmaByContextHistory(org.mamute.dto.KarmaByContextHistory) ReputationEvent(org.mamute.model.ReputationEvent) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 3 with ReputationEvent

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

the class ReputationEventDAOTest method should_group_reputation_karma_reward_by_post.

@Test
public void should_group_reputation_karma_reward_by_post() throws Exception {
    ReputationEvent event1Question1 = event30MinAgo(EventType.ANSWER_DOWNVOTE);
    ReputationEvent event2Question1 = event30MinAgo(EventType.QUESTION_DOWNVOTE);
    Integer question1Karma = event1Question1.getKarmaReward() + event2Question1.getKarmaReward();
    ReputationEvent event1Question2 = new ReputationEvent(EventType.QUESTION_UPVOTE, questionInvolved2, author);
    ReputationEvent event2Question2 = new ReputationEvent(EventType.QUESTION_UPVOTE, questionInvolved2, author);
    Integer question2Karma = event1Question2.getKarmaReward() + event2Question2.getKarmaReward();
    ReputationEvent eventOut = TimeMachine.goTo(new DateTime().minusHours(2)).andExecute(new Block<ReputationEvent>() {

        @Override
        public ReputationEvent run() {
            return new ReputationEvent(EventType.QUESTION_UPVOTE, questionInvolved3, author);
        }
    });
    session.save(eventOut);
    session.save(event1Question1);
    session.save(event1Question2);
    session.save(event2Question1);
    session.save(event2Question2);
    KarmaByContextHistory karmaByQuestion = reputationEvents.karmaWonByQuestion(author, new DateTime().minusHours(1));
    List<KarmaAndContext> history = karmaByQuestion.getHistory();
    assertEquals(2, history.size());
    KarmaAndContext ck1 = Iterables.find(history, contextOf(questionInvolved1), null);
    assertNotNull(ck1);
    KarmaAndContext ck2 = Iterables.find(history, contextOf(questionInvolved2), null);
    assertNotNull(ck2);
    assertEquals(question1Karma.longValue(), ck1.getKarma().longValue());
    assertEquals(question2Karma.longValue(), ck2.getKarma().longValue());
}
Also used : KarmaByContextHistory(org.mamute.dto.KarmaByContextHistory) KarmaAndContext(org.mamute.dto.KarmaAndContext) ReputationEvent(org.mamute.model.ReputationEvent) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 4 with ReputationEvent

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

the class ReputationEventDAOTest method should_return_tag_answerer_summary.

@Test
public void should_return_tag_answerer_summary() {
    User solutionAuthor = user("solutionAuthor", "solution@x.com");
    User otherAnswerAuthor = user("answerAuthor", "other@x.com");
    Tag tag = tag("teste");
    Question question1 = question(author, tag);
    Answer answer1 = answer("BLABLABLABLABLABLABLABLABLABLABLABLABLABLABLABLABLABLA", question1, solutionAuthor);
    Answer answer2 = answer("BLABLABLABLABLABLABLABLABLABLABLABLABLABLABLABLABLABLA", question1, otherAnswerAuthor);
    Question question2 = question(author, tag);
    Answer answer3 = answer("BLABLABLABLABLABLABLABLABLABLABLABLABLABLABLABLABLABLA", question2, solutionAuthor);
    ReputationEvent answer3Upvote = new ReputationEvent(EventType.ANSWER_UPVOTE, question2, solutionAuthor);
    for (int i = 0; i < 30; i++) {
        ReputationEvent questionUpvote = new ReputationEvent(EventType.QUESTION_UPVOTE, question1, author);
        session.save(questionUpvote);
    }
    ReputationEvent answerIsSolution = new ReputationEvent(EventType.SOLVED_QUESTION, question1, solutionAuthor);
    ReputationEvent solutionUpvote = new ReputationEvent(EventType.ANSWER_UPVOTE, question1, solutionAuthor);
    long karmaRewardForSolution = SOLVED_QUESTION.reward().longValue() + ANSWER_UPVOTE.reward().longValue() * 2;
    ReputationEvent answer2Upvote = new ReputationEvent(EventType.ANSWER_UPVOTE, question1, otherAnswerAuthor);
    ReputationEvent answer2Upvote2 = new ReputationEvent(EventType.ANSWER_UPVOTE, question1, otherAnswerAuthor);
    long karmaRewardForOtherAnswer = ANSWER_UPVOTE.reward().longValue() * 2;
    session.save(tag);
    session.save(question1);
    session.save(question2);
    session.save(otherAnswerAuthor);
    session.save(solutionAuthor);
    session.save(answer1);
    session.save(answer2);
    session.save(answer3);
    session.save(answerIsSolution);
    session.save(solutionUpvote);
    session.save(answer2Upvote);
    session.save(answer2Upvote2);
    session.save(answer3Upvote);
    List<UserSummaryForTag> summaryForTag = reputationEvents.getTopAnswerersSummaryAllTime(tag);
    assertEquals(2, summaryForTag.size());
    assertEquals(karmaRewardForSolution, summaryForTag.get(0).getKarmaReward().longValue());
    assertEquals(solutionAuthor, summaryForTag.get(0).getUser());
    assertEquals(2l, summaryForTag.get(0).getCount().longValue());
    assertEquals(karmaRewardForOtherAnswer, summaryForTag.get(1).getKarmaReward().longValue());
    assertEquals(otherAnswerAuthor, summaryForTag.get(1).getUser());
    assertEquals(1l, summaryForTag.get(1).getCount().longValue());
}
Also used : Answer(org.mamute.model.Answer) UserSummaryForTag(org.mamute.dto.UserSummaryForTag) User(org.mamute.model.User) LoggedUser(org.mamute.model.LoggedUser) Question(org.mamute.model.Question) Tag(org.mamute.model.Tag) UserSummaryForTag(org.mamute.dto.UserSummaryForTag) ReputationEvent(org.mamute.model.ReputationEvent) Test(org.junit.Test)

Example 5 with ReputationEvent

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

Aggregations

ReputationEvent (org.mamute.model.ReputationEvent)10 Test (org.junit.Test)6 User (org.mamute.model.User)5 DateTime (org.joda.time.DateTime)3 KarmaByContextHistory (org.mamute.dto.KarmaByContextHistory)3 LoggedUser (org.mamute.model.LoggedUser)3 KarmaAndContext (org.mamute.dto.KarmaAndContext)2 UserSummaryForTag (org.mamute.dto.UserSummaryForTag)2 Question (org.mamute.model.Question)2 ReputationEventContext (org.mamute.model.ReputationEventContext)2 Tag (org.mamute.model.Tag)2 Vote (org.mamute.model.Vote)2 ReceivedVoteEvent (org.mamute.reputation.rules.ReceivedVoteEvent)2 VotedAtSomethingEvent (org.mamute.reputation.rules.VotedAtSomethingEvent)2 SimpleBrutauthRules (br.com.caelum.brutauth.auth.annotations.SimpleBrutauthRules)1 Post (br.com.caelum.vraptor.Post)1 List (java.util.List)1 Answer (org.mamute.model.Answer)1 AnswerInformation (org.mamute.model.AnswerInformation)1 EventType (org.mamute.model.EventType)1