<commit_before>package lila.push

import org.joda.time.DateTime

import reactivemongo.bson._

import lila.db.dsl._
import lila.user.User

final class WebSubscriptionApi(coll: Coll) {

  def getSubscriptions(max: Int)(userId: User.ID): Fu[List[WebSubscription]] =
    coll.find($doc(
      "userId" -> userId
    )).sort($doc("seenAt" -> -1)).list[Bdoc](max).map { docs =>
      docs.flatMap { doc =>
        for {
          endpoint <- doc.getAs[String]("_id")
          auth <- doc.getAs[String]("auth")
          p256dh <- doc.getAs[String]("p256dh")
        } yield WebSubscription(endpoint, auth, p256dh)
      }
    }

  def subscribe(user: User, subscription: WebSubscription, sessionId: String): Funit = {
    coll.update($id(subscription.endpoint), $doc(
      "userId" -> user.id,
      "sessionId" -> sessionId,
      "auth" -> subscription.auth,
      "p256dh" -> subscription.p256dh,
      "seenAt" -> DateTime.now
    ), upsert = true).void
  }

  def unsubscribeBySession(sessionId: String): Funit = {
    coll.remove($doc("sessionId" -> sessionId)).void
  }

  def unsubscribeByUser(user: User): Funit = {
    coll.remove($doc("userId" -> user.id)).void
  }

  def unsubscribeByUserExceptSession(user: User, sessionId: String): Funit = {
    coll.remove($doc(
      "userId" -> user.id,
      "sessionId" -> $ne(sessionId)
    )).void
  }
}
<commit_msg>at most one push subscription per session (closes #5302)<commit_after>package lila.push

import org.joda.time.DateTime

import reactivemongo.bson._

import lila.db.dsl._
import lila.user.User

final class WebSubscriptionApi(coll: Coll) {

  def getSubscriptions(max: Int)(userId: User.ID): Fu[List[WebSubscription]] =
    coll.find($doc(
      "userId" -> userId
    )).sort($doc("seenAt" -> -1)).list[Bdoc](max).map { docs =>
      docs.flatMap { doc =>
        for {
          endpoint <- doc.getAs[String]("endpoint")
          auth <- doc.getAs[String]("auth")
          p256dh <- doc.getAs[String]("p256dh")
        } yield WebSubscription(endpoint, auth, p256dh)
      }
    }

  def subscribe(user: User, subscription: WebSubscription, sessionId: String): Funit = {
    coll.update($id(sessionId), $doc(
      "userId" -> user.id,
      "endpoint" -> subscription.endpoint,
      "auth" -> subscription.auth,
      "p256dh" -> subscription.p256dh,
      "seenAt" -> DateTime.now
    ), upsert = true).void
  }

  def unsubscribeBySession(sessionId: String): Funit = {
    coll.remove($id(sessionId)).void
  }

  def unsubscribeByUser(user: User): Funit = {
    coll.remove($doc("userId" -> user.id)).void
  }

  def unsubscribeByUserExceptSession(user: User, sessionId: String): Funit = {
    coll.remove($doc(
      "userId" -> user.id,
      "_id" -> $ne(sessionId)
    )).void
  }
}

{'fraction_non_alphanumeric': 0.15900036218761318, 'fraction_numerical': 0.013038754074610649, 'mean_word_length': 3.0028985507246375, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 28, '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': '362394', 'n_tokens_mistral': 961, 'n_tokens_neox': 927, 'n_words': 175}