Files
outline/server/commands/personalCollectionLoader.ts
Tom MoorandClaude Opus 5.5 00b124d03c chore: Rename APIContext to Context and flatten its fields (#14684)
* chore: Rename APIContext to Context and flatten its fields

Auth and transaction now live directly on the context instead of in
ctx.state and a nested ctx.context, and model helpers read them directly.
APIContext remains as a deprecated alias.

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

* chore: Remove deprecated APIContext alias

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

* fix: Read auth from context in OAuth provider callbacks

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

* chore: Add withUser and withTransaction context helpers

Replaces manual createContext rebuilds that copy fields from an existing context.

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

* chore: Remove arbitrary property access from Koa context types

AppContext and Context no longer inherit the any index signature from
Koa's DefaultContext, custom fields are declared explicitly, and importing
Koa's loose Context type is disallowed by lint.

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

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-23 23:17:06 -05:00

58 lines
1.9 KiB
TypeScript

import { PersonalCollectionId } from "@shared/constants";
import { withTransaction } from "@server/context";
import { Collection } from "@server/models";
import { authorize } from "@server/policies";
import { sequelize } from "@server/storage/database";
import type { Context } from "@server/types";
/**
* Loads the personal collection of the acting user, and creates it when the
* user does not have one yet.
*
* @param ctx the context containing the acting user.
* @returns the personal collection, with the memberships of the user loaded.
* @throws AuthorizationError when the collection does not exist and the user
* may not create it.
*/
export default async function personalCollectionLoader(
ctx: Context
): Promise<Collection> {
const { user } = ctx.auth;
const { transaction } = ctx;
const existing = await Collection.findPersonal(user, { transaction });
if (existing) {
return existing;
}
authorize(user, "createPersonalCollection", user.team);
if (transaction) {
return Collection.findOrCreatePersonal(ctx);
}
// The creation lock is only held inside a transaction.
return sequelize.transaction((innerTransaction) =>
Collection.findOrCreatePersonal(withTransaction(ctx, innerTransaction))
);
}
/**
* Resolves the collectionId "personal" to the id of the personal collection of
* the acting user, which is created when it does not exist yet. Other values
* are returned unchanged.
*
* @param ctx the context containing the acting user.
* @param collectionId the requested collectionId.
* @returns the resolved collectionId.
* @throws AuthorizationError when the collection does not exist and the user
* may not create it.
*/
export async function resolvePersonalCollectionId<
T extends string | null | undefined,
>(ctx: Context, collectionId: T): Promise<T | string> {
return collectionId === PersonalCollectionId
? (await personalCollectionLoader(ctx)).id
: collectionId;
}