From 633d6fdb52ca0f37447f44e1969ca15998faee2e Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Sun, 1 Sep 2013 11:33:39 -0400 Subject: [PATCH] api: group invitations, leave, join --- archive/derby_controllers/groups.coffee | 2 -- assets/js/controllers/groupsCtrl.js | 14 ++++++++ assets/js/services/groupServices.js | 3 +- src/controllers/groups.js | 44 +++++++++++++++++++++++++ src/routes/api.js | 1 + views/options/groups/group.jade | 12 +++---- views/options/groups/index.jade | 11 +++---- views/options/groups/tavern.jade | 1 - 8 files changed, 71 insertions(+), 17 deletions(-) diff --git a/archive/derby_controllers/groups.coffee b/archive/derby_controllers/groups.coffee index a1a3517b34..79992f0f6a 100644 --- a/archive/derby_controllers/groups.coffee +++ b/archive/derby_controllers/groups.coffee @@ -9,8 +9,6 @@ module.exports.app = (appExports, model, app) -> # Every 60 seconds, reset the current time so that the chat can update relative times setInterval (->_currentTime.set +new Date), 60000 - user = model.at('_user') - appExports.groupCreate = (e,el) -> type = $(el).attr('data-type') newGroup = diff --git a/assets/js/controllers/groupsCtrl.js b/assets/js/controllers/groupsCtrl.js index 766cacfd3b..90de57da22 100644 --- a/assets/js/controllers/groupsCtrl.js +++ b/assets/js/controllers/groupsCtrl.js @@ -61,6 +61,14 @@ habitrpg $('.chat-btn').removeClass('disabled'); }); } + + $scope.invitee = ''; + $scope.invite = function(group, uuid){ + debugger + group.$invite({uuid:uuid}, function(){ + $scope.invitee = ''; + }); + } } ]) @@ -90,6 +98,12 @@ habitrpg $scope.type = 'party'; $scope.text = 'Party'; $scope.group = $scope.groups.party; + $scope.join = function(party){ + // workaround since group isn't currently a resource, this won't get saved to the server + var group = new Groups({_id: party.id, name: party.name}); + debugger + group.$join(); + } } ]) diff --git a/assets/js/services/groupServices.js b/assets/js/services/groupServices.js index caf75ca535..280f7fff8f 100644 --- a/assets/js/services/groupServices.js +++ b/assets/js/services/groupServices.js @@ -13,7 +13,8 @@ angular.module('groupServices', ['ngResource']). //'query': {method: "GET", isArray:false} postChat: {method: "POST", url: API_URL + '/api/v1/groups/:gid/chat'}, join: {method: "POST", url: API_URL + '/api/v1/groups/:gid/join'}, - leave: {method: "POST", url: API_URL + '/api/v1/groups/:gid/leave'} + leave: {method: "POST", url: API_URL + '/api/v1/groups/:gid/leave'}, + invite: {method: "POST", url: API_URL + '/api/v1/groups/:gid/invite'} }); return Group; diff --git a/src/controllers/groups.js b/src/controllers/groups.js index 154f2b495a..adb971bcfb 100644 --- a/src/controllers/groups.js +++ b/src/controllers/groups.js @@ -122,6 +122,16 @@ api.join = function(req, res, next) { if (err) return res.json(500,{err:err}); res.json(saved); }); + + if (group.type == 'party' && group._id == (user.invitations && user.invitations.party && user.invitations.party.id)) { + user.invitations.party = undefined; + user.save(); + } + else if (group.type == 'guild' && user.invitations && user.invitations.guilds) { + var i = _.findIndex(user.invitations.guilds, {id:group._id}); + if (~i) user.invitations.guilds.splice(i,1); + user.save(); + } } api.leave = function(req, res, next) { @@ -132,4 +142,38 @@ api.leave = function(req, res, next) { if (err) return res.json(500,{err:err}); res.send(200, saved); }) +} + +api.invite = function(req, res, next) { + var group = res.locals.group; + var uuid = req.query.uuid; + + User.findById(uuid, function(err,invite){ + if (err) return res.json(500,{err:err}); + if (!invite) + return res.json(400,{err:'User with id '+req.query.uid+' not found'}); + if (group.type == 'guild') { + if (_.contains(group.members,uuid)) + return res.json(400,{err: "User already in that group"}); + if (invite.invitations && invite.invitations.guilds && _.find(invite.invitations.guilds, {id:group._id})) + return res.json(400, {err:"User already invited to that group"}); + sendInvite(); + } else if (group.type == 'party') { + if (invite.invitations && invite.invitations.party) + return res.json(400,{err:"User already pending invitation."}); + Group.find({type:'party', members:{$in:[uuid]}}, function(err, groups){ + if (err) return res.json(500,{err:err}); + if (!_.isEmpty(groups)) + return res.json(400,{err:"User already in a party."}) + sendInvite(); + }) + } + + function sendInvite (){ + //req.body.type in 'guild', 'party' + invite.invitations.party = {id:group._id, name: group.name} + invite.save(); + res.json(group); + } + }); } \ No newline at end of file diff --git a/src/routes/api.js b/src/routes/api.js index e59c5ddc49..ada2abea63 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -63,6 +63,7 @@ router.get('/groups', auth, groups.getGroups); router.post('/groups/:gid/join', auth, groups.attachGroup, groups.join); router.post('/groups/:gid/leave', auth, groups.attachGroup, groups.leave); +router.post('/groups/:gid/invite', auth, groups.attachGroup, groups.invite); //GET /groups/:gid/chat router.post('/groups/:gid/chat', auth, groups.attachGroup, groups.postChat); diff --git a/views/options/groups/group.jade b/views/options/groups/group.jade index e2d06af2a0..592430a901 100644 --- a/views/options/groups/group.jade +++ b/views/options/groups/group.jade @@ -42,6 +42,11 @@ a.pull-right.gem-wallet(rel='popover', data-trigger='hover', data-title='Guild B a(target='_blank', ng-href='{{website}}') {{website}} accordion-group(heading='Members') + form.form-inline(ng-submit='invite(group, invitee)') + .alert.alert-danger(ng-show='_groupError') {{_groupError}} + .control-group + input.input-medium(type='text', placeholder='User Id', ng-model='invitee') + input.btn(type='submit', value='Invite') table.table.table-striped tr(ng-repeat='member in group.members') td @@ -56,13 +61,6 @@ a.pull-right.gem-wallet(rel='popover', data-trigger='hover', data-title='Guild B | {{username(member.auth, member.profile.name)}} td | ({{member._id}}) - // {#with group as :group} - form.form-inline(x-bind='submit:groupInvite', data-type='{{group.type}}') - .alert.alert-danger(ng-show='_groupError') {{_groupError}} - .control-group - input.input-medium(type='text', placeholder='User Id', value='{{_groupInvitee}}') - input.btn(type='submit', value='Invite') - // {/} //-accordion-group(heading='Challenges') span.label diff --git a/views/options/groups/index.jade b/views/options/groups/index.jade index b1862803dd..b17a890e71 100644 --- a/views/options/groups/index.jade +++ b/views/options/groups/index.jade @@ -1,5 +1,4 @@ div(ng-controller='GroupsCtrl') - h4.alert.alert-warning Coming Soon! (Presently Incomplete & Read-Only) // FIXME note, due to https://github.com/angular-ui/bootstrap/issues/783 we can't use nested angular-bootstrap tabs // Subscribe to that ticket & change this when they fix @@ -10,16 +9,16 @@ div(ng-controller='GroupsCtrl') li a(data-target='#groups-guilds', data-toggle='tab', ng-click='fetchGuilds()') Guilds - .tab-content + .tab-content(ng-controller='PartyCtrl') #groups-party.tab-pane.active Party - div(ng-show='group._id', ng-controller='PartyCtrl') + div(ng-show='group._id') include ./group div(ng-hide='group._id') div(ng-show='user.invitations.party') // #with required for the accept/reject buttons // {#with _user.invitations.party as :party} - h2 You're Invited To {{party.name}} - a.btn.btn-success(data-type='party', x-bind='click:acceptInvitation') Accept + h2 You're Invited To {{user.invitations.party.name}} + a.btn.btn-success(data-type='party', ng-click='join(user.invitations.party)') Accept a.btn.btn-danger(x-bind='click:rejectInvitation') Reject // {/} div(ng-hide='user.invitations.party', ng-controller='PartyCtrl') @@ -42,7 +41,7 @@ div(ng-controller='GroupsCtrl') .tab-pane.active#groups-public-guilds div(ng-repeat='invitation in user.invitations.guilds') h3 You're Invited To {{invitation.name}} - a.btn.btn-success(data-type='guild', x-bind='click:acceptInvitation') Accept + a.btn.btn-success(data-type='guild', ng-click='join(group)') Accept a.btn.btn-danger(x-bind='click:rejectInvitation') Reject // Public Groups table.table.table-striped diff --git a/views/options/groups/tavern.jade b/views/options/groups/tavern.jade index b04ee125b7..fe274d1be8 100644 --- a/views/options/groups/tavern.jade +++ b/views/options/groups/tavern.jade @@ -1,4 +1,3 @@ -h4.alert.alert-warning Coming Soon! (Presently Incomplete & Read-Only) .row-fluid(ng-controller='TavernCtrl') .span4 .tavern-pane