package io.github.t73liu.news;

import com.google.common.primitives.Longs;
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
import it.unimi.dsi.fastutil.objects.ObjectArraySet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import twitter4j.*;
import twitter4j.api.UsersResources;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;

import javax.annotation.PostConstruct;
import java.util.Set;

@Component
@ConfigurationProperties("news.twitter")
public class TwitterService {
    private static final Logger LOGGER = LoggerFactory.getLogger(TwitterService.class);

    @Value("${news.twitter.consumerKey}")
    private String consumerKey;

    @Value("${news.twitter.consumerSecret}")
    private String consumerSecret;

    @Value("${news.twitter.accessToken}")
    private String accessToken;

    @Value("${news.twitter.accessTokenSecret}")
    private String accessTokenSecret;

    private Set<String> users = new ObjectArraySet<>();

    public Set<String> getUsers() {
        return users;
    }

    public Object getLatest() {
        return null;
    }

    @PostConstruct
    private void initializeStream() {
        Configuration configuration = new ConfigurationBuilder()
                .setDebugEnabled(true)
                .setOAuthConsumerKey(consumerKey)
                .setOAuthConsumerSecret(consumerSecret)
                .setOAuthAccessToken(accessToken)
                .setOAuthAccessTokenSecret(accessTokenSecret)
                .build();
        UsersResources usersResources = new TwitterFactory(configuration).getInstance().users();
        Set<Long> userIds = new LongOpenHashSet();
        for (String userName : users) {
            try {
                userIds.add(usersResources.showUser(userName).getId());
            } catch (TwitterException e) {
                LOGGER.warn("Unable to lookup user name: {}", userName);
            }
        }
        TwitterStream twitterStream = new TwitterStreamFactory(configuration).getInstance();
        StatusListener listener = new StatusListener() {
            @Override
            public void onStatus(Status status) {
                if (userIds.contains(status.getUser().getId())) {
                    LOGGER.info("User: {}, Text: {}", status.getUser().getScreenName(), status.getText());
                }
            }

            @Override
            public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
                LOGGER.info("Got a status deletion notice id: {}", statusDeletionNotice.getStatusId());
            }

            @Override
            public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
                LOGGER.info("Got track limitation notice: {}", numberOfLimitedStatuses);
            }

            @Override
            public void onScrubGeo(long userId, long upToStatusId) {
                LOGGER.info("Got scrub_geo event userId: {}, upToStatusId: {}", userId, upToStatusId);
            }

            @Override
            public void onStallWarning(StallWarning warning) {
                LOGGER.warn("Got stall warning: {}", warning);
            }

            @Override
            public void onException(Exception ex) {
                LOGGER.error("Twitter Exception: {}", ex.getMessage(), ex);
            }
        };
        twitterStream.addListener(listener);
        twitterStream.filter(new FilterQuery(Longs.toArray(userIds)).language("en"));
    }
}

@@ -434,16 +434,54 @@
 er4j.*;%0A
+import twitter4j.api.TweetsResources;%0A
 import t
@@ -501,32 +501,32 @@
 UsersResources;%0A
-
 import twitter4j
@@ -1741,37 +1741,23 @@
     
-UsersResources usersResources
+Twitter twitter
  = n
@@ -1806,13 +1806,122 @@
 ce()
-.user
+;%0A        UsersResources usersResources = twitter.users();%0A        TweetsResources tweetsResources = twitter.tweet
 s();
@@ -2526,24 +2526,24 @@
 getId())) %7B%0A
-
             
@@ -2633,24 +2633,993 @@
 getText());%0A
+                    long inReplyToStatusId = status.getInReplyToStatusId();%0A                    if (inReplyToStatusId %3E 0) %7B%0A                        try %7B%0A                            Status repliedToTweet = tweetsResources.showStatus(inReplyToStatusId);%0A                            LOGGER.info(%22User: %7B%7D, Text: %7B%7D%22, repliedToTweet.getUser().getScreenName(), repliedToTweet.getText());%0A                        %7D catch (TwitterException e) %7B%0A                            LOGGER.warn(%22Unable to lookup repliedToTweet status: %7B%7D%22, inReplyToStatusId);%0A                        %7D%0A                    %7D%0A                    // TODO verify if required%0A                    if (status.isRetweet()) %7B%0A                        Status retweetedStatus = status.getRetweetedStatus();%0A                        LOGGER.info(%22Retweeted By: %7B%7D - User: %7B%7D, Text: %7B%7D%22, status.getUser().getScreenName(), retweetedStatus.getUser().getScreenName(), retweetedStatus.getText());%0A                    %7D%0A
             

Include retweet + reply tweet