fix: Document editors are sometimes not included in insights/collaboratorIds, closes #7613

This commit is contained in:
Tom Moor
2024-09-21 15:30:56 -04:00
parent 2fd18f7fdb
commit 176cfff7f8
2 changed files with 16 additions and 12 deletions

View File

@@ -111,16 +111,14 @@ export default class PersistenceExtension implements Extension {
return;
}
const collaboratorIds = Array.from(documentCollaboratorIds.values());
const sessionCollaboratorIds = Array.from(documentCollaboratorIds.values());
this.documentCollaboratorIds.delete(documentName);
try {
await documentCollaborativeUpdater({
documentId,
ydoc: document,
// TODO: Right now we're attributing all changes to the last editor,
// It would be nice in the future to have multiple editors per revision.
userId: collaboratorIds.pop(),
sessionCollaboratorIds,
isLastConnection: clientsCount === 0,
});
} catch (err) {

View File

@@ -10,20 +10,20 @@ import { Document, Event } from "@server/models";
import { sequelize } from "@server/storage/database";
type Props = {
/** The document ID to update */
/** The document ID to update. */
documentId: string;
/** Current collaobrative state */
/** Current collaobrative state. */
ydoc: Y.Doc;
/** The user ID that is performing the update, if known */
userId?: string;
/** Whether the last connection to the document left */
/** The user IDs that have modified the document since it was last persisted. */
sessionCollaboratorIds: string[];
/** Whether the last connection to the document left. */
isLastConnection: boolean;
};
export default async function documentCollaborativeUpdater({
documentId,
ydoc,
userId,
sessionCollaboratorIds,
isLastConnection,
}: Props) {
return sequelize.transaction(async (transaction) => {
@@ -47,7 +47,9 @@ export default async function documentCollaborativeUpdater({
const node = Node.fromJSON(schema, content);
const text = serializer.serialize(node, undefined);
const isUnchanged = isEqual(document.content, content);
const lastModifiedById = userId ?? document.lastModifiedById;
const lastModifiedById =
sessionCollaboratorIds[sessionCollaboratorIds.length - 1] ??
document.lastModifiedById;
if (isUnchanged) {
return;
@@ -61,7 +63,11 @@ export default async function documentCollaborativeUpdater({
// extract collaborators from doc user data
const pud = new Y.PermanentUserData(ydoc);
const pudIds = Array.from(pud.clients.values());
const collaboratorIds = uniq([...document.collaboratorIds, ...pudIds]);
const collaboratorIds = uniq([
...document.collaboratorIds,
...sessionCollaboratorIds,
...pudIds,
]);
await document.update(
{