all files / src/socket.io/ meta.js

22.58% Statements 7/31
0% Branches 0/21
0% Functions 0/5
22.58% Lines 7/31
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74                                                                                                                                     
'use strict';
 
var meta = require('../meta'),
	user = require('../user'),
	topics = require('../topics'),
	emitter = require('../emitter'),
 
	websockets = require('./'),
 
	SocketMeta = {
		rooms: {}
	};
 
SocketMeta.reconnected = function(socket, data, callback) {
	if (socket.uid) {
		topics.pushUnreadCount(socket.uid);
		user.notifications.pushCount(socket.uid);
	}
};
 
emitter.on('nodebb:ready', function() {
	websockets.server.emit('event:nodebb.ready', {
		'cache-buster': meta.config['cache-buster']
	});
});
 
 
/* Rooms */
 
SocketMeta.rooms.enter = function(socket, data, callback) {
	if (!socket.uid) {
		return callback();
	}
 
	if (!data) {
		return callback(new Error('[[error:invalid-data]]'));
	}
 
	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;
	}
	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;