make NameGenerator check availability

This commit is contained in:
Thibault Duplessis 2020-01-18 09:40:03 -06:00
parent 04d5dbb7e2
commit f6490b0f36
4 changed files with 35 additions and 24 deletions

View file

@ -95,16 +95,18 @@ final class Clas(
.get(clas, userId) .get(clas, userId)
.map2(lila.clas.Student.WithPassword(_, lila.user.User.ClearPassword(password))) .map2(lila.clas.Student.WithPassword(_, lila.user.User.ClearPassword(password)))
case _ => fuccess(none) case _ => fuccess(none)
} map { created => } flatMap { created =>
Ok( env.clas.forms.student.generate map { createForm =>
html.clas.student.form( Ok(
clas, html.clas.student.form(
students, clas,
env.clas.forms.student.invite, students,
env.clas.forms.student.create, env.clas.forms.student.invite,
created createForm,
created
)
) )
) }
} }
} }
} }

View file

@ -6,8 +6,9 @@ import scala.concurrent.duration._
final class ClasForm( final class ClasForm(
lightUserAsync: lila.common.LightUser.Getter, lightUserAsync: lila.common.LightUser.Getter,
securityForms: lila.security.DataForm securityForms: lila.security.DataForm,
) { nameGenerator: NameGenerator
)(implicit ec: scala.concurrent.ExecutionContext) {
import ClasForm._ import ClasForm._
@ -27,13 +28,21 @@ final class ClasForm(
object student { object student {
def create = def create: Form[NewStudent] =
Form( Form(
mapping( mapping(
"username" -> securityForms.signup.username, "username" -> securityForms.signup.username,
"realName" -> nonEmptyText "realName" -> nonEmptyText(maxLength = 100)
)(NewStudent.apply)(NewStudent.unapply) )(NewStudent.apply)(NewStudent.unapply)
) fill generateStudent )
def generate: Fu[Form[NewStudent]] = nameGenerator() map { username =>
create fill
NewStudent(
username = ~username,
realName = ""
)
}
def invite = def invite =
Form( Form(
@ -75,11 +84,6 @@ object ClasForm {
realName: String realName: String
) )
def generateStudent = NewStudent(
username = ~NameGenerator(),
realName = ""
)
case class StudentData( case class StudentData(
realName: String, realName: String,
notes: String notes: String

View file

@ -1,6 +1,5 @@
package lila.clas package lila.clas
import play.api.Configuration
import com.softwaremill.macwire._ import com.softwaremill.macwire._
import lila.common.config._ import lila.common.config._
@ -16,6 +15,8 @@ final class Env(
baseUrl: BaseUrl baseUrl: BaseUrl
)(implicit ec: scala.concurrent.ExecutionContext) { )(implicit ec: scala.concurrent.ExecutionContext) {
lazy val nameGenerator = wire[NameGenerator]
lazy val forms = wire[ClasForm] lazy val forms = wire[ClasForm]
private val colls = wire[ClasColls] private val colls = wire[ClasColls]

View file

@ -1,14 +1,18 @@
package lila.clas package lila.clas
import scala.util.Random import scala.util.Random
import scala.concurrent.ExecutionContext
private object NameGenerator { final class NameGenerator(userRepo: lila.user.UserRepo)(implicit ec: ExecutionContext) {
def apply(maxSize: Int = 16, triesLeft: Int = 100): Option[String] = { def apply(maxSize: Int = 16, triesLeft: Int = 100): Fu[Option[String]] = {
val name = anyOf(combinations).map(anyOf).mkString val name = anyOf(combinations).map(anyOf).mkString
if (name.size <= maxSize) name.some if (name.size <= maxSize) userRepo.nameExists(name) flatMap {
case true => apply(maxSize, triesLeft - 1)
case _ => fuccess(name.some)
}
else if (triesLeft > 0) apply(maxSize, triesLeft - 1) else if (triesLeft > 0) apply(maxSize, triesLeft - 1)
else none else fuccess(none)
} }
private def anyOf[A](vec: Vector[A]): A = private def anyOf[A](vec: Vector[A]): A =