make NameGenerator check availability
parent
04d5dbb7e2
commit
f6490b0f36
|
@ -95,16 +95,18 @@ final class Clas(
|
|||
.get(clas, userId)
|
||||
.map2(lila.clas.Student.WithPassword(_, lila.user.User.ClearPassword(password)))
|
||||
case _ => fuccess(none)
|
||||
} map { created =>
|
||||
Ok(
|
||||
html.clas.student.form(
|
||||
clas,
|
||||
students,
|
||||
env.clas.forms.student.invite,
|
||||
env.clas.forms.student.create,
|
||||
created
|
||||
} flatMap { created =>
|
||||
env.clas.forms.student.generate map { createForm =>
|
||||
Ok(
|
||||
html.clas.student.form(
|
||||
clas,
|
||||
students,
|
||||
env.clas.forms.student.invite,
|
||||
createForm,
|
||||
created
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,8 +6,9 @@ import scala.concurrent.duration._
|
|||
|
||||
final class ClasForm(
|
||||
lightUserAsync: lila.common.LightUser.Getter,
|
||||
securityForms: lila.security.DataForm
|
||||
) {
|
||||
securityForms: lila.security.DataForm,
|
||||
nameGenerator: NameGenerator
|
||||
)(implicit ec: scala.concurrent.ExecutionContext) {
|
||||
|
||||
import ClasForm._
|
||||
|
||||
|
@ -27,13 +28,21 @@ final class ClasForm(
|
|||
|
||||
object student {
|
||||
|
||||
def create =
|
||||
def create: Form[NewStudent] =
|
||||
Form(
|
||||
mapping(
|
||||
"username" -> securityForms.signup.username,
|
||||
"realName" -> nonEmptyText
|
||||
"realName" -> nonEmptyText(maxLength = 100)
|
||||
)(NewStudent.apply)(NewStudent.unapply)
|
||||
) fill generateStudent
|
||||
)
|
||||
|
||||
def generate: Fu[Form[NewStudent]] = nameGenerator() map { username =>
|
||||
create fill
|
||||
NewStudent(
|
||||
username = ~username,
|
||||
realName = ""
|
||||
)
|
||||
}
|
||||
|
||||
def invite =
|
||||
Form(
|
||||
|
@ -75,11 +84,6 @@ object ClasForm {
|
|||
realName: String
|
||||
)
|
||||
|
||||
def generateStudent = NewStudent(
|
||||
username = ~NameGenerator(),
|
||||
realName = ""
|
||||
)
|
||||
|
||||
case class StudentData(
|
||||
realName: String,
|
||||
notes: String
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package lila.clas
|
||||
|
||||
import play.api.Configuration
|
||||
import com.softwaremill.macwire._
|
||||
|
||||
import lila.common.config._
|
||||
|
@ -16,6 +15,8 @@ final class Env(
|
|||
baseUrl: BaseUrl
|
||||
)(implicit ec: scala.concurrent.ExecutionContext) {
|
||||
|
||||
lazy val nameGenerator = wire[NameGenerator]
|
||||
|
||||
lazy val forms = wire[ClasForm]
|
||||
|
||||
private val colls = wire[ClasColls]
|
||||
|
|
|
@ -1,14 +1,18 @@
|
|||
package lila.clas
|
||||
|
||||
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
|
||||
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 none
|
||||
else fuccess(none)
|
||||
}
|
||||
|
||||
private def anyOf[A](vec: Vector[A]): A =
|
||||
|
|
Loading…
Reference in New Issue