Files
outline/app/models/Notification.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

368 lines
10 KiB
TypeScript

import type { TFunction } from "i18next";
import { action, computed, observable } from "mobx";
import type { NotificationData } from "@shared/types";
import { FileOperationState, NotificationEventType } from "@shared/types";
import {
collectionPath,
commentPath,
documentPath,
settingsPath,
} from "~/utils/routeHelpers";
import Collection from "./Collection";
import Comment from "./Comment";
import Document from "./Document";
import FileOperation from "./FileOperation";
import Group from "./Group";
import User from "./User";
import Model from "./base/Model";
import Field from "./decorators/Field";
import Relation from "./decorators/Relation";
export type NotificationFilter =
| "all"
| "mentions"
| "comments"
| "reactions"
| "documents"
| "collections"
| "system";
class Notification extends Model {
static modelName = "Notification";
constructor(fields: Record<string, unknown>, store: Model["store"]) {
super(fields, store);
this.initialize(fields);
}
static filterCategories: Record<NotificationFilter, NotificationEventType[]> =
{
all: [],
mentions: [
NotificationEventType.MentionedInDocument,
NotificationEventType.MentionedInComment,
NotificationEventType.GroupMentionedInDocument,
NotificationEventType.GroupMentionedInComment,
],
comments: [
NotificationEventType.CreateComment,
NotificationEventType.ResolveComment,
NotificationEventType.ReactionsCreate,
],
reactions: [NotificationEventType.ReactionsCreate],
documents: [
NotificationEventType.PublishDocument,
NotificationEventType.UpdateDocument,
NotificationEventType.CreateRevision,
NotificationEventType.AddUserToDocument,
NotificationEventType.AddOwnerToDocument,
NotificationEventType.RequestDocumentAccess,
],
collections: [
NotificationEventType.CreateCollection,
NotificationEventType.AddUserToCollection,
NotificationEventType.AddOwnerToCollection,
],
system: [
NotificationEventType.InviteAccepted,
NotificationEventType.Onboarding,
NotificationEventType.Features,
NotificationEventType.ExportCompleted,
NotificationEventType.AddUserToGroup,
NotificationEventType.RemoveUserFromGroup,
],
};
/**
* The date the notification was marked as read.
*/
@Field
@observable
viewedAt: Date | null;
/**
* The date the notification was archived.
*/
@Field
@observable
archivedAt: Date | null;
/**
* Request ID on notifications for access requests.
*/
@Field
@observable
accessRequestId?: string = undefined;
/**
* Status of the associated access request.
*/
@Field
@observable
accessRequestStatus?: string = undefined;
/**
* The user that triggered the notification.
*/
@Relation(() => User)
actor?: User;
/**
* The document ID that the notification is associated with.
*/
documentId?: string;
/**
* The document that the notification is associated with.
*/
@Relation(() => Document, { onDelete: "cascade" })
document?: Document;
/**
* The collection ID that the notification is associated with.
*/
collectionId?: string;
/**
* The collection that the notification is associated with.
*/
@Relation(() => Collection, { onDelete: "cascade" })
collection?: Collection;
/**
* The group ID that the notification is associated with.
*/
groupId?: string;
/**
* The group that the notification is associated with.
*/
@Relation(() => Group, { onDelete: "cascade" })
group?: Group;
/**
* The file operation ID that the notification is associated with.
*/
fileOperationId?: string;
/**
* The file operation that the notification is associated with.
*/
@Relation(() => FileOperation, { onDelete: "ignore" })
fileOperation?: FileOperation;
commentId?: string;
/**
* The comment that the notification is associated with.
*/
@Relation(() => Comment, { onDelete: "cascade" })
comment?: Comment;
/**
* The type of notification.
*/
event: NotificationEventType;
/**
* Additional data associated with the notification.
*/
data: NotificationData;
/**
* Mark the notification as read or unread
*
* @returns A promise that resolves when the notification has been saved.
*/
@action
toggleRead() {
return this.save({ viewedAt: this.viewedAt ? null : new Date() });
}
/**
* Mark the notification as read
*
* @returns A promise that resolves when the notification has been saved.
*/
@action
markAsRead() {
if (this.viewedAt) {
return;
}
return this.save({ viewedAt: new Date() });
}
/**
* Archive the notification
*
* @returns A promise that resolves when the notification has been archived.
*/
@action
archive() {
if (this.archivedAt) {
return;
}
return this.save({ archivedAt: new Date() });
}
/**
* Returns translated text that describes the notification
*
* @param t - The translation function
* @returns The event text
*/
eventText(t: TFunction): string {
switch (this.event) {
case NotificationEventType.PublishDocument:
return t("published");
case NotificationEventType.UpdateDocument:
case NotificationEventType.CreateRevision:
return t("edited");
case NotificationEventType.CreateCollection:
return t("created the collection");
case NotificationEventType.MentionedInDocument:
case NotificationEventType.MentionedInComment:
return t("mentioned you in");
case NotificationEventType.GroupMentionedInComment:
case NotificationEventType.GroupMentionedInDocument:
return t("mentioned your group in");
case NotificationEventType.CreateComment:
return t("left a comment on");
case NotificationEventType.ResolveComment:
return t("resolved a comment on");
case NotificationEventType.ReactionsCreate:
return t("reacted {{ emoji }} to your comment on", {
emoji: this.data.emoji,
});
case NotificationEventType.AddUserToDocument:
return t("shared");
case NotificationEventType.AddOwnerToDocument:
return t("made you an owner of");
case NotificationEventType.AddUserToCollection:
return t("invited you to");
case NotificationEventType.AddOwnerToCollection:
return t("made you an owner of");
case NotificationEventType.AddUserToGroup:
return t("added you to the group");
case NotificationEventType.RemoveUserFromGroup:
return t("removed you from the group");
case NotificationEventType.ExportCompleted:
switch (this.fileOperation?.state) {
case FileOperationState.Complete:
return t("export completed");
case FileOperationState.Error:
return t("export failed");
default:
return t("export is no longer available");
}
case NotificationEventType.RequestDocumentAccess:
if (this.accessRequestStatus === "approved") {
return t("was granted access to");
}
if (this.accessRequestStatus === "dismissed") {
return t("requested access to");
}
return t("is requesting access to");
default:
return this.event;
}
}
/**
* Returns translated text for the subject of the notification. This is the
* name of the associated document, collection, group, or export.
*
* @param t - The translation function.
* @returns The subject.
*/
subject(t: TFunction): string {
if (this.fileOperationId) {
return this.fileOperation?.displayName ?? t("Your");
}
if (this.documentId) {
return this.document?.title ?? t("a document");
}
if (this.collectionId) {
return this.collection?.displayName ?? t("a collection");
}
if (this.groupId) {
return this.group?.name ?? t("a group");
}
return t("Unknown");
}
/**
* Returns the URL to download the file of the notification, if available.
*
* @returns The download URL.
*/
@computed
get downloadUrl(): string | undefined {
return this.fileOperation?.state === FileOperationState.Complete
? this.fileOperation.downloadUrl
: undefined;
}
/**
* Returns the path to the model associated with the notification that can be
* used with the router.
*
* @returns The router path.
*/
@computed
get path() {
switch (this.event) {
case NotificationEventType.PublishDocument:
case NotificationEventType.UpdateDocument:
case NotificationEventType.CreateRevision: {
return this.document ? documentPath(this.document) : "";
}
case NotificationEventType.AddUserToCollection:
case NotificationEventType.AddOwnerToCollection:
case NotificationEventType.CreateCollection: {
const collection = this.collectionId
? this.store.rootStore.collections.get(this.collectionId)
: undefined;
return collection ? collectionPath(collection) : "";
}
case NotificationEventType.RequestDocumentAccess:
case NotificationEventType.AddUserToDocument:
case NotificationEventType.AddOwnerToDocument:
case NotificationEventType.GroupMentionedInDocument:
case NotificationEventType.MentionedInDocument: {
return this.document?.path;
}
case NotificationEventType.GroupMentionedInComment:
case NotificationEventType.MentionedInComment:
case NotificationEventType.ResolveComment:
case NotificationEventType.CreateComment:
case NotificationEventType.ReactionsCreate: {
return this.document && this.comment
? commentPath(this.document, this.comment)
: this.document?.path;
}
case NotificationEventType.InviteAccepted: {
return settingsPath("users");
}
case NotificationEventType.Onboarding:
case NotificationEventType.Features: {
return "";
}
case NotificationEventType.ExportCompleted: {
return settingsPath("export");
}
case NotificationEventType.AddUserToGroup:
case NotificationEventType.RemoveUserFromGroup: {
return this.groupId
? settingsPath("groups", this.groupId, "members")
: "";
}
default:
this.event satisfies never;
return;
}
}
}
export default Notification;