Search in sources :

Example 1 with User

use of com.jeanchampemont.wtfdyum.dto.User in project WTFDYUM by jchampemont.

the class AbstractFeatureStrategyTest method unfollowers.

protected List<User> unfollowers(final Principal principal, final Set<Long> followersIds, final BiConsumer<OngoingStubbing<Set<Long>>, Set<Long>> l) throws WTFDYUMException {
    final Set<Long> unfollowers = new HashSet<>(Arrays.asList(10L, 11L));
    l.accept(when(followersService.getUnfollowers(principal.getUserId(), followersIds)), unfollowers);
    // unfollowers 10 and 11 details :
    final User user10 = new User();
    user10.setId(10L);
    user10.setScreenName("user10");
    final User user11 = new User();
    user11.setId(11L);
    user11.setScreenName("user11");
    when(twitterService.getUsers(principal, 10L, 11L)).thenReturn(Arrays.asList(user10, user11));
    return Arrays.asList(user10, user11);
}
Also used : User(com.jeanchampemont.wtfdyum.dto.User)

Example 2 with User

use of com.jeanchampemont.wtfdyum.dto.User in project WTFDYUM by jchampemont.

the class TwitterServiceImpl method getUsers.

@Override
public List<User> getUsers(final Principal principal, final long... ids) throws WTFDYUMException {
    final List<User> result = new ArrayList<>();
    if (ids.length == 0) {
        return result;
    }
    try {
        final List<twitter4j.User> users = new ArrayList<>();
        for (int i = 0; i <= (ids.length - 1) / 100; i++) {
            final ResponseList<twitter4j.User> lookupUsers = twitter(principal).users().lookupUsers(Arrays.copyOfRange(ids, i * 100, Math.min((i + 1) * 100, ids.length)));
            users.addAll(lookupUsers);
        }
        for (final twitter4j.User u : users) {
            result.add(mapper.map(u, User.class));
        }
    } catch (final TwitterException e) {
        if (e.getErrorCode() == 17) {
            log.debug("Error while getUsers for ids: " + Arrays.toString(ids) + ". Seems like those users are not on twitter anymore.");
        } else {
            log.debug("Error while getUsers for ids: " + Arrays.toString(ids), e);
            throw new WTFDYUMException(e, WTFDYUMExceptionType.TWITTER_ERROR);
        }
    }
    return result;
}
Also used : User(com.jeanchampemont.wtfdyum.dto.User) WTFDYUMException(com.jeanchampemont.wtfdyum.utils.WTFDYUMException) twitter4j(twitter4j)

Example 3 with User

use of com.jeanchampemont.wtfdyum.dto.User in project WTFDYUM by jchampemont.

the class TweetUnfollowFeatureStrategy method cron.

@Override
public Set<Event> cron(final Long userId) throws WTFDYUMException {
    final Set<Event> result = new HashSet<>();
    final Principal principal = principalService.get(userId);
    final Set<Long> followers = twitterService.getFollowers(userId, Optional.ofNullable(principal));
    final Set<Long> unfollowersId = followersService.getUnfollowers(userId, followers);
    final List<User> unfollowers = twitterService.getUsers(principal, Longs.toArray(unfollowersId));
    for (final User unfollower : unfollowers) {
        result.add(new Event(EventType.UNFOLLOW, unfollower.getScreenName()));
        twitterService.tweet(principal, String.format(unfollowTweetText, unfollower.getScreenName()));
    }
    return result;
}
Also used : User(com.jeanchampemont.wtfdyum.dto.User) Event(com.jeanchampemont.wtfdyum.dto.Event) Principal(com.jeanchampemont.wtfdyum.dto.Principal) HashSet(java.util.HashSet)

Example 4 with User

use of com.jeanchampemont.wtfdyum.dto.User in project WTFDYUM by jchampemont.

the class NotifyUnfollowFeatureStrategyTest method cronTest.

@Test
public void cronTest() throws Exception {
    final Principal principal = principal(1L);
    final Set<Long> followers = followers(principal, (s, f) -> s.thenReturn(f));
    final List<User> unfollowers = unfollowers(principal, followers, (s, u) -> s.thenReturn(u));
    final Set<Event> events = sut.cron(1L);
    verifyUnfollowDM(principal, unfollowers.get(0));
    verifyUnfollowDM(principal, unfollowers.get(1));
    assertThat(events.contains(new Event(EventType.UNFOLLOW, unfollowers.get(0).getScreenName())));
    assertThat(events.contains(new Event(EventType.UNFOLLOW, unfollowers.get(1).getScreenName())));
}
Also used : User(com.jeanchampemont.wtfdyum.dto.User) Event(com.jeanchampemont.wtfdyum.dto.Event) Principal(com.jeanchampemont.wtfdyum.dto.Principal) Test(org.junit.Test)

Example 5 with User

use of com.jeanchampemont.wtfdyum.dto.User in project WTFDYUM by jchampemont.

the class TweetUnfollowFeatureStrategyTest method cronTest.

@Test
public void cronTest() throws Exception {
    final Principal principal = principal(1L);
    final Set<Long> followers = followers(principal, (s, f) -> s.thenReturn(f));
    final List<User> unfollowers = unfollowers(principal, followers, (s, u) -> s.thenReturn(u));
    final Set<Event> events = sut.cron(1L);
    verifyUnfollowTweet(principal, unfollowers.get(0));
    verifyUnfollowTweet(principal, unfollowers.get(1));
    assertThat(events.contains(new Event(EventType.UNFOLLOW, unfollowers.get(0).getScreenName())));
    assertThat(events.contains(new Event(EventType.UNFOLLOW, unfollowers.get(1).getScreenName())));
}
Also used : User(com.jeanchampemont.wtfdyum.dto.User) Event(com.jeanchampemont.wtfdyum.dto.Event) Principal(com.jeanchampemont.wtfdyum.dto.Principal) Test(org.junit.Test)

Aggregations

User (com.jeanchampemont.wtfdyum.dto.User)8 Event (com.jeanchampemont.wtfdyum.dto.Event)5 Principal (com.jeanchampemont.wtfdyum.dto.Principal)5 Test (org.junit.Test)3 WTFDYUMException (com.jeanchampemont.wtfdyum.utils.WTFDYUMException)2 HashSet (java.util.HashSet)2 twitter4j (twitter4j)2