From cb0f665f33e2bba9b0748f0c493017ab33d0d416 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Mon, 20 May 2013 16:29:12 -0300 Subject: [PATCH] improve friendship creation/revokation --- app/views/friend/button.scala.html | 13 ++++++++----- modules/friend/src/main/Cached.scala | 5 +++++ modules/friend/src/main/FriendApi.scala | 8 ++------ modules/friend/src/main/FriendRepo.scala | 2 +- modules/friend/src/main/QuickStatus.scala | 4 ++-- public/javascripts/big.js | 8 ++++---- todo | 1 + 7 files changed, 23 insertions(+), 18 deletions(-) diff --git a/app/views/friend/button.scala.html b/app/views/friend/button.scala.html index 8ad708deed..451fc1739a 100644 --- a/app/views/friend/button.scala.html +++ b/app/views/friend/button.scala.html @@ -1,18 +1,21 @@ @(userId: String)(implicit ctx: Context) +
@friendStatus(userId).map { status => @if(status.friends) { -@trans.friends() +@trans.friends() } else { @if(status.pending) { -@trans.pendingRequest +@trans.pendingRequest() +@trans.cancel() } else { @if(status.requested) { -@trans.accept() -@trans.decline() +@trans.accept() +@trans.decline() } else { -@trans.addFriend() +@trans.addFriend() } } } } +
diff --git a/modules/friend/src/main/Cached.scala b/modules/friend/src/main/Cached.scala index 183893c202..033a34fb28 100644 --- a/modules/friend/src/main/Cached.scala +++ b/modules/friend/src/main/Cached.scala @@ -11,4 +11,9 @@ private[friend] final class Cached { val requestIds = AsyncCache(RequestRepo.requestedUserIds, maxCapacity = 5000) val nbRequests = AsyncCache(RequestRepo.countByFriendId, maxCapacity = 5000) + + private[friend] def invalidate(userId: ID): Funit = + friendIds.remove(userId) >> + requestIds.remove(userId) >> + nbRequests.remove(userId) } diff --git a/modules/friend/src/main/FriendApi.scala b/modules/friend/src/main/FriendApi.scala index 516a468b0d..ad8b7b421c 100644 --- a/modules/friend/src/main/FriendApi.scala +++ b/modules/friend/src/main/FriendApi.scala @@ -23,7 +23,7 @@ final class FriendApi(cached: Cached) { case ((_, true), _) ⇒ QuickStatus(u1, u2, false, true.some) case ((_, _), true) ⇒ QuickStatus(u1, u2, false, false.some) case _ ⇒ QuickStatus(u1, u2, false, none) - } + } def friendsOf(userId: ID): Fu[List[User]] = cached friendIds userId flatMap UserRepo.byIds map { _ sortBy (_.id) } @@ -64,9 +64,5 @@ final class FriendApi(cached: Cached) { accept ?? makeFriends(request.user, request.friend) private def invalidate(userIds: String*): Funit = - userIds.toList.map(userId ⇒ - cached.friendIds.remove(userId) >> - cached.requestIds.remove(userId) >> - cached.nbRequests.remove(userId) - ).sequence.void + userIds.toList.map(cached.invalidate).sequence.void } diff --git a/modules/friend/src/main/FriendRepo.scala b/modules/friend/src/main/FriendRepo.scala index 64a9a91dce..bf0677b6ae 100644 --- a/modules/friend/src/main/FriendRepo.scala +++ b/modules/friend/src/main/FriendRepo.scala @@ -18,7 +18,7 @@ private[friend] object FriendRepo { _.flatten filterNot (userId ==) } - def add(u1: ID, u2: ID): Funit = $insert(Friend.make(u1, u2)) + def add(u1: ID, u2: ID): Funit = $insert(Friend.make(u1, u2).pp) def remove(u1: ID, u2: ID): Funit = $remove byId Friend.makeId(u1, u2) } diff --git a/modules/friend/src/main/QuickStatus.scala b/modules/friend/src/main/QuickStatus.scala index 77d9b8ae81..30c4bea003 100644 --- a/modules/friend/src/main/QuickStatus.scala +++ b/modules/friend/src/main/QuickStatus.scala @@ -6,7 +6,7 @@ case class QuickStatus( friends: Boolean, request: Option[Boolean]) { - def requested = ~request + def pending = request == Some(true) - def pending = request == Some(false) + def requested = request == Some(false) } diff --git a/public/javascripts/big.js b/public/javascripts/big.js index e31807ab53..3fe13c0877 100644 --- a/public/javascripts/big.js +++ b/public/javascripts/big.js @@ -309,12 +309,12 @@ var lichess_sri = Math.random().toString(36).substring(5); // 8 chars $('#friend_box').friends(); - $('#lichess').on('click', 'a.friend_button', function() { - var $button = $(this); + $('#lichess').on('click', 'div.friend_button a', function() { + var $a = $(this).text('...'); $.ajax({ - url: $button.attr('href'), + url: $a.attr('href'), type: 'post', - success: function(html) { $button.replaceWith(html); } + success: function(html) { $a.parent().replaceWith(html); } }); return false; }); diff --git a/todo b/todo index 4dcddfefe4..4984c6c306 100644 --- a/todo +++ b/todo @@ -65,6 +65,7 @@ from MoralIntentions email: - Several options of how points should be given to players (for example: Score * Sonneborn-Berger * Tournament Performance Rating * Percentage Score [for example: 0.64] / 1000) and therefore also several Leaderboards. - The possibility of team matches (with solutions for players who are in several teams), where the team leader has to choose the board order, where the team results are shown and where the team player (who has played all of the games of the match) with the highest Percentage Score gets crowned Player of the Match of team "A". safari has high bounce rate +send message to sockets when a friendship is created or revoked DEPLOY p21 ----------