v1.18.x
barisusakli 9 years ago
parent 441232e71d
commit 268aa12cc2

@ -314,7 +314,7 @@ var async = require('async'),
Messaging.canMessageUser = function(uid, toUid, callback) { Messaging.canMessageUser = function(uid, toUid, callback) {
if (parseInt(meta.config.disableChat) === 1 || !uid || uid === toUid) { if (parseInt(meta.config.disableChat) === 1 || !uid || uid === toUid) {
return callback(null, false); return callback(new Error('[[error:chat-disabled]]'));
} }
async.waterfall([ async.waterfall([
@ -323,43 +323,40 @@ var async = require('async'),
}, },
function (exists, next) { function (exists, next) {
if (!exists) { if (!exists) {
return callback(null, false); return callback(new Error('[[error:no-user]]'));
} }
user.getUserFields(uid, ['banned', 'email:confirmed'], next); user.getUserFields(uid, ['banned', 'email:confirmed'], next);
}, },
function (userData, next) { function (userData, next) {
if (parseInt(userData.banned, 10) === 1) { if (parseInt(userData.banned, 10) === 1) {
return callback(null, false); return callback(new Error('[[error:user-banned]]'));
} }
if (parseInt(meta.config.requireEmailConfirmation, 10) === 1 && parseInt(userData['email:confirmed'], 10) !== 1) { if (parseInt(meta.config.requireEmailConfirmation, 10) === 1 && parseInt(userData['email:confirmed'], 10) !== 1) {
return callback(null, false); return callback(new Error('[[error:email-not-confirmed-chat]]'));
} }
user.getSettings(toUid, next); async.parallel({
settings: async.apply(user.getSettings, toUid),
isAdmin: async.apply(user.isAdministrator, uid),
isFollowing: async.apply(user.isFollowing, toUid, uid)
}, next);
}, },
function(settings, next) { function(results, next) {
if (!settings.restrictChat) { if (!results.settings.restrictChat || results.isAdmin || results.isFollowing) {
return callback(null, true); return next();
} }
user.isAdministrator(uid, next); next(new Error('[[error:chat-restricted]]'));
},
function(isAdmin, next) {
if (isAdmin) {
return callback(null, true);
}
user.isFollowing(toUid, uid, next);
} }
], callback); ], callback);
}; };
Messaging.canMessageRoom = function(uid, roomId, callback) { Messaging.canMessageRoom = function(uid, roomId, callback) {
if (parseInt(meta.config.disableChat) === 1 || !uid) { if (parseInt(meta.config.disableChat) === 1 || !uid) {
return callback(new Error('[[error:chat-disabled]]')); return callback(new Error('[[error:chat-disabled]]'));
} }
async.waterfall([ async.waterfall([
function (next) { function (next) {
Messaging.isUserInRoom(uid, roomId, next); Messaging.isUserInRoom(uid, roomId, next);
@ -368,14 +365,14 @@ var async = require('async'),
if (!inRoom) { if (!inRoom) {
return next(new Error('[[error:not-in-room]]')); return next(new Error('[[error:not-in-room]]'));
} }
Messaging.getUserCountInRoom(roomId, next); Messaging.getUserCountInRoom(roomId, next);
}, },
function(count, next) { function(count, next) {
if (count < 2) { if (count < 2) {
return next(new Error('[[error:no-users-in-room]]')); return next(new Error('[[error:no-users-in-room]]'));
} }
user.getUserFields(uid, ['banned', 'email:confirmed'], next); user.getUserFields(uid, ['banned', 'email:confirmed'], next);
}, },
function (userData, next) { function (userData, next) {

@ -62,8 +62,8 @@ SocketModules.chats.newRoom = function(socket, data, callback) {
} }
Messaging.canMessageUser(socket.uid, data.touid, function(err, allowed) { Messaging.canMessageUser(socket.uid, data.touid, function(err, allowed) {
if (err || !allowed) { if (err) {
return callback(err || new Error('[[error:chat-restricted]]')); return callback(err);
} }
Messaging.newRoom(socket.uid, [data.touid], callback); Messaging.newRoom(socket.uid, [data.touid], callback);

@ -27,39 +27,39 @@ describe('Messaging Library', function() {
describe('.canMessage()', function() { describe('.canMessage()', function() {
it('should not error out', function(done) { it('should not error out', function(done) {
Messaging.canMessageUser(testUids[1], testUids[2], function(err, allowed) { Messaging.canMessageUser(testUids[1], testUids[2], function(err) {
assert.ifError(err); assert.ifError(err);
done(); done();
}); });
}); });
it('should allow messages to be sent to an unrestricted user', function(done) { it('should allow messages to be sent to an unrestricted user', function(done) {
Messaging.canMessageUser(testUids[1], testUids[2], function(err, allowed) { Messaging.canMessageUser(testUids[1], testUids[2], function(err) {
assert.strictEqual(allowed, true, 'should be true, received ' + allowed); assert.ifError(err);
done(); done();
}); });
}); });
it('should NOT allow messages to be sent to a restricted user', function(done) { it('should NOT allow messages to be sent to a restricted user', function(done) {
User.setSetting(testUids[1], 'restrictChat', '1', function() { User.setSetting(testUids[1], 'restrictChat', '1', function() {
Messaging.canMessageUser(testUids[2], testUids[1], function(err, allowed) { Messaging.canMessageUser(testUids[2], testUids[1], function(err) {
assert.strictEqual(allowed, false, 'should be false, received ' + allowed); assert.strictEqual(err.message, '[[error:chat-restricted]]');
done(); done();
}); });
}); });
}); });
it('should always allow admins through', function(done) { it('should always allow admins through', function(done) {
Messaging.canMessageUser(testUids[0], testUids[1], function(err, allowed) { Messaging.canMessageUser(testUids[0], testUids[1], function(err) {
assert.strictEqual(allowed, true, 'should be true, received ' + allowed); assert.ifError(err);
done(); done();
}); });
}); });
it('should allow messages to be sent to a restricted user if restricted user follows sender', function(done) { it('should allow messages to be sent to a restricted user if restricted user follows sender', function(done) {
User.follow(testUids[1], testUids[2], function() { User.follow(testUids[1], testUids[2], function() {
Messaging.canMessageUser(testUids[2], testUids[1], function(err, allowed) { Messaging.canMessageUser(testUids[2], testUids[1], function(err) {
assert.strictEqual(allowed, true, 'should be true, received ' + allowed); assert.ifError(err);
done(); done();
}); });
}); });

Loading…
Cancel
Save