Files
outline/server/utils/indexing.ts
T
Tom Moor 754672e9dc Centralize document access rules for list queries (#14783)
* wip

* Route document list queries through findAllWithCtx

Add findAllWithCtx and countWithCtx on Document and Template, unsafe* methods
for system reads, and a lint rule that bans direct list queries. Remove the
cached membership list, accessibleSql and findByIds in favour of one gate.

* Remove test

* Include drafts when loading starred documents

* Scope the access gate to the user's team, move whereToSql to QueryHelper

* Move collection access to Collection.findAllWithCtx

Collection.accessibleIds keeps the cached id list inside the collection gate, and user.collectionIds() is a shortcut to it. List queries use findAllWithCtx and countWithCtx, system reads use the shared unsafe methods, and the admin listing in collections.list is explicit.

* Check permanentDelete for each document in empty_trash
2026-11-27 15:30:43 -05:00

98 lines
2.6 KiB
TypeScript

import fractionalIndex from "fractional-index";
import type { FindOptions } from "sequelize";
import naturalSort from "@shared/utils/naturalSort";
import { Collection, Document, Star } from "@server/models";
/**
* Ensures all collections in a team have a fractional index, generating
* indexes for any collections without one.
*
* @param teamId the team to index collections for.
* @param options find options including an optional transaction.
* @returns a map of collection ids to their indexes.
*/
export async function collectionIndexing(
teamId: string,
{ transaction }: FindOptions<Collection>
) {
const collections = await Collection.unsafeFindAll({
where: {
teamId,
},
transaction,
});
const sortable = naturalSort(collections, (collection) => collection.name);
// for each collection with null index, use previous collection index to create new index
let previousIndex = null;
const promises = [];
for (const collection of sortable) {
if (collection.index === null) {
collection.index = fractionalIndex(previousIndex, null);
promises.push(
collection.save({ fields: ["index"], silent: true, transaction })
);
}
previousIndex = collection.index;
}
await Promise.all(promises);
const indexedCollections: Record<string, string | null> = {};
sortable.forEach((collection) => {
indexedCollections[collection.id] = collection.index;
});
return indexedCollections;
}
/**
* Ensures all stars belonging to a user have a fractional index, generating
* indexes for any stars without one.
*
* @param userId the user to index stars for.
* @returns a map of star ids to their indexes.
*/
export async function starIndexing(userId: string) {
const stars = await Star.findAll({
where: { userId },
});
const documents = await Document.unsafeFindAll({
attributes: ["id", "updatedAt"],
where: {
id: stars.map((star) => star.documentId).filter(Boolean) as string[],
},
order: [["updatedAt", "DESC"]],
});
const sortable = stars.sort(function (a, b) {
return (
documents.findIndex((d) => d.id === a.documentId) -
documents.findIndex((d) => d.id === b.documentId)
);
});
let previousIndex = null;
const promises = [];
for (const star of sortable) {
if (star.index === null) {
star.index = fractionalIndex(previousIndex, null);
promises.push(star.save({ silent: true }));
}
previousIndex = star.index;
}
await Promise.all(promises);
const indexedStars: Record<string, string | null> = {};
sortable.forEach((star) => {
indexedStars[star.id] = star.index;
});
return indexedStars;
}