forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessage.scala
89 lines (77 loc) · 2.61 KB
/
Message.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
87
88
89
package controllers
import play.api._
import play.api.data.Form
import play.api.mvc._
import play.api.mvc.Results._
import play.twirl.api.Html
import lila.api.Context
import lila.app._
import lila.user.{ User => UserModel, UserRepo }
import lila.security.Granter
import views._
object Message extends LilaController {
private def api = Env.message.api
private def security = Env.message.security
private def forms = Env.message.forms
private def relationApi = Env.relation.api
def inbox(page: Int) = Auth { implicit ctx =>
me =>
NotForKids {
api.inbox(me, page) map { html.message.inbox(me, _) }
}
}
def thread(id: String) = Auth { implicit ctx =>
implicit me =>
NotForKids {
OptionFuOk(api.thread(id, me)) { thread =>
relationApi.fetchBlocks(thread otherUserId me, me.id) map { blocked =>
html.message.thread(thread, forms.post, blocked)
}
} map NoCache
}
}
def answer(id: String) = AuthBody { implicit ctx =>
implicit me =>
OptionFuResult(api.thread(id, me)) { thread =>
implicit val req = ctx.body
forms.post.bindFromRequest.fold(
err => relationApi.fetchBlocks(thread otherUserId me, me.id) map { blocked =>
BadRequest(html.message.thread(thread, err, blocked))
},
text => api.makePost(thread, text, me) inject Redirect(routes.Message.thread(thread.id) + "#bottom")
)
}
}
def form = Auth { implicit ctx =>
implicit me =>
NotForKids {
renderForm(me, get("title"), identity) map { Ok(_) }
}
}
def create = AuthBody { implicit ctx =>
implicit me =>
NotForKids {
implicit val req = ctx.body
forms.thread(me).bindFromRequest.fold(
err => renderForm(me, none, _ => err) map { BadRequest(_) },
data => api.makeThread(data, me) map { thread =>
Redirect(routes.Message.thread(thread.id))
})
}
}
private def renderForm(me: UserModel, title: Option[String], f: Form[_] => Form[_])(implicit ctx: Context): Fu[Html] =
get("user") ?? UserRepo.named flatMap { user =>
user.fold(fuccess(true))(u => security.canMessage(me.id, u.id)) map { canMessage =>
html.message.form(f(forms thread me), user, title,
canMessage = canMessage || Granter(_.MessageAnyone)(me))
}
}
def delete(id: String) = AuthBody { implicit ctx =>
implicit me =>
api.deleteThread(id, me) inject Redirect(routes.Message.inbox(1))
}
def markAsRead(id: String) = AuthBody { implicit ctx =>
implicit me =>
api.markThreadAsRead(id, me)
}
}