forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataForm.scala
81 lines (66 loc) · 2.16 KB
/
DataForm.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
81
package lila.qa
import play.api.data._
import play.api.data.Forms._
import play.api.i18n.Lang
private[qa] final class DataForm(
val captcher: akka.actor.ActorSelection,
detectLanguage: lila.common.DetectLanguage) extends lila.hub.CaptchedForm {
lazy val question = Form(
mapping(
"title" -> nonEmptyText(minLength = 10, maxLength = 150)
.verifying(languageMessage, validateLanguage _),
"body" -> nonEmptyText(minLength = 10, maxLength = 10000)
.verifying(languageMessage, validateLanguage _),
"hidden-tags" -> text,
"gameId" -> text,
"move" -> text
)(QuestionData.apply)(QuestionData.unapply)
.verifying(captchaFailMessage, validateCaptcha _))
def editQuestion(q: Question) = question fill QuestionData(
title = q.title,
body = q.body,
`hidden-tags` = q.tags mkString ",",
gameId = "",
move = "")
lazy val answer = Form(
mapping(
"body" -> nonEmptyText(minLength = 30)
.verifying(languageMessage, validateLanguage _),
"gameId" -> text,
"move" -> text
)(AnswerData.apply)(AnswerData.unapply)
.verifying(captchaFailMessage, validateCaptcha _))
lazy val editAnswer = Form(
single(
"body" -> nonEmptyText(minLength = 30)
.verifying(languageMessage, validateLanguage _)
))
lazy val moveAnswer = Form(single(
"to" -> nonEmptyText
))
lazy val comment = Form(
mapping(
"body" -> nonEmptyText(minLength = 20)
.verifying(languageMessage, validateLanguage _)
)(CommentData.apply)(CommentData.unapply)
)
val vote = Form(single(
"vote" -> number
))
private val languageMessage = "I didn't understand that. Is it written in english?"
private def validateLanguage(str: String) =
detectLanguage(str).awaitSeconds(5).??(_ == Lang("en"))
}
private[qa] case class QuestionData(
title: String,
body: String,
`hidden-tags`: String,
gameId: String,
move: String) {
def tags = `hidden-tags`.split(',').toList.map(_.trim.toLowerCase).filter(_.nonEmpty)
}
private[qa] case class AnswerData(
body: String,
gameId: String,
move: String)
private[qa] case class CommentData(body: String)