Skip to content

Instantly share code, notes, and snippets.

@ryanpbrewster
Last active May 4, 2016 20:32
Show Gist options
  • Select an option

  • Save ryanpbrewster/e313cb6463c115345a647a7638731638 to your computer and use it in GitHub Desktop.

Select an option

Save ryanpbrewster/e313cb6463c115345a647a7638731638 to your computer and use it in GitHub Desktop.
sealed trait Event
case class NewPost(userId: Id[User], groupId: Id[Group], postId: Id[Post]) extends Event
case class NewMessage(from: Id[User], to: Id[User], msgId: Id[Message]) extends Event
def generateNotifications(activity: Seq[Event]): Seq[String] = {
val (allUsers, allGroups, allPosts, allMsgs) = {
val userIds: Set[Id[User]] = activity.collect {
case NewPost(userId, _, _) => userId
case NewMessage(userId, _, _) => userId
}.toSet
val groupIds: Set[Id[Group]] = activity.collect {
case NewPost(_, groupId, _) => groupId
}.toSet
val postIds: Set[Id[Post]] = activity.collect {
case NewPost(_, _, postId) => postId
}.toSet
val msgIds: Set[Id[Message]] = activity.collect {
case NewMessage(_, _, msgId) => msgId
}.toSet
(getManyUsersFromDb(userIds),
getManyGroupsFromDb(groupIds),
getManyPostsFromDb(postIds),
getManyMsgsFromDb(msgIds))
}
activity.map {
case NewPost(userId, groupId, postId) =>
val user = allUsers(userId)
val group = allGroups(groupId)
val post = allPosts(postId)
s"${user.fullName} shared ${post.title} with ${group.name}"
case NewMessage(fromId, toId, msgId) =>
val (from, to) = (allUsers(fromId), allUsers(toId))
val msg = allMsgs(msgId)
s"${from.fullName} said {msg.text} to ${to.fullName}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment