Last active
December 10, 2015 02:19
-
-
Save negator/4366785 to your computer and use it in GitHub Desktop.
Build an activity from a post, using its associated like and comments.
This file contains 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
type CommentsOrLikes = Either[Comments, Likes] | |
def buildActivity(post: Post): Promise[Activity] = { | |
val likeUrl = LIKE_URL % (post.id, target.token) | |
val commentsUrl = COMMENT_URL % (post.id, target.token) | |
/*Construct paging enumerators, mapping each value to either Left or Right*/ | |
val comments = pagingEnumerator(commentsUrl).map(Left.apply) | |
val likes = pagingEnumerator(likeUrl).map(Right.apply) | |
//Enumerate likes and comments in parallel with the 'interleave' function | |
val content:Enumerator[CommentsOrLikes] = likes interleave comments | |
/*Initial value for fold*/ | |
val activity = Activity(Nil, post, Nil) | |
/*Fold over each enumerated value, building the activity as we go*/ | |
val activityIterateePromise = | |
content |>> Iteratee.fold[CommentsOrLikes, Activity](activity) { | |
case Left(comments) => activity copy (comments = activity.comments ++ comments) | |
case Right(likes) => activity copy (likes = activity.likes ++ likes) | |
} | |
/* Finally, activityIterateePromise will be a Promise[Iteratee[Activity]], | |
* which we need to turn into an Iteratee[Activity] and then run it to | |
* actually build our activity */ | |
Iteratee.flatten(activityIterateePromise).run | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment