Fix possible user is undefined in realtime events
This commit is contained in:
parent
1ae5f174c3
commit
d140a140de
1 changed files with 26 additions and 17 deletions
|
@ -211,6 +211,7 @@ function getStatus(callback) {
|
||||||
var distinctregaddresses = [];
|
var distinctregaddresses = [];
|
||||||
Object.keys(users).forEach(function (key) {
|
Object.keys(users).forEach(function (key) {
|
||||||
var user = users[key];
|
var user = users[key];
|
||||||
|
if (!user) return;
|
||||||
var found = false;
|
var found = false;
|
||||||
for (var i = 0; i < distinctaddresses.length; i++) {
|
for (var i = 0; i < distinctaddresses.length; i++) {
|
||||||
if (user.address == distinctaddresses[i]) {
|
if (user.address == distinctaddresses[i]) {
|
||||||
|
@ -306,8 +307,9 @@ function emitOnlineUsers(socket) {
|
||||||
|
|
||||||
function emitUserStatus(socket) {
|
function emitUserStatus(socket) {
|
||||||
var noteId = socket.noteId;
|
var noteId = socket.noteId;
|
||||||
if (!noteId || !notes[noteId]) return;
|
var user = users[socket.id];
|
||||||
var out = buildUserOutData(users[socket.id]);
|
if (!noteId || !notes[noteId] || !user) return;
|
||||||
|
var out = buildUserOutData(user);
|
||||||
socket.broadcast.to(noteId).emit('user status', out);
|
socket.broadcast.to(noteId).emit('user status', out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -532,7 +534,9 @@ function disconnect(socket) {
|
||||||
var note = notes[noteId];
|
var note = notes[noteId];
|
||||||
if (note) {
|
if (note) {
|
||||||
// delete user in users
|
// delete user in users
|
||||||
delete note.users[socket.id];
|
if (note.users[socket.id]) {
|
||||||
|
delete note.users[socket.id];
|
||||||
|
}
|
||||||
// remove sockets in the note socks
|
// remove sockets in the note socks
|
||||||
do {
|
do {
|
||||||
var index = note.socks.indexOf(socket);
|
var index = note.socks.indexOf(socket);
|
||||||
|
@ -649,14 +653,14 @@ function operationCallback(socket, operation) {
|
||||||
var userId = null;
|
var userId = null;
|
||||||
// save authors
|
// save authors
|
||||||
if (socket.request.user && socket.request.user.logged_in) {
|
if (socket.request.user && socket.request.user.logged_in) {
|
||||||
var socketId = socket.id;
|
var user = users[socket.id];
|
||||||
var user = users[socketId];
|
if (!user) return;
|
||||||
userId = socket.request.user.id;
|
userId = socket.request.user.id;
|
||||||
if (!note.authors[userId]) {
|
if (!note.authors[userId]) {
|
||||||
models.Author.create({
|
models.Author.create({
|
||||||
noteId: noteId,
|
noteId: noteId,
|
||||||
userId: userId,
|
userId: userId,
|
||||||
color: users[socketId].color
|
color: user.color
|
||||||
}).then(function (author) {
|
}).then(function (author) {
|
||||||
note.authors[author.userId] = {
|
note.authors[author.userId] = {
|
||||||
userid: author.userId,
|
userid: author.userId,
|
||||||
|
@ -743,11 +747,11 @@ function connection(socket) {
|
||||||
//received user status
|
//received user status
|
||||||
socket.on('user status', function (data) {
|
socket.on('user status', function (data) {
|
||||||
var noteId = socket.noteId;
|
var noteId = socket.noteId;
|
||||||
if (!noteId || !notes[noteId]) return;
|
var user = users[socket.id];
|
||||||
|
if (!noteId || !notes[noteId] || !user) return;
|
||||||
if (config.debug)
|
if (config.debug)
|
||||||
logger.info('SERVER received [' + noteId + '] user status from [' + socket.id + ']: ' + JSON.stringify(data));
|
logger.info('SERVER received [' + noteId + '] user status from [' + socket.id + ']: ' + JSON.stringify(data));
|
||||||
if (data) {
|
if (data) {
|
||||||
var user = users[socket.id];
|
|
||||||
user.idle = data.idle;
|
user.idle = data.idle;
|
||||||
user.type = data.type;
|
user.type = data.type;
|
||||||
}
|
}
|
||||||
|
@ -840,7 +844,9 @@ function connection(socket) {
|
||||||
logger.info('user changed');
|
logger.info('user changed');
|
||||||
var noteId = socket.noteId;
|
var noteId = socket.noteId;
|
||||||
if (!noteId || !notes[noteId]) return;
|
if (!noteId || !notes[noteId]) return;
|
||||||
updateUserData(socket, notes[noteId].users[socket.id]);
|
var user = notes[noteId].users[socket.id];
|
||||||
|
if (!user) return;
|
||||||
|
updateUserData(socket, user);
|
||||||
emitOnlineUsers(socket);
|
emitOnlineUsers(socket);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -872,26 +878,29 @@ function connection(socket) {
|
||||||
//received cursor focus
|
//received cursor focus
|
||||||
socket.on('cursor focus', function (data) {
|
socket.on('cursor focus', function (data) {
|
||||||
var noteId = socket.noteId;
|
var noteId = socket.noteId;
|
||||||
if (!noteId || !notes[noteId]) return;
|
var user = users[socket.id];
|
||||||
users[socket.id].cursor = data;
|
if (!noteId || !notes[noteId] || !user) return;
|
||||||
var out = buildUserOutData(users[socket.id]);
|
user.cursor = data;
|
||||||
|
var out = buildUserOutData(user);
|
||||||
socket.broadcast.to(noteId).emit('cursor focus', out);
|
socket.broadcast.to(noteId).emit('cursor focus', out);
|
||||||
});
|
});
|
||||||
|
|
||||||
//received cursor activity
|
//received cursor activity
|
||||||
socket.on('cursor activity', function (data) {
|
socket.on('cursor activity', function (data) {
|
||||||
var noteId = socket.noteId;
|
var noteId = socket.noteId;
|
||||||
if (!noteId || !notes[noteId]) return;
|
var user = users[socket.id];
|
||||||
users[socket.id].cursor = data;
|
if (!noteId || !notes[noteId] || !user) return;
|
||||||
var out = buildUserOutData(users[socket.id]);
|
user.cursor = data;
|
||||||
|
var out = buildUserOutData(user);
|
||||||
socket.broadcast.to(noteId).emit('cursor activity', out);
|
socket.broadcast.to(noteId).emit('cursor activity', out);
|
||||||
});
|
});
|
||||||
|
|
||||||
//received cursor blur
|
//received cursor blur
|
||||||
socket.on('cursor blur', function () {
|
socket.on('cursor blur', function () {
|
||||||
var noteId = socket.noteId;
|
var noteId = socket.noteId;
|
||||||
if (!noteId || !notes[noteId]) return;
|
var user = users[socket.id];
|
||||||
users[socket.id].cursor = null;
|
if (!noteId || !notes[noteId] || !user) return;
|
||||||
|
user.cursor = null;
|
||||||
var out = {
|
var out = {
|
||||||
id: socket.id
|
id: socket.id
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue