Skip to content

Instantly share code, notes, and snippets.

@makiftutuncu
Last active June 29, 2016 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makiftutuncu/d97361eb060b63522b24b5ba0d65c869 to your computer and use it in GitHub Desktop.
Save makiftutuncu/d97361eb060b63522b24b5ba0d65c869 to your computer and use it in GitHub Desktop.
A draft for parsing notifications from new quup
sealed trait NotificationType {
val regex: Regex
}
object NotificationTypes {
case object EntryLike extends NotificationType {override val regex: Regex = """.+gönderini.+beğendi.*""".r}
case object CommentLike extends NotificationType {override val regex: Regex = """.+yorumunu.+beğendi.*""".r}
case object Comment extends NotificationType {override val regex: Regex = """.+yorum.+yaptı.*""".r}
case object Mention extends NotificationType {override val regex: Regex = """.+senden.+bahsetti.*""".r}
case object Message extends NotificationType {override val regex: Regex = """.+sana.+gönderdi.*""".r}
case object Follow extends NotificationType {override val regex: Regex = """.+takip.*""".r}
case object Share extends NotificationType {override val regex: Regex = """.+paylaştı.*""".r}
private val types: List[NotificationType] = List(EntryLike, CommentLike, Comment, Mention, Message, Follow, Share)
def getFromContext(context: String): NotificationType = {
types.find(_.regex.findFirstMatchIn(context).isDefined).get
}
}
case class User(id: String, userName: String, fullName: String) {
val profile: String = s"/@$userName"
val avatar: String = s"/social/avatar/$id"
}
case class Notification(entryId: String, notificationType: NotificationType, timestamp: Long, users: List[User]) {
val url: String = s"/social/entry/$id"
}
def getNotificationsFromQuup(): JsValue = ???
def getNotifications(): List[Notification] = {
val notificationsJsonFromQuup: JsValue = getNotificationsFromQuup()
val notificationsJsonArray: JsArray = (notificationsJsonFromQuup \ "Data").as[JsArray]
val notificationJsons: List[JsValue] = (data \\ "n").toList
val entryIdToNotificationMap: Map[String, Notification] = notificationJsons.foldLeft(Map.empty[String, Notification]) {
case (currentEntryIdToNotificationMap: Map[String, Notification], notificationJson: JsValue) => {
val userId: String = (notificationJson \ "me_mi").as[String]
val userName: String = (notificationJson \ "me_un").as[String]
val fullName: String = (notificationJson \ "me_dn").as[String]
val notificationType: NotificationType = NotificationTypes.getFromContext(context)
val user: User = User(userId, userName, fullName)
val entryId: String = (notificationJson \ "ei").as[String]
val notification: Notification = currentEntryIdToNotificationMap.get(entryId).map {
n: Notification => {
n.copy(users = n.users :+ user)
}
}.getOrElse {
val timestamp: Long = (notificationJson \ "cs").as[String].toLong
val context: String = (notificationJson \ "dt").as[String]
Notification(entryId, notificationType, timestamp, List(user))
}
currentEntryIdToNotificationMap + (entryId -> notification)
}
}
val notifications: List[Notification] = entryIdToNotificationMap.values.sortBy(_.timestamp).reverse.toList
notifications
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment