fix notification cache

This commit is contained in:
Thibault Duplessis 2016-06-01 00:18:42 +02:00
parent fc94564718
commit e92d6c9a21
2 changed files with 10 additions and 5 deletions

View file

@ -304,7 +304,7 @@ private[controllers] trait LilaController
Env.team.api.nbRequests(me.id) zip
Env.message.api.unreadIds(me.id) zip
Env.challenge.api.countInFor(me.id) zip
Env.notifyModule.notifyApi.getUnseenNotificationCount(Notifies(me.id))
Env.notifyModule.notifyApi.countUnread(Notifies(me.id))
}
} map {
case (pref, ((((friends, teamNbRequests), messageIds), nbChallenges), nbNotifications)) =>

View file

@ -25,9 +25,13 @@ final class NotifyApi(
maxPerPage = perPage
)
def markAllRead(userId: Notification.Notifies) = repo.markAllRead(userId)
def markAllRead(userId: Notification.Notifies) =
repo.markAllRead(userId) >> unreadCountCache.remove(userId)
def getUnseenNotificationCount = AsyncCache(repo.unreadNotificationsCount, maxCapacity = 20000)
private val unreadCountCache =
AsyncCache(repo.unreadNotificationsCount, maxCapacity = 20000)
def countUnread(userId: Notification.Notifies) = unreadCountCache(userId)
def addNotification(notification: Notification): Funit = {
@ -35,7 +39,7 @@ final class NotifyApi(
insertOrDiscardNotification(notification) map {
_ ?? {
notif =>
getUnseenNotificationCount(notif.notifies).
unreadCountCache(notif.notifies).
map(NewNotification(notif, _)).
foreach(notifyConnectedClients)
}
@ -54,7 +58,6 @@ final class NotifyApi(
* If the user does not already have an unread notification on the topic, returns it unmodified.
*/
private def insertOrDiscardNotification(notification: Notification): Fu[Option[Notification]] = {
notification.content match {
case MentionedInThread(_, _, topicId, _, _) => {
repo.hasRecentUnseenNotificationsInThread(notification.notifies, topicId).flatMap(alreadyNotified =>
@ -66,6 +69,8 @@ final class NotifyApi(
if (alreadyNotified) fuccess(None) else repo.insert(notification).inject(notification.some))
}
}
} flatMap { res =>
unreadCountCache.remove(notification.notifies) inject res
}
private def notifyConnectedClients(newNotification: NewNotification): Unit = {