mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-29 01:56:12 +01:00
Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
160 lines
3.9 KiB
JavaScript
160 lines
3.9 KiB
JavaScript
"use strict";
|
|
|
|
var meta = require('../meta'),
|
|
Messaging = require('../messaging'),
|
|
utils = require('../../public/src/utils'),
|
|
|
|
async = require('async'),
|
|
|
|
server = require('./'),
|
|
|
|
SocketModules = {
|
|
chats: {},
|
|
sounds: {},
|
|
settings: {}
|
|
};
|
|
|
|
/* Chat */
|
|
|
|
SocketModules.chats.get = function(socket, data, callback) {
|
|
if(!data) {
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
}
|
|
|
|
Messaging.getMessages({
|
|
fromuid: socket.uid,
|
|
touid: data.touid,
|
|
since: data.since,
|
|
isNew: false
|
|
}, callback);
|
|
};
|
|
|
|
SocketModules.chats.getRaw = function(socket, data, callback) {
|
|
if(!data || !data.hasOwnProperty('mid')) {
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
}
|
|
|
|
Messaging.getMessageField(data.mid, 'content', callback);
|
|
};
|
|
|
|
SocketModules.chats.send = function(socket, data, callback) {
|
|
if (!data) {
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
}
|
|
|
|
var now = Date.now(),
|
|
touid = parseInt(data.touid, 10);
|
|
|
|
// Websocket rate limiting
|
|
socket.lastChatMessageTime = socket.lastChatMessageTime || 0;
|
|
if (now - socket.lastChatMessageTime < 200) {
|
|
return callback(new Error('[[error:too-many-messages]]'));
|
|
} else {
|
|
socket.lastChatMessageTime = now;
|
|
}
|
|
|
|
Messaging.canMessage(socket.uid, touid, function(err, allowed) {
|
|
if (err || !allowed) {
|
|
return callback(err || new Error('[[error:chat-restricted]]'));
|
|
}
|
|
|
|
Messaging.addMessage(socket.uid, touid, data.message, function(err, message) {
|
|
if (err) {
|
|
return callback(err);
|
|
}
|
|
|
|
Messaging.notifyUser(socket.uid, touid, message);
|
|
|
|
callback();
|
|
});
|
|
});
|
|
};
|
|
|
|
SocketModules.chats.edit = function(socket, data, callback) {
|
|
if (!data) {
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
}
|
|
|
|
Messaging.canEdit(data.mid, socket.uid, function(err, allowed) {
|
|
if (allowed) {
|
|
Messaging.editMessage(data.mid, data.message, callback);
|
|
} else {
|
|
return callback(new Error('[[error:cant-edit-chat-message]]'));
|
|
}
|
|
});
|
|
};
|
|
|
|
SocketModules.chats.delete = function(socket, data, callback) {
|
|
if (!data) {
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
}
|
|
|
|
Messaging.canEdit(data.messageId, socket.uid, function(err, allowed) {
|
|
if (allowed) {
|
|
Messaging.deleteMessage(data.messageId, callback);
|
|
}
|
|
});
|
|
}
|
|
|
|
SocketModules.chats.canMessage = function(socket, toUid, callback) {
|
|
Messaging.canMessage(socket.uid, toUid, function(err, allowed) {
|
|
callback(!allowed ? new Error('[[error:chat-restricted]]') : undefined);
|
|
});
|
|
};
|
|
|
|
SocketModules.chats.markRead = function(socket, touid, callback) {
|
|
Messaging.markRead(socket.uid, touid, function(err) {
|
|
if (err) {
|
|
return callback(err);
|
|
}
|
|
|
|
Messaging.pushUnreadCount(socket.uid);
|
|
callback();
|
|
});
|
|
};
|
|
|
|
SocketModules.chats.userStartTyping = function(socket, data, callback) {
|
|
sendTypingNotification('event:chats.userStartTyping', socket, data, callback);
|
|
};
|
|
|
|
SocketModules.chats.userStopTyping = function(socket, data, callback) {
|
|
sendTypingNotification('event:chats.userStopTyping', socket, data, callback);
|
|
};
|
|
|
|
function sendTypingNotification(event, socket, data, callback) {
|
|
if (!socket.uid || !data) {
|
|
return;
|
|
}
|
|
server.in('uid_' + data.touid).emit(event, data.fromUid);
|
|
}
|
|
|
|
SocketModules.chats.getRecentChats = function(socket, data, callback) {
|
|
if (!data || !utils.isNumber(data.after)) {
|
|
return callback(new Error('[[error:invalid-data]]'));
|
|
}
|
|
var start = parseInt(data.after, 10),
|
|
stop = start + 9;
|
|
|
|
Messaging.getRecentChats(socket.uid, start, stop, callback);
|
|
};
|
|
|
|
|
|
/* Sounds */
|
|
SocketModules.sounds.getSounds = function(socket, data, callback) {
|
|
// Read sounds from local directory
|
|
meta.sounds.getFiles(callback);
|
|
};
|
|
|
|
SocketModules.sounds.getMapping = function(socket, data, callback) {
|
|
meta.sounds.getMapping(callback);
|
|
};
|
|
|
|
SocketModules.sounds.getData = function(socket, data, callback) {
|
|
async.parallel({
|
|
mapping: async.apply(meta.sounds.getMapping),
|
|
files: async.apply(meta.sounds.getFiles)
|
|
}, callback);
|
|
};
|
|
|
|
module.exports = SocketModules;
|