update notifications from chat state WIP - for #5985

pull/6062/head
Thibault Duplessis 2020-02-19 12:52:13 -06:00
parent c1d78041d6
commit e8c0a9b6e4
4 changed files with 28 additions and 10 deletions

View File

@ -41,7 +41,7 @@ final class MsgApi(
(userId != me.id) ?? lightUserApi.async(userId).flatMap {
_ ?? { contact =>
for {
_ <- setReadBy(threadId, me)
_ <- setReadBy(threadId, me, userId)
msgs <- threadMsgsFor(threadId, me, before)
relations <- relationApi.fetchRelations(me.id, userId)
postable <- security.may.post(me.id, userId, isNew = msgs.headOption.isEmpty)
@ -116,7 +116,7 @@ final class MsgApi(
true
)
.map { res =>
if (res.nModified > 0) notifier.onRead(threadId)
if (res.nModified > 0) notifier.onRead(threadId, userId, contactId)
}
}
@ -187,7 +187,7 @@ final class MsgApi(
.sort($sort desc "date")
.list[Msg](msgsPerPage.value)
private def setReadBy(threadId: MsgThread.Id, me: User): Funit =
private def setReadBy(threadId: MsgThread.Id, me: User, contactId: User.ID): Funit =
colls.thread.updateField(
$id(threadId) ++ $doc(
"lastMsg.user" $ne me.id,
@ -196,6 +196,6 @@ final class MsgApi(
"lastMsg.read",
true
) map { res =>
if (res.nModified > 0) notifier.onRead(threadId)
if (res.nModified > 0) notifier.onRead(threadId, me.id, contactId)
}
}

View File

@ -22,7 +22,18 @@ final private class MsgNotify(
def onPost(threadId: MsgThread.Id): Unit = schedule(threadId)
def onRead(threadId: MsgThread.Id): Unit = cancel(threadId)
def onRead(threadId: MsgThread.Id, userId: User.ID, contactId: User.ID): Funit = {
!cancel(threadId) ??
notifyApi
.markRead(
lila.notify.Notification.Notifies(userId),
$doc(
"content.type" -> "privateMessage",
"content.user" -> contactId
)
)
.void
}
def deleteAllBy(threads: List[MsgThread], user: User): Funit =
threads
@ -49,8 +60,8 @@ final private class MsgNotify(
}
)
private def cancel(threadId: MsgThread.Id): Unit =
Option(delayed remove threadId).foreach(_.cancel)
private def cancel(threadId: MsgThread.Id): Boolean =
Option(delayed remove threadId).map(_.cancel).isDefined
private def doNotify(threadId: MsgThread.Id): Funit =
colls.thread.byId[MsgThread](threadId.value) flatMap {

View File

@ -14,10 +14,13 @@ final private class NotificationRepo(val coll: Coll)(implicit ec: scala.concurre
coll.delete.one(userNotificationsQuery(notifies) ++ selector).void
def markAllRead(notifies: Notification.Notifies): Funit =
coll.update.one(unreadOnlyQuery(notifies), $set("read" -> true), multi = true).void
markManyRead(unreadOnlyQuery(notifies))
def markAllRead(notifies: Iterable[Notification.Notifies]): Funit =
coll.update.one(unreadOnlyQuery(notifies), $set("read" -> true), multi = true).void
markManyRead(unreadOnlyQuery(notifies))
def markManyRead(doc: Bdoc): Funit =
coll.update.one(doc, $set("read" -> true), multi = true).void
def unreadNotificationsCount(userId: Notification.Notifies): Fu[Int] =
coll.countSel(unreadOnlyQuery(userId))

View File

@ -19,7 +19,7 @@ final class NotifyApi(
maxPerPage: MaxPerPage
)(implicit ec: scala.concurrent.ExecutionContext) {
import BSONHandlers.NotificationBSONHandler
import BSONHandlers.{ NotificationBSONHandler, NotifiesHandler }
import jsonHandlers._
def getNotifications(userId: Notification.Notifies, page: Int): Fu[Paginator[Notification]] = Paginator(
@ -69,6 +69,10 @@ final class NotifyApi(
def remove(notifies: Notification.Notifies, selector: Bdoc): Funit =
repo.remove(notifies, selector) >>- unreadCountCache.invalidate(notifies)
def markRead(notifies: Notification.Notifies, selector: Bdoc): Funit =
repo.markManyRead(selector ++ $doc("notifies" -> notifies, "read" -> false)) >>-
unreadCountCache.invalidate(notifies)
def exists = repo.exists _
private def shouldSkip(notification: Notification) =