delete chat message

This commit is contained in:
Matteo Pagliazzi
2013-09-08 17:22:03 +02:00
parent d7b21eb32a
commit c9f3d3029c
5 changed files with 37 additions and 4 deletions

View File

@@ -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)) {

View File

@@ -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'}

View File

@@ -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;

View File

@@ -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);

View File

@@ -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')