diff --git a/archive/derby_controllers/groups.coffee b/archive/derby_controllers/groups.coffee index f026131fe6..e26d71101d 100644 --- a/archive/derby_controllers/groups.coffee +++ b/archive/derby_controllers/groups.coffee @@ -121,39 +121,6 @@ module.exports.app = (appExports, model, app) -> model.on 'unshift', '_party.chat', -> $('.chat-message').tooltip() model.on 'unshift', '_habitrpg.chat', -> $('.chat-message').tooltip() - appExports.sendChat = (e,el) -> - text = model.get '_chatMessage' - # Check for non-whitespace characters - return unless /\S/.test text - - group = e.at() - - # get rid of duplicate member ids - this is a weird place to put it, but works for now - members = group.get('members'); uniqMembers = _.uniq(members) - group.set('members', uniqMembers) if !_.isEqual(uniqMembers, members) - - chat = group.at('chat') - model.set('_chatMessage', '') - - message = - id: model.id() - uuid: user.get('id') - contributor: user.get('backer.contributor') - npc: user.get('backer.npc') - text: text - user: helpers.username(model.get('_user.auth'), model.get('_user.profile.name')) - timestamp: +new Date - - # FIXME - sometimes racer will send many duplicates via chat.unshift. I think because it can't make connection, keeps - # trying, but all attempts go through. Unfortunately we can't do chat.set without potentially clobbering other chatters - messages = chat.get() or [] - messages.unshift(message) - messages.splice(200) - chat.set messages - - type = $(el).attr('data-type') - model.set '_user.party.lastMessageSeen', chat.get()[0].id if group.get('type') is 'party' - appExports.chatKeyup = (e, el, next) -> return next() unless e.keyCode is 13 appExports.sendChat(e, el) diff --git a/assets/js/controllers/groupsCtrl.js b/assets/js/controllers/groupsCtrl.js index d75388476a..f505886b72 100644 --- a/assets/js/controllers/groupsCtrl.js +++ b/assets/js/controllers/groupsCtrl.js @@ -2,11 +2,22 @@ habitrpg - .controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', '$location', - function($scope, $rootScope, Groups) { + .controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', '$location', '$http', 'API_URL', + function($scope, $rootScope, Groups, $http, API_URL) { + $scope._chatMessage = ''; $scope.groups = Groups.query(function(groups){ $scope.members = groups.members; }); + $scope.postChat = function(group, message){ + if (_.isEmpty(message)) return + $('.chat-btn').addClass('disabled'); + $http.post('/api/v1/groups/'+group._id+'/chat', {message:message}) + .success(function(data){ + $scope._chatMessage = ''; + group.chat = data; + $('.chat-btn').removeClass('disabled'); + }); + } $scope.party = true; } ]) diff --git a/assets/js/services/groupServices.js b/assets/js/services/groupServices.js index 1d862b3389..18cb0262d9 100644 --- a/assets/js/services/groupServices.js +++ b/assets/js/services/groupServices.js @@ -9,7 +9,11 @@ angular.module('groupServices', ['ngResource']). function(API_URL, $resource, User) { var Group = $resource(API_URL + '/api/v1/groups/:gid', {gid:'@_id'}, - {'query': {method: "GET", isArray:false}}); + { + 'query': {method: "GET", isArray:false} + //postChat: {method: "POST"} + }); + return Group; } ]); diff --git a/src/controllers/groups.js b/src/controllers/groups.js index da6f5c8d64..fa0baad6bf 100644 --- a/src/controllers/groups.js +++ b/src/controllers/groups.js @@ -12,7 +12,7 @@ var api = module.exports; /* ------------------------------------------------------------------------ - Party + Groups ------------------------------------------------------------------------ */ @@ -40,13 +40,7 @@ api.getGroups = function(req, res, next) { Group.findOne({_id: 'habitrpg'}, cb); }, "public": function(cb) { - Group.find({ - privacy: 'public' - }, { - name: 1, - description: 1, - members: 1 - }, cb); + Group.find({privacy: 'public'}, {name:1, description:1, members:1}, cb); } }, function(err, results){ if (err) return res.json(500, {err: err}); @@ -55,6 +49,46 @@ api.getGroups = function(req, res, next) { var i = _.findIndex(results.party.members, {_id:user._id}); if (~i) results.party.members.splice(i,1); + // Sort public groups by members length (not easily doable in mongoose) + results.public = _.sortBy(results.public, function(group){ + return -group.members.length; + }) + res.json(results); }) -}; \ No newline at end of file +}; + +api.attachGroup = function(req, res, next) { + Group.findById(req.params.gid, function(err, group){ + if(err) return res.json(500, {err:err}); + res.locals.group = group; + next(); + }) +} + +api.postChat = function(req, res, next) { + var user = res.locals.user + var group = res.locals.group; + var message = { + id: helpers.uuid(), + uuid: user._id, + contributor: user.backer && user.backer.contributor, + npc: user.backer && user.backer.npc, + text: req.body.message, + user: helpers.username(user.auth, user.profile.name), + timestamp: +(new Date) + }; + + group.chat.unshift(message); + group.chat.splice(200); + + if (group.type === 'party') { + user.party.lastMessageSeen = message.id; + user.save(); + } + + group.save(function(err, group){ + if (err) return res.json(500, {err:err}); + res.json(group.chat); + }) +} \ No newline at end of file diff --git a/src/routes/api.js b/src/routes/api.js index 7a6616d622..eea0f92157 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -62,7 +62,7 @@ router.get('/groups', auth, groups.getGroups); //DELETE /groups/:gid //GET /groups/:gid/chat -//POST /groups/:gid/chat +router.post('/groups/:gid/chat', auth, groups.attachGroup, groups.postChat); //PUT /groups/:gid/chat/:messageId //DELETE /groups/:gid/chat/:messageId diff --git a/views/options/groups/chat-box.jade b/views/options/groups/chat-box.jade index b10454fa7b..0d2f1738ef 100644 --- a/views/options/groups/chat-box.jade +++ b/views/options/groups/chat-box.jade @@ -1,4 +1,4 @@ -form(x-bind='submit:sendChat') - textarea.span6(rows='3', x-bind='keyup:chatKeyup') {{_chatMessage}} +form(ng-submit='postChat(group,_chatMessage)') + textarea.span6(rows='3', x-bind='keyup:chatKeyup', ng-model='_chatMessage') br - input.btn(type='submit', value='Send Chat') + input.btn.chat-btn(type='submit', value='Send Chat') diff --git a/views/options/groups/index.jade b/views/options/groups/index.jade index 318d8ff895..83fed721ca 100644 --- a/views/options/groups/index.jade +++ b/views/options/groups/index.jade @@ -49,7 +49,7 @@ div(ng-controller='GroupsCtrl') tr(ng-repeat='group in groups.public') td ul.pull-right.challenge-accordion-header-specs - li {{count(group.members)}} member(s) + li {{group.members.length}} member(s) li // join / leave a.btn.btn-small.btn-danger(ng-show='indexOf(group.members,user.id)', x-bind='click:groupLeave', data-id='{{group.id}}')