lila/app/controllers/Relation.scala

75 lines
2.2 KiB
Scala
Raw Normal View History

2013-05-23 07:38:55 -06:00
package controllers
2013-05-24 15:55:14 -06:00
import play.api.mvc._
import play.api.templates.Html
2013-05-23 07:38:55 -06:00
import lila.app._
2014-02-17 02:12:19 -07:00
import lila.user.{ User => UserModel, UserRepo }
2013-05-23 07:38:55 -06:00
import views._
object Relation extends LilaController {
private def env = Env.relation
2014-02-17 02:12:19 -07:00
def follow(userId: String) = Open { implicit ctx =>
ctx.userId.fold(Ok(html.relation.actions(userId, true)).fuccess) { myId =>
2014-02-11 14:07:28 -07:00
env.api.follow(myId, userId).nevermind inject getBool("mini").fold(
html.relation.mini(userId),
html.relation.actions(userId)
)
2013-05-23 07:38:55 -06:00
}
}
2014-02-17 02:12:19 -07:00
def unfollow(userId: String) = Auth { implicit ctx =>
me =>
2014-02-11 14:07:28 -07:00
env.api.unfollow(me.id, userId).nevermind inject getBool("mini").fold(
html.relation.mini(userId),
html.relation.actions(userId)
)
2013-05-23 07:38:55 -06:00
}
2014-02-17 02:12:19 -07:00
def block(userId: String) = Open { implicit ctx =>
ctx.userId.fold(Ok(html.relation.actions(userId, true)).fuccess) { myId =>
2014-02-11 14:07:28 -07:00
env.api.block(myId, userId).nevermind inject getBool("mini").fold(
html.relation.mini(userId),
html.relation.actions(userId)
)
2013-05-23 07:38:55 -06:00
}
}
2014-02-17 02:12:19 -07:00
def unblock(userId: String) = Auth { implicit ctx =>
me =>
2014-02-11 14:07:28 -07:00
env.api.unblock(me.id, userId).nevermind inject getBool("mini").fold(
html.relation.mini(userId),
html.relation.actions(userId)
)
2013-05-23 07:38:55 -06:00
}
2014-02-17 02:12:19 -07:00
def following(username: String) = Open { implicit ctx =>
OptionFuOk(UserRepo named username) { user =>
env.api.following(user.id) flatMap UserRepo.byIds map { users =>
2013-05-24 15:55:14 -06:00
html.relation.following(user, users.sorted)
}
}
}
2014-02-17 02:12:19 -07:00
def followers(username: String) = Open { implicit ctx =>
OptionFuOk(UserRepo named username) { user =>
env.api.followers(user.id) flatMap UserRepo.byIds map { users =>
2013-05-24 15:55:14 -06:00
html.relation.followers(user, users.sorted)
}
}
}
2014-02-17 02:12:19 -07:00
def suggest(username: String) = Open { implicit ctx =>
OptionFuOk(UserRepo named username) { user =>
lila.game.BestOpponents(user.id, 50) zip
env.api.onlinePopularUsers(20) map {
2014-02-17 02:12:19 -07:00
case (opponents, popular) => popular.filterNot(user ==).foldLeft(opponents) {
case (xs, x) => xs.exists(_._1 == x).fold(xs, xs :+ (x, 0))
} |> { html.relation.suggest(user, _) }
}
}
}
2013-05-23 07:38:55 -06:00
}