diff --git a/public/language/en-GB/admin/settings/chat.json b/public/language/en-GB/admin/settings/chat.json index 0b22127341..c538790b95 100644 --- a/public/language/en-GB/admin/settings/chat.json +++ b/public/language/en-GB/admin/settings/chat.json @@ -5,5 +5,7 @@ "disable-editing-help": "Administrators and global moderators are exempt from this restriction", "max-length": "Maximum length of chat messages", "max-room-size": "Maximum number of users in chat rooms", - "delay": "Time between chat messages in milliseconds" + "delay": "Time between chat messages in milliseconds", + "restrictions.seconds-edit-after": "Number of seconds before users are allowed to edit chat messages after posting. (0 disabled)", + "restrictions.seconds-delete-after": "Number of seconds before users are allowed to delete chat messages after posting. (0 disabled)" } \ No newline at end of file diff --git a/public/language/en-GB/error.json b/public/language/en-GB/error.json index 66516bd3c5..71c045efc3 100644 --- a/public/language/en-GB/error.json +++ b/public/language/en-GB/error.json @@ -137,6 +137,8 @@ "cant-edit-chat-message": "You are not allowed to edit this message", "cant-remove-last-user": "You can't remove the last user", "cant-delete-chat-message": "You are not allowed to delete this message", + "chat-edit-duration-expired": "You are only allowed to edit chat messages for %1 second(s) after posting", + "chat-delete-duration-expired": "You are only allowed to delete chat messages for %1 second(s) after posting", "already-voting-for-this-post": "You have already voted for this post.", "reputation-system-disabled": "Reputation system is disabled.", diff --git a/src/messaging/edit.js b/src/messaging/edit.js index f9c664d67f..b118ca03c5 100644 --- a/src/messaging/edit.js +++ b/src/messaging/edit.js @@ -44,10 +44,25 @@ module.exports = function (Messaging) { }; Messaging.canEdit = function (messageId, uid, callback) { + canEditDelete(messageId, uid, 'edit', callback); + }; + + Messaging.canDelete = function (messageId, uid, callback) { + canEditDelete(messageId, uid, 'delete', callback); + }; + + function canEditDelete(messageId, uid, type, callback) { + var durationConfig = ''; + if (type === 'edit') { + durationConfig = 'chatEditDuration'; + } else if (type === 'delete') { + durationConfig = 'chatDeleteDuration'; + } + if (parseInt(meta.config.disableChat, 10) === 1) { - return callback(null, false); + return callback(new Error('[[error:chat-disabled]]')); } else if (parseInt(meta.config.disableChatMessageEditing, 10) === 1) { - return callback(null, false); + return callback(new Error('[[error:chat-message-editing-disabled]]')); } async.waterfall([ @@ -56,25 +71,36 @@ module.exports = function (Messaging) { }, function (userData, next) { 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) { - return callback(null, false); + return callback(new Error('[[error:email-not-confirmed]]')); + } + async.parallel({ + isAdmin: function (next) { + user.isAdministrator(uid, next); + }, + messageData: function (next) { + Messaging.getMessageFields(messageId, ['fromuid', 'timestamp'], next); + }, + }, next); + }, + function (results, next) { + if (results.isAdmin) { + return callback(); + } + var chatConfigDuration = parseInt(meta.config[durationConfig], 10); + if (chatConfigDuration && Date.now() - parseInt(results.messageData.timestamp, 10) > chatConfigDuration * 1000) { + return callback(new Error('[[error:chat-' + type + '-duration-expired, ' + meta.config[durationConfig] + ']]')); } - Messaging.getMessageField(messageId, 'fromuid', next); - }, - function (fromUid, next) { - if (parseInt(fromUid, 10) === parseInt(uid, 10)) { - return callback(null, true); + if (parseInt(results.messageData.fromuid, 10) === parseInt(uid, 10)) { + return callback(); } - user.isAdministrator(uid, next); - }, - function (isAdmin, next) { - next(null, isAdmin); + next(new Error('[[error:cant-' + type + '-chat-message]]')); }, ], callback); - }; + } }; diff --git a/src/socket.io/modules.js b/src/socket.io/modules.js index e277c5c5e7..d58fb7fa59 100644 --- a/src/socket.io/modules.js +++ b/src/socket.io/modules.js @@ -246,10 +246,7 @@ SocketModules.chats.edit = function (socket, data, callback) { function (next) { Messaging.canEdit(data.mid, socket.uid, next); }, - function (allowed, next) { - if (!allowed) { - return next(new Error('[[error:cant-edit-chat-message]]')); - } + function (next) { Messaging.editMessage(socket.uid, data.mid, data.roomId, data.message, next); }, ], callback); @@ -262,13 +259,9 @@ SocketModules.chats.delete = function (socket, data, callback) { async.waterfall([ function (next) { - Messaging.canEdit(data.messageId, socket.uid, next); + Messaging.canDelete(data.messageId, socket.uid, next); }, - function (allowed, next) { - if (!allowed) { - return next(new Error('[[error:cant-delete-chat-message]]')); - } - + function (next) { Messaging.deleteMessage(data.messageId, data.roomId, next); }, ], callback); diff --git a/src/views/admin/settings/chat.tpl b/src/views/admin/settings/chat.tpl index 612413d428..b6448adfb6 100644 --- a/src/views/admin/settings/chat.tpl +++ b/src/views/admin/settings/chat.tpl @@ -23,6 +23,16 @@

[[admin/settings/chat:disable-editing-help]]

+
+ + +
+ +
+ + +
+