forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.scala
86 lines (63 loc) · 1.96 KB
/
model.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
82
83
84
85
86
package lila.qa
import org.joda.time._
import lila.user.User
case class Question(
_id: QuestionId, // autoincrement integer
userId: String,
title: String,
body: String, // markdown
tags: List[String],
vote: Vote,
comments: List[Comment],
views: Int,
answers: Int,
createdAt: DateTime,
updatedAt: DateTime,
acceptedAt: Option[DateTime],
editedAt: Option[DateTime]) {
def id = _id
def slug = {
val s = lila.common.String slugify title
if (s.isEmpty) "-" else s
}
def ownBy(user: User) = userId == user.id
def withVote(f: Vote => Vote) = copy(vote = f(vote))
def updateNow = copy(updatedAt = DateTime.now)
def editNow = copy(editedAt = Some(DateTime.now)).updateNow
def accepted = acceptedAt.isDefined
}
case class Answer(
_id: AnswerId,
questionId: QuestionId,
userId: String,
body: String,
vote: Vote,
comments: List[Comment],
acceptedAt: Option[DateTime],
createdAt: DateTime,
editedAt: Option[DateTime]) {
def id = _id
def accepted = acceptedAt.isDefined
def withVote(f: Vote => Vote) = copy(vote = f(vote))
def ownBy(user: User) = userId == user.id
def editNow = copy(editedAt = Some(DateTime.now))
}
case class AnswerWithQuestion(answer: Answer, question: Question)
case class Vote(up: Set[String], down: Set[String], score: Int) {
def add(user: String, v: Boolean) = (if (v) addUp _ else addDown _)(user)
def addUp(user: String) = copy(up = up + user, down = down - user).computeScore
def addDown(user: String) = copy(up = up - user, down = down + user).computeScore
def of(userId: String): Option[Boolean] =
if (up(userId)) Some(true)
else if (down(userId)) Some(false)
else None
private def computeScore = copy(score = up.size - down.size)
}
case class Comment(
id: CommentId, // random string
userId: String,
body: String,
createdAt: DateTime)
object Comment {
def makeId = ornicar.scalalib.Random nextStringUppercase 8
}