Files
NodeBB/src/socket.io/modules.js

109 lines
2.7 KiB
JavaScript
Raw Normal View History

2014-01-10 16:00:03 -05:00
"use strict";
var meta = require('../meta'),
2014-01-10 16:00:03 -05:00
Messaging = require('../messaging'),
utils = require('../../public/src/utils'),
2014-01-10 16:00:03 -05:00
2014-01-16 20:14:09 -05:00
server = require('./'),
2014-11-25 23:08:28 -05:00
SocketModules = {
chats: {},
sounds: {},
settings: {}
};
2014-01-10 16:00:03 -05:00
/* Chat */
SocketModules.chats.get = function(socket, data, callback) {
2014-01-17 12:55:38 -05:00
if(!data) {
2014-04-09 21:36:57 -04:00
return callback(new Error('[[error:invalid-data]]'));
2014-01-17 12:55:38 -05:00
}
2014-09-17 16:29:03 -04:00
Messaging.getMessages(socket.uid, data.touid, data.since, false, callback);
2014-01-10 16:00:03 -05:00
};
2014-03-28 12:13:01 -04:00
SocketModules.chats.send = function(socket, data, callback) {
2014-11-13 15:47:25 -05:00
if (!data) {
2014-04-09 21:36:57 -04:00
return callback(new Error('[[error:invalid-data]]'));
2014-01-17 12:55:38 -05:00
}
2014-01-10 16:00:03 -05:00
var now = Date.now(),
touid = parseInt(data.touid, 10);
2014-11-13 15:47:25 -05:00
// Websocket rate limiting
2014-10-15 14:49:11 -04:00
socket.lastChatMessageTime = socket.lastChatMessageTime || 0;
if (now - socket.lastChatMessageTime < 200) {
return callback(new Error('[[error:too-many-messages]]'));
} else {
socket.lastChatMessageTime = now;
2014-10-15 14:49:11 -04:00
}
Messaging.canMessage(socket.uid, touid, function(err, allowed) {
if (err || !allowed) {
return callback(err || new Error('[[error:chat-restricted]]'));
2014-11-13 16:03:11 -05:00
}
Messaging.addMessage(socket.uid, touid, data.message, function(err, message) {
if (err) {
return callback(err);
2014-10-03 15:28:46 -04:00
}
2014-11-13 15:49:43 -05:00
Messaging.notifyUser(socket.uid, touid, message);
2014-11-13 15:49:43 -05:00
callback();
2014-11-13 15:49:43 -05:00
});
2014-10-30 17:50:07 -04:00
});
};
2014-10-03 15:28:46 -04:00
2014-10-30 17:50:07 -04:00
SocketModules.chats.canMessage = function(socket, toUid, callback) {
Messaging.canMessage(socket.uid, toUid, function(err, allowed) {
callback(!allowed ? new Error('[[error:chat-restricted]]') : undefined);
2014-01-10 16:00:03 -05:00
});
};
2014-07-19 10:33:27 -04:00
SocketModules.chats.markRead = function(socket, touid, callback) {
Messaging.markRead(socket.uid, touid, function(err) {
if (!err) {
Messaging.pushUnreadCount(socket.uid);
2014-07-19 10:33:27 -04:00
}
});
};
2014-04-15 12:48:28 -04:00
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;
}
2014-09-03 18:07:56 -04:00
server.in('uid_' + data.touid).emit(event, data.fromUid);
2014-04-15 12:48:28 -04:00
}
2014-09-15 16:26:25 -04:00
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;
2014-09-15 16:26:25 -04:00
Messaging.getRecentChats(socket.uid, start, stop, callback);
2014-01-10 16:00:03 -05:00
};
/* 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);
};
2014-04-10 20:31:57 +01:00
module.exports = SocketModules;