diff --git a/public/js/controllers/groupsCtrl.js b/public/js/controllers/groupsCtrl.js index 04c284c175..324e180a16 100644 --- a/public/js/controllers/groupsCtrl.js +++ b/public/js/controllers/groupsCtrl.js @@ -53,6 +53,7 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A .controller('ChatCtrl', ['$scope', 'Groups', 'User', function($scope, Groups, User){ $scope._chatMessage = ''; + $scope.postChat = function(group, message){ if (_.isEmpty(message)) return $('.chat-btn').addClass('disabled'); @@ -62,9 +63,20 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A $('.chat-btn').removeClass('disabled'); }); } + + $scope.deleteChatMessage = function(group, message){ + if(message.uuid === User.user.id){ + group.$deleteChatMessage({messageId: message.id}, function(){ + var i = _.indexOf(group.chat, message); + if(i !== -1) group.chat.splice(i, 1); + }); + } + } + $scope.sync = function(group){ group.$get(); } + $scope.nameTagClasses = function(message){ if (message.contributor) { if (message.contributor.match(/npc/i) || message.contributor.match(/royal/i)) { diff --git a/public/js/services/groupServices.js b/public/js/services/groupServices.js index 5dfbf7aa56..d4c7b5ee13 100644 --- a/public/js/services/groupServices.js +++ b/public/js/services/groupServices.js @@ -8,10 +8,11 @@ angular.module('groupServices', ['ngResource']). factory('Groups', ['API_URL', '$resource', 'User', '$q', 'Members', function(API_URL, $resource, User, $q, Members) { var Group = $resource(API_URL + '/api/v1/groups/:gid', - {gid:'@_id'}, + {gid:'@_id', messageId: '@_messageId'}, { //'query': {method: "GET", isArray:false} postChat: {method: "POST", url: API_URL + '/api/v1/groups/:gid/chat'}, + deleteChatMessage: {method: "DELETE", url: API_URL + '/api/v1/groups/:gid/chat/:messageId'}, join: {method: "POST", url: API_URL + '/api/v1/groups/:gid/join'}, leave: {method: "POST", url: API_URL + '/api/v1/groups/:gid/leave'}, invite: {method: "POST", url: API_URL + '/api/v1/groups/:gid/invite'} diff --git a/src/controllers/groups.js b/src/controllers/groups.js index 8e2372e6b5..bd52cb09e0 100644 --- a/src/controllers/groups.js +++ b/src/controllers/groups.js @@ -166,6 +166,26 @@ api.postChat = function(req, res, next) { }) } +api.deleteChatMessage = function(req, res, next){ + var user = res.locals.user + var group = res.locals.group; + var message = _.find(group.chat, {id: req.params.messageId, uuid: user.id}); + + if(message === undefined) return res.json(500, {err: "Message not found!"}); + + if(user.id !== message.uuid || (user.backer && !user.backer.admin)){ + return res.json(500, {err: "Not authorized to delete this message!"}); + } + + group.chat = _.without(group.chat, message); + + group.save(function(err, data){ + if(err) return res.json(500, {err: err}); + + res.send(204); + }); +} + api.join = function(req, res, next) { var user = res.locals.user, group = res.locals.group; diff --git a/src/routes/api.js b/src/routes/api.js index df595bc43c..223adba3d4 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -61,8 +61,8 @@ router.post('/groups/:gid/invite', auth.auth, groups.attachGroup, groups.invite) //GET /groups/:gid/chat router.post('/groups/:gid/chat', auth.auth, groups.attachGroup, groups.postChat); +router["delete"]('/groups/:gid/chat/:messageId', auth.auth, groups.attachGroup, groups.deleteChatMessage); //PUT /groups/:gid/chat/:messageId -//DELETE /groups/:gid/chat/:messageId /* Members */ router.get('/members/:uid', groups.getMember); diff --git a/views/options/groups/chat-message.jade b/views/options/groups/chat-message.jade index 5c7c65926e..392627fbcc 100644 --- a/views/options/groups/chat-message.jade +++ b/views/options/groups/chat-message.jade @@ -5,6 +5,6 @@ li(ng-repeat='message in group.chat', ng-class='{highlight: message.text.indexOf span(ng-bind-html="message.text | linky:'_blank'") - span span.muted.time - | {{relativeDate(message.timestamp, _currentTime)}} - a(ng-show='user.backer.admin || message.uuid == user.id', x-bind='click:deleteChatMessage') + | {{relativeDate(message.timestamp, _currentTime) + ' '}} + a(ng-show='user.backer.admin || message.uuid == user.id', ng-click='deleteChatMessage(group, message)') i.icon-remove(tooltip='Delete')