@@ -211,6 +211,7 @@ function getStatus(callback) {
211211 var distinctregaddresses = [ ] ;
212212 Object . keys ( users ) . forEach ( function ( key ) {
213213 var user = users [ key ] ;
214+ if ( ! user ) return ;
214215 var found = false ;
215216 for ( var i = 0 ; i < distinctaddresses . length ; i ++ ) {
216217 if ( user . address == distinctaddresses [ i ] ) {
@@ -306,8 +307,9 @@ function emitOnlineUsers(socket) {
306307
307308function emitUserStatus ( socket ) {
308309 var noteId = socket . noteId ;
309- if ( ! noteId || ! notes [ noteId ] ) return ;
310- var out = buildUserOutData ( users [ socket . id ] ) ;
310+ var user = users [ socket . id ] ;
311+ if ( ! noteId || ! notes [ noteId ] || ! user ) return ;
312+ var out = buildUserOutData ( user ) ;
311313 socket . broadcast . to ( noteId ) . emit ( 'user status' , out ) ;
312314}
313315
@@ -532,7 +534,9 @@ function disconnect(socket) {
532534 var note = notes [ noteId ] ;
533535 if ( note ) {
534536 // delete user in users
535- delete note . users [ socket . id ] ;
537+ if ( note . users [ socket . id ] ) {
538+ delete note . users [ socket . id ] ;
539+ }
536540 // remove sockets in the note socks
537541 do {
538542 var index = note . socks . indexOf ( socket ) ;
@@ -649,14 +653,14 @@ function operationCallback(socket, operation) {
649653 var userId = null ;
650654 // save authors
651655 if ( socket . request . user && socket . request . user . logged_in ) {
652- var socketId = socket . id ;
653- var user = users [ socketId ] ;
656+ var user = users [ socket . id ] ;
657+ if ( ! user ) return ;
654658 userId = socket . request . user . id ;
655659 if ( ! note . authors [ userId ] ) {
656660 models . Author . create ( {
657661 noteId : noteId ,
658662 userId : userId ,
659- color : users [ socketId ] . color
663+ color : user . color
660664 } ) . then ( function ( author ) {
661665 note . authors [ author . userId ] = {
662666 userid : author . userId ,
@@ -743,11 +747,11 @@ function connection(socket) {
743747 //received user status
744748 socket . on ( 'user status' , function ( data ) {
745749 var noteId = socket . noteId ;
746- if ( ! noteId || ! notes [ noteId ] ) return ;
750+ var user = users [ socket . id ] ;
751+ if ( ! noteId || ! notes [ noteId ] || ! user ) return ;
747752 if ( config . debug )
748753 logger . info ( 'SERVER received [' + noteId + '] user status from [' + socket . id + ']: ' + JSON . stringify ( data ) ) ;
749754 if ( data ) {
750- var user = users [ socket . id ] ;
751755 user . idle = data . idle ;
752756 user . type = data . type ;
753757 }
@@ -840,7 +844,9 @@ function connection(socket) {
840844 logger . info ( 'user changed' ) ;
841845 var noteId = socket . noteId ;
842846 if ( ! noteId || ! notes [ noteId ] ) return ;
843- updateUserData ( socket , notes [ noteId ] . users [ socket . id ] ) ;
847+ var user = notes [ noteId ] . users [ socket . id ] ;
848+ if ( ! user ) return ;
849+ updateUserData ( socket , user ) ;
844850 emitOnlineUsers ( socket ) ;
845851 } ) ;
846852
@@ -872,26 +878,29 @@ function connection(socket) {
872878 //received cursor focus
873879 socket . on ( 'cursor focus' , function ( data ) {
874880 var noteId = socket . noteId ;
875- if ( ! noteId || ! notes [ noteId ] ) return ;
876- users [ socket . id ] . cursor = data ;
877- var out = buildUserOutData ( users [ socket . id ] ) ;
881+ var user = users [ socket . id ] ;
882+ if ( ! noteId || ! notes [ noteId ] || ! user ) return ;
883+ user . cursor = data ;
884+ var out = buildUserOutData ( user ) ;
878885 socket . broadcast . to ( noteId ) . emit ( 'cursor focus' , out ) ;
879886 } ) ;
880887
881888 //received cursor activity
882889 socket . on ( 'cursor activity' , function ( data ) {
883890 var noteId = socket . noteId ;
884- if ( ! noteId || ! notes [ noteId ] ) return ;
885- users [ socket . id ] . cursor = data ;
886- var out = buildUserOutData ( users [ socket . id ] ) ;
891+ var user = users [ socket . id ] ;
892+ if ( ! noteId || ! notes [ noteId ] || ! user ) return ;
893+ user . cursor = data ;
894+ var out = buildUserOutData ( user ) ;
887895 socket . broadcast . to ( noteId ) . emit ( 'cursor activity' , out ) ;
888896 } ) ;
889897
890898 //received cursor blur
891899 socket . on ( 'cursor blur' , function ( ) {
892900 var noteId = socket . noteId ;
893- if ( ! noteId || ! notes [ noteId ] ) return ;
894- users [ socket . id ] . cursor = null ;
901+ var user = users [ socket . id ] ;
902+ if ( ! noteId || ! notes [ noteId ] || ! user ) return ;
903+ user . cursor = null ;
895904 var out = {
896905 id : socket . id
897906 } ;
0 commit comments