fix: Cannot read properties of undefined (reading 'id')
This commit is contained in:
@@ -7,6 +7,7 @@ import env from "@server/env";
|
|||||||
import Logger from "@server/logging/Logger";
|
import Logger from "@server/logging/Logger";
|
||||||
import { trace } from "@server/logging/tracing";
|
import { trace } from "@server/logging/tracing";
|
||||||
import { TooManyConnections } from "./CloseEvents";
|
import { TooManyConnections } from "./CloseEvents";
|
||||||
|
import { withContext } from "./types";
|
||||||
|
|
||||||
@trace()
|
@trace()
|
||||||
export class ConnectionLimitExtension implements Extension {
|
export class ConnectionLimitExtension implements Extension {
|
||||||
@@ -19,9 +20,7 @@ export class ConnectionLimitExtension implements Extension {
|
|||||||
* onDisconnect hook
|
* onDisconnect hook
|
||||||
* @param data The disconnect payload
|
* @param data The disconnect payload
|
||||||
*/
|
*/
|
||||||
onDisconnect(data: onDisconnectPayload) {
|
onDisconnect({ documentName, socketId }: withContext<onDisconnectPayload>) {
|
||||||
const { documentName, socketId } = data;
|
|
||||||
|
|
||||||
const connections = this.connectionsByDocument.get(documentName);
|
const connections = this.connectionsByDocument.get(documentName);
|
||||||
if (connections) {
|
if (connections) {
|
||||||
connections.delete(socketId);
|
connections.delete(socketId);
|
||||||
@@ -45,9 +44,7 @@ export class ConnectionLimitExtension implements Extension {
|
|||||||
* onConnect hook
|
* onConnect hook
|
||||||
* @param data The connect payload
|
* @param data The connect payload
|
||||||
*/
|
*/
|
||||||
onConnect(data: onConnectPayload) {
|
onConnect({ documentName, socketId }: withContext<onConnectPayload>) {
|
||||||
const { documentName } = data;
|
|
||||||
|
|
||||||
const connections =
|
const connections =
|
||||||
this.connectionsByDocument.get(documentName) || new Set();
|
this.connectionsByDocument.get(documentName) || new Set();
|
||||||
if (connections?.size >= env.COLLABORATION_MAX_CLIENTS_PER_DOCUMENT) {
|
if (connections?.size >= env.COLLABORATION_MAX_CLIENTS_PER_DOCUMENT) {
|
||||||
@@ -60,7 +57,7 @@ export class ConnectionLimitExtension implements Extension {
|
|||||||
return Promise.reject(TooManyConnections);
|
return Promise.reject(TooManyConnections);
|
||||||
}
|
}
|
||||||
|
|
||||||
connections.add(data.socketId);
|
connections.add(socketId);
|
||||||
this.connectionsByDocument.set(documentName, connections);
|
this.connectionsByDocument.set(documentName, connections);
|
||||||
|
|
||||||
Logger.debug(
|
Logger.debug(
|
||||||
|
|||||||
@@ -5,19 +5,20 @@ import {
|
|||||||
Extension,
|
Extension,
|
||||||
} from "@hocuspocus/server";
|
} from "@hocuspocus/server";
|
||||||
import Logger from "@server/logging/Logger";
|
import Logger from "@server/logging/Logger";
|
||||||
|
import { withContext } from "./types";
|
||||||
|
|
||||||
export default class LoggerExtension implements Extension {
|
export default class LoggerExtension implements Extension {
|
||||||
async onLoadDocument(data: onLoadDocumentPayload) {
|
async onLoadDocument(data: withContext<onLoadDocumentPayload>) {
|
||||||
Logger.info("multiplayer", `Loaded document "${data.documentName}"`, {
|
Logger.info("multiplayer", `Loaded document "${data.documentName}"`, {
|
||||||
userId: data.context.user?.id,
|
userId: data.context.user?.id,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async onConnect(data: onConnectPayload) {
|
async onConnect(data: withContext<onConnectPayload>) {
|
||||||
Logger.info("multiplayer", `New connection to "${data.documentName}"`);
|
Logger.info("multiplayer", `New connection to "${data.documentName}"`);
|
||||||
}
|
}
|
||||||
|
|
||||||
async onDisconnect(data: onDisconnectPayload) {
|
async onDisconnect(data: withContext<onDisconnectPayload>) {
|
||||||
Logger.info("multiplayer", `Closed connection to "${data.documentName}"`, {
|
Logger.info("multiplayer", `Closed connection to "${data.documentName}"`, {
|
||||||
userId: data.context.user?.id,
|
userId: data.context.user?.id,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,9 +6,13 @@ import {
|
|||||||
Extension,
|
Extension,
|
||||||
} from "@hocuspocus/server";
|
} from "@hocuspocus/server";
|
||||||
import Metrics from "@server/logging/Metrics";
|
import Metrics from "@server/logging/Metrics";
|
||||||
|
import { withContext } from "./types";
|
||||||
|
|
||||||
export default class MetricsExtension implements Extension {
|
export default class MetricsExtension implements Extension {
|
||||||
async onLoadDocument({ documentName, instance }: onLoadDocumentPayload) {
|
async onLoadDocument({
|
||||||
|
documentName,
|
||||||
|
instance,
|
||||||
|
}: withContext<onLoadDocumentPayload>) {
|
||||||
Metrics.increment("collaboration.load_document", {
|
Metrics.increment("collaboration.load_document", {
|
||||||
documentName,
|
documentName,
|
||||||
});
|
});
|
||||||
@@ -24,7 +28,7 @@ export default class MetricsExtension implements Extension {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async onConnect({ documentName, instance }: onConnectPayload) {
|
async onConnect({ documentName, instance }: withContext<onConnectPayload>) {
|
||||||
Metrics.increment("collaboration.connect", {
|
Metrics.increment("collaboration.connect", {
|
||||||
documentName,
|
documentName,
|
||||||
});
|
});
|
||||||
@@ -38,7 +42,10 @@ export default class MetricsExtension implements Extension {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async onDisconnect({ documentName, instance }: onDisconnectPayload) {
|
async onDisconnect({
|
||||||
|
documentName,
|
||||||
|
instance,
|
||||||
|
}: withContext<onDisconnectPayload>) {
|
||||||
Metrics.increment("collaboration.disconnect", {
|
Metrics.increment("collaboration.disconnect", {
|
||||||
documentName,
|
documentName,
|
||||||
});
|
});
|
||||||
@@ -52,7 +59,7 @@ export default class MetricsExtension implements Extension {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async onStoreDocument({ documentName }: onChangePayload) {
|
async onStoreDocument({ documentName }: withContext<onChangePayload>) {
|
||||||
Metrics.increment("collaboration.change", {
|
Metrics.increment("collaboration.change", {
|
||||||
documentName,
|
documentName,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import Document from "@server/models/Document";
|
|||||||
import ProsemirrorHelper from "@server/models/helpers/ProsemirrorHelper";
|
import ProsemirrorHelper from "@server/models/helpers/ProsemirrorHelper";
|
||||||
import { sequelize } from "@server/storage/database";
|
import { sequelize } from "@server/storage/database";
|
||||||
import documentCollaborativeUpdater from "../commands/documentCollaborativeUpdater";
|
import documentCollaborativeUpdater from "../commands/documentCollaborativeUpdater";
|
||||||
|
import { withContext } from "./types";
|
||||||
|
|
||||||
@trace()
|
@trace()
|
||||||
export default class PersistenceExtension implements Extension {
|
export default class PersistenceExtension implements Extension {
|
||||||
@@ -20,7 +21,10 @@ export default class PersistenceExtension implements Extension {
|
|||||||
*/
|
*/
|
||||||
documentCollaboratorIds = new Map<string, Set<string>>();
|
documentCollaboratorIds = new Map<string, Set<string>>();
|
||||||
|
|
||||||
async onLoadDocument({ documentName, ...data }: onLoadDocumentPayload) {
|
async onLoadDocument({
|
||||||
|
documentName,
|
||||||
|
...data
|
||||||
|
}: withContext<onLoadDocumentPayload>) {
|
||||||
const [, documentId] = documentName.split(".");
|
const [, documentId] = documentName.split(".");
|
||||||
const fieldName = "default";
|
const fieldName = "default";
|
||||||
|
|
||||||
@@ -67,14 +71,16 @@ export default class PersistenceExtension implements Extension {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async onChange({ context, documentName }: onChangePayload) {
|
async onChange({ context, documentName }: withContext<onChangePayload>) {
|
||||||
Logger.debug(
|
Logger.debug(
|
||||||
"multiplayer",
|
"multiplayer",
|
||||||
`${context.user?.name} changed ${documentName}`
|
`${context.user?.name} changed ${documentName}`
|
||||||
);
|
);
|
||||||
|
|
||||||
const state = this.documentCollaboratorIds.get(documentName) ?? new Set();
|
const state = this.documentCollaboratorIds.get(documentName) ?? new Set();
|
||||||
state.add(context.user?.id);
|
if (context.user) {
|
||||||
|
state.add(context.user.id);
|
||||||
|
}
|
||||||
this.documentCollaboratorIds.set(documentName, state);
|
this.documentCollaboratorIds.set(documentName, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { Minute } from "@shared/utils/time";
|
|||||||
import Logger from "@server/logging/Logger";
|
import Logger from "@server/logging/Logger";
|
||||||
import { trace } from "@server/logging/tracing";
|
import { trace } from "@server/logging/tracing";
|
||||||
import { View } from "@server/models";
|
import { View } from "@server/models";
|
||||||
|
import { withContext } from "./types";
|
||||||
|
|
||||||
@trace()
|
@trace()
|
||||||
export class ViewsExtension implements Extension {
|
export class ViewsExtension implements Extension {
|
||||||
@@ -21,7 +22,15 @@ export class ViewsExtension implements Extension {
|
|||||||
*
|
*
|
||||||
* @param data The change payload
|
* @param data The change payload
|
||||||
*/
|
*/
|
||||||
async onChange({ documentName, context, socketId }: onChangePayload) {
|
async onChange({
|
||||||
|
documentName,
|
||||||
|
context,
|
||||||
|
socketId,
|
||||||
|
}: withContext<onChangePayload>) {
|
||||||
|
if (!context.user) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const lastUpdate = this.lastViewBySocket.get(socketId);
|
const lastUpdate = this.lastViewBySocket.get(socketId);
|
||||||
const [, documentId] = documentName.split(".");
|
const [, documentId] = documentName.split(".");
|
||||||
|
|
||||||
|
|||||||
7
server/collaboration/types.ts
Normal file
7
server/collaboration/types.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import type { User } from "@server/models";
|
||||||
|
|
||||||
|
export type withContext<T> = Omit<T, "context"> & {
|
||||||
|
context: {
|
||||||
|
user?: User;
|
||||||
|
};
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user