Search in sources :

Example 1 with Flaggable

use of org.mamute.model.interfaces.Flaggable in project mamute by caelum.

the class FlagController method addFlag.

@SimpleBrutauthRules({ EnvironmentKarmaRule.class })
@EnvironmentAccessLevel(PermissionRules.CREATE_FLAG)
@Post
public void addFlag(String flaggableType, Long flaggableId, FlagType flagType, String reason) {
    Class<?> clazz = urlMapping.getClassFor(flaggableType);
    if (flagType == null) {
        result.use(http()).sendError(400);
        return;
    }
    if (flags.alreadyFlagged(loggedUser.getCurrent(), flaggableId, clazz)) {
        // conflict
        result.use(http()).sendError(409);
        return;
    }
    Flaggable flaggable = flaggables.getById(flaggableId, clazz);
    flagTrigger.fire(flaggable);
    Flag flag = new Flag(flagType, loggedUser.getCurrent());
    if (flagType.equals(FlagType.OTHER)) {
        flag.setReason(reason);
    }
    flags.save(flag);
    flaggable.add(flag);
    result.nothing();
}
Also used : Flaggable(org.mamute.model.interfaces.Flaggable) Flag(org.mamute.model.Flag) Post(br.com.caelum.vraptor.Post) SimpleBrutauthRules(br.com.caelum.brutauth.auth.annotations.SimpleBrutauthRules)

Example 2 with Flaggable

use of org.mamute.model.interfaces.Flaggable in project mamute by caelum.

the class RemoveSolutionFlaggedByModeratorTest method should_not_handle_flag_of_normal_user.

@Test
public void should_not_handle_flag_of_normal_user() {
    RemoveSolutionFlaggedByModerator action = new RemoveSolutionFlaggedByModerator(loggedUser);
    Flaggable flaggable = new AnswerChild();
    assertFalse(action.shouldHandle(flaggable));
}
Also used : RemoveSolutionFlaggedByModerator(org.mamute.model.flag.RemoveSolutionFlaggedByModerator) Flaggable(org.mamute.model.interfaces.Flaggable) Test(org.junit.Test)

Example 3 with Flaggable

use of org.mamute.model.interfaces.Flaggable in project mamute by caelum.

the class FlaggableDAOTest method setup.

@Before
public void setup() {
    flaggedInvisible.remove();
    addFlags(commentWithOneFlag, 1, author);
    addFlags(commentWithTwo, 2, author);
    addFlags(flaggedInvisible, 2, author);
    session.save(author);
    session.save(anyone);
    session.save(commentWithTwo);
    session.save(flaggedInvisible);
    session.save(commentWithOneFlag);
    session.save(java);
    session.save(question);
    session.flush();
    flaggables = new FlaggableDAO(session, meta);
    questions = new QuestionDAO(session, new InvisibleForUsersRule(new LoggedUser(anyone, null)));
    List<Class<? extends Flaggable>> moderatableTypes = new ArrayList<>();
    moderatableTypes.add(Question.class);
    moderatableTypes.add(Answer.class);
    moderatableTypes.add(Comment.class);
    moderatableTypes.add(News.class);
    when(meta.getFlaggableTypes()).thenReturn(moderatableTypes);
}
Also used : FlaggableDAO(org.mamute.dao.FlaggableDAO) Flaggable(org.mamute.model.interfaces.Flaggable) ArrayList(java.util.ArrayList) LoggedUser(org.mamute.model.LoggedUser) Before(org.junit.Before)

Example 4 with Flaggable

use of org.mamute.model.interfaces.Flaggable in project mamute by caelum.

the class FlagTriggerTest method should_trigger_correct_actions.

@Test
public void should_trigger_correct_actions() {
    FlagAction neverExecuteAction = mock(FlagAction.class);
    FlagAction alwaysExecuteAction = mock(FlagAction.class);
    Flaggable flaggable = mock(Flaggable.class);
    when(neverExecuteAction.shouldHandle(Mockito.any(Flaggable.class))).thenReturn(false);
    when(alwaysExecuteAction.shouldHandle(Mockito.any(Flaggable.class))).thenReturn(true);
    FlagTrigger flagTrigger = new FlagTrigger(Arrays.asList(neverExecuteAction, alwaysExecuteAction));
    flagTrigger.fire(flaggable);
    verify(neverExecuteAction, never()).fire(flaggable);
    verify(alwaysExecuteAction, times(1)).fire(flaggable);
}
Also used : Flaggable(org.mamute.model.interfaces.Flaggable) FlagTrigger(org.mamute.model.flag.FlagTrigger) FlagAction(org.mamute.model.flag.FlagAction) Test(org.junit.Test)

Aggregations

Flaggable (org.mamute.model.interfaces.Flaggable)4 Test (org.junit.Test)2 SimpleBrutauthRules (br.com.caelum.brutauth.auth.annotations.SimpleBrutauthRules)1 Post (br.com.caelum.vraptor.Post)1 ArrayList (java.util.ArrayList)1 Before (org.junit.Before)1 FlaggableDAO (org.mamute.dao.FlaggableDAO)1 Flag (org.mamute.model.Flag)1 LoggedUser (org.mamute.model.LoggedUser)1 FlagAction (org.mamute.model.flag.FlagAction)1 FlagTrigger (org.mamute.model.flag.FlagTrigger)1 RemoveSolutionFlaggedByModerator (org.mamute.model.flag.RemoveSolutionFlaggedByModerator)1