rewrite friend api
This commit is contained in:
parent
1b5e6acc3a
commit
f6c0b4e4dc
|
@ -1,42 +1,43 @@
|
|||
package lila.friend
|
||||
|
||||
import lila.user.{ User, UserRepo }
|
||||
import lila.user.tube.userTube
|
||||
import tube.{ friendTube, requestTube }
|
||||
import lila.db.api._
|
||||
import lila.db.Implicits._
|
||||
|
||||
import org.scala_tools.time.Imports._
|
||||
|
||||
import lila.user.{ User, UserRepo }
|
||||
final class FriendApi(cached: Cached) {
|
||||
|
||||
// final class FriendApi( cached: Cached) {
|
||||
def areFriends(u1: String, u2: String): Fu[Boolean] =
|
||||
cached friendIds u1 map (_ contains u2)
|
||||
|
||||
// def areFriends(u1: String, u2: String) = friendIds(u1) contains u2
|
||||
def requestable(userId: String, friendId: String): Fu[Boolean] =
|
||||
RequestRepo.exists(userId, friendId) map (!_)
|
||||
|
||||
// val friendIds = cached friendIds _
|
||||
def createRequest(friend: User, setup: RequestSetup, user: User): Funit =
|
||||
requestable(friend.id, user.id) flatMap {
|
||||
_ ?? $insert(Request.make(user = user.id, friend = friend.id, message = setup.message)) >>
|
||||
(cached.nbRequests remove friend.id)
|
||||
}
|
||||
|
||||
// def requestable(userId: String, friendId: String): IO[Boolean] =
|
||||
// requestRepo.exists(userId, friendId) map (!_)
|
||||
def requestsWithUsers(userId: String): Fu[List[RequestWithUser]] = for {
|
||||
requests ← RequestRepo findByFriendId userId
|
||||
users ← $find.byOrderedIds[User](requests.map(_.user))
|
||||
} yield requests zip users map {
|
||||
case (request, user) ⇒ RequestWithUser(request, user)
|
||||
}
|
||||
|
||||
// def createRequest(friend: User, setup: RequestSetup, user: User): IO[Unit] = for {
|
||||
// able ← requestable(friend.id, user.id)
|
||||
// request = Request(user = user.id, friend = friend.id, message = setup.message)
|
||||
// _ ← requestRepo.add(request) >> io(cached invalidateNbRequests friend.id) doIf able
|
||||
// } yield ()
|
||||
def processRequest(userId: String, request: Request, accept: Boolean): Funit =
|
||||
$remove.byId[Request](request.id) >>
|
||||
(cached.nbRequests remove userId) >>
|
||||
accept ?? $find.byId[User](request.user) flatten "requester not found" flatMap { requester ⇒
|
||||
FriendRepo.add(requester.id, userId) >>
|
||||
(cached.friendIds remove requester.id) >>
|
||||
(cached.friendIds remove userId)
|
||||
}
|
||||
|
||||
// def requestsWithUsers(userId: String): IO[List[RequestWithUser]] = for {
|
||||
// requests ← requestRepo findByFriendId userId
|
||||
// users ← userRepo byOrderedIds requests.map(_.user)
|
||||
// } yield requests zip users map {
|
||||
// case (request, user) ⇒ RequestWithUser(request, user)
|
||||
// }
|
||||
|
||||
// def processRequest(userId: String, request: Request, accept: Boolean): IO[Unit] = for {
|
||||
// _ ← requestRepo remove request.id
|
||||
// _ ← io(cached invalidateNbRequests userId)
|
||||
// requesterOption ← userRepo byId request.user
|
||||
// _ ← ~requesterOption.map(requester ⇒
|
||||
// friendRepo.add(requester.id, userId) >>
|
||||
// io(cached.invalidateFriendIds(requester.id)) >>
|
||||
// io(cached.invalidateFriendIds(userId)) doIf accept
|
||||
// )
|
||||
// } yield ()
|
||||
|
||||
// def friendsOf(userId: String): IO[List[User]] =
|
||||
// userRepo byIds friendIds(userId) map { _ sortBy (_.id) }
|
||||
// }
|
||||
def friendsOf(userId: String): Fu[List[User]] =
|
||||
cached friendIds userId flatMap UserRepo.byIds map { _ sortBy (_.id) }
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue