Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add user objects in TweetList data #336

Merged
merged 2 commits into from
Dec 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,5 @@

<url>https://github.com/Redouane59/twittered</url>

<version>2.13</version>
<version>2.14-SNAPSHOT</version>
</project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.redouane59.twitter.dto.tweet;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.github.redouane59.twitter.dto.tweet.TweetV2.Includes;
import java.util.List;
import lombok.AllArgsConstructor;
Expand All @@ -14,6 +15,7 @@
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonDeserialize(using = TweetListDeserializer.class)
public class TweetList {

private List<TweetV2.TweetData> data;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.github.redouane59.twitter.dto.tweet;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import io.github.redouane59.twitter.TwitterClient;
import io.github.redouane59.twitter.dto.tweet.TweetList.TweetMeta;
import io.github.redouane59.twitter.dto.tweet.TweetV2.Includes;
import io.github.redouane59.twitter.dto.tweet.TweetV2.TweetData;
import io.github.redouane59.twitter.dto.user.UserV2.UserData;
import java.io.IOException;
import java.util.List;
import java.util.Optional;

public class TweetListDeserializer extends StdDeserializer<TweetList> {

protected TweetListDeserializer() {
super(TweetListDeserializer.class);
}

@Override
public TweetList deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException {
TweetList result = TweetList.builder().build();
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
if (node.has("meta")) {
result.setMeta(TwitterClient.OBJECT_MAPPER.readValue(node.get("meta").toString(), TweetMeta.class));
}
if (node.has("data")) {
List<TweetData>
list =
TwitterClient.OBJECT_MAPPER.readValue(node.get("data").toString(),
TwitterClient.OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, TweetData.class));
if (node.has("includes")) {
result.setIncludes(TwitterClient.OBJECT_MAPPER.readValue(node.get("includes").toString(), Includes.class));
// in order to enrich the TweetData object (from data field) adding the User object (instead of just the author_id)
for (TweetData tweetData : list) {
Optional<UserData> matchingUser = result.getIncludes().getUsers().stream().filter(p -> p.getId().equals(tweetData.getAuthorId())).
findFirst();
matchingUser.ifPresent(tweetData::setUser);
}
}
result.setData(list);
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.github.redouane59.twitter.dto.tweet.entities.UserMentionEntity;
import io.github.redouane59.twitter.dto.user.User;
import io.github.redouane59.twitter.dto.user.UserV2;
import io.github.redouane59.twitter.dto.user.UserV2.UserData;
import io.github.redouane59.twitter.helpers.ConverterHelper;
import java.time.LocalDateTime;
import java.util.Collections;
Expand Down Expand Up @@ -264,6 +265,8 @@ public static class TweetData implements Tweet {
private Geo geo;
private Attachments attachments;
private String source;
@JsonIgnore
private UserData user;

@Override
@JsonIgnore
Expand Down Expand Up @@ -334,12 +337,6 @@ public List<StreamRule> getMatchingRules() {
return Collections.emptyList();
}

@Override
@JsonIgnore
public User getUser() {
throw new UnsupportedOperationException();
}

@Override
public String getAuthorId() {
return authorId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,15 @@ public void testGetFollowersWithLimitException() {

@Test
public void testGetTweetsByIds() {
List<String> tweetIds = Arrays.asList("1294174710624849921,1294380029430960128,1294375095746666496");
List<String> tweetIds = Arrays.asList("1224041905333379073,1460323737035677698,1464697044006846474");
TweetList tweets = twitterClient.getTweets(tweetIds);
assertTrue(tweets.getData().size() > 0);
assertTrue(tweets.getData().get(0).getText().length() > 0);
assertTrue(tweets.getData().get(1).getText().length() > 0);
assertTrue(tweets.getData().get(2).getText().length() > 0);
assertNotNull(tweets.getData().get(0).getUser());
assertNotNull(tweets.getData().get(1).getUser());
assertNotNull(tweets.getData().get(2).getUser());
}

@Test
Expand Down Expand Up @@ -311,8 +314,8 @@ public void testGetTweetType() {
public void testGetTweetIdWithTwoTypes() {
assertEquals("1264255917043920904",
twitterClient.getTweet("1264256827690270722").getInReplyToStatusId(TweetType.RETWEETED));
assertEquals("1263783602485157889",
twitterClient.getTweet("1264255917043920904").getInReplyToStatusId(TweetType.QUOTED));
assertEquals("1455953449422516226",
twitterClient.getTweet("1456903038136954883").getInReplyToStatusId(TweetType.QUOTED));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.github.redouane59.twitter.TwitterClient;
import io.github.redouane59.twitter.dto.tweet.ReplySettings;
import io.github.redouane59.twitter.dto.tweet.Tweet;
import io.github.redouane59.twitter.dto.tweet.TweetList;
import io.github.redouane59.twitter.dto.tweet.TweetV2.MediaEntityV2;
import io.github.redouane59.twitter.dto.user.User;
import io.github.redouane59.twitter.helpers.ConverterHelper;
import java.io.File;
import java.io.IOException;
import org.junit.jupiter.api.Test;

public class TweetListDeserializerTest {

private File tweetListFile = new File(getClass().getClassLoader().getResource("tests/tweetlist_example_v2.json").getFile());
private File tweetListFile = new File(getClass().getClassLoader().getResource("tests/tweet_list_v2_example.json").getFile());
private TweetList tweetList = TwitterClient.OBJECT_MAPPER.readValue(tweetListFile, TweetList.class);

public TweetListDeserializerTest() throws IOException {
Expand All @@ -21,24 +26,67 @@ public TweetListDeserializerTest() throws IOException {
@Test
public void testData() {
assertNotNull(tweetList.getData());
assertEquals(10, tweetList.getData().size());
assertTrue(tweetList.getData().get(0).getText().contains("Bonjour"));
assertEquals("1412422509601902608", tweetList.getData().get(0).getId());
assertEquals("1150439027486707713", tweetList.getData().get(0).getAuthorId());
assertEquals(3, tweetList.getData().size());
}

@Test
public void testMeta() {
assertNotNull(tweetList.getMeta());
assertEquals("b26v89c19zqg8o3fpdj7j5aekwz7gmhbijc3a5s0ezp19", tweetList.getMeta().getNextToken());
assertEquals(10, tweetList.getMeta().getResultCount());
public void testData0() {
Tweet tweet = tweetList.getData().get(0);
assertEquals("1120050519182016513", tweet.getAuthorId());
assertEquals("1224041905333379073", tweet.getId());
assertEquals("Twitter Web App", tweet.getSource());
assertEquals("1224041905333379073", tweet.getConversationId());
assertEquals("en", tweet.getLang());
assertEquals(5, tweet.getRetweetCount());
assertEquals(6, tweet.getReplyCount());
assertEquals(26, tweet.getLikeCount());
assertEquals(1, tweet.getQuoteCount());
assertEquals(ReplySettings.EVERYONE, tweet.getReplySettings());
assertTrue(tweet.getText().contains("JAVA"));
assertEquals(ConverterHelper.getDateFromTwitterStringV2("2020-02-02T18:48:26.000Z"), tweet.getCreatedAt());
}

@Test
public void testIncludes() {
assertNotNull(tweetList.getIncludes());
assertEquals(28, tweetList.getIncludes().getUsers().size());
assertEquals(8, tweetList.getIncludes().getTweets().size());
assertNotNull(tweetList.getIncludes().getUsers());
assertEquals(3, tweetList.getIncludes().getMedia().size());
assertNotNull(tweetList.getIncludes().getMedia());
assertEquals(4, tweetList.getIncludes().getUsers().size());
}

@Test
public void testIncludes0() {
User user = tweetList.getIncludes().getUsers().get(0);
assertEquals("Redouane", user.getDisplayedName());
assertEquals("RedouaneBali", user.getName());
assertEquals("Algiers, Algeria", user.getLocation());
assertEquals(2018, user.getFollowersCount());
assertEquals(1251, user.getFollowingCount());
assertEquals(838, user.getTweetCount());
assertEquals("1120050519182016513", user.getId());
}

@Test
public void testMedia0() {
MediaEntityV2 media = tweetList.getIncludes().getMedia().get(0);
assertEquals("7_1460322142680072196", media.getKey());
assertEquals(900, media.getHeight());
assertEquals("video", media.getType());
assertEquals(11093, media.getDuration());
assertEquals(64061, media.getPublicMetrics().getViewCount());
assertEquals(1600, media.getWidth());
}

@Test
public void testUserIncludeInTweetObject() {
User user = tweetList.getData().get(0).getUser();
assertEquals("Redouane", user.getDisplayedName());
assertEquals("RedouaneBali", user.getName());
assertEquals("Algiers, Algeria", user.getLocation());
assertEquals(2018, user.getFollowersCount());
assertEquals(1251, user.getFollowingCount());
assertEquals(838, user.getTweetCount());
assertEquals("1120050519182016513", user.getId());
}

}
Loading