-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
UserForm.scala
80 lines (67 loc) · 2.42 KB
/
UserForm.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package lila.user
import chess.PlayerTitle
import play.api.data.*
import play.api.data.Forms.*
import play.api.data.validation.Constraints
import lila.common.Form.{
cleanNonEmptyText,
cleanText,
cleanNoSymbolsText,
cleanNoSymbolsAndNonEmptyText,
playerTitle,
into,
trim,
given
}
import lila.common.LameName
import lila.core.user.Profile
final class UserForm:
def username(user: User): Form[UserName] = Form(
single(
"username" -> cleanNonEmptyText
.into[UserName]
.verifying(
"changeUsernameNotSame",
name => name.id == user.username.id && name != user.username
)
.verifying(
"usernameUnacceptable",
name => !LameName.hasTitle(name.value) || LameName.hasTitle(user.username.value)
)
)
).fill(user.username)
def usernameOf(user: User) = username(user).fill(user.username)
val profile: Form[Profile] = Form:
mapping(
"flag" -> optional(text.verifying(Flags.codeSet contains _)),
"location" -> optional(cleanNoSymbolsAndNonEmptyText(maxLength = 80)),
"bio" -> optional(cleanNoSymbolsAndNonEmptyText(maxLength = 400)),
"realName" -> optional(cleanNoSymbolsText(minLength = 1, maxLength = 100)),
"fideRating" -> optional(number(min = 1400, max = 3000)),
"uscfRating" -> optional(number(min = 100, max = 3000)),
"ecfRating" -> optional(number(min = 0, max = 3000)),
"rcfRating" -> optional(number(min = 0, max = 3000)),
"cfcRating" -> optional(number(min = 0, max = 3000)),
"dsbRating" -> optional(number(min = 0, max = 3000)),
"links" -> optional(cleanNoSymbolsAndNonEmptyText(maxLength = 3000))
)(Profile.apply)(unapply)
def profileOf(user: User) = profile.fill(user.profileOrDefault)
def flair(using Me) = Form[Option[Flair]]:
single(FlairApi.formPair())
object UserForm:
val note = Form:
mapping(
"text" -> cleanText(minLength = 3, maxLength = 2000),
"noteType" -> text
)((text, noteType) => NoteData(text, noteType == "mod" || noteType == "dox", noteType == "dox"))(_ =>
none
)
val apiNote = Form:
mapping(
"text" -> cleanText(minLength = 3, maxLength = 2000),
"mod" -> boolean,
"dox" -> default(boolean, false)
)(NoteData.apply)(unapply)
case class NoteData(text: String, mod: Boolean, dox: Boolean)
val title = Form:
single("title" -> optional(playerTitle.field))