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"
This commit is contained in:
Tom Moor authored and GitHub committed 2026-08-05 20:07:03 -04:00
1 parent 61e9f74fce
commit 81d9bed9ba
10 files changed
+77 -19

No files matched your search

+11 -1
View File
@@ -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;
@@ -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<Document>;
};
/** Authentication types that are surfaced in history, mapped to a display name. */
const authTypeNames: Partial<Record<AuthenticationType, string>> = {
[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 (
<EventItem>
<IconWrapper size="xsmall" type="secondary">
@@ -105,10 +96,8 @@ const EventListItem = ({ item }: Props) => {
</IconWrapper>
<Text size="xsmall" type="secondary">
{meta}
{authTypeName
? ` ${t("via {{ authType }}", { authType: authTypeName })}`
: ""}{" "}
&middot; <Time dateTime={item.createdAt} relative shorten addSuffix />
{authTypeSuffix(item.authType, t)} &middot;{" "}
<Time dateTime={item.createdAt} relative shorten addSuffix />
</Text>
</EventItem>
);
@@ -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")} &middot; {collaboratorText}
{authTypeSuffix(item.sourceMetadata?.authType, t)}
</>
) : (
t("{{userName}} edited", { userName: collaboratorText })
`${t("{{userName}} edited", { userName: collaboratorText })}${authTypeSuffix(item.sourceMetadata?.authType, t)}`
);
to = {
pathname: documentHistoryPath(
@@ -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<Record<AuthenticationType, string>> = {
[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.
+1
View File
@@ -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);
@@ -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");
},
};
+10 -1
View File
@@ -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;
+5
View File
@@ -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,
+1 -1
View File
@@ -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",
+2
View File
@@ -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 = {