<commit_before>package lila.security

import play.api.i18n.Lang
import scala.concurrent.duration._
import scalatags.Text.all._

import lila.common.config._
import lila.common.EmailAddress
import lila.i18n.I18nKeys.{ emails => trans }
import lila.user.{ User, UserRepo }

final class Reopen(
    mailgun: Mailgun,
    userRepo: UserRepo,
    baseUrl: BaseUrl,
    tokenerSecret: Secret
)(implicit ec: scala.concurrent.ExecutionContext) {

  import Mailgun.html._

  def prepare(
      username: String,
      email: EmailAddress,
      hasModClose: User.ID => Fu[Boolean]
  ): Fu[Either[(String, String), User]] =
    userRepo.enabledWithEmail(email.normalize) flatMap {
      case Some(_) =>
        fuccess(Left("emailUsed" -> "This email address is already in use by an active account."))
      case _ =>
        val userId = User normalize username
        userRepo.byId(userId) flatMap {
          case None =>
            fuccess(Left("noUser" -> "No account has ever been created with this username."))
          case Some(user) if user.enabled =>
            fuccess(Left("alreadyActive" -> "This account is already active."))
          case Some(user) =>
            userRepo.prevEmail(user.id) flatMap {
              case None =>
                fuccess(
                  Left("noEmail" -> "That account doesn't have any associated email, and cannot be reopened.")
                )
              case Some(prevEmail) if !email.similarTo(prevEmail) =>
                fuccess(Left("differentEmail" -> "That account has a different email address."))
              case _ =>
                hasModClose(user.id) map {
                  case true => Left("nope" -> "Sorry, that account can no longer be reopened.")
                  case _    => Right(user)
                }
            }
        }
    }

  def send(user: User, email: EmailAddress)(implicit lang: Lang): Funit =
    tokener make user.id flatMap { token =>
      lila.mon.email.send.reopen.increment()
      val url = s"$baseUrl/account/reopen/login/$token"
      mailgun send Mailgun.Message(
        to = email,<commit_msg>Tweak criteria for account reopening<commit_after>package lila.security

import play.api.i18n.Lang
import scala.concurrent.duration._
import scalatags.Text.all._

import lila.common.config._
import lila.common.EmailAddress
import lila.i18n.I18nKeys.{ emails => trans }
import lila.user.{ User, UserRepo }

final class Reopen(
    mailgun: Mailgun,
    userRepo: UserRepo,
    baseUrl: BaseUrl,
    tokenerSecret: Secret
)(implicit ec: scala.concurrent.ExecutionContext) {

  import Mailgun.html._

  def prepare(
      username: String,
      email: EmailAddress,
      hasModClose: User.ID => Fu[Boolean]
  ): Fu[Either[(String, String), User]] =
    userRepo.enabledWithEmail(email.normalize) flatMap {
      case Some(_) =>
        fuccess(Left("emailUsed" -> "This email address is already in use by an active account."))
      case _ =>
        val userId = User normalize username
        userRepo.byId(userId) flatMap {
          case None =>
            fuccess(Left("noUser" -> "No account has ever been created with this username."))
          case Some(user) if user.enabled =>
            fuccess(Left("alreadyActive" -> "This account is already active."))
          case Some(user) =>
            userRepo.prevEmail(user.id) flatMap {
              case None =>
                fuccess(
                  Left("noEmail" -> "That account doesn't have any associated email, and cannot be reopened.")
                )
              case Some(prevEmail) if !email.similarTo(prevEmail) =>
                fuccess(Left("differentEmail" -> "That account has a different email address."))
              case _ =>
                (hasModClose(user.id) || user.marks.alt) map {
                  case true => Left("nope" -> "Sorry, that account can no longer be reopened.")
                  case _    => Right(user)
                }
            }
        }
    }

  def send(user: User, email: EmailAddress)(implicit lang: Lang): Funit =
    tokener make user.id flatMap { token =>
      lila.mon.email.send.reopen.increment()
      val url = s"$baseUrl/account/reopen/login/$token"
      mailgun send Mailgun.Message(
        to = email,
{'fraction_non_alphanumeric': 0.1088339222614841, 'fraction_numerical': 0.0028268551236749115, 'mean_word_length': 2.263643351268255, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, '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': '2477694', 'n_tokens_mistral': 1232, 'n_tokens_neox': 1204, 'n_words': 332}