From 81d9bed9ba951c47063302a2f3b40da78ef8bec4 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 5 Aug 2026 20:07:03 -0400 Subject: [PATCH] feat: Attribute revisions to the auth type they were made with (#13312) Adds a sourceMetadata JSON column to revisions, populated with the authentication type of the request that produced the edit, so the document history sidebar can show "Tom edited via MCP" --- app/models/Revision.ts | 12 ++++++++- .../components/History/EventListItem.tsx | 17 +++--------- .../components/History/RevisionListItem.tsx | 5 ++-- .../Document/components/History/utils.ts | 26 +++++++++++++++++++ server/commands/revisionCreator.test.ts | 1 + ...805120000-add-revisions-source-metadata.js | 15 +++++++++++ server/models/Revision.ts | 11 +++++++- server/presenters/revision.ts | 5 ++++ shared/i18n/locales/en_US/translation.json | 2 +- shared/types.ts | 2 ++ 10 files changed, 77 insertions(+), 19 deletions(-) create mode 100644 server/migrations/20260805120000-add-revisions-source-metadata.js diff --git a/app/models/Revision.ts b/app/models/Revision.ts index 4017f373eb..0696616c8e 100644 --- a/app/models/Revision.ts +++ b/app/models/Revision.ts @@ -1,5 +1,9 @@ import { computed, observable } from "mobx"; -import { type ExportContentType, type ProsemirrorData } from "@shared/types"; +import { + type AuthenticationType, + type ExportContentType, + type ProsemirrorData, +} from "@shared/types"; import { isRTL } from "@shared/utils/rtl"; import Document from "./Document"; import User from "./User"; @@ -44,6 +48,12 @@ class Revision extends ParanoidModel { @observable html: string; + /** Metadata about how the revision came to be */ + sourceMetadata?: { + /** The type of authentication used to create the revision */ + authType?: AuthenticationType; + }; + /** @deprecated The ID of the user who created the revision */ createdById: string; diff --git a/app/scenes/Document/components/History/EventListItem.tsx b/app/scenes/Document/components/History/EventListItem.tsx index 1bd0f279a4..341ee8f062 100644 --- a/app/scenes/Document/components/History/EventListItem.tsx +++ b/app/scenes/Document/components/History/EventListItem.tsx @@ -13,24 +13,17 @@ import { useTranslation } from "react-i18next"; import styled, { css } from "styled-components"; import { s } from "@shared/styles"; import Text from "@shared/components/Text"; -import { AuthenticationType } from "@shared/types"; import type Document from "~/models/Document"; import type Event from "~/models/Event"; import Time from "~/components/Time"; import Logger from "~/utils/Logger"; +import { authTypeSuffix } from "./utils"; type Props = { document: Document; item: Event; }; -/** Authentication types that are surfaced in history, mapped to a display name. */ -const authTypeNames: Partial> = { - [AuthenticationType.API]: "API", - [AuthenticationType.OAUTH]: "API", - [AuthenticationType.MCP]: "MCP", -}; - const EventListItem = ({ item }: Props) => { const { t } = useTranslation(); const opts = { @@ -96,8 +89,6 @@ const EventListItem = ({ item }: Props) => { return null; } - const authTypeName = item.authType ? authTypeNames[item.authType] : undefined; - return ( @@ -105,10 +96,8 @@ const EventListItem = ({ item }: Props) => { {meta} - {authTypeName - ? ` ${t("via {{ authType }}", { authType: authTypeName })}` - : ""}{" "} - · ); diff --git a/app/scenes/Document/components/History/RevisionListItem.tsx b/app/scenes/Document/components/History/RevisionListItem.tsx index 4191caf534..87576d3343 100644 --- a/app/scenes/Document/components/History/RevisionListItem.tsx +++ b/app/scenes/Document/components/History/RevisionListItem.tsx @@ -29,7 +29,7 @@ import { documentHistoryPath } from "~/utils/routeHelpers"; import { EventItem, lineStyle } from "./EventListItem"; import Facepile from "~/components/Facepile"; import Text from "~/components/Text"; -import { revisionCollaboratorText } from "./utils"; +import { authTypeSuffix, revisionCollaboratorText } from "./utils"; type Props = { document: Document; @@ -76,9 +76,10 @@ const RevisionListItem = ({ item, document, ...rest }: Props) => { meta = isLatestRevision ? ( <> {t("Current version")} · {collaboratorText} + {authTypeSuffix(item.sourceMetadata?.authType, t)} ) : ( - t("{{userName}} edited", { userName: collaboratorText }) + `${t("{{userName}} edited", { userName: collaboratorText })}${authTypeSuffix(item.sourceMetadata?.authType, t)}` ); to = { pathname: documentHistoryPath( diff --git a/app/scenes/Document/components/History/utils.ts b/app/scenes/Document/components/History/utils.ts index 2493c0647d..f3ad7d954d 100644 --- a/app/scenes/Document/components/History/utils.ts +++ b/app/scenes/Document/components/History/utils.ts @@ -1,6 +1,32 @@ import type { TFunction } from "i18next"; +import { AuthenticationType } from "@shared/types"; import type Revision from "~/models/Revision"; +/** Authentication types that are surfaced in history, mapped to a display name. */ +const authTypeNames: Partial> = { + [AuthenticationType.API]: "API", + [AuthenticationType.OAUTH]: "API", + [AuthenticationType.MCP]: "MCP", +}; + +/** + * Returns a suffix describing how the change was made, for authentication + * types that are worth surfacing in history. + * + * @param authType the authentication type the change was made with. + * @param t translation function. + * @returns the suffix, or an empty string if it should not be surfaced. + */ +export function authTypeSuffix( + authType: AuthenticationType | null | undefined, + t: TFunction +): string { + const authTypeName = authType ? authTypeNames[authType] : undefined; + return authTypeName + ? ` ${t("via {{ authType }}", { authType: authTypeName })}` + : ""; +} + /** * Returns a human-readable summary of who collaborated on a revision. Uses the * collaborator list when available, falling back to the creator's name. diff --git a/server/commands/revisionCreator.test.ts b/server/commands/revisionCreator.test.ts index c791a36b96..f5271d3d7b 100644 --- a/server/commands/revisionCreator.test.ts +++ b/server/commands/revisionCreator.test.ts @@ -26,6 +26,7 @@ describe("revisionCreator", () => { expect(revision.documentId).toEqual(document.id); expect(revision.userId).toEqual(user.id); expect(revision.createdAt).toEqual(document.updatedAt); + expect(revision.sourceMetadata?.authType).toEqual(AuthenticationType.APP); expect(event!.name).toEqual("revisions.create"); expect(event!.modelId).toEqual(revision.id); expect(event!.authType).toEqual(AuthenticationType.APP); diff --git a/server/migrations/20260805120000-add-revisions-source-metadata.js b/server/migrations/20260805120000-add-revisions-source-metadata.js new file mode 100644 index 0000000000..91b90e9878 --- /dev/null +++ b/server/migrations/20260805120000-add-revisions-source-metadata.js @@ -0,0 +1,15 @@ +"use strict"; + +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.addColumn("revisions", "sourceMetadata", { + type: Sequelize.JSONB, + allowNull: true, + }); + }, + + async down(queryInterface) { + await queryInterface.removeColumn("revisions", "sourceMetadata"); + }, +}; diff --git a/server/models/Revision.ts b/server/models/Revision.ts index 8d9f8bac5c..c81fc03017 100644 --- a/server/models/Revision.ts +++ b/server/models/Revision.ts @@ -1,6 +1,7 @@ import type { InferAttributes, InferCreationAttributes } from "sequelize"; import { Op } from "sequelize"; import { + AllowNull, DataType, BelongsTo, Column, @@ -11,7 +12,7 @@ import { Length as SimpleLength, BeforeDestroy, } from "sequelize-typescript"; -import type { ProsemirrorData } from "@shared/types"; +import type { ProsemirrorData, SourceMetadata } from "@shared/types"; import { DocumentValidation, RevisionValidation } from "@shared/validations"; import type { APIContext } from "@server/types"; import Document from "./Document"; @@ -94,6 +95,12 @@ class Revision extends ParanoidModel< @SkipChangeset color: string | null; + /** Metadata about how the revision came to be, such as the authentication type used. */ + @AllowNull + @Column(DataType.JSONB) + @SkipChangeset + sourceMetadata: SourceMetadata | null; + // associations @BelongsTo(() => Document, "documentId") @@ -199,6 +206,8 @@ class Revision extends ParanoidModel< collaboratorIds?: string[] ) { const revision = this.buildFromDocument(document); + const authType = ctx.context.auth?.type; + revision.sourceMetadata = authType ? { authType } : null; if (collaboratorIds) { revision.collaboratorIds = collaboratorIds; diff --git a/server/presenters/revision.ts b/server/presenters/revision.ts index eaf88f3954..8ed495c008 100644 --- a/server/presenters/revision.ts +++ b/server/presenters/revision.ts @@ -36,6 +36,11 @@ async function presentRevision( icon: revision.icon ?? emoji, color: revision.color, collaborators: collaborators.map((user) => presentUser(user)), + sourceMetadata: revision.sourceMetadata + ? { + authType: revision.sourceMetadata.authType, + } + : undefined, createdAt: revision.createdAt, createdBy: presentUser(revision.user), createdById: revision.userId, diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index d887fd8996..1f1422cc59 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -912,7 +912,6 @@ "{{userName}} published": "{{userName}} published", "{{userName}} unpublished": "{{userName}} unpublished", "{{userName}} moved": "{{userName}} moved", - "via {{ authType }}": "via {{ authType }}", "Previous version": "Previous version", "Highlight changes": "Highlight changes", "Compare to": "Compare to", @@ -920,6 +919,7 @@ "Revision deleted": "Revision deleted", "Current version": "Current version", "{{userName}} edited": "{{userName}} edited", + "via {{ authType }}": "via {{ authType }}", "{{count}} people": "{{count}} person", "{{count}} people_plural": "{{count}} people", "Source": "Source", diff --git a/shared/types.ts b/shared/types.ts index 8f791cdaad..5034977fbe 100644 --- a/shared/types.ts +++ b/shared/types.ts @@ -402,6 +402,8 @@ export type SourceMetadata = { originalDocumentId?: string; /** The ID of the original collection when this collection was duplicated. */ originalCollectionId?: string; + /** The type of authentication used to make the change. */ + authType?: AuthenticationType; }; export type CustomTheme = {