mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-27 09:06:15 +01:00
fix: #9149, server-side handling of disableChatMessageEditing
This commit is contained in:
@@ -47,9 +47,11 @@ module.exports = function (Messaging) {
|
|||||||
durationConfig = 'chatDeleteDuration';
|
durationConfig = 'chatDeleteDuration';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const isAdminOrGlobalMod = await user.isAdminOrGlobalMod(uid);
|
||||||
|
|
||||||
if (meta.config.disableChat) {
|
if (meta.config.disableChat) {
|
||||||
throw new Error('[[error:chat-disabled]]');
|
throw new Error('[[error:chat-disabled]]');
|
||||||
} else if (meta.config.disableChatMessageEditing) {
|
} else if (!isAdminOrGlobalMod && meta.config.disableChatMessageEditing) {
|
||||||
throw new Error('[[error:chat-message-editing-disabled]]');
|
throw new Error('[[error:chat-message-editing-disabled]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,19 +59,17 @@ module.exports = function (Messaging) {
|
|||||||
if (userData.banned) {
|
if (userData.banned) {
|
||||||
throw new Error('[[error:user-banned]]');
|
throw new Error('[[error:user-banned]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
const canChat = await privileges.global.can('chat', uid);
|
const canChat = await privileges.global.can('chat', uid);
|
||||||
if (!canChat) {
|
if (!canChat) {
|
||||||
throw new Error('[[error:no-privileges]]');
|
throw new Error('[[error:no-privileges]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
const [isAdmin, messageData] = await Promise.all([
|
const messageData = await Messaging.getMessageFields(messageId, ['fromuid', 'timestamp', 'system']);
|
||||||
user.isAdministrator(uid),
|
if (isAdminOrGlobalMod && !messageData.system) {
|
||||||
Messaging.getMessageFields(messageId, ['fromuid', 'timestamp', 'system']),
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (isAdmin && !messageData.system) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const chatConfigDuration = meta.config[durationConfig];
|
const chatConfigDuration = meta.config[durationConfig];
|
||||||
if (chatConfigDuration && Date.now() - messageData.timestamp > chatConfigDuration * 1000) {
|
if (chatConfigDuration && Date.now() - messageData.timestamp > chatConfigDuration * 1000) {
|
||||||
throw new Error('[[error:chat-' + type + '-duration-expired, ' + meta.config[durationConfig] + ']]');
|
throw new Error('[[error:chat-' + type + '-duration-expired, ' + meta.config[durationConfig] + ']]');
|
||||||
|
|||||||
@@ -597,12 +597,15 @@ describe('Messaging Library', function () {
|
|||||||
describe('edit/delete', function () {
|
describe('edit/delete', function () {
|
||||||
var socketModules = require('../src/socket.io/modules');
|
var socketModules = require('../src/socket.io/modules');
|
||||||
var mid;
|
var mid;
|
||||||
before(function (done) {
|
let mid2;
|
||||||
socketModules.chats.send({ uid: fooUid }, { roomId: roomId, message: 'first chat message' }, function (err, messageData) {
|
before(async function () {
|
||||||
assert.ifError(err);
|
await socketModules.chats.addUserToRoom({ uid: fooUid }, { roomId: roomId, username: 'baz' });
|
||||||
mid = messageData.mid;
|
mid = (await socketModules.chats.send({ uid: fooUid }, { roomId: roomId, message: 'first chat message' })).mid;
|
||||||
done();
|
mid2 = (await socketModules.chats.send({ uid: bazUid }, { roomId: roomId, message: 'second chat message' })).mid;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
after(async () => {
|
||||||
|
await socketModules.chats.leave({ uid: bazUid }, roomId);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail to edit message with invalid data', function (done) {
|
it('should fail to edit message with invalid data', function (done) {
|
||||||
@@ -723,6 +726,38 @@ describe('Messaging Library', function () {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('disabled via ACP', () => {
|
||||||
|
before(async () => {
|
||||||
|
meta.config.disableChatMessageEditing = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
after(async () => {
|
||||||
|
meta.config.disableChatMessageEditing = false;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should error out for regular users', async () => {
|
||||||
|
try {
|
||||||
|
await socketModules.chats.delete({ uid: bazUid }, { messageId: mid2, roomId: roomId });
|
||||||
|
} catch (err) {
|
||||||
|
assert.strictEqual('[[error:chat-message-editing-disabled]]', err.message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should succeed for administrators', async () => {
|
||||||
|
await socketModules.chats.delete({ uid: fooUid }, { messageId: mid2, roomId: roomId });
|
||||||
|
await socketModules.chats.restore({ uid: fooUid }, { messageId: mid2, roomId: roomId });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should succeed for global moderators', async () => {
|
||||||
|
await Groups.join(['Global Moderators'], bazUid);
|
||||||
|
|
||||||
|
await socketModules.chats.delete({ uid: fooUid }, { messageId: mid2, roomId: roomId });
|
||||||
|
await socketModules.chats.restore({ uid: fooUid }, { messageId: mid2, roomId: roomId });
|
||||||
|
|
||||||
|
await Groups.leave(['Global Moderators'], bazUid);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('controller', function () {
|
describe('controller', function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user