Skip to content

Commit

Permalink
tweak timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Apr 10, 2017
1 parent 6cedb6a commit 745c380
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/main/java/com/maxdemarzi/likes/Likes.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,12 @@ public Response createLike(@PathParam("username") final String username,
results.put(USERNAME, user2.getProperty(USERNAME));
results.put(NAME, user2.getProperty(NAME));
results.put(LIKES, post.getDegree(RelationshipTypes.LIKES));
results.put(REPOSTS, post.getDegree() - 1 - post.getDegree(RelationshipTypes.LIKES));

results.put(REPOSTS, post.getDegree(Direction.INCOMING)
- 1 // for the Posted Relationship Type
- post.getDegree(RelationshipTypes.LIKES)
- post.getDegree(RelationshipTypes.REPLIED_TO));
results.put(LIKED, true);
results.put(REPOSTED, userRepostedPost(user, post));
tx.success();
}
return Response.ok().entity(objectMapper.writeValueAsString(results)).build();
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/maxdemarzi/posts/Posts.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ public Response createRepost(@PathParam("username") final String username,
- 1 // for the Posted Relationship Type
- post.getDegree(RelationshipTypes.LIKES)
- post.getDegree(RelationshipTypes.REPLIED_TO));
results.put(LIKED, userLikesPost(user, post));
results.put(REPOSTED, true);

}
tx.success();
}
Expand All @@ -234,6 +237,16 @@ public static Node getAuthor(Node post, Long time) {

public static boolean userRepostedPost(Node user, Node post) {
boolean alreadyReposted = false;

if (post.getDegree(Direction.INCOMING) < 1000) {
for (Relationship r1 : post.getRelationships(Direction.INCOMING)) {
if (r1.getStartNode().equals(user) && r1.getType().name().startsWith("REPOSTED_ON_")) {
alreadyReposted = true;
break;
}
}
}

LocalDateTime now = LocalDateTime.now(utc);
LocalDateTime dateTime = LocalDateTime.ofEpochSecond((Long)post.getProperty(TIME), 0, ZoneOffset.UTC).truncatedTo(ChronoUnit.DAYS);

Expand Down
17 changes: 14 additions & 3 deletions src/main/java/com/maxdemarzi/timeline/Timeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import java.util.*;

import static com.maxdemarzi.Properties.*;
import static com.maxdemarzi.likes.Likes.userLikesPost;
import static com.maxdemarzi.posts.Posts.getAuthor;
import static com.maxdemarzi.posts.Posts.userRepostedPost;
import static java.util.Collections.reverseOrder;

@Path("/users/{username}/timeline")
Expand Down Expand Up @@ -74,7 +76,12 @@ public Response getTimeline(@PathParam("username") final String username,
properties.put(NAME, followProperties.get(NAME));
properties.put(HASH, followProperties.get(HASH));
properties.put(LIKES, post.getDegree(RelationshipTypes.LIKES));
properties.put(REPOSTS, post.getDegree() - 1 - post.getDegree(RelationshipTypes.LIKES));
properties.put(REPOSTS, post.getDegree(Direction.INCOMING)
- 1 // for the Posted Relationship Type
- post.getDegree(RelationshipTypes.LIKES)
- post.getDegree(RelationshipTypes.REPLIED_TO));
properties.put(LIKED, userLikesPost(user, post));
properties.put(REPOSTED, userRepostedPost(user, post));
results.add(properties);
}
}
Expand All @@ -91,10 +98,14 @@ public Response getTimeline(@PathParam("username") final String username,
properties.put(REPOSTER_NAME, followProperties.get(NAME));
properties.put(HASH, followProperties.get(HASH));
properties.put(LIKES, post.getDegree(RelationshipTypes.LIKES));
properties.put(REPOSTS, post.getDegree() - 1 - post.getDegree(RelationshipTypes.LIKES));
properties.put(REPOSTS, post.getDegree(Direction.INCOMING)
- 1 // for the Posted Relationship Type
- post.getDegree(RelationshipTypes.LIKES)
- post.getDegree(RelationshipTypes.REPLIED_TO));
properties.put(LIKED, userLikesPost(user, post));
properties.put(REPOSTED, userRepostedPost(user, post));

Node author = getAuthor(post, (Long)properties.get(TIME));

properties.put(USERNAME, author.getProperty(USERNAME));
properties.put(NAME, author.getProperty(NAME));
results.add(properties);
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/maxdemarzi/likes/CreateLikesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,7 @@ public void shouldNotCreateLikesTwice() {
put("time", 1490140299);
put("likes", 1);
put("reposts", 0);
put("liked", true);
put("reposted", false);
}};
}
2 changes: 2 additions & 0 deletions src/test/java/com/maxdemarzi/posts/CreateRepostTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,7 @@ public void shouldNotCreateRepostTwice() throws InterruptedException {
put("time", 1490140299);
put("likes", 0);
put("reposts", 1);
put("liked", false);
put("reposted", true);
}};
}
17 changes: 14 additions & 3 deletions src/test/java/com/maxdemarzi/timeline/GetTimelineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public void shouldGetTimelineSince() {
"CREATE (laeg)-[:POSTED_ON_2017_03_22 {time: 1490208700}]->(post2)" +
"CREATE (max)-[:POSTED_ON_2017_03_22 {time: 1490208800}]->(post3)" +
"CREATE (laeg)-[:LIKES {time:1490143299}]->(post1)" +
"CREATE (laeg)-[:REPOSTED_ON_2017_03_22 {time:1490208000}]->(post1)";
"CREATE (laeg)-[:REPOSTED_ON_2017_03_22 {time:1490208000}]->(post1)" +
"CREATE (max)-[:LIKES {time: 1490214800}]->(post2)" ;

private static final ArrayList<HashMap<String, Object>> expected = new ArrayList<HashMap<String, Object>>() {{
add(new HashMap<String, Object>() {{
Expand All @@ -88,15 +89,20 @@ public void shouldGetTimelineSince() {
put("time", 1490208800);
put("likes", 0);
put("reposts", 0);
put("liked", false);
put("reposted", false);

}});
add(new HashMap<String, Object>() {{
put("username", "laexample");
put("name", "Luke Gannon");
put("hash", "hash");
put("status", "How are you!");
put("time", 1490208700);
put("likes", 0);
put("likes", 1);
put("reposts", 0);
put("liked", true);
put("reposted", false);
}});
add(new HashMap<String, Object>() {{
put("reposter_username", "laexample");
Expand All @@ -109,6 +115,9 @@ public void shouldGetTimelineSince() {
put("time", 1490140299);
put("likes", 1);
put("reposts", 1);
put("liked", false);
put("reposted", false);

}});
}};

Expand All @@ -120,6 +129,8 @@ public void shouldGetTimelineSince() {
put("time", 1490140299);
put("likes", 1);
put("reposts", 1);
}};
put("liked", false);
put("reposted", false);
}};

}

0 comments on commit 745c380

Please sign in to comment.