<commit_before>package lila.notify

import lila.db.dsl._
import org.joda.time.DateTime

private final class NotificationRepo(val coll: Coll) {

  import BSONHandlers._

  def insert(notification: Notification) = {
    coll.insert(notification).void
  }

  def remove(notifies: Notification.Notifies, selector: Bdoc): Funit =
    coll.remove(userNotificationsQuery(notifies) ++ selector).void

  def markAllRead(notifies: Notification.Notifies): Funit = {
    coll.update(unreadOnlyQuery(notifies), $set("read" -> true), multi = true).void
  }

  def unreadNotificationsCount(userId: Notification.Notifies): Fu[Int] = {
    coll.count(unreadOnlyQuery(userId).some)
  }

  private val hasOld = $doc(
    "read" -> false,
    "createdAt" $gt DateTime.now.minusDays(3)
  )
  private val hasUnread = $doc( // recent, read
    "createdAt" $gt DateTime.now.minusMinutes(10)
  )
  private val hasOldOrUnread =
    $doc("$or" -> List(hasOld, hasUnread))

  def hasRecentStudyInvitation(userId: Notification.Notifies, studyId: InvitedToStudy.StudyId): Fu[Boolean] =
    coll.exists($doc(
      "notifies" -> userId,
      "content.type" -> "invitedStudy",
      "content.studyId" -> studyId
    ) ++ hasOldOrUnread)

  def hasRecentNotificationsInThread(userId: Notification.Notifies, topicId: MentionedInThread.TopicId): Fu[Boolean] =
    coll.exists($doc(
      "notifies" -> userId,
      "content.type" -> "mention",
      "content.topicId" -> topicId
    ) ++ hasOldOrUnread)

  def hasRecentPrivateMessageFrom(userId: Notification.Notifies, thread: PrivateMessage.Thread): Fu[Boolean] =
    coll.exists($doc(
      "notifies" -> userId,
      "content.type" -> "privateMessage",
      "content.thread.id" -> thread.id
    ) ++ hasOld)

  def exists(notifies: Notification.Notifies, selector: Bdoc): Fu[Boolean] =<commit_msg>Fix #3562<commit_after>package lila.notify

import lila.db.dsl._
import org.joda.time.DateTime

private final class NotificationRepo(val coll: Coll) {

  import BSONHandlers._

  def insert(notification: Notification) = {
    coll.insert(notification).void
  }

  def remove(notifies: Notification.Notifies, selector: Bdoc): Funit =
    coll.remove(userNotificationsQuery(notifies) ++ selector).void

  def markAllRead(notifies: Notification.Notifies): Funit = {
    coll.update(unreadOnlyQuery(notifies), $set("read" -> true), multi = true).void
  }

  def unreadNotificationsCount(userId: Notification.Notifies): Fu[Int] = {
    coll.count(unreadOnlyQuery(userId).some)
  }

  private def hasOld = $doc(
    "read" -> false,
    "createdAt" $gt DateTime.now.minusDays(3)
  )
  private def hasUnread = $doc( // recent, read
    "createdAt" $gt DateTime.now.minusMinutes(10)
  )
  private def hasOldOrUnread =
    $doc("$or" -> List(hasOld, hasUnread))

  def hasRecentStudyInvitation(userId: Notification.Notifies, studyId: InvitedToStudy.StudyId): Fu[Boolean] =
    coll.exists($doc(
      "notifies" -> userId,
      "content.type" -> "invitedStudy",
      "content.studyId" -> studyId
    ) ++ hasOldOrUnread)

  def hasRecentNotificationsInThread(userId: Notification.Notifies, topicId: MentionedInThread.TopicId): Fu[Boolean] =
    coll.exists($doc(
      "notifies" -> userId,
      "content.type" -> "mention",
      "content.topicId" -> topicId
    ) ++ hasOldOrUnread)

  def hasRecentPrivateMessageFrom(userId: Notification.Notifies, thread: PrivateMessage.Thread): Fu[Boolean] =
    coll.exists($doc(
      "notifies" -> userId,
      "content.type" -> "privateMessage",
      "content.thread.id" -> thread.id
    ) ++ hasOld)

  def exists(notifies: Notification.Notifies, selector: Bdoc): Fu[Boolean] =
{'fraction_non_alphanumeric': 0.13558855885588558, 'fraction_numerical': 0.0027502750275027505, 'mean_word_length': 3.995879120879121, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2758635', 'n_tokens_mistral': 1216, 'n_tokens_neox': 1173, 'n_words': 225}