forum post redirection url

pull/83/head
Thibault Duplessis 2013-05-27 17:01:19 +02:00
parent d40c014f5a
commit 9d376119e8
6 changed files with 22 additions and 1 deletions

View File

@ -44,4 +44,11 @@ object ForumPost extends LilaController with ForumController {
me
postApi.delete(id, me)
}
def redirect(id: String) = Open { implicit ctx
OptionResult(postApi.urlData(id, ctx.troll)) {
case lila.forum.PostUrlData(categ, topic, page, number)
Redirect(routes.ForumTopic.show(categ, topic, page).url + "#" + number)
}
}
}

View File

@ -151,6 +151,7 @@ GET /forum/:categSlug/:slug controllers.ForumTopic.show(categSlug: St
POST /forum/:categSlug/:slug/close controllers.ForumTopic.close(categSlug: String, slug: String)
POST /forum/:categSlug/:slug/new controllers.ForumPost.create(categSlug: String, slug: String, page: Int ?= 1)
POST /forum/delete/post/:id controllers.ForumPost.delete(id: String)
GET /forum/redirect/post/:id controllers.ForumPost.redirect(id: String)
# Message
GET /inbox controllers.Message.inbox(page: Int ?= 1)

View File

@ -47,6 +47,14 @@ final class PostApi(
}) inject post
}
def urlData(postId: String, troll: Boolean): Fu[Option[PostUrlData]] = get(postId) flatMap {
case Some((topic, post)) if (!troll && post.troll) => fuccess(none[PostUrlData])
case Some((topic, post)) => PostRepo(troll).countBeforeNumber(topic.id, post.number) map { nb =>
val page = nb / maxPerPage + 1
PostUrlData(topic.categId, topic.slug, page, post.number).some
}
}
def get(postId: String): Fu[Option[(Topic, Post)]] = for {
post optionT($find.byId[Post](postId))
topic optionT($find.byId[Topic](post.topicId))

View File

@ -20,6 +20,9 @@ sealed abstract class PostRepo(troll: Boolean) {
Json.obj("troll" -> false)
)
def countBeforeNumber(topicId: String, number: Int): Fu[Int] =
$count(selectTopic(topicId) ++ Json.obj("number" -> $lt(number)))
def isFirstPost(topicId: String, postId: String): Fu[Boolean] =
$primitive.one(
selectTopic(topicId),

View File

@ -42,3 +42,5 @@ case class PostView(
}
case class PostLiteView(post: Post, topic: Topic, topicLastPage: Int)
case class PostUrlData(categ: String, topic: String, page: Int, number: Int)

View File

@ -33,7 +33,7 @@ private[forumSearch] object Post {
def from(view: PostLiteView): JsObject = Json.obj(
body -> view.post.text,
topic -> view.topic.name,
author -> ~(view.post.userId orElse view.post.author),
author -> ~(view.post.userId orElse view.post.author map (_.toLowerCase)),
topicId -> view.topic.id,
staff -> view.post.isStaff,
troll -> view.post.troll