forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mod.scala
349 lines (309 loc) · 13.1 KB
/
Mod.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package controllers
import lila.api.{ Context, BodyContext }
import lila.app._
import lila.chat.Chat
import lila.common.{ IpAddress, EmailAddress, HTTPRequest }
import lila.report.{ Suspect, Mod => AsMod, SuspectId }
import lila.security.Permission
import lila.user.{ UserRepo, User => UserModel, Title }
import ornicar.scalalib.Zero
import views._
import play.api.data._
import play.api.data.Forms._
import play.api.mvc._
import scala.concurrent.duration._
object Mod extends LilaController {
private def modApi = Env.mod.api
private def modLogApi = Env.mod.logApi
private def assessApi = Env.mod.assessApi
def engine(username: String, v: Boolean) = OAuthModBody(_.MarkEngine) { me =>
withSuspect(username) { sus =>
for {
inquiry <- Env.report.api.inquiries ofModId me.id
_ <- modApi.setEngine(AsMod(me), sus, v)
} yield (inquiry, sus).some
}
}(ctx => me => {
case (inquiry, suspect) => Report.onInquiryClose(inquiry, me, suspect.some)(ctx)
})
def publicChat = Secure(_.ChatTimeout) { implicit ctx => _ =>
Env.mod.publicChat.all map {
case (tournamentsAndChats, simulsAndChats) =>
Ok(html.mod.publicChat(tournamentsAndChats, simulsAndChats))
}
}
def booster(username: String, v: Boolean) = OAuthModBody(_.MarkBooster) { me =>
withSuspect(username) { prev =>
for {
inquiry <- Env.report.api.inquiries ofModId me.id
suspect <- modApi.setBooster(AsMod(me), prev, v)
} yield (inquiry, suspect).some
}
}(ctx => me => {
case (inquiry, suspect) => Report.onInquiryClose(inquiry, me, suspect.some)(ctx)
})
def troll(username: String, v: Boolean) = OAuthModBody(_.MarkTroll) { me =>
withSuspect(username) { prev =>
for {
inquiry <- Env.report.api.inquiries ofModId me.id
suspect <- modApi.setTroll(AsMod(me), prev, v)
} yield (inquiry, suspect).some
}
}(ctx => me => {
case (inquiry, suspect) => Report.onInquiryClose(inquiry, me, suspect.some)(ctx)
})
def warn(username: String, subject: String) = OAuthModBody(_.ModMessage) { me =>
lila.message.ModPreset.bySubject(subject) ?? { preset =>
withSuspect(username) { prev =>
for {
inquiry <- Env.report.api.inquiries ofModId me.id
suspect <- modApi.setTroll(AsMod(me), prev, prev.user.troll)
thread <- Env.message.api.sendPreset(me, suspect.user, preset)
_ <- Env.mod.logApi.modMessage(thread.creatorId, thread.invitedId, thread.name)
} yield (inquiry, suspect).some
}
}
}(ctx => me => {
case (inquiry, suspect) => Report.onInquiryClose(inquiry, me, suspect.some)(ctx)
})
def ban(username: String, v: Boolean) = OAuthMod(_.IpBan) { _ => me =>
withSuspect(username) { sus =>
modApi.setBan(AsMod(me), sus, v) map some
}
}(actionResult(username))
def deletePmsAndChats(username: String) = OAuthMod(_.MarkTroll) { _ => me =>
withSuspect(username) { sus =>
Env.mod.publicChat.delete(sus) >>
Env.message.api.deleteThreadsBy(sus.user) map some
}
}(actionResult(username))
def disableTwoFactor(username: String) = Secure(_.DisableTwoFactor) { implicit ctx => me =>
modApi.disableTwoFactor(me.id, username) >> User.modZoneOrRedirect(username, me)
}
def closeAccount(username: String) = OAuthMod(_.CloseAccount) { _ => me =>
modApi.closeAccount(me.id, username).flatMap {
_.?? { user =>
Env.current.closeAccount(user.id, self = false) map some
}
}
}(actionResult(username))
def reopenAccount(username: String) = OAuthMod(_.ReopenAccount) { _ => me =>
modApi.reopenAccount(me.id, username) map some
}(actionResult(username))
def reportban(username: String, v: Boolean) = OAuthMod(_.ReportBan) { _ => me =>
withSuspect(username) { sus =>
modApi.setReportban(AsMod(me), sus, v) map some
}
}(actionResult(username))
def rankban(username: String, v: Boolean) = OAuthMod(_.RemoveRanking) { _ => me =>
withSuspect(username) { sus =>
modApi.setRankban(AsMod(me), sus, v) map some
}
}(actionResult(username))
def impersonate(username: String) = Auth { implicit ctx => me =>
if (username == "-" && lila.mod.Impersonate.isImpersonated(me)) fuccess {
lila.mod.Impersonate.stop(me)
Redirect(routes.User.show(me.username))
}
else if (isGranted(_.Impersonate)) OptionFuRedirect(UserRepo named username) { user =>
lila.mod.Impersonate.start(me, user)
fuccess(routes.User.show(user.username))
}
else notFound
}
def setTitle(username: String) = SecureBody(_.SetTitle) { implicit ctx => me =>
implicit def req = ctx.body
lila.user.DataForm.title.bindFromRequest.fold(
err => fuccess(redirect(username, mod = true)),
title => modApi.setTitle(me.id, username, title map Title.apply) >>
Env.security.automaticEmail.onTitleSet(username) >>-
Env.user.uncacheLightUser(UserModel normalize username) inject
redirect(username, mod = false)
)
}
def setEmail(username: String) = SecureBody(_.SetEmail) { implicit ctx => me =>
implicit def req = ctx.body
OptionFuResult(UserRepo named username) { user =>
Env.security.forms.modEmail(user).bindFromRequest.fold(
err => BadRequest(err.toString).fuccess,
rawEmail => {
val email = Env.security.emailAddressValidator.validate(EmailAddress(rawEmail)) err s"Invalid email ${rawEmail}"
modApi.setEmail(me.id, user.id, email) inject redirect(user.username, mod = true)
}
)
}
}
def notifySlack(username: String) = OAuthMod(_.ModNote) { _ => me =>
withSuspect(username) { sus =>
Env.slack.api.userMod(user = sus.user, mod = me) map some
}
}(actionResult(username))
def log = Secure(_.ModLog) { implicit ctx => me =>
modLogApi.recent map { html.mod.log(_) }
}
private def communications(username: String, priv: Boolean) = Secure {
perms => if (priv) perms.ViewPrivateComms else perms.MarkTroll
} { implicit ctx => me =>
OptionFuOk(UserRepo named username) { user =>
lila.game.GameRepo.recentPovsByUserFromSecondary(user, 80) flatMap { povs =>
priv.?? {
Env.chat.api.playerChat optionsByOrderedIds povs.map(_.gameId).map(Chat.Id.apply)
} zip
priv.?? {
lila.message.ThreadRepo.visibleByUser(user.id, 60).map {
_ filter (_ hasPostsWrittenBy user.id) take 30
}
} zip
(Env.shutup.api getPublicLines user.id) zip
(Env.security userSpy user) zip
Env.user.noteApi.forMod(user.id) zip
Env.mod.logApi.userHistory(user.id) zip
Env.report.api.inquiries.ofModId(me.id) map {
case chats ~ threads ~ publicLines ~ spy ~ notes ~ history ~ inquiry =>
if (priv && !inquiry.??(_.isRecentCommOf(Suspect(user))))
Env.slack.api.commlog(mod = me, user = user, inquiry.map(_.oldestAtom.by.value))
val povWithChats = (povs zip chats) collect {
case (p, Some(c)) if c.nonEmpty => p -> c
} take 15
val filteredNotes = notes.filter(_.from != "irwin")
html.mod.communication(user, povWithChats, threads, publicLines, spy, filteredNotes, history, priv)
}
}
}
}
def communicationPublic(username: String) = communications(username, false)
def communicationPrivate(username: String) = communications(username, true)
def ipIntel(ip: String) = Secure(_.IpBan) { ctx => me =>
Env.security.ipIntel.failable(IpAddress(ip)).map { Ok(_) }.recover {
case e: Exception => InternalServerError(e.getMessage)
}
}
protected[controllers] def redirect(username: String, mod: Boolean = true) =
Redirect(routes.User.show(username).url + mod.??("?mod"))
def refreshUserAssess(username: String) = Secure(_.MarkEngine) { implicit ctx => me =>
OptionFuResult(UserRepo named username) { user =>
assessApi.refreshAssessByUsername(username) >>
Env.irwin.api.requests.fromMod(Suspect(user), AsMod(me)) >>
User.renderModZoneActions(username)
}
}
def spontaneousInquiry(username: String) = Secure(_.SeeReport) { implicit ctx => me =>
OptionFuResult(UserRepo named username) { user =>
Env.report.api.inquiries.spontaneous(AsMod(me), Suspect(user)) inject redirect(user.username, true)
}
}
def gamify = Secure(_.SeeReport) { implicit ctx => me =>
Env.mod.gamify.leaderboards zip
Env.mod.gamify.history(orCompute = true) map {
case (leaderboards, history) => Ok(html.mod.gamify.index(leaderboards, history))
}
}
def gamifyPeriod(periodStr: String) = Secure(_.SeeReport) { implicit ctx => me =>
lila.mod.Gamify.Period(periodStr).fold(notFound) { period =>
Env.mod.gamify.leaderboards map { leaderboards =>
Ok(html.mod.gamify.period(leaderboards, period))
}
}
}
def search = Secure(_.UserSearch) { implicit ctx => me =>
val query = (~get("q")).trim
Env.mod.search(query) map { users =>
html.mod.search(query, users)
}
}
def chatUser(username: String) = Secure(_.ChatTimeout) { implicit ctx => me =>
implicit val lightUser = Env.user.lightUserSync _
JsonOptionOk {
Env.chat.api.userChat userModInfo username map2 lila.chat.JsonView.userModInfo
}
}
def permissions(username: String) = Secure(_.ChangePermission) { implicit ctx => me =>
OptionOk(UserRepo named username) { user =>
html.mod.permissions(user)
}
}
def savePermissions(username: String) = SecureBody(_.ChangePermission) { implicit ctx => me =>
implicit def req = ctx.body
import lila.security.Permission
OptionFuResult(UserRepo named username) { user =>
Form(single(
"permissions" -> list(nonEmptyText.verifying { str =>
Permission.allButSuperAdmin.exists(_.name == str)
})
)).bindFromRequest.fold(
err => BadRequest(html.mod.permissions(user)).fuccess,
permissions =>
modApi.setPermissions(me.id, user.username, Permission(permissions)) >> {
(Permission(permissions) diff Permission(user.roles) contains Permission.Coach) ??
Env.security.automaticEmail.onBecomeCoach(user)
} >> {
Permission(permissions).exists(_ is Permission.SeeReport) ?? Env.plan.api.setLifetime(user)
} inject redirect(user.username, true)
)
}
}
def emailConfirm = SecureBody(_.SetEmail) { implicit ctx => me =>
get("q") match {
case None => Ok(html.mod.emailConfirm("", none, none)).fuccess
case Some(rawQuery) =>
val query = rawQuery.trim.split(' ').toList
val email = query.headOption.map(EmailAddress.apply) flatMap Env.security.emailAddressValidator.validate
val username = query lift 1
def tryWith(setEmail: EmailAddress, q: String): Fu[Option[Result]] = Env.mod.search(q) flatMap {
case List(user) => (!user.everLoggedIn).?? {
lila.mon.user.register.modConfirmEmail()
modApi.setEmail(me.id, user.id, setEmail)
} >>
UserRepo.email(user.id) map { email =>
Ok(html.mod.emailConfirm("", user.some, email)).some
}
case _ => fuccess(none)
}
email.?? { em =>
tryWith(em, em.value) orElse {
username ?? { tryWith(em, _) }
}
} getOrElse BadRequest(html.mod.emailConfirm(rawQuery, none, none)).fuccess
}
}
def chatPanic = Secure(_.MarkTroll) { implicit ctx => me =>
Ok(html.mod.chatPanic(Env.chat.panic.get)).fuccess
}
def chatPanicPost = OAuthMod(_.MarkTroll) { req => me =>
val v = getBool("v", req)
Env.chat.panic.set(v)
Env.slack.api.chatPanic(me, v)
fuccess(().some)
}(ctx => me => _ => Redirect(routes.Mod.chatPanic).fuccess)
def cheatList(gameId: String) = SecureBody(_.Hunter) { implicit ctx => me =>
OptionFuOk(lila.game.GameRepo game gameId) { game =>
Env.mod.cheatList.set(game, getBool("v"), lila.report.Mod(me))
}
}
def eventStream = OAuthSecure(_.Admin) { req => me =>
Ok.chunked(Env.mod.stream.enumerator).fuccess
}
private def withSuspect[A](username: String)(f: Suspect => Fu[A])(implicit zero: Zero[A]): Fu[A] =
Env.report.api getSuspect username flatMap {
_ ?? f
}
private def OAuthMod[A](perm: Permission.Selector)(f: RequestHeader => UserModel => Fu[Option[A]])(
secure: Context => UserModel => A => Fu[Result]
): Action[Unit] = SecureOrScoped(perm)(
secure = ctx => me => f(ctx.req)(me) flatMap { _ ?? secure(ctx)(me) },
scoped = req => me => f(req)(me) flatMap { res =>
res.isDefined ?? fuccess(jsonOkResult)
}
)
private def OAuthModBody[A](perm: Permission.Selector)(f: UserModel => Fu[Option[A]])(
secure: BodyContext[_] => UserModel => A => Fu[Result]
): Action[AnyContent] = SecureOrScopedBody(perm)(
secure = ctx => me => f(me) flatMap { _ ?? secure(ctx)(me) },
scoped = _ => me => f(me) flatMap { res =>
res.isDefined ?? fuccess(jsonOkResult)
}
)
private def actionResult(username: String)(ctx: Context)(me: UserModel)(res: Any) =
if (HTTPRequest isSynchronousHttp ctx.req) fuccess(Mod.redirect(username))
else User.renderModZoneActions(username)(ctx)
}