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

148 lines
3.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
2015-07-24 14:25:23 -04:00
async = require('async'),
2014-01-16 20:14:09 -05:00
server = require('./'),
2015-07-24 14:25:23 -04:00
rooms = require('./rooms'),
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
}
Messaging.getMessages({
fromuid: socket.uid,
touid: data.touid,
since: data.since,
isNew: 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
};
2015-07-24 14:25:23 -04:00
SocketModules.chats.sync = function(socket, data, callback) {
var chats = [],
uids = [],
socketIds = rooms.clients('uid_' + socket.uid);
rooms.broadcast(socket, 'uid_' + socket.uid, 'query:chats.sync', {}, function(err, sessionData) {
sessionData.forEach(function(data) {
data.forEach(function(chat) {
if (uids.indexOf(chat.uid) === -1) {
chats.push(chat);
uids.push(chat.uid);
}
});
});
callback(err, chats);
});
};
SocketModules.chats.open = function(socket, data, callback) {
rooms.broadcast(socket, 'uid_' + socket.uid, 'event:chats.open', data);
};
SocketModules.chats.close = function(socket, data, callback) {
rooms.broadcast(socket, 'uid_' + socket.uid, 'event:chats.close', data);
};
SocketModules.chats.toggleNew = function(socket, data, callback) {
rooms.broadcast(socket, 'uid_' + socket.uid, 'event:chats.toggleNew', data);
};
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;