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

74 lines
1.4 KiB
JavaScript
Raw Normal View History

2014-07-20 18:44:08 -04:00
'use strict';
var meta = require('../meta'),
user = require('../user'),
topics = require('../topics'),
2014-06-02 17:42:29 -04:00
emitter = require('../emitter'),
2014-07-20 18:44:08 -04:00
websockets = require('./'),
SocketMeta = {
rooms: {}
};
2014-06-02 17:42:29 -04:00
SocketMeta.reconnected = function(socket, data, callback) {
2014-12-03 19:43:15 -05:00
if (socket.uid) {
topics.pushUnreadCount(socket.uid);
user.notifications.pushCount(socket.uid);
}
};
2014-07-20 18:44:08 -04:00
emitter.on('nodebb:ready', function() {
2015-09-25 16:16:07 -04:00
websockets.server.emit('event:nodebb.ready', {
2015-09-11 16:18:32 -04:00
'cache-buster': meta.config['cache-buster']
});
2014-07-20 18:44:08 -04:00
});
/* Rooms */
2014-07-20 18:44:08 -04:00
SocketMeta.rooms.enter = function(socket, data, callback) {
if (!socket.uid) {
2015-05-15 00:02:59 -04:00
return callback();
}
2015-05-14 13:53:02 -04:00
2014-09-06 00:19:46 -04:00
if (!data) {
2014-04-09 21:27:20 -04:00
return callback(new Error('[[error:invalid-data]]'));
2014-01-17 12:52:04 -05:00
}
if (data.enter) {
data.enter = data.enter.toString();
}
2015-05-14 13:53:02 -04:00
if (data.enter && data.enter.startsWith('uid_') && data.enter !== 'uid_' + socket.uid) {
return callback(new Error('[[error:not-allowed]]'));
}
2015-10-20 17:53:44 -04:00
leaveCurrentRoom(socket);
if (data.enter) {
socket.join(data.enter);
socket.currentRoom = data.enter;
}
2015-05-15 00:02:59 -04:00
callback();
};
2015-10-20 17:53:44 -04:00
SocketMeta.rooms.leaveCurrent = function(socket, data, callback) {
if (!socket.uid || !socket.currentRoom) {
return callback();
}
leaveCurrentRoom(socket);
callback();
};
function leaveCurrentRoom(socket) {
if (socket.currentRoom) {
socket.leave(socket.currentRoom);
2015-10-20 17:53:44 -04:00
socket.currentRoom = '';
}
}
2014-04-10 20:31:57 +01:00
module.exports = SocketMeta;