mirror of
https://github.com/outline/outline.git
synced 2026-11-30 09:00:18 +03:00
fix: Race condition when setting up socket listeners
This commit is contained in:
1 parent
d8bc6515dd
commit
a28dfa77ee
1 file changed
+199
-179
+199
-179
@@ -31,21 +31,6 @@ io.adapter(
|
||||
})
|
||||
);
|
||||
|
||||
// receive multiplayer "sync" messages from other nodes (awareness and doc updates),
|
||||
// applies data to doc if in memory otherwise the request is ignored
|
||||
io.of("/").adapter.customHook = (event, callback) => {
|
||||
io.of("/").clients((err, socketIds) => {
|
||||
if (!socketIds.includes(event.socketId)) {
|
||||
multiplayer.handleRemoteSync(
|
||||
event.socketId,
|
||||
event.documentId,
|
||||
event.data
|
||||
);
|
||||
}
|
||||
});
|
||||
callback(true);
|
||||
};
|
||||
|
||||
SocketAuth(io, {
|
||||
authenticate: async (socket, data, callback) => {
|
||||
const { token } = data;
|
||||
@@ -81,173 +66,208 @@ SocketAuth(io, {
|
||||
|
||||
// join all of the rooms at once
|
||||
socket.join(rooms);
|
||||
|
||||
socket.on("sync", (event) => {
|
||||
// handleJoin must be called before handleSync to ensure authentication
|
||||
// to communicate changes for the document/socket combo. Messages received
|
||||
// before handleJoin will be logged and discarded.
|
||||
multiplayer.handleSync(
|
||||
socket,
|
||||
event.documentId,
|
||||
new Uint8Array(event.data)
|
||||
);
|
||||
|
||||
// forward "sync" messages to all nodes (awareness and doc updates) so
|
||||
// that any docs held in memory can be kept up to date.
|
||||
// TODO: optimize by proactively keeping track of which nodes have doc in
|
||||
// memory? Perf gains for large horizontal scaling.
|
||||
io.of("/").adapter.customRequest(
|
||||
{
|
||||
socketId: socket.id,
|
||||
documentId: event.documentId,
|
||||
data: event.data,
|
||||
},
|
||||
(err) => {
|
||||
if (err) {
|
||||
log(err);
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
// allow the client to request to join rooms
|
||||
socket.on("join", async (event) => {
|
||||
log("join", event.documentId, socket.id);
|
||||
// user is joining a collection channel, because their permissions have
|
||||
// changed, granting them access.
|
||||
if (event.collectionId) {
|
||||
const collection = await Collection.scope({
|
||||
method: ["withMembership", user.id],
|
||||
}).findByPk(event.collectionId);
|
||||
|
||||
if (can(user, "read", collection)) {
|
||||
socket.join(`collection-${event.collectionId}`);
|
||||
}
|
||||
}
|
||||
|
||||
// user is joining a document channel, because they have navigated to
|
||||
// view a document.
|
||||
if (event.documentId) {
|
||||
const team = await Team.findByPk(user.teamId);
|
||||
const document = await Document.findByPk(event.documentId, {
|
||||
userId: user.id,
|
||||
});
|
||||
|
||||
if (can(user, "read", document)) {
|
||||
const room = `document-${event.documentId}`;
|
||||
|
||||
// new logic for multiplayer editing completely changes "presence"
|
||||
// detection and propagation, so split at a high-level here.
|
||||
if (team.multiplayerEditor) {
|
||||
log("joined multiplayer", socket.id);
|
||||
socket.join(room, () => {
|
||||
socket.emit("user.join", {
|
||||
userId: user.id,
|
||||
documentId: event.documentId,
|
||||
});
|
||||
|
||||
multiplayer.handleJoin({
|
||||
io,
|
||||
document,
|
||||
socket: socket,
|
||||
documentId: event.documentId,
|
||||
});
|
||||
});
|
||||
} else {
|
||||
// old deprecated logic to be removed in the future once multiplayer
|
||||
// has stabilized
|
||||
|
||||
await View.touch(event.documentId, user.id);
|
||||
const editing = await View.findRecentlyEditingByDocument(
|
||||
event.documentId
|
||||
);
|
||||
|
||||
socket.join(room, () => {
|
||||
// let everyone else in the room know that a new user joined
|
||||
io.to(room).emit("user.join", {
|
||||
userId: user.id,
|
||||
documentId: event.documentId,
|
||||
isEditing: event.isEditing,
|
||||
});
|
||||
|
||||
// let this user know who else is already present in the room
|
||||
io.in(room).clients(async (err, socketIds) => {
|
||||
if (err) throw err;
|
||||
|
||||
// because a single user can have multiple socket connections we
|
||||
// need to make sure that only unique userIds are returned. A Map
|
||||
// makes this easy.
|
||||
let userIds = new Map();
|
||||
for (const socketId of socketIds) {
|
||||
const userId = await client.hget(socketId, "userId");
|
||||
userIds.set(userId, userId);
|
||||
}
|
||||
socket.emit("document.presence", {
|
||||
documentId: event.documentId,
|
||||
userIds: Array.from(userIds.keys()),
|
||||
editingIds: editing.map((view) => view.userId),
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// allow the client to request to leave rooms
|
||||
socket.on("leave", (event) => {
|
||||
if (event.collectionId) {
|
||||
socket.leave(`collection-${event.collectionId}`);
|
||||
}
|
||||
if (event.documentId) {
|
||||
const room = `document-${event.documentId}`;
|
||||
socket.leave(room, () => {
|
||||
io.to(room).emit("user.leave", {
|
||||
userId: user.id,
|
||||
documentId: event.documentId,
|
||||
});
|
||||
});
|
||||
|
||||
multiplayer.handleLeave(socket.id, event.documentId);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("disconnecting", () => {
|
||||
const rooms = Object.keys(socket.rooms);
|
||||
|
||||
rooms.forEach((room) => {
|
||||
if (room.startsWith("document-")) {
|
||||
const documentId = room.replace("document-", "");
|
||||
io.to(room).emit("user.leave", {
|
||||
userId: user.id,
|
||||
documentId,
|
||||
});
|
||||
multiplayer.handleLeave(socket.id, documentId);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
socket.on("presence", async (event) => {
|
||||
const room = `document-${event.documentId}`;
|
||||
|
||||
if (event.documentId && socket.rooms[room]) {
|
||||
const view = await View.touch(
|
||||
event.documentId,
|
||||
user.id,
|
||||
event.isEditing
|
||||
);
|
||||
view.user = user;
|
||||
|
||||
io.to(room).emit("user.presence", {
|
||||
userId: user.id,
|
||||
documentId: event.documentId,
|
||||
isEditing: event.isEditing,
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
// receive multiplayer "sync" messages from other nodes (awareness and doc updates),
|
||||
// applies data to doc if in memory otherwise the request is ignored
|
||||
io.of("/").adapter.customHook = (event, callback) => {
|
||||
io.of("/").clients((err, socketIds) => {
|
||||
if (!socketIds.includes(event.socketId)) {
|
||||
multiplayer.handleRemoteSync(
|
||||
event.socketId,
|
||||
event.documentId,
|
||||
event.data
|
||||
);
|
||||
}
|
||||
});
|
||||
callback(true);
|
||||
};
|
||||
|
||||
io.on("connection", (socket) => {
|
||||
socket.on("sync", (event) => {
|
||||
if (!socket.auth) {
|
||||
return;
|
||||
}
|
||||
|
||||
// handleJoin must be called before handleSync to ensure authentication
|
||||
// to communicate changes for the document/socket combo. Messages received
|
||||
// before handleJoin will be logged and discarded.
|
||||
multiplayer.handleSync(
|
||||
socket,
|
||||
event.documentId,
|
||||
new Uint8Array(event.data)
|
||||
);
|
||||
|
||||
// forward "sync" messages to all nodes (awareness and doc updates) so
|
||||
// that any docs held in memory can be kept up to date.
|
||||
// TODO: optimize by proactively keeping track of which nodes have doc in
|
||||
// memory? Perf gains for large horizontal scaling.
|
||||
io.of("/").adapter.customRequest(
|
||||
{
|
||||
socketId: socket.id,
|
||||
documentId: event.documentId,
|
||||
data: event.data,
|
||||
},
|
||||
(err) => {
|
||||
if (err) {
|
||||
log(err);
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
// allow the client to request to join rooms
|
||||
socket.on("join", async (event) => {
|
||||
if (!socket.auth) {
|
||||
return;
|
||||
}
|
||||
|
||||
log("join", event.documentId, socket.id);
|
||||
const { user } = socket.client;
|
||||
|
||||
// user is joining a collection channel, because their permissions have
|
||||
// changed, granting them access.
|
||||
if (event.collectionId) {
|
||||
const collection = await Collection.scope({
|
||||
method: ["withMembership", user.id],
|
||||
}).findByPk(event.collectionId);
|
||||
|
||||
if (can(user, "read", collection)) {
|
||||
socket.join(`collection-${event.collectionId}`);
|
||||
}
|
||||
}
|
||||
|
||||
// user is joining a document channel, because they have navigated to
|
||||
// view a document.
|
||||
if (event.documentId) {
|
||||
const team = await Team.findByPk(user.teamId);
|
||||
const document = await Document.findByPk(event.documentId, {
|
||||
userId: user.id,
|
||||
});
|
||||
|
||||
if (can(user, "read", document)) {
|
||||
const room = `document-${event.documentId}`;
|
||||
|
||||
// new logic for multiplayer editing completely changes "presence"
|
||||
// detection and propagation, so split at a high-level here.
|
||||
if (team.multiplayerEditor) {
|
||||
log("joined multiplayer", socket.id);
|
||||
socket.join(room, () => {
|
||||
socket.emit("user.join", {
|
||||
userId: user.id,
|
||||
documentId: event.documentId,
|
||||
});
|
||||
|
||||
multiplayer.handleJoin({
|
||||
io,
|
||||
document,
|
||||
socket: socket,
|
||||
documentId: event.documentId,
|
||||
});
|
||||
});
|
||||
} else {
|
||||
// old deprecated logic to be removed in the future once multiplayer
|
||||
// has stabilized
|
||||
|
||||
await View.touch(event.documentId, user.id);
|
||||
const editing = await View.findRecentlyEditingByDocument(
|
||||
event.documentId
|
||||
);
|
||||
|
||||
socket.join(room, () => {
|
||||
// let everyone else in the room know that a new user joined
|
||||
io.to(room).emit("user.join", {
|
||||
userId: user.id,
|
||||
documentId: event.documentId,
|
||||
isEditing: event.isEditing,
|
||||
});
|
||||
|
||||
// let this user know who else is already present in the room
|
||||
io.in(room).clients(async (err, socketIds) => {
|
||||
if (err) throw err;
|
||||
|
||||
// because a single user can have multiple socket connections we
|
||||
// need to make sure that only unique userIds are returned. A Map
|
||||
// makes this easy.
|
||||
let userIds = new Map();
|
||||
for (const socketId of socketIds) {
|
||||
const userId = await client.hget(socketId, "userId");
|
||||
userIds.set(userId, userId);
|
||||
}
|
||||
socket.emit("document.presence", {
|
||||
documentId: event.documentId,
|
||||
userIds: Array.from(userIds.keys()),
|
||||
editingIds: editing.map((view) => view.userId),
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// allow the client to request to leave rooms
|
||||
socket.on("leave", (event) => {
|
||||
if (!socket.auth) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.collectionId) {
|
||||
socket.leave(`collection-${event.collectionId}`);
|
||||
}
|
||||
|
||||
if (event.documentId) {
|
||||
const room = `document-${event.documentId}`;
|
||||
socket.leave(room, () => {
|
||||
io.to(room).emit("user.leave", {
|
||||
userId: socket.client.user.id,
|
||||
documentId: event.documentId,
|
||||
});
|
||||
});
|
||||
|
||||
multiplayer.handleLeave(socket.id, event.documentId);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("disconnecting", () => {
|
||||
if (!socket.auth) {
|
||||
return;
|
||||
}
|
||||
const rooms = Object.keys(socket.rooms);
|
||||
|
||||
rooms.forEach((room) => {
|
||||
if (room.startsWith("document-")) {
|
||||
const documentId = room.replace("document-", "");
|
||||
io.to(room).emit("user.leave", {
|
||||
userId: socket.client.user.id,
|
||||
documentId,
|
||||
});
|
||||
multiplayer.handleLeave(socket.id, documentId);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
socket.on("presence", async (event) => {
|
||||
if (!socket.auth) {
|
||||
return;
|
||||
}
|
||||
const room = `document-${event.documentId}`;
|
||||
const { user } = socket.client;
|
||||
|
||||
if (event.documentId && socket.rooms[room]) {
|
||||
const view = await View.touch(event.documentId, user.id, event.isEditing);
|
||||
view.user = user;
|
||||
|
||||
io.to(room).emit("user.presence", {
|
||||
userId: user.id,
|
||||
documentId: event.documentId,
|
||||
isEditing: event.isEditing,
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
server.on("error", (err) => {
|
||||
throw err;
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user