Skip to content

Instantly share code, notes, and snippets.

@deque-blog
Last active March 16, 2018 14:19
Show Gist options
  • Save deque-blog/a226aa12818e65fc1bf439106333cfbe to your computer and use it in GitHub Desktop.
Save deque-blog/a226aa12818e65fc1bf439106333cfbe to your computer and use it in GitHub Desktop.
class SuggestPopularFriendPost {
private final IAccessProfileInfo profileInfo;
public SuggestPopularFriendPost(IAccessProfileInfo profileInfo) {
this.profileInfo = profileInfo;
}
public Iterable<PostSummary> suggestedPostsFor(ProfileId profileId) {
Iterable<ProfileId> friendIds = friendsOf(profileId);
Set<Topic> topics = favoriteTopicsOf(profileId);
Stream<PostSummary> interestingPosts = stream(friendIds)
.flatMap(friendId -> lastPostsOf(friendId))
.filter(post -> post.isAbout(topics));
return mostLiked(3, interestingPosts);
}
private Iterable<ProfileId> friendsOf(ProfileId profileId) {
return profileInfo.friendsOf(profileId);
}
private Set<Topic> favoriteTopicsOf(ProfileId profileId) {
return profileInfo.favoriteTopicsOf(profileId);
}
private Stream<PostSummary> lastPostsOf(ProfileId profileId) {
return stream(profileInfo.lastPostsOf(profileId));
}
private List<PostSummary> mostLiked(int count, Stream<PostSummary> posts) {
return posts
.sorted(Comparator.comparing(PostSummary::getLikesCount))
.limit(count)
.collect(Collectors.toList());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment