From ee948f2447a0127ef2d45d1a1cb31ea4b8929bae Mon Sep 17 00:00:00 2001 From: Keith Holliday Date: Wed, 11 May 2016 12:34:29 -0500 Subject: [PATCH 1/5] Updated party query and create --- website/public/js/controllers/groupsCtrl.js | 2 +- website/public/js/controllers/headerCtrl.js | 20 ++++++++++++-------- website/public/js/controllers/partyCtrl.js | 20 ++++++-------------- website/public/js/controllers/rootCtrl.js | 8 +++++--- website/public/js/services/groupServices.js | 3 ++- 5 files changed, 26 insertions(+), 27 deletions(-) diff --git a/website/public/js/controllers/groupsCtrl.js b/website/public/js/controllers/groupsCtrl.js index 238cf75c49..da8016bee0 100644 --- a/website/public/js/controllers/groupsCtrl.js +++ b/website/public/js/controllers/groupsCtrl.js @@ -24,7 +24,7 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Shared', 'Groups', ' // Similarly, if we're dealing with the user's current party, return true. if(group.type === 'party') { - var currentParty = Groups.party(); + var currentParty = group; if(currentParty._id && currentParty._id === group._id) return true; } diff --git a/website/public/js/controllers/headerCtrl.js b/website/public/js/controllers/headerCtrl.js index 68000da3b9..390d2c92a9 100644 --- a/website/public/js/controllers/headerCtrl.js +++ b/website/public/js/controllers/headerCtrl.js @@ -8,15 +8,19 @@ habitrpg.controller("HeaderCtrl", ['$scope', 'Groups', 'User', $scope.inviteOrStartParty = Groups.inviteOrStartParty; - $scope.party = Groups.party(function(){ - var triggerResort = function() { - $scope.partyMinusSelf = resortParty(); - }; + function handlePartyResponse (party) { + $scope.party = party; - triggerResort(); - $scope.$watch('user.party.order', triggerResort); - $scope.$watch('user.party.orderAscending', triggerResort); - }); + var triggerResort = function() { + $scope.partyMinusSelf = resortParty(); + }; + + triggerResort(); + $scope.$watch('user.party.order', triggerResort); + $scope.$watch('user.party.orderAscending', triggerResort); + } + + Groups.party().then(handlePartyResponse, handlePartyResponse); function resortParty() { var result = _.sortBy( diff --git a/website/public/js/controllers/partyCtrl.js b/website/public/js/controllers/partyCtrl.js index cfac272b34..64511ee13e 100644 --- a/website/public/js/controllers/partyCtrl.js +++ b/website/public/js/controllers/partyCtrl.js @@ -8,15 +8,6 @@ habitrpg.controller("PartyCtrl", ['$rootScope','$scope','Groups','Chat','User',' $scope.type = 'party'; $scope.text = window.env.t('party'); - //@TODO: cache - Groups.Group.syncParty() - .then(function successCallback(response) { - $scope.group = response.data.data; - checkForNotifications(); - }, function errorCallback(response) { - $scope.newGroup = $scope.group = { type: 'party' }; - }); - $scope.inviteOrStartParty = Groups.inviteOrStartParty; $scope.loadWidgets = Social.loadWidgets; @@ -52,11 +43,12 @@ habitrpg.controller("PartyCtrl", ['$rootScope','$scope','Groups','Chat','User',' $scope.create = function(group) { if (!group.name) group.name = env.t('possessiveParty', {name: User.user.profile.name}); - Groups.Group.create(group, function() { - Analytics.track({'hitType':'event', 'eventCategory':'behavior', 'eventAction':'join group', 'owner':true, 'groupType':'party', 'privacy':'private'}); - Analytics.updateUser({'party.id': group.id, 'partySize': 1}); - $rootScope.hardRedirect('/#/options/groups/party'); - }); + Groups.Group.create(group) + .then(function(response) { + Analytics.track({'hitType':'event', 'eventCategory':'behavior', 'eventAction':'join group', 'owner':true, 'groupType':'party', 'privacy':'private'}); + Analytics.updateUser({'party.id': group.id, 'partySize': 1}); + $rootScope.hardRedirect('/#/options/groups/party'); + }); }; $scope.join = function (party) { diff --git a/website/public/js/controllers/rootCtrl.js b/website/public/js/controllers/rootCtrl.js index 0c841ee677..bc91436e2f 100644 --- a/website/public/js/controllers/rootCtrl.js +++ b/website/public/js/controllers/rootCtrl.js @@ -277,9 +277,11 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$ if (spell.target == 'self') { $scope.castEnd(null, 'self'); } else if (spell.target == 'party') { - var party = Groups.party(); - party = (_.isArray(party) ? party : []).concat(User.user); - $scope.castEnd(party, 'party'); + Groups.party() + .then(function (party) { + party = (_.isArray(party) ? party : []).concat(User.user); + $scope.castEnd(party, 'party'); + }); } } diff --git a/website/public/js/services/groupServices.js b/website/public/js/services/groupServices.js index 14a788c289..4bbea80ceb 100644 --- a/website/public/js/services/groupServices.js +++ b/website/public/js/services/groupServices.js @@ -110,7 +110,8 @@ angular.module('habitrpg') data.party = response.data.data; deferred.resolve(data.party); }, function (response) { - deferred.reject(response); + data.party = { type: 'party' }; + deferred.reject(data.party); }); } else { deferred.resolve(data.party); From 12eba7bbe95aa6ef1c15832a850425a58815f04c Mon Sep 17 00:00:00 2001 From: Keith Holliday Date: Wed, 11 May 2016 14:18:32 -0500 Subject: [PATCH 2/5] Ensured login callback happens after user sync --- website/public/js/services/userServices.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/website/public/js/services/userServices.js b/website/public/js/services/userServices.js index b333a12346..45c26ad5c1 100644 --- a/website/public/js/services/userServices.js +++ b/website/public/js/services/userServices.js @@ -46,7 +46,7 @@ angular.module('habitrpg') user._wrapped = false; function sync() { - $http({ + return $http({ method: "GET", url: '/api/v3/user/', }) @@ -349,10 +349,11 @@ angular.module('habitrpg') settings.auth.apiToken = token; settings.online = true; save(); - sync(); - if (cb) { - cb(); - } + sync().then(function () { + if (cb) { + cb(); + } + }); //@TODO: Do we need the timezone set? // userServices.log({}, function(){ // // If they don't have timezone, set it From eff2f04b459dbad474262e10f82f03c7fc0ff723 Mon Sep 17 00:00:00 2001 From: Keith Holliday Date: Wed, 11 May 2016 14:34:36 -0500 Subject: [PATCH 3/5] Add challenges to groups. Fixed isMemberOfGuild check --- website/public/js/app.js | 8 +++- website/public/js/controllers/groupsCtrl.js | 3 +- website/public/js/controllers/guildsCtrl.js | 9 ++-- website/public/js/controllers/partyCtrl.js | 49 +++++++++++---------- website/public/js/services/chatServices.js | 2 +- 5 files changed, 38 insertions(+), 33 deletions(-) diff --git a/website/public/js/app.js b/website/public/js/app.js index 5d685de42a..a128c2ce88 100644 --- a/website/public/js/app.js +++ b/website/public/js/app.js @@ -150,8 +150,8 @@ window.habitrpg = angular.module('habitrpg', url: '/:gid', templateUrl: 'partials/options.social.guilds.detail.html', title: env.t('titleGuilds'), - controller: ['$scope', 'Groups', 'Chat', '$stateParams', 'Members', - function($scope, Groups, Chat, $stateParams, Members){ + controller: ['$scope', 'Groups', 'Chat', '$stateParams', 'Members', 'Challenges', + function($scope, Groups, Chat, $stateParams, Members, Challenges){ Groups.Group.get($stateParams.gid) .then(function (response) { $scope.group = response.data.data; @@ -164,6 +164,10 @@ window.habitrpg = angular.module('habitrpg', .then(function (response) { $scope.group.invites = response.data.data; }); + Challenges.getGroupChallenges($scope.group._id) + .then(function (response) { + $scope.group.challenges = response.data.data; + }); }); }] }) diff --git a/website/public/js/controllers/groupsCtrl.js b/website/public/js/controllers/groupsCtrl.js index da8016bee0..cafbe3b158 100644 --- a/website/public/js/controllers/groupsCtrl.js +++ b/website/public/js/controllers/groupsCtrl.js @@ -18,8 +18,7 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Shared', 'Groups', ' // If the group is a guild, just check for an intersection with the // current user's guilds, rather than checking the members of the group. if(group.type === 'guild') { - var guilds = Groups.myGuilds(); - return _.detect(guilds, function(g) { return g._id === group._id }); + return _.detect(User.user.guilds, function(guildId) { return guildId === group._id }); } // Similarly, if we're dealing with the user's current party, return true. diff --git a/website/public/js/controllers/guildsCtrl.js b/website/public/js/controllers/guildsCtrl.js index dfedbddc7c..6555495e7e 100644 --- a/website/public/js/controllers/guildsCtrl.js +++ b/website/public/js/controllers/guildsCtrl.js @@ -3,8 +3,8 @@ habitrpg.controller("GuildsCtrl", ['$scope', 'Groups', 'User', 'Challenges', '$rootScope', '$state', '$location', '$compile', 'Analytics', function($scope, Groups, User, Challenges, $rootScope, $state, $location, $compile, Analytics) { $scope.groups = { - guilds: Groups.myGuilds(), - public: Groups.publicGuilds(), + guilds: [], + public: [], }; Groups.myGuilds() @@ -88,8 +88,9 @@ habitrpg.controller("GuildsCtrl", ['$scope', 'Groups', 'User', 'Challenges', '$r var html, title; - Challenges.Challenge.query(function(challenges) { - challenges = _.pluck(_.filter(challenges, function(c) { + Challenges.getGroupChallenges(group._id) + .then(function(response) { + var challenges = _.pluck(_.filter(response.data.data, function(c) { return c.group._id == group._id; }), '_id'); diff --git a/website/public/js/controllers/partyCtrl.js b/website/public/js/controllers/partyCtrl.js index 64511ee13e..68623f0827 100644 --- a/website/public/js/controllers/partyCtrl.js +++ b/website/public/js/controllers/partyCtrl.js @@ -84,31 +84,32 @@ habitrpg.controller("PartyCtrl", ['$rootScope','$scope','Groups','Chat','User',' title = window.env.t('leavePartyCha'); //TODO: Move this to challenge service - //@TODO: Implement this when we convert front-end challenge service - // Challenges.Challenge.query(function(challenges) { - // challenges = _.pluck(_.filter(challenges, function(c) { - // return c.group._id == group._id; - // }), '_id'); - // if (_.intersection(challenges, User.user.challenges).length > 0) { - // html = $compile( - // '' + window.env.t('removeTasks') + '
\n' + window.env.t('keepTasks') + '
\n' + window.env.t('cancel') + '
' - // )($scope); - // title = window.env.t('leavePartyCha'); - // } else { - // html = $compile( - // '' + window.env.t('confirm') + '
\n' + window.env.t('cancel') + '
' - // )($scope); - // title = window.env.t('leaveParty'); - // } + Challenges.getGroupChallenges(group._id) + .then(function(response) { + var challenges = _.pluck(_.filter(response.data.data, function(c) { + return c.group._id == group._id; + }), '_id'); - $scope.popoverEl.popover('destroy').popover({ - html: true, - placement: 'top', - trigger: 'manual', - title: title, - content: html - }).popover('show'); - // }); + if (_.intersection(challenges, User.user.challenges).length > 0) { + html = $compile( + '' + window.env.t('removeTasks') + '
\n' + window.env.t('keepTasks') + '
\n' + window.env.t('cancel') + '
' + )($scope); + title = window.env.t('leavePartyCha'); + } else { + html = $compile( + '' + window.env.t('confirm') + '
\n' + window.env.t('cancel') + '
' + )($scope); + title = window.env.t('leaveParty'); + } + + $scope.popoverEl.popover('destroy').popover({ + html: true, + placement: 'top', + trigger: 'manual', + title: title, + content: html + }).popover('show'); + }); }; $scope.clickStartQuest = function () { diff --git a/website/public/js/services/chatServices.js b/website/public/js/services/chatServices.js index e7bb663c8e..ed812e08ad 100644 --- a/website/public/js/services/chatServices.js +++ b/website/public/js/services/chatServices.js @@ -63,7 +63,7 @@ angular.module('habitrpg') } function markChatSeen (groupId) { - if (User.user.newMessages) delete User.user.newMessages[gid]; + if (User.user.newMessages) delete User.user.newMessages[groupId]; return $http({ method: 'POST', url: apiV3Prefix + '/groups/' + groupId + '/chat/seen', From 5d7ebd82a4d621aa40f893c176d1e0f09c8458d7 Mon Sep 17 00:00:00 2001 From: Keith Holliday Date: Wed, 11 May 2016 17:06:16 -0500 Subject: [PATCH 4/5] Updated party and group tests --- test/spec/controllers/groupCtrlSpec.js | 10 ++-------- test/spec/controllers/partyCtrlSpec.js | 14 +++++++------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/test/spec/controllers/groupCtrlSpec.js b/test/spec/controllers/groupCtrlSpec.js index cade1bb658..6a5819ec67 100644 --- a/test/spec/controllers/groupCtrlSpec.js +++ b/test/spec/controllers/groupCtrlSpec.js @@ -93,12 +93,9 @@ describe('Groups Controller', function() { members: [user._id] }); - var myGuilds = sandbox.stub(groups, "myGuilds", function() { - return [guild]; - }); + user.guilds = [guild._id]; expect(scope.isMemberOfGroup(user._id, guild)).to.be.ok; - expect(myGuilds).to.be.called; }); it('does not return true if guild is not included in myGuilds call', function(){ @@ -109,12 +106,9 @@ describe('Groups Controller', function() { members: ['not-user-id'] }); - var myGuilds = sandbox.stub(groups,"myGuilds", function() { - return []; - }); + user.guilds = []; expect(scope.isMemberOfGroup(user._id, guild)).to.not.be.ok; - expect(myGuilds).to.be.calledOnce; }); }); diff --git a/test/spec/controllers/partyCtrlSpec.js b/test/spec/controllers/partyCtrlSpec.js index 88a94cfc28..f9571c8ef1 100644 --- a/test/spec/controllers/partyCtrlSpec.js +++ b/test/spec/controllers/partyCtrlSpec.js @@ -78,11 +78,11 @@ describe("Party Controller", function() { initializeControllerWithStubbedState(); setTimeout(function() { - expect(User.set).to.be.calledTwice; + expect(User.set).to.be.calledOnce; expect(User.set).to.be.calledWith( { 'achievements.partyUp': true } ); - expect(rootScope.openModal).to.be.calledTwice; + expect(rootScope.openModal).to.be.calledOnce; expect(rootScope.openModal).to.be.calledWith('achievements/partyUp'); done(); }, 1000); @@ -103,11 +103,11 @@ describe("Party Controller", function() { initializeControllerWithStubbedState(); setTimeout(function(){ - expect(User.set).to.be.calledTwice; + expect(User.set).to.be.calledOnce; expect(User.set).to.be.calledWith( { 'achievements.partyOn': true } ); - expect(rootScope.openModal).to.be.calledTwice; + expect(rootScope.openModal).to.be.calledOnce; expect(rootScope.openModal).to.be.calledWith('achievements/partyOn'); done(); }, 1000); @@ -152,9 +152,9 @@ describe("Party Controller", function() { var partyStub; beforeEach(function () { - partyStub = sandbox.stub(groups.Group, "create", function() { - return party; - }); + partyStub = sinon.stub(groups.Group, "create"); + partyStub.returns(Promise.resolve(party)); + sinon.stub(rootScope, 'hardRedirect'); }); it("creates a new party", function() { From 01a8fde1244058ae74b808fd9152b0468bafa343 Mon Sep 17 00:00:00 2001 From: Keith Holliday Date: Wed, 11 May 2016 23:40:20 -0500 Subject: [PATCH 5/5] Fixed cron test --- test/api/v3/unit/middlewares/cronMiddleware.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/api/v3/unit/middlewares/cronMiddleware.js b/test/api/v3/unit/middlewares/cronMiddleware.js index 196de33dad..71b4843b8b 100644 --- a/test/api/v3/unit/middlewares/cronMiddleware.js +++ b/test/api/v3/unit/middlewares/cronMiddleware.js @@ -109,11 +109,12 @@ describe('cron middleware', () => { it('should call next is user was not modified after cron', (done) => { let hpBefore = user.stats.hp; user.lastCron = moment(new Date()).subtract({days: 2}); - generateDaily(user); - cronMiddleware(req, res, () => { - expect(user.stats.hp).to.be.equal(hpBefore); - done(); + user.save().then(function () { + cronMiddleware(req, res, function () { + expect(hpBefore).to.equal(user.stats.hp); + done(); + }); }); });