Files
outline/app/models/Collection.ts
T
Tom MoorandClaude 767758a1a8 feat: Document and collection owners (#14437)
* feat: Add document owners

Documents can now have owners, stored as an array of user IDs. Owners
must be active members or admins, and changing them requires the
existing manage users permission on the document, gated behind a new
DocumentOwners plan entitlement that is included in every plan.

- ownerIds accepted by documents.create and documents.update and
  returned by the document presenter
- ownerIds filter field (eq/in) for documents.list and documents.search
- ownerIds on the MCP create_document and update_document tools, and an
  ownerId filter on list_documents
- Owners section in the document Insights dialog to add and remove owners
- Owner filter on the search page
- In-app and email notification when a user is made an owner

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFdSZ39ZGPf31jyGcYz

* feat: Give document owners responsibility for their documents

- Access requests are routed to owners that can manage the document
  before falling back to document, collection and workspace admins
- Owners are subscribed to a document when they are added, respecting
  a previous unsubscribe
- Owners that can edit a document can also manage its access, owners
  and archive it; ownership never grants access on its own
- Owners are shown in the document byline, list items and search
  results, and document hover previews

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFdSZ39ZGPf31jyGcYz

* feat: Manage document owners from the share panel

- Owner is a role in the share panel permission menu and invite picker for
  individual users, adding an owner gives them the manage permission
- Lowering an owner's permission or removing their access removes the
  ownership
- Owners are sorted to the top of the member list
- Owners added through the API or MCP are given the manage permission
- Owners do not also receive the "added to document" notification
- Only added owners are validated, so suspended owners can still be removed
- Deleted users are removed from document owners
- Removes the owners section from Insights

* fix: Address review feedback on document owners

- Authorize owner changes before assigning owners on create
- Lock the document when adding an owner so concurrent invitations are kept
- Enforce the owner limit on every path
- Run MCP document updates in a transaction
- Broadcast owner removals to connected clients

* feat: Add collection owners

- Collections can have owners, who also own every document in the
  collection, shown first in document bylines
- Owner is a role in the collection share panel and invite picker for
  individual users, adding an owner gives them the manage permission
- Lowering an owner's permission or removing their access removes the
  ownership
- ownerIds on collections.create, collections.update and the MCP
  collection tools
- The document owner filter includes documents in owned collections
- Access requests go to document and collection owners together
- Collection owners are subscribed to the collection and notified when
  they are added
- Deleted users are removed from collection owners
- Renames the personal collection policy helper to
  isPersonalCollectionOwner

* feat: Show profile cards on owner names, hide inherited owners in collection lists

* fix: Allow adding yourself as a collection owner, include collection owners in MCP exact match

* fix: Correct singular owner translations

* feat: Allow users to make themselves a document owner

- The share panel suggests the current user when they can manage owners,
  and only offers the Owner permission for them
- documents.add_user accepts the actor's own ID when changing ownership,
  without changing their own access
- An owner's own row offers "Remove as owner", which keeps their access
- Hides the duplicate "You have full access" row when the current user is
  listed as a member

* fix: Restore singular owner translations

* fix: Limit owners to 5, use no key update locks when adding owners

* refactor: Move owner logic into Document and Collection model methods

- addOwner, removeOwner, grantOwnerAccess and setOwnerIds on both models,
  locking the row so concurrent owner changes are kept
- Owner events are created by an update hook from the change in ownerIds
- The manageOwners check runs at the route level when ownerIds is sent
- Removes the documentOwners and collectionOwners command modules
- Removes duplicate owner tests

* fix: Owner selection and membership edge cases

- Remove document owners from the legacy plan
- Selecting yourself in the share picker only suggests other users that can
  be owners, and you are not suggested next to groups or guests
- Users that make themselves an owner through the API get the manage
  permission, so they are listed in the share panel

* perf: Scope the collection owner filter subquery to the team

* chore: Drop redundant teamId UUID check

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-25 19:21:26 -05:00

501 lines
12 KiB
TypeScript

import i18n from "i18next";
import invariant from "invariant";
import {
action,
comparer,
computed,
observable,
override,
runInAction,
} from "mobx";
import {
type CollectionPermission,
type FileOperationFormat,
type NavigationNode,
NavigationNodeType,
type ProsemirrorData,
} from "@shared/types";
import { sortNavigationNodes } from "@shared/utils/collections";
import type CollectionsStore from "~/stores/CollectionsStore";
import type Document from "~/models/Document";
import { CoalescedFetch } from "~/models/base/CoalescedFetch";
import ParanoidModel from "~/models/base/ParanoidModel";
import { client } from "~/utils/ApiClient";
import type User from "./User";
import Field from "./decorators/Field";
import { AfterChange } from "./decorators/Lifecycle";
export default class Collection extends ParanoidModel {
static modelName = "Collection";
constructor(fields: Record<string, unknown>, store: CollectionsStore) {
super(fields, store);
this.initialize(fields);
}
store: CollectionsStore;
/** The name of the collection. */
@Field
@observable
name: string;
/** The reason this collection is archived. */
@observable
deprecatedReason: string | null = null;
/** Collection description in Prosemirror format. */
@Field
@observable.shallow
data: ProsemirrorData;
/** An icon (or) emoji to use as the collection icon. */
@Field
@observable
icon: string;
/** The color to use for the collection icon and other highlights. */
@Field
@observable
color?: string | null = undefined;
/** The default permission for workspace users. */
@Field
@observable
permission?: CollectionPermission = undefined;
/**
* The user that owns this collection, if it is a personal collection. Not to
* be confused with `ownerIds`.
*/
@observable
ownerId: string | null;
/** The ids of users that are responsible for this collection. */
@observable
ownerIds: string[] = [];
/**
* Whether public sharing is enabled for the collection. Note this can also be disabled at the
* workspace level.
*/
@Field
@observable
sharing: boolean;
/** The sort index for the collection. */
@Field
@observable
index: string;
/** The sort field and direction for documents in the collection. */
@Field
@observable
sort: {
field: string;
direction: "asc" | "desc";
};
/** The minimum permission level required to manage templates in this collection. */
@Field
@observable
templateManagement: CollectionPermission;
/**
* Whether commenting is enabled for the collection.
*/
@Field
@observable
commenting?: boolean | null = undefined;
/** The child documents of the collection. */
@observable
documents?: NavigationNode[] = undefined;
/** @deprecated Use path instead. */
@observable
url: string;
/** The ID that appears in the collection slug. */
@observable
urlId: string;
/**
* The date and time the collection was archived.
*/
@observable
archivedAt: string;
/**
* User who archived the collection.
*/
@observable
archivedBy?: User = undefined;
@computed
get searchContent(): string {
return this.displayName;
}
/**
* The name to show for the collection. A personal collection is always shown
* with a translated name, whatever name is stored.
*
* @returns the name to display.
*/
@computed
get displayName(): string {
return this.isPersonal ? i18n.t("Personal") : this.name;
}
/** Returns whether the collection is empty, or undefined if not loaded. */
@computed
get isEmpty(): boolean | undefined {
if (!this.documents) {
return undefined;
}
return (
this.documents.length === 0 &&
this.store.rootStore.documents.inCollection(this.id).length === 0
);
}
/**
* Convenience method to return if a collection is considered private.
* This means that a membership is required to view it rather than just being
* a workspace member.
*
* @returns boolean
*/
get isPrivate(): boolean {
return this.permission === null;
}
/**
* Whether this collection is the personal collection of a user.
*
* @returns boolean
*/
get isPersonal(): boolean {
return !!this.ownerId;
}
@computed
get isStarred(): boolean {
return !!this.store.rootStore.stars.orderedData.find(
(star) => star.collectionId === this.id
);
}
/**
* Returns whether there is a subscription for this collection in the store.
*
* @returns True if there is a subscription, false otherwise.
*/
@computed
get isSubscribed(): boolean {
return !!this.store.rootStore.subscriptions.getByCollectionId(this.id);
}
@computed
get isManualSort(): boolean {
return this.sort.field === "index";
}
@computed({ equals: comparer.structural })
get sortedDocuments(): NavigationNode[] | undefined {
if (!this.documents) {
return undefined;
}
return sortNavigationNodes(this.documents, this.sort);
}
/**
* Returns a lookup from document id to child documents.
*
* @returns a map of document id to child document nodes.
*/
@computed({ keepAlive: true })
get childrenByDocumentId(): Map<string, NavigationNode[]> {
const childrenByDocumentId = new Map<string, NavigationNode[]>();
const travelNodes = (nodes: NavigationNode[]) => {
for (const node of nodes) {
childrenByDocumentId.set(node.id, node.children);
travelNodes(node.children);
}
};
if (this.sortedDocuments) {
travelNodes(this.sortedDocuments);
}
return childrenByDocumentId;
}
/** The initial letter of the collection name as a string. */
@computed
get initial() {
return (this.displayName?.charAt(0) ?? "?").toUpperCase();
}
@computed
get path() {
return this.url;
}
/**
* Returns users that have been individually given access to the collection.
*
* @returns A list of users that have been given access to the collection.
*/
@computed
get members(): User[] {
return this.store.rootStore.memberships.orderedData
.filter((m) => m.collectionId === this.id)
.map((m) => m.user)
.filter(Boolean);
}
@computed
get isArchived() {
return !!this.archivedAt;
}
@override
get isDeleted() {
return !!this.deletedAt;
}
@computed
get isActive() {
return !this.isArchived && !this.isDeleted;
}
@computed
get hasDocuments() {
return !!this.documents?.length;
}
fetchDocuments = async (options?: { force: boolean }) => {
if (this.documents && options?.force !== true) {
return;
}
await this.documentsFetch.run(async () => {
const res = await client.post("/collections.documents", {
id: this.id,
});
invariant(res?.data, "Data should be available");
runInAction(() => {
this.documents = res.data;
});
}, options?.force);
};
/**
* Updates the document identified by the given id in the collection in memory.
* Does not update the document in the database.
*
* @param document The document properties stored in the collection
*/
@action
updateDocument(
document: Pick<Document, "id" | "title" | "path" | "color" | "icon">
) {
if (!this.documents) {
return;
}
const travelNodes = (nodes: NavigationNode[]) =>
nodes.forEach((node) => {
if (node.id === document.id) {
node.color = document.color ?? undefined;
node.icon = document.icon ?? undefined;
node.title = document.title;
node.url = document.path;
} else {
travelNodes(node.children);
}
});
travelNodes(this.documents);
}
/**
* Removes the document identified by the given id from the collection in
* memory. Does not remove the document from the database.
*
* @param documentId The id of the document to remove.
*/
@action
removeDocument(documentId: string) {
if (!this.documents) {
return;
}
this.documents = this.documents.filter(function f(node): boolean {
if (node.id === documentId) {
return false;
}
if (node.children) {
node.children = node.children.filter(f);
}
return true;
});
}
/**
* Adds the document identified by the given id to the collection in
* memory, if it is not already present. Does not add the document to the
* database or store.
*
* @param document The document to add.
* @param parentDocumentId The id of the document to add the new document to.
*/
@action
addDocument(document: Document, parentDocumentId?: string) {
if (!this.documents || this.childrenByDocumentId.has(document.id)) {
return;
}
if (!parentDocumentId) {
this.documents.unshift(document.asNavigationNode);
return;
}
const travelNodes = (nodes: NavigationNode[]) =>
nodes.forEach((node) => {
if (node.id === parentDocumentId) {
node.children = [document.asNavigationNode, ...(node.children ?? [])];
} else {
travelNodes(node.children);
}
});
travelNodes(this.documents);
}
@action
updateIndex(index: string) {
this.index = index;
}
getChildrenForDocument(documentId: string) {
return this.childrenByDocumentId.get(documentId) ?? [];
}
@computed
get asNavigationNode(): NavigationNode {
return {
type: NavigationNodeType.Collection,
id: this.id,
title: this.name,
color: this.color ?? undefined,
icon: this.icon ?? undefined,
children: this.documents ?? [],
url: this.url,
};
}
pathToDocument(documentId: string) {
let path: NavigationNode[] | undefined = [];
const document = this.store.rootStore.documents.get(documentId);
if (!document) {
return path;
}
const travelNodes = (
nodes: NavigationNode[],
previousPath: NavigationNode[]
) => {
nodes.forEach((node) => {
const newPath = [...previousPath, node];
if (node.id === documentId) {
path = newPath;
return;
}
if (
document.parentDocumentId &&
node.id === document.parentDocumentId
) {
path = [...newPath, document.asNavigationNode];
return;
}
return travelNodes(node.children, newPath);
});
};
if (this.documents) {
travelNodes(this.documents, path);
}
return path;
}
@action
star = async (index?: string) => this.store.star(this, index);
@action
unstar = async () => this.store.unstar(this);
/**
* Subscribes the current user to this collection.
*
* @returns A promise that resolves when the subscription is created.
*/
@action
subscribe = () => this.store.subscribe(this);
/**
* Unsubscribes the current user from this collection.
*
* @returns A promise that resolves when the subscription is destroyed.
*/
@action
unsubscribe = () => this.store.unsubscribe(this);
archive = () => this.store.archive(this);
restore = () => this.store.restore(this);
/**
* Duplicates the collection and the published documents within it.
*
* @returns A promise that resolves to the duplicated collection.
*/
duplicate = (options?: { name?: string }) =>
this.store.duplicate(this, options);
export = (format: FileOperationFormat, includeAttachments: boolean) =>
client.post("/collections.export", {
id: this.id,
format,
includeAttachments,
});
// hooks
@AfterChange
static removePolicies(
model: Collection,
previousAttributes: Partial<Collection>
) {
if (
previousAttributes &&
(model.sharing !== previousAttributes?.sharing ||
model.permission !== previousAttributes?.permission)
) {
const { documents, policies } = model.store.rootStore;
documents.inCollection(model.id).forEach((document) => {
policies.remove(document.id);
});
}
}
private documentsFetch = new CoalescedFetch();
}