lila/app/controllers/ForumPost.scala

95 lines
3.1 KiB
Scala
Raw Normal View History

package controllers
import lila.app._
2017-02-15 17:53:15 -07:00
import lila.common.{ HTTPRequest, IpAddress }
2017-01-24 16:11:42 -07:00
import scala.concurrent.duration._
import views._
2019-12-04 16:39:16 -07:00
final class ForumPost(env: Env) extends LilaController(env) with ForumController {
2019-12-13 07:30:20 -07:00
private val CreateRateLimit =
2020-07-08 11:56:35 -06:00
new lila.memo.RateLimit[IpAddress](4, 5.minutes, key = "forum.post")
2020-05-05 22:11:15 -06:00
def search(text: String, page: Int) =
OpenBody { implicit ctx =>
NotForKids {
2020-07-07 02:34:48 -06:00
if (text.trim.isEmpty) Redirect(routes.ForumCateg.index()).fuccess
2020-05-05 22:11:15 -06:00
else env.forumSearch(text, page, ctx.troll) map { html.forum.search(text, _) }
}
2015-04-10 02:47:00 -06:00
}
2020-05-05 22:11:15 -06:00
def create(categSlug: String, slug: String, page: Int) =
2020-08-02 06:59:14 -06:00
AuthBody { implicit ctx => me =>
2020-08-03 11:12:17 -06:00
NoBot {
CategGrantWrite(categSlug) {
implicit val req = ctx.body
2020-09-21 01:28:28 -06:00
OptionFuResult(topicApi.show(categSlug, slug, page, ctx.me)) { case (categ, topic, posts) =>
if (topic.closed) fuccess(BadRequest("This topic is closed"))
else if (topic.isOld) fuccess(BadRequest("This topic is archived"))
else
forms
.post(me)
.bindFromRequest()
.fold(
err =>
for {
captcha <- forms.anyCaptcha
unsub <- env.timeline.status(s"forum:${topic.id}")(me.id)
canModCateg <- isGrantedMod(categ.slug)
} yield BadRequest(
html.forum.topic
.show(categ, topic, posts, Some(err -> captcha), unsub, canModCateg = canModCateg)
),
data =>
CreateRateLimit(HTTPRequest lastRemoteAddress ctx.req) {
postApi.makePost(categ, topic, data, me) map { post =>
Redirect(routes.ForumPost.redirect(post.id))
}
}(rateLimitedFu)
)
2020-08-03 11:12:17 -06:00
}
2020-05-05 22:11:15 -06:00
}
2013-05-06 11:46:12 -06:00
}
}
2020-05-05 22:11:15 -06:00
def edit(postId: String) =
AuthBody { implicit ctx => me =>
implicit val req = ctx.body
2020-08-02 06:59:14 -06:00
forms
.postEdit(me)
2020-07-22 04:52:52 -06:00
.bindFromRequest()
.fold(
_ => Redirect(routes.ForumPost.redirect(postId)).fuccess,
data =>
CreateRateLimit(HTTPRequest lastRemoteAddress ctx.req) {
postApi.editPost(postId, data.changes, me).map { post =>
Redirect(routes.ForumPost.redirect(post.id))
}
}(rateLimitedFu)
)
2020-05-05 22:11:15 -06:00
}
2016-09-15 16:36:36 -06:00
2020-05-05 22:11:15 -06:00
def delete(categSlug: String, id: String) =
Auth { implicit ctx => me =>
CategGrantMod(categSlug) {
postApi.delete(categSlug, id, me) map { Ok(_) }
}
2017-01-24 16:11:42 -07:00
}
2013-05-27 09:01:19 -06:00
2020-05-05 22:11:15 -06:00
def react(id: String, reaction: String, v: Boolean) =
Auth { implicit ctx => me =>
postApi.react(id, me, reaction, v) map {
_ ?? { post =>
2020-08-16 06:42:29 -06:00
Ok(views.html.forum.post.reactions(post, canReact = true))
2020-05-05 22:11:15 -06:00
}
2020-04-08 12:29:04 -06:00
}
}
2020-05-05 22:11:15 -06:00
def redirect(id: String) =
Open { implicit ctx =>
2020-09-21 01:28:28 -06:00
OptionResult(postApi.urlData(id, ctx.me)) { case lila.forum.PostUrlData(categ, topic, page, number) =>
Redirect(routes.ForumTopic.show(categ, topic, page).url + "#" + number)
2020-05-05 22:11:15 -06:00
}
2013-05-27 09:01:19 -06:00
}
}