Files
outline/app/models/Template.ts
T
Claude 597acedf0b fix: Complete customizable document retention
Follow-up fixes to the retention feature.

The superseded CleanupDeletedDocumentsTask was still registered and ran
hourly, hard deleting documents 30 days after `deletedAt` regardless of the
team's configured period. Any retention longer than 30 days — including
infinite — had no effect.

The `destroyedAt` column was declared with a bare `@Column`. Decorator
metadata is disabled in the server build, so sequelize-typescript could not
infer its type and threw while loading the Document model.

Documents pending permanent deletion stayed readable and restorable through
`documents.info`, `documents.restore` and the MCP restore tool. They are now
treated as if they no longer exist.

Also:

- Add trash and data retention controls to workspace security settings, the
  only way to configure either was to call the API directly.
- Derive the retention tranches to process from the periods teams actually
  use, so a team holding a period that is no longer offered is not skipped.
- Bound the trash expiry task by `limit`, it previously ran an unbounded
  UPDATE across the whole documents table every hour.
- Match team preferences on JSON text rather than casting to int, so one bad
  value cannot fail the query for every other team in the batch.
- Derive the API validation schema from the same presets the UI offers.
- Send `ASC` rather than `asc` from the trash list, the sort direction was
  silently coerced back to descending.
- Move the retention badge in with the other document badges, pluralize it,
  and never show a negative day count.
- Drop the unused `Template.destroyedAt` computed, which returned a future
  date under a name meaning the opposite on the sibling model.
- Rename the migration to match its position in the sequence and the column
  it adds.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01U3kXbDxLMJesYLM5E4P6HQ
2026-08-16 14:36:32 -04:00

181 lines
3.8 KiB
TypeScript

import i18n from "i18next";
import { computed, observable } from "mobx";
import type { ProsemirrorData } from "@shared/types";
import { ProsemirrorDataHelper } from "@shared/utils/ProsemirrorDataHelper";
import { isRTL } from "@shared/utils/rtl";
import slugify from "@shared/utils/slugify";
import type TemplatesStore from "~/stores/TemplatesStore";
import User from "~/models/User";
import { settingsPath } from "~/utils/routeHelpers";
import Collection from "./Collection";
import ParanoidModel from "./base/ParanoidModel";
import Field from "./decorators/Field";
import Relation from "./decorators/Relation";
import type { Searchable } from "./interfaces/Searchable";
export default class Template extends ParanoidModel implements Searchable {
static modelName = "Template";
store: TemplatesStore;
@Field
@observable.shallow
data: ProsemirrorData;
@computed
get searchContent(): string {
return this.title;
}
@computed
get searchSuppressed(): boolean {
return this.isDeleted;
}
/**
* The id of the collection that this template belongs to, if any.
*/
@Field
@observable
collectionId?: string | null;
/**
* The collection that this template belongs to.
*/
@Relation(() => Collection, { onDelete: "cascade" })
collection?: Collection;
/**
* The title of the template.
*/
@Field
@observable
title: string;
/**
* An icon (or) emoji to use as the template icon.
*/
@Field
@observable
icon?: string | null;
/**
* The color to use for the template icon.
*/
@Field
@observable
color?: string | null;
/**
* Whether the template layout is displayed full page width.
*/
@Field
@observable
fullWidth: boolean;
/**
* The likely language of the template, in ISO 639-1 format.
*/
@Field
@observable
language: string | undefined;
@Relation(() => User)
createdBy: User | undefined;
@Relation(() => User)
updatedBy: User | undefined;
@observable
urlId: string;
/**
* The date the template was published, and so made available to other members
* of the workspace.
*/
@observable
publishedAt: string | undefined;
/**
* Publishes the template, making it available to other members of the
* workspace.
*
* @returns a promise that resolves when the template has been published.
*/
publish = () => this.save(undefined, { publish: true });
/**
* Returns the direction of the template text, either "rtl" or "ltr"
*/
@computed
get dir(): "rtl" | "ltr" {
return this.rtl ? "rtl" : "ltr";
}
/**
* Returns true if the template text is right-to-left
*/
@computed
get rtl() {
return isRTL(this.title);
}
@computed
get path(): string {
if (!this.title) {
return `${settingsPath("templates")}/untitled-${this.urlId}`;
}
const slugifiedTitle = slugify(this.title);
return `${settingsPath("templates")}/${slugifiedTitle}-${this.urlId}`;
}
@computed
get isDeleted(): boolean {
return !!this.deletedAt;
}
get hasEmptyTitle(): boolean {
return this.title === "";
}
/**
* Whether the template has neither a title nor any content.
*/
@computed
get isEmpty(): boolean {
return (
!this.title?.trim() &&
(!this.data || ProsemirrorDataHelper.isEmpty(this.data))
);
}
@computed
get isWorkspaceTemplate(): boolean {
return !this.collectionId;
}
/**
* Whether the template is an unpublished draft, only visible to the user that
* created it.
*/
@computed
get isDraft(): boolean {
return !this.publishedAt;
}
get titleWithDefault(): string {
return this.title || i18n.t("Untitled");
}
@computed
get initial(): string {
return (this.titleWithDefault?.charAt(0) ?? "?").toUpperCase();
}
@computed
get isActive(): boolean {
return !this.isDeleted;
}
}