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
|
|
|
|
2014-04-17 03:34:55 -06:00
|
|
|
import lila.api.Context
|
2013-05-23 07:38:55 -06:00
|
|
|
import lila.app._
|
2016-02-22 20:54:37 -07:00
|
|
|
import lila.common.paginator.{ Paginator, AdapterLike, PaginatorJson }
|
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) =
|
2016-01-23 14:33:41 -07:00
|
|
|
(ctx.userId ?? { env.api.fetchRelation(_, userId) }) zip
|
2014-05-03 01:39:20 -06:00
|
|
|
(ctx.isAuth ?? { Env.pref.api followable userId }) zip
|
2016-01-23 14:33:41 -07:00
|
|
|
(ctx.userId ?? { env.api.fetchBlocks(userId, _) }) flatMap {
|
2017-02-16 16:37:22 -07:00
|
|
|
case relation ~ followable ~ blocked => negotiate(
|
2015-05-04 16:28:51 -06:00
|
|
|
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,
|
2016-01-23 14:33:41 -07:00
|
|
|
"following" -> relation.contains(true),
|
|
|
|
"blocking" -> relation.contains(false)
|
2015-05-04 16:28:51 -06:00
|
|
|
)))
|
|
|
|
)
|
|
|
|
}
|
2014-04-17 03:34:55 -06:00
|
|
|
|
2017-02-14 08:34:07 -07:00
|
|
|
def follow(userId: String) = Auth { implicit ctx => me =>
|
|
|
|
env.api.follow(me.id, UserModel normalize userId).nevermind >> renderActions(userId, getBool("mini"))
|
2013-05-23 07:38:55 -06:00
|
|
|
}
|
|
|
|
|
2017-02-14 08:34:07 -07:00
|
|
|
def unfollow(userId: String) = Auth { implicit ctx => me =>
|
|
|
|
env.api.unfollow(me.id, UserModel normalize userId).nevermind >> renderActions(userId, getBool("mini"))
|
2013-05-23 07:38:55 -06:00
|
|
|
}
|
|
|
|
|
2017-02-14 08:34:07 -07:00
|
|
|
def block(userId: String) = Auth { implicit ctx => me =>
|
|
|
|
env.api.block(me.id, UserModel normalize userId).nevermind >> renderActions(userId, getBool("mini"))
|
2013-05-23 07:38:55 -06:00
|
|
|
}
|
|
|
|
|
2017-02-14 08:34:07 -07:00
|
|
|
def unblock(userId: String) = Auth { implicit ctx => me =>
|
|
|
|
env.api.unblock(me.id, UserModel normalize userId).nevermind >> renderActions(userId, getBool("mini"))
|
2013-05-23 07:38:55 -06:00
|
|
|
}
|
2013-05-23 10:59:46 -06:00
|
|
|
|
2016-01-23 13:04:46 -07:00
|
|
|
def following(username: String, page: Int) = Open { implicit ctx =>
|
2016-03-05 20:29:39 -07:00
|
|
|
Reasonable(page, 20) {
|
|
|
|
OptionFuResult(UserRepo named username) { user =>
|
|
|
|
RelatedPager(env.api.followingPaginatorAdapter(user.id), page) flatMap { pag =>
|
|
|
|
negotiate(
|
|
|
|
html = env.api countFollowers user.id map { nbFollowers =>
|
2017-08-23 17:56:39 -06:00
|
|
|
Ok(html.relation.following(user, pag, nbFollowers))
|
|
|
|
},
|
2017-02-14 08:34:07 -07:00
|
|
|
api = _ => Ok(jsonRelatedPaginator(pag)).fuccess
|
|
|
|
)
|
2016-03-05 20:29:39 -07:00
|
|
|
}
|
2013-05-23 10:59:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 13:04:46 -07:00
|
|
|
def followers(username: String, page: Int) = Open { implicit ctx =>
|
2016-03-05 20:29:39 -07:00
|
|
|
Reasonable(page, 20) {
|
|
|
|
OptionFuResult(UserRepo named username) { user =>
|
|
|
|
RelatedPager(env.api.followersPaginatorAdapter(user.id), page) flatMap { pag =>
|
|
|
|
negotiate(
|
|
|
|
html = env.api countFollowing user.id map { nbFollowing =>
|
2017-08-23 17:56:39 -06:00
|
|
|
Ok(html.relation.followers(user, pag, nbFollowing))
|
|
|
|
},
|
2017-02-14 08:34:07 -07:00
|
|
|
api = _ => Ok(jsonRelatedPaginator(pag)).fuccess
|
|
|
|
)
|
2016-03-05 20:29:39 -07:00
|
|
|
}
|
2013-05-23 10:59:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-01 05:45:52 -06:00
|
|
|
|
2016-02-22 20:54:37 -07:00
|
|
|
private def jsonRelatedPaginator(pag: Paginator[Related]) = {
|
|
|
|
import lila.user.JsonView.nameWrites
|
|
|
|
import lila.relation.JsonView.relatedWrites
|
2016-03-05 20:35:52 -07:00
|
|
|
Json.obj("paginator" -> PaginatorJson(pag.mapResults { r =>
|
2016-03-05 20:50:34 -07:00
|
|
|
relatedWrites.writes(r) ++ Json.obj(
|
|
|
|
"perfs" -> r.user.perfs.bestPerfType.map { best =>
|
2016-04-17 01:44:37 -06:00
|
|
|
lila.user.JsonView.perfs(r.user, best.some)
|
2017-02-14 08:34:07 -07:00
|
|
|
}
|
2017-07-08 05:58:31 -06:00
|
|
|
).add("online" -> Env.user.isOnline(r.user.id))
|
2016-03-05 20:35:52 -07:00
|
|
|
}))
|
2016-02-22 20:54:37 -07:00
|
|
|
}
|
|
|
|
|
2017-02-14 08:34:07 -07:00
|
|
|
def blocks(page: Int) = Auth { implicit ctx => me =>
|
|
|
|
Reasonable(page, 20) {
|
|
|
|
RelatedPager(env.api.blockingPaginatorAdapter(me.id), page) map { pag =>
|
|
|
|
html.relation.blocks(me, pag)
|
2015-01-15 17:34:25 -07:00
|
|
|
}
|
2017-02-14 08:34:07 -07:00
|
|
|
}
|
2015-01-15 17:34:25 -07:00
|
|
|
}
|
|
|
|
|
2016-01-23 13:04:46 -07:00
|
|
|
private def RelatedPager(adapter: AdapterLike[String], page: Int)(implicit ctx: Context) = Paginator(
|
|
|
|
adapter = adapter mapFutureList followship,
|
|
|
|
currentPage = page,
|
2017-12-29 08:05:11 -07:00
|
|
|
maxPerPage = lila.common.MaxPerPage(30)
|
2017-02-14 08:34:07 -07:00
|
|
|
)
|
2016-01-23 13:04:46 -07:00
|
|
|
|
|
|
|
private def followship(userIds: Seq[String])(implicit ctx: Context): Fu[List[Related]] =
|
2017-03-25 05:23:01 -06:00
|
|
|
UserRepo usersFromSecondary userIds.map(UserModel.normalize) flatMap { users =>
|
2014-05-03 01:39:20 -06:00
|
|
|
(ctx.isAuth ?? { Env.pref.api.followableIds(users map (_.id)) }) flatMap { followables =>
|
|
|
|
users.map { u =>
|
2016-01-23 14:33:41 -07:00
|
|
|
ctx.userId ?? { env.api.fetchRelation(_, u.id) } map { rel =>
|
2016-02-28 19:06:43 -07:00
|
|
|
lila.relation.Related(u, none, followables(u.id), rel)
|
2014-05-03 01:39:20 -06:00
|
|
|
}
|
|
|
|
}.sequenceFu
|
|
|
|
}
|
|
|
|
}
|
2013-05-23 07:38:55 -06:00
|
|
|
}
|