Last active
March 16, 2018 14:19
-
-
Save deque-blog/a226aa12818e65fc1bf439106333cfbe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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