lila/app/controllers/Relation.scala

121 lines
4.1 KiB
Scala
Raw Normal View History

2013-05-23 07:38:55 -06:00
package controllers
2015-05-29 05:12:15 -06:00
import play.api.libs.json.Json
2013-05-24 15:55:14 -06:00
import play.api.mvc._
2014-06-01 15:22:17 -06:00
import play.twirl.api.Html
2013-05-24 15:55:14 -06:00
2014-04-17 03:34:55 -06:00
import lila.api.Context
2013-05-23 07:38:55 -06:00
import lila.app._
2014-05-03 01:39:20 -06:00
import lila.relation.Related
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-04-17 03:34:55 -06:00
private def renderActions(userId: String, mini: Boolean)(implicit ctx: Context) =
(ctx.userId ?? { env.api.relation(_, userId) }) zip
2014-05-03 01:39:20 -06:00
(ctx.isAuth ?? { Env.pref.api followable userId }) zip
(ctx.userId ?? { env.api.blocks(userId, _) }) flatMap {
case ((relation, followable), blocked) => negotiate(
html = fuccess(Ok(mini.fold(
html.relation.mini(userId, blocked = blocked, followable = followable, relation = relation),
html.relation.actions(userId, relation = relation, blocked = blocked, followable = followable)
))),
api = _ => fuccess(Ok(Json.obj(
"followable" -> followable,
"following" -> relation.exists(true ==),
"blocking" -> relation.exists(false ==)
)))
)
}
2014-04-17 03:34:55 -06:00
def follow(userId: String) = Auth { implicit ctx =>
me =>
env.api.follow(me.id, userId).nevermind >> renderActions(userId, getBool("mini"))
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-04-17 03:34:55 -06:00
env.api.unfollow(me.id, userId).nevermind >> renderActions(userId, getBool("mini"))
2013-05-23 07:38:55 -06:00
}
2014-04-17 03:34:55 -06:00
def block(userId: String) = Auth { implicit ctx =>
me =>
env.api.block(me.id, userId).nevermind >> renderActions(userId, getBool("mini"))
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-04-17 03:34:55 -06:00
env.api.unblock(me.id, userId).nevermind >> renderActions(userId, getBool("mini"))
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 followship flatMap { rels =>
2014-05-03 01:39:20 -06:00
env.api nbFollowers user.id map { followers =>
html.relation.following(user, rels, followers)
2014-04-17 03:34:55 -06:00
}
}
}
}
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 followship flatMap { rels =>
2014-05-03 01:39:20 -06:00
env.api nbFollowing user.id map { following =>
html.relation.followers(user, rels, following)
2014-04-17 03:34:55 -06:00
}
}
}
}
2015-01-15 17:34:25 -07:00
def blocks = Auth { implicit ctx =>
me =>
env.api.blocking(me.id) flatMap followship map { rels =>
2015-01-15 17:34:25 -07:00
html.relation.blocks(me, rels)
}
}
private def followship(userIds: Set[String])(implicit ctx: Context): Fu[List[Related]] =
2014-05-03 01:39:20 -06:00
UserRepo byIds userIds flatMap { users =>
(ctx.isAuth ?? { Env.pref.api.followableIds(users map (_.id)) }) flatMap { followables =>
users.map { u =>
ctx.userId ?? { env.api.relation(_, u.id) } map { rel =>
lila.relation.Related(u, 0, followables(u.id), rel)
}
}.sequenceFu
}
}
2014-02-17 02:12:19 -07:00
def suggest(username: String) = Open { implicit ctx =>
2015-05-29 05:12:15 -06:00
OptionFuResult(UserRepo named username) { user =>
2014-05-03 01:39:20 -06:00
lila.game.BestOpponents(user.id, 50) flatMap { opponents =>
Env.pref.api.followableIds(opponents map (_._1.id)) zip
env.api.onlinePopularUsers(20) flatMap {
case (followables, popular) =>
popular.filterNot(user ==).foldLeft(opponents filter {
case (u, _) => followables contains u.id
}) {
case (xs, x) => xs.exists(_._1 == x).fold(xs, xs :+ (x, 0))
}.map {
case (u, nb) => env.api.relation(user.id, u.id) map {
lila.relation.Related(u, nb, true, _)
}
2015-05-29 05:12:15 -06:00
}.sequenceFu flatMap { rels =>
negotiate(
html = fuccess(Ok(html.relation.suggest(user, rels))),
api = _ => fuccess {
implicit val userWrites = play.api.libs.json.Writes[UserModel] { Env.user.jsonView(_, true) }
Ok(Json.obj(
"user" -> user,
"suggested" -> play.api.libs.json.JsArray(rels.map(_.toJson))))
})
2014-05-03 01:39:20 -06:00
}
}
}
}
}
2013-05-23 07:38:55 -06:00
}