Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Luc Bourdot
codimd
Commits
d140a140
Commit
d140a140
authored
Dec 20, 2016
by
Wu Cheng-Han
Browse files
Fix possible user is undefined in realtime events
parent
1ae5f174
Changes
1
Hide whitespace changes
Inline
Side-by-side
lib/realtime.js
View file @
d140a140
...
...
@@ -211,6 +211,7 @@ function getStatus(callback) {
var
distinctregaddresses
=
[];
Object
.
keys
(
users
).
forEach
(
function
(
key
)
{
var
user
=
users
[
key
];
if
(
!
user
)
return
;
var
found
=
false
;
for
(
var
i
=
0
;
i
<
distinctaddresses
.
length
;
i
++
)
{
if
(
user
.
address
==
distinctaddresses
[
i
])
{
...
...
@@ -306,8 +307,9 @@ function emitOnlineUsers(socket) {
function
emitUserStatus
(
socket
)
{
var
noteId
=
socket
.
noteId
;
if
(
!
noteId
||
!
notes
[
noteId
])
return
;
var
out
=
buildUserOutData
(
users
[
socket
.
id
]);
var
user
=
users
[
socket
.
id
];
if
(
!
noteId
||
!
notes
[
noteId
]
||
!
user
)
return
;
var
out
=
buildUserOutData
(
user
);
socket
.
broadcast
.
to
(
noteId
).
emit
(
'
user status
'
,
out
);
}
...
...
@@ -532,7 +534,9 @@ function disconnect(socket) {
var
note
=
notes
[
noteId
];
if
(
note
)
{
// 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
do
{
var
index
=
note
.
socks
.
indexOf
(
socket
);
...
...
@@ -649,14 +653,14 @@ function operationCallback(socket, operation) {
var
userId
=
null
;
// save authors
if
(
socket
.
request
.
user
&&
socket
.
request
.
user
.
logged_in
)
{
var
socketId
=
socket
.
id
;
var
user
=
users
[
socketId
];
var
user
=
users
[
socket
.
id
]
;
if
(
!
user
)
return
;
userId
=
socket
.
request
.
user
.
id
;
if
(
!
note
.
authors
[
userId
])
{
models
.
Author
.
create
({
noteId
:
noteId
,
userId
:
userId
,
color
:
user
s
[
socketId
]
.
color
color
:
user
.
color
}).
then
(
function
(
author
)
{
note
.
authors
[
author
.
userId
]
=
{
userid
:
author
.
userId
,
...
...
@@ -743,11 +747,11 @@ function connection(socket) {
//received user status
socket
.
on
(
'
user status
'
,
function
(
data
)
{
var
noteId
=
socket
.
noteId
;
if
(
!
noteId
||
!
notes
[
noteId
])
return
;
var
user
=
users
[
socket
.
id
];
if
(
!
noteId
||
!
notes
[
noteId
]
||
!
user
)
return
;
if
(
config
.
debug
)
logger
.
info
(
'
SERVER received [
'
+
noteId
+
'
] user status from [
'
+
socket
.
id
+
'
]:
'
+
JSON
.
stringify
(
data
));
if
(
data
)
{
var
user
=
users
[
socket
.
id
];
user
.
idle
=
data
.
idle
;
user
.
type
=
data
.
type
;
}
...
...
@@ -840,7 +844,9 @@ function connection(socket) {
logger
.
info
(
'
user changed
'
);
var
noteId
=
socket
.
noteId
;
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
);
});
...
...
@@ -872,26 +878,29 @@ function connection(socket) {
//received cursor focus
socket
.
on
(
'
cursor focus
'
,
function
(
data
)
{
var
noteId
=
socket
.
noteId
;
if
(
!
noteId
||
!
notes
[
noteId
])
return
;
users
[
socket
.
id
].
cursor
=
data
;
var
out
=
buildUserOutData
(
users
[
socket
.
id
]);
var
user
=
users
[
socket
.
id
];
if
(
!
noteId
||
!
notes
[
noteId
]
||
!
user
)
return
;
user
.
cursor
=
data
;
var
out
=
buildUserOutData
(
user
);
socket
.
broadcast
.
to
(
noteId
).
emit
(
'
cursor focus
'
,
out
);
});
//received cursor activity
socket
.
on
(
'
cursor activity
'
,
function
(
data
)
{
var
noteId
=
socket
.
noteId
;
if
(
!
noteId
||
!
notes
[
noteId
])
return
;
users
[
socket
.
id
].
cursor
=
data
;
var
out
=
buildUserOutData
(
users
[
socket
.
id
]);
var
user
=
users
[
socket
.
id
];
if
(
!
noteId
||
!
notes
[
noteId
]
||
!
user
)
return
;
user
.
cursor
=
data
;
var
out
=
buildUserOutData
(
user
);
socket
.
broadcast
.
to
(
noteId
).
emit
(
'
cursor activity
'
,
out
);
});
//received cursor blur
socket
.
on
(
'
cursor blur
'
,
function
()
{
var
noteId
=
socket
.
noteId
;
if
(
!
noteId
||
!
notes
[
noteId
])
return
;
users
[
socket
.
id
].
cursor
=
null
;
var
user
=
users
[
socket
.
id
];
if
(
!
noteId
||
!
notes
[
noteId
]
||
!
user
)
return
;
user
.
cursor
=
null
;
var
out
=
{
id
:
socket
.
id
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment