forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQaComment.scala
48 lines (41 loc) · 1.53 KB
/
QaComment.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
package controllers
import play.api.mvc._
import lila.api.Context
import lila.app._
import lila.qa.{ QuestionId, AnswerId, QaAuth }
object QaComment extends QaController {
def question(id: QuestionId) = AuthBody { implicit ctx => me =>
IfCanComment {
WithQuestion(id) { q =>
implicit val req = ctx.body
forms.comment.bindFromRequest.fold(
err => renderQuestion(q, None),
data => api.comment.create(data, Left(q), me) map { comment =>
Redirect(routes.QaQuestion.show(q.id, q.slug) + "#comment-" + comment.id)
}
)
}
}
}
def answer(questionId: QuestionId, answerId: AnswerId) = AuthBody { implicit ctx => me =>
IfCanComment {
(api.question findById questionId) zip (api.answer findById answerId) flatMap {
case (Some(q), Some(a)) =>
implicit val req = ctx.body
forms.comment.bindFromRequest.fold(
err => renderQuestion(q, None),
data => api.comment.create(data, Right(a), me) map { comment =>
Redirect(routes.QaQuestion.show(q.id, q.slug) + "#comment-" + comment.id)
}
)
case _ => notFound
}
}
}
def remove(questionId: QuestionId, commentId: String) = Secure(_.ModerateQa) { implicit ctx => me =>
api.comment.remove(questionId, commentId) inject
Redirect(routes.QaQuestion.show(questionId, "redirect"))
}
private def IfCanComment(block: => Fu[Result])(implicit ctx: Context) =
if (QaAuth.canComment) block else fuccess(Forbidden)
}