mirror of
https://github.com/outline/outline.git
synced 2026-11-30 09:00:18 +03:00
* feat: Filter DSL for collections.list Adds the structured `filters` expression language to collections.list, matching documents.list and documents.search. Supported fields are name, createdAt, updatedAt, archivedAt, createdById and permission, each validated against its column type at the input layer. Deprecates the ad-hoc `query` and `statusFilter` params, which now translate into filter leaves so that one code path serves both shapes, and returns a 400 when a caller combines them with `filters`. Note that `query` matching is no longer accent-insensitive, as the unaccent comparison is replaced by the shared iLike conversion. * fix: Make filter expressions assignable to a JSON request body Filters are sent as a JSON body, but `FilterCondition` and `FilterGroup` were interfaces, which TypeScript does not give an implicit index signature, and `FilterValue` admitted a `Date` that JSON cannot carry. Both stores worked around this by asserting the request body to JSONObject, which suppressed checking of every other field as well. Declare the two shapes as type aliases and drop `Date` from the value union so a filter type-checks against the request body directly, and remove the assertions from the collection and document stores. * feat: Restrict filter fields to a set of accepted values Add an optional `values` set to a filter field definition, checked against scalar and array values at the input layer so that an unsupported value returns a 400 rather than an empty result or, for a field backed by a Postgres enum, a database error. A field with a fixed value set is matched exactly, so pattern operators are rejected for it in the same way as for non-text columns. Apply it to `permission` on collections.list. * refactor: Build the list_collections name filter with the filter DSL The MCP tool carried its own copy of the unaccent name comparison that collections.list used before it moved to the DSL. Build the clause with buildWhere instead, so both paths match names the same way, and drop the replacements plumbing it needed.
262 lines
6.9 KiB
TypeScript
262 lines
6.9 KiB
TypeScript
import invariant from "invariant";
|
|
import { isEmpty, orderBy, sortBy } from "es-toolkit/compat";
|
|
import { computed, action, runInAction } from "mobx";
|
|
import type { Filter } from "@shared/helpers/FilterHelper";
|
|
import {
|
|
CollectionPermission,
|
|
type FileOperationFormat,
|
|
SubscriptionType,
|
|
} from "@shared/types";
|
|
import Collection from "~/models/Collection";
|
|
import type { PaginationParams, Properties } from "~/types";
|
|
import { client } from "~/utils/ApiClient";
|
|
import type RootStore from "./RootStore";
|
|
import Store from "./base/Store";
|
|
|
|
export default class CollectionsStore extends Store<Collection> {
|
|
constructor(rootStore: RootStore) {
|
|
super(rootStore, Collection);
|
|
}
|
|
|
|
/**
|
|
* Returns the currently active collection, or undefined if not in the context of a collection.
|
|
*
|
|
* @returns The active Collection or undefined
|
|
*/
|
|
@computed
|
|
get active(): Collection | undefined {
|
|
return this.rootStore.ui.activeCollectionId
|
|
? this.data.get(this.rootStore.ui.activeCollectionId)
|
|
: undefined;
|
|
}
|
|
|
|
@computed
|
|
get allActive() {
|
|
return this.orderedData.filter((c) => c.isActive);
|
|
}
|
|
|
|
@computed
|
|
get orderedData(): Collection[] {
|
|
let collections = Array.from(this.data.values());
|
|
collections = collections
|
|
.filter((collection) => !collection.deletedAt)
|
|
.filter((collection) => {
|
|
const can = this.rootStore.policies.abilities(collection.id);
|
|
return isEmpty(can) || can.readDocument;
|
|
});
|
|
return collections.sort((a, b) => {
|
|
if (a.index === b.index) {
|
|
return a.updatedAt > b.updatedAt ? -1 : 1;
|
|
}
|
|
|
|
return a.index < b.index ? -1 : 1;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Returns all collections that are require explicit permission to access.
|
|
*/
|
|
@computed
|
|
get private(): Collection[] {
|
|
return this.all.filter((collection) => collection.isPrivate);
|
|
}
|
|
|
|
/**
|
|
* Returns all collections that are accessible by default.
|
|
*/
|
|
@computed
|
|
get nonPrivate(): Collection[] {
|
|
return this.all.filter(
|
|
(collection) => collection.isActive && !collection.isPrivate
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Returns all collections that are accessible to the current user.
|
|
*/
|
|
@computed
|
|
get all(): Collection[] {
|
|
return sortBy(
|
|
Array.from(this.data.values()),
|
|
(collection) => collection.name
|
|
);
|
|
}
|
|
|
|
@action
|
|
import = async (
|
|
attachmentId: string,
|
|
options: { format?: string; permission?: CollectionPermission | null }
|
|
) => {
|
|
await client.post("/collections.import", {
|
|
attachmentId,
|
|
...options,
|
|
});
|
|
};
|
|
|
|
@action
|
|
duplicate = async (
|
|
collection: Collection,
|
|
options?: {
|
|
name?: string;
|
|
}
|
|
): Promise<Collection> => {
|
|
const res = await client.post("/collections.duplicate", {
|
|
id: collection.id,
|
|
...options,
|
|
});
|
|
invariant(res?.data, "Data should be available");
|
|
|
|
this.addPolicies(res.policies);
|
|
return this.add(res.data);
|
|
};
|
|
|
|
@action
|
|
move = async (collectionId: string, index: string) => {
|
|
const res = await client.post("/collections.move", {
|
|
id: collectionId,
|
|
index,
|
|
});
|
|
invariant(res?.success, "Collection could not be moved");
|
|
const collection = this.get(collectionId);
|
|
|
|
if (collection) {
|
|
collection.updateIndex(res.data.index);
|
|
}
|
|
};
|
|
|
|
@action
|
|
archive = async (collection: Collection) => {
|
|
const res = await client.post("/collections.archive", {
|
|
id: collection.id,
|
|
});
|
|
runInAction("Collection#archive", () => {
|
|
invariant(res?.data, "Data should be available");
|
|
this.add(res.data);
|
|
this.addPolicies(res.policies);
|
|
});
|
|
};
|
|
|
|
@action
|
|
restore = async (collection: Collection) => {
|
|
const res = await client.post("/collections.restore", {
|
|
id: collection.id,
|
|
});
|
|
runInAction("Collection#restore", () => {
|
|
invariant(res?.data, "Data should be available");
|
|
this.add(res.data);
|
|
this.addPolicies(res.policies);
|
|
});
|
|
};
|
|
|
|
async update(params: Properties<Collection>): Promise<Collection> {
|
|
const result = await super.update(params);
|
|
|
|
// If we're changing sharing permissions on the collection then we need to
|
|
// remove all locally cached policies for documents in the collection as they
|
|
// are now invalid
|
|
if (params.sharing !== undefined) {
|
|
this.rootStore.documents.inCollection(result.id).forEach((document) => {
|
|
this.rootStore.policies.remove(document.id);
|
|
});
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
@action
|
|
fetchNamedPage = async (
|
|
request = "list",
|
|
options: (PaginationParams & { filters?: Filter[] }) | undefined
|
|
): Promise<Collection[]> => {
|
|
this.isFetching = true;
|
|
|
|
try {
|
|
const res = await client.post(`/collections.${request}`, options);
|
|
invariant(res?.data, "Collection list not available");
|
|
return runInAction("CollectionsStore#fetchNamedPage", () => {
|
|
const collections = res.data.map(this.add);
|
|
this.addPolicies(res.policies);
|
|
this.isLoaded = true;
|
|
return collections;
|
|
});
|
|
} finally {
|
|
this.isFetching = false;
|
|
}
|
|
};
|
|
|
|
@action
|
|
fetchArchived = async (options?: PaginationParams): Promise<Collection[]> =>
|
|
this.fetchNamedPage("list", {
|
|
...options,
|
|
filters: [{ field: "archivedAt", operator: "isNotNull" }],
|
|
});
|
|
|
|
get(id: string = ""): Collection | undefined {
|
|
return (
|
|
this.data.get(id) ??
|
|
this.orderedData.find((collection) => id.endsWith(collection.urlId))
|
|
);
|
|
}
|
|
|
|
@computed
|
|
get archived(): Collection[] {
|
|
return orderBy(this.orderedData, "archivedAt", "desc").filter(
|
|
(c) => c.isArchived && !c.isDeleted
|
|
);
|
|
}
|
|
|
|
@computed
|
|
get publicCollections() {
|
|
return this.orderedData.filter(
|
|
(collection) =>
|
|
collection.permission &&
|
|
Object.values(CollectionPermission).includes(collection.permission)
|
|
);
|
|
}
|
|
|
|
star = async (collection: Collection, index?: string) => {
|
|
await this.rootStore.stars.create({
|
|
collectionId: collection.id,
|
|
index,
|
|
});
|
|
};
|
|
|
|
unstar = async (collection: Collection) => {
|
|
const star = this.rootStore.stars.orderedData.find(
|
|
(s) => s.collectionId === collection.id
|
|
);
|
|
await star?.delete();
|
|
};
|
|
|
|
subscribe = (collection: Collection) =>
|
|
this.rootStore.subscriptions.create({
|
|
collectionId: collection.id,
|
|
event: SubscriptionType.Document,
|
|
});
|
|
|
|
unsubscribe = (collection: Collection) => {
|
|
const subscription = this.rootStore.subscriptions.getByCollectionId(
|
|
collection.id
|
|
);
|
|
|
|
return subscription?.delete();
|
|
};
|
|
|
|
@computed
|
|
get navigationNodes() {
|
|
return this.orderedData.map((collection) => collection.asNavigationNode);
|
|
}
|
|
|
|
async delete(collection: Collection) {
|
|
await super.delete(collection);
|
|
await this.rootStore.documents.fetchRecentlyUpdated();
|
|
await this.rootStore.documents.fetchRecentlyViewed();
|
|
}
|
|
|
|
export = (options: {
|
|
format: FileOperationFormat;
|
|
includeAttachments: boolean;
|
|
includePrivate: boolean;
|
|
}) => client.post("/collections.export_all", options);
|
|
}
|