diff --git a/bin/mongodb/play21.js b/bin/mongodb/play21.js index 29541381fb..fb8bdb7085 100644 --- a/bin/mongodb/play21.js +++ b/bin/mongodb/play21.js @@ -71,8 +71,6 @@ print('create relation collection'); db.createCollection('relation') db.relation.ensureIndex({u1:1}) db.relation.ensureIndex({u2:1}) -db.relation.ensureIndex({u1:1, f:1}) -db.relation.ensureIndex({u2:1, f:1}) // print("Reset lobby_room"); // db.lobby_room.drop(); diff --git a/modules/common/src/main/PimpedJson.scala b/modules/common/src/main/PimpedJson.scala index af5e1b9460..30262ab450 100644 --- a/modules/common/src/main/PimpedJson.scala +++ b/modules/common/src/main/PimpedJson.scala @@ -17,6 +17,9 @@ trait PimpedJson { def long(key: String): Option[Long] = (js \ key).asOpt[Long] + def boolean(key: String): Option[Boolean] = + (js \ key).asOpt[Boolean] + def obj(key: String): Option[JsObject] = (js \ key).asOpt[JsObject] diff --git a/modules/relation/src/main/RelationRepo.scala b/modules/relation/src/main/RelationRepo.scala index 79cfb4c300..d2b6892d6c 100644 --- a/modules/relation/src/main/RelationRepo.scala +++ b/modules/relation/src/main/RelationRepo.scala @@ -3,6 +3,7 @@ package lila.relation import lila.db.Implicits._ import lila.db.api._ import tube.relationTube +import lila.common.PimpedJson._ import play.api.libs.json._ @@ -19,11 +20,15 @@ private[relation] object RelationRepo { def blockers(userId: ID) = relaters(userId, Block) def blocking(userId: ID) = relating(userId, Block) - private def relaters(userId: ID, relation: Boolean): Fu[Set[ID]] = - $primitive(Json.obj("u2" -> userId, "r" -> relation), "u1")(_.asOpt[ID]) map (_.toSet) + private def relaters(userId: ID, relation: Relation): Fu[Set[ID]] = + $projection(Json.obj("u2" -> userId), Seq("u1", "f")) { obj ⇒ + obj str "u1" map { _ -> ~(obj boolean "f") } + } map (_.filter(_._2 == relation).map(_._1).toSet) - private def relating(userId: ID, relation: Boolean): Fu[Set[ID]] = - $primitive(Json.obj("u1" -> userId, "r" -> relation), "u2")(_.asOpt[ID]) map (_.toSet) + private def relating(userId: ID, relation: Relation): Fu[Set[ID]] = + $projection(Json.obj("u1" -> userId), Seq("u2", "f")) { obj ⇒ + obj str "u2" map { _ -> ~(obj boolean "f") } + } map (_.filter(_._2 == relation).map(_._1).toSet) def follow(u1: ID, u2: ID): Funit = save(u1, u2, Follow) def unfollow(u1: ID, u2: ID): Funit = remove(u1, u2)