all files / src/messaging/ edit.js

21.88% Statements 7/32
0% Branches 0/12
7.69% Functions 1/13
21.88% Lines 7/32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80                                                                                                                                                 
'use strict';
 
var async = require('async');
 
var meta = require('../meta');
var user = require('../user');
 
var sockets = require('../socket.io');
 
 
module.exports = function(Messaging) {
 
	Messaging.editMessage = function(uid, mid, roomId, content, callback) {
		var uids;
		async.waterfall([
			function(next) {
				Messaging.getMessageField(mid, 'content', next);
			},
			function (raw, next) {
				if (raw === content) {
					return callback();
				}
 
				Messaging.setMessageFields(mid, {
					content: content,
					edited: Date.now()
				}, next);
			},
			function (next) {
				Messaging.getUidsInRoom(roomId, 0, -1, next);
			},
			function (_uids, next) {
				uids = _uids;
				Messaging.getMessagesData([mid], uid, roomId, true, next);
			},
			function (messages, next) {
				uids.forEach(function(uid) {
					sockets.in('uid_' + uid).emit('event:chats.edit', {
						messages: messages
					});
				});
				next();
			}
		], callback);
	};
 
	Messaging.canEdit = function(messageId, uid, callback) {
		if (parseInt(meta.config.disableChat) === 1) {
			return callback(null, false);
		}
 
		async.waterfall([
			function (next) {
				user.getUserFields(uid, ['banned', 'email:confirmed'], next);
			},
			function (userData, next) {
				if (parseInt(userData.banned, 10) === 1) {
					return callback(null, false);
				}
 
				if (parseInt(meta.config.requireEmailConfirmation, 10) === 1 && parseInt(userData['email:confirmed'], 10) !== 1) {
					return callback(null, false);
				}
 
				Messaging.getMessageField(messageId, 'fromuid', next);
			},
			function(fromUid, next) {
				if (parseInt(fromUid, 10) === parseInt(uid, 10)) {
					return callback(null, true);
				}
 
				user.isAdministrator(uid, next);
			},
			function(isAdmin, next) {
				next(null, isAdmin);
			}
		], callback);
	};
 
};