You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.4 KiB
JavaScript

11 years ago
'use strict';
var meta = require('../meta'),
user = require('../user'),
topics = require('../topics'),
11 years ago
emitter = require('../emitter'),
11 years ago
websockets = require('./'),
SocketMeta = {
rooms: {}
};
11 years ago
SocketMeta.reconnected = function(socket, data, callback) {
if (socket.uid) {
topics.pushUnreadCount(socket.uid);
user.notifications.pushCount(socket.uid);
}
};
11 years ago
emitter.on('nodebb:ready', function() {
10 years ago
websockets.server.emit('event:nodebb.ready', {
10 years ago
'cache-buster': meta.config['cache-buster']
});
11 years ago
});
/* Rooms */
11 years ago
SocketMeta.rooms.enter = function(socket, data, callback) {
if (!socket.uid) {
10 years ago
return callback();
}
if (!data) {
return callback(new Error('[[error:invalid-data]]'));
11 years ago
}
if (data.enter) {
data.enter = data.enter.toString();
}
if (data.enter && data.enter.startsWith('uid_') && data.enter !== 'uid_' + socket.uid) {
return callback(new Error('[[error:not-allowed]]'));
}
leaveCurrentRoom(socket);
if (data.enter) {
socket.join(data.enter);
socket.currentRoom = data.enter;
}
10 years ago
callback();
};
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);
socket.currentRoom = '';
}
}
module.exports = SocketMeta;