Fix realtime on forbidden not clean up properly and handle on updating note which already been clean up

This commit is contained in:
Wu Cheng-Han 2016-07-30 11:10:43 +08:00
parent 23c53f3d15
commit 725e98288b

View file

@ -80,6 +80,8 @@ var updater = setInterval(function () {
if (note.server.isDirty) { if (note.server.isDirty) {
if (config.debug) logger.info("updater found dirty note: " + key); if (config.debug) logger.info("updater found dirty note: " + key);
updateNote(note, function(err, _note) { updateNote(note, function(err, _note) {
// handle when note already been clean up
if (!notes[key] || !notes[key].server) return callback(null, null);
if (!_note) { if (!_note) {
realtime.io.to(note.id).emit('info', { realtime.io.to(note.id).emit('info', {
code: 404 code: 404
@ -89,7 +91,9 @@ var updater = setInterval(function () {
if (err || !_note) { if (err || !_note) {
for (var i = 0, l = note.socks.length; i < l; i++) { for (var i = 0, l = note.socks.length; i < l; i++) {
var sock = note.socks[i]; var sock = note.socks[i];
sock.disconnect(true); if (typeof sock !== 'undefined' && sock) {
sock.disconnect(true);
}
} }
return callback(err, null); return callback(err, null);
} }
@ -139,6 +143,7 @@ function updateNote(note, callback) {
}); });
} }
function finishUpdateNote(note, _note, callback) { function finishUpdateNote(note, _note, callback) {
if (!note || !note.server) return callback(null, null);
var body = note.server.document; var body = note.server.document;
var title = models.Note.parseNoteTitle(body); var title = models.Note.parseNoteTitle(body);
title = LZString.compressToBase64(title); title = LZString.compressToBase64(title);
@ -329,6 +334,16 @@ function connectNextSocket() {
startConnection(connectionSocketQueue[0]); startConnection(connectionSocketQueue[0]);
} }
function interruptConnection(socket, note, user) {
if (note) delete note;
if (user) delete user;
if (socket)
clearSocketQueue(connectionSocketQueue, socket);
else
connectionSocketQueue.shift();
connectNextSocket();
}
var isConnectionBusy = false; var isConnectionBusy = false;
var connectionSocketQueue = []; var connectionSocketQueue = [];
var isDisconnectBusy = false; var isDisconnectBusy = false;
@ -337,20 +352,14 @@ var disconnectSocketQueue = [];
function finishConnection(socket, note, user) { function finishConnection(socket, note, user) {
// if no valid info provided will drop the client // if no valid info provided will drop the client
if (!socket || !note || !user) { if (!socket || !note || !user) {
if (note) delete note; return interruptConnection(socket, note, user);
if (user) delete user;
if (socket)
clearSocketQueue(connectionSocketQueue, socket);
else
connectionSocketQueue.shift();
connectNextSocket();
return;
} }
//check view permission //check view permission
if (note.permission == 'private') { if (note.permission == 'private') {
if (socket.request.user && socket.request.user.logged_in && socket.request.user.id == note.owner) { if (socket.request.user && socket.request.user.logged_in && socket.request.user.id == note.owner) {
//na //na
} else { } else {
interruptConnection(socket, note, user);
return failConnection(403, 'connection forbidden', socket); return failConnection(403, 'connection forbidden', socket);
} }
} }