store forum post language; filter recent posts by user languages

This commit is contained in:
Thibault Duplessis 2014-02-26 23:05:15 +01:00
parent 5fb9a093c9
commit fb37bea3a8
8 changed files with 76 additions and 53 deletions

View file

@ -51,6 +51,7 @@ private[forum] final class CategApi(env: Env) {
text = "Welcome to the %s forum!\nOnly members of the team can post here, but everybody can read." format name,
number = 1,
troll = false,
lang = "en".some,
categId = categ.id)
$insert(categ) >>
$insert(post) >>

View file

@ -3,6 +3,7 @@ package lila.forum
import akka.actor._
import com.typesafe.config.Config
import lila.common.DetectLanguage
import lila.common.PimpedConfig._
import lila.hub.actorApi.forum._
import lila.mod.ModlogApi
@ -12,6 +13,7 @@ final class Env(
db: lila.db.Env,
modLog: ModlogApi,
hub: lila.hub.Env,
detectLanguage: DetectLanguage,
system: ActorSystem) {
private val settings = new {
@ -33,14 +35,16 @@ final class Env(
indexer = hub.actor.forumIndexer,
maxPerPage = TopicMaxPerPage,
modLog = modLog,
timeline = hub.actor.timeline)
timeline = hub.actor.timeline,
detectLanguage = detectLanguage)
lazy val postApi = new PostApi(
env = this,
indexer = hub.actor.forumIndexer,
maxPerPage = PostMaxPerPage,
modLog = modLog,
timeline = hub.actor.timeline)
timeline = hub.actor.timeline,
detectLanguage = detectLanguage)
lazy val forms = new DataForm(hub.actor.captcher)
lazy val recent = new Recent(postApi, RecentTtl, RecentNb)
@ -77,5 +81,6 @@ object Env {
db = lila.db.Env.current,
modLog = lila.mod.Env.current.logApi,
hub = lila.hub.Env.current,
detectLanguage = DetectLanguage(lila.common.PlayApp loadConfig "detectlanguage"),
system = lila.common.PlayApp.system)
}

View file

@ -15,6 +15,7 @@ case class Post(
text: String,
number: Int,
troll: Boolean,
lang: Option[String],
createdAt: DateTime) {
def showAuthor = (author map (_.trim) filter ("" !=)) | User.anonymous
@ -38,6 +39,7 @@ object Post {
ip: Option[String],
text: String,
number: Int,
lang: Option[String],
troll: Boolean): Post = Post(
id = Random nextStringUppercase idSize,
topicId = topicId,
@ -46,6 +48,7 @@ object Post {
ip = ip,
text = text,
number = number,
lang = lang,
troll = troll,
createdAt = DateTime.now,
categId = categId)

View file

@ -19,32 +19,35 @@ final class PostApi(
indexer: ActorSelection,
maxPerPage: Int,
modLog: ModlogApi,
timeline: ActorSelection) {
timeline: ActorSelection,
detectLanguage: lila.common.DetectLanguage) {
def makePost(
categ: Categ,
topic: Topic,
data: DataForm.PostData)(implicit ctx: UserContext): Fu[Post] =
lastNumberOf(topic) flatMap { number =>
val post = Post.make(
topicId = topic.id,
author = data.author,
userId = ctx.me map (_.id),
ip = ctx.req.remoteAddress.some,
text = data.text,
number = number + 1,
troll = ctx.troll,
categId = categ.id)
$insert(post) >>
$update(topic withPost post) >>
$update(categ withTopic post) >>-
(indexer ! InsertPost(post)) >>
(env.recent.invalidate inject post) >>-
((ctx.userId ifFalse post.troll) ?? { userId =>
timeline ! Propagate(ForumPost(userId, topic.name, post.id)).|>(prop =>
post.isStaff.fold(prop.toStaffFriendsOf(userId), prop.toFriendsOf(userId))
)
}) inject post
lastNumberOf(topic) zip detectLanguage(data.text) flatMap {
case (number, lang) =>
val post = Post.make(
topicId = topic.id,
author = data.author,
userId = ctx.me map (_.id),
ip = ctx.req.remoteAddress.some,
text = data.text,
number = number + 1,
lang = lang map (_.language),
troll = ctx.troll,
categId = categ.id)
$insert(post) >>
$update(topic withPost post) >>
$update(categ withTopic post) >>-
(indexer ! InsertPost(post)) >>
(env.recent.invalidate inject post) >>-
((ctx.userId ifFalse post.troll) ?? { userId =>
timeline ! Propagate(ForumPost(userId, topic.name, post.id)).|>(prop =>
post.isStaff.fold(prop.toStaffFriendsOf(userId), prop.toFriendsOf(userId))
)
}) inject post
}
def urlData(postId: String, troll: Boolean): Fu[Option[PostUrlData]] = get(postId) flatMap {

View file

@ -39,8 +39,8 @@ sealed abstract class PostRepo(troll: Boolean) {
def lastByTopics(topics: List[String]): Fu[Option[Post]] =
$find.one($query(selectTopics(topics)) sort $sort.createdDesc)
def recentInCategs(nb: Int)(categIds: List[String]): Fu[List[Post]] =
$find($query(selectCategs(categIds)) sort $sort.createdDesc, nb)
def recentInCategs(nb: Int)(categIds: List[String], langs: List[String]): Fu[List[Post]] =
$find($query(selectCategs(categIds) ++ selectLangs(langs) pp) sort $sort.createdDesc, nb)
def removeByTopic(topicId: String): Fu[Unit] =
$remove(selectTopic(topicId))
@ -51,5 +51,7 @@ sealed abstract class PostRepo(troll: Boolean) {
def selectCateg(categId: String) = Json.obj("categId" -> categId) ++ trollFilter
def selectCategs(categIds: List[String]) = Json.obj("categId" -> $in(categIds)) ++ trollFilter
def selectLangs(langs: List[String]) = Json.obj("lang" -> $in(langs))
def sortQuery = $sort.createdAsc
}

View file

@ -28,9 +28,11 @@ private[forum] final class Recent(
private def userCacheKey(user: Option[User], getTeams: GetTeams): Fu[String] =
user.map(_.id) ?? getTeams map { teams =>
user.fold("en")(_.langs.mkString(",")) :: {
(user.??(_.troll) ?? List("[troll]")) :::
(user ?? MasterGranter(Permission.StaffForum)).fold(staffCategIds, publicCategIds) :::
(teams map teamSlug)
}
} map (_ mkString ";")
private lazy val publicCategIds =
@ -40,9 +42,11 @@ private[forum] final class Recent(
private val cache: Cache[List[PostLiteView]] = LruCache(timeToLive = ttl)
private def parseLangs(langStr: String) = langStr.split(",").toList
private def fetch(key: String): Fu[List[PostLiteView]] =
(key.split(";").toList match {
case "[troll]" :: categs => PostRepoTroll.recentInCategs(nb)(categs)
case categs => PostRepo.recentInCategs(nb)(categs)
case langs :: "[troll]" :: categs => PostRepoTroll.recentInCategs(nb)(categs, parseLangs(langs))
case langs :: categs => PostRepo.recentInCategs(nb)(categs, parseLangs(langs))
}) flatMap postApi.liteViews
}

View file

@ -17,7 +17,8 @@ private[forum] final class TopicApi(
indexer: ActorSelection,
maxPerPage: Int,
modLog: lila.mod.ModlogApi,
timeline: ActorSelection) {
timeline: ActorSelection,
detectLanguage: lila.common.DetectLanguage) {
def show(categSlug: String, slug: String, page: Int, troll: Boolean): Fu[Option[(Categ, Topic, Paginator[Post])]] =
for {
@ -34,31 +35,33 @@ private[forum] final class TopicApi(
def makeTopic(
categ: Categ,
data: DataForm.TopicData)(implicit ctx: UserContext): Fu[Topic] =
TopicRepo.nextSlug(categ, data.name) flatMap { slug =>
val topic = Topic.make(
categId = categ.slug,
slug = slug,
name = data.name,
troll = ctx.troll)
val post = Post.make(
topicId = topic.id,
author = data.post.author,
userId = ctx.me map (_.id),
ip = ctx.isAnon option ctx.req.remoteAddress,
troll = ctx.troll,
text = data.post.text,
number = 1,
categId = categ.id)
$insert(post) >>
$insert(topic withPost post) >>
$update(categ withTopic post) >>-
(indexer ! InsertPost(post)) >>
env.recent.invalidate >>-
((ctx.userId ifFalse post.troll) ?? { userId =>
timeline ! Propagate(ForumPost(userId, topic.name, post.id)).|>(prop =>
post.isStaff.fold(prop.toStaffFriendsOf(userId), prop.toFriendsOf(userId))
)
}) inject topic
TopicRepo.nextSlug(categ, data.name) zip detectLanguage(data.post.text) flatMap {
case (slug, lang) =>
val topic = Topic.make(
categId = categ.slug,
slug = slug,
name = data.name,
troll = ctx.troll)
val post = Post.make(
topicId = topic.id,
author = data.post.author,
userId = ctx.me map (_.id),
ip = ctx.isAnon option ctx.req.remoteAddress,
troll = ctx.troll,
text = data.post.text,
lang = lang map (_.language),
number = 1,
categId = categ.id)
$insert(post) >>
$insert(topic withPost post) >>
$update(categ withTopic post) >>-
(indexer ! InsertPost(post)) >>
env.recent.invalidate >>-
((ctx.userId ifFalse post.troll) ?? { userId =>
timeline ! Propagate(ForumPost(userId, topic.name, post.id)).|>(prop =>
post.isStaff.fold(prop.toStaffFriendsOf(userId), prop.toFriendsOf(userId))
)
}) inject topic
}
def paginator(categ: Categ, page: Int, troll: Boolean): Fu[Paginator[TopicView]] = Paginator(

View file

@ -31,6 +31,8 @@ case class User(
override def toString = s"User $username games:${count.game} rating:$rating troll:$troll engine:$engine"
def langs = ("en" :: lang.toList).distinct.sorted
def compare(other: User) = id compare other.id
def noTroll = !troll