Files
outline/server/routes/api/fileOperations/fileOperations.ts
T
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

152 lines
4.0 KiB
TypeScript

import Router from "@koa/router";
import { Op, type WhereOptions } from "sequelize";
import { UserRole } from "@shared/types";
import { ValidationError } from "@server/errors";
import auth from "@server/middlewares/authentication";
import { transaction } from "@server/middlewares/transaction";
import validate from "@server/middlewares/validate";
import { FileOperation, Team } from "@server/models";
import { authorize } from "@server/policies";
import { presentFileOperation, presentPolicies } from "@server/presenters";
import FileStorage from "@server/storage/files";
import type { Context } from "@server/types";
import pagination from "../middlewares/pagination";
import * as T from "./schema";
const router = new Router();
router.post(
"fileOperations.info",
auth(),
validate(T.FileOperationsInfoSchema),
async (ctx: Context<T.FileOperationsInfoReq>) => {
const { id } = ctx.input.body;
const { user } = ctx.auth;
const fileOperation = await FileOperation.findByPk(id, {
userId: user.id,
rejectOnEmpty: true,
});
authorize(user, "read", fileOperation);
ctx.body = {
data: presentFileOperation(fileOperation),
};
}
);
router.post(
"fileOperations.list",
auth({ role: UserRole.Admin }),
pagination(),
validate(T.FileOperationsListSchema),
async (ctx: Context<T.FileOperationsListReq>) => {
const { direction, sort, type } = ctx.input.body;
const { user } = ctx.auth;
const where: WhereOptions<FileOperation> = {
teamId: user.teamId,
type,
// Exclude operations on the personal collections of other users.
[Op.and]: [
{
[Op.or]: [
{ "$collection.ownerId$": null },
{ "$collection.ownerId$": user.id },
],
},
{
[Op.or]: [
{ "$document.collection.ownerId$": null },
{ "$document.collection.ownerId$": user.id },
],
},
{
[Op.or]: [
{ "options.personalOwnerId": null },
{ "options.personalOwnerId": user.id },
],
},
],
};
const team = await Team.findByPk(user.teamId);
authorize(user, "update", team);
const [fileOperations, total] = await Promise.all([
FileOperation.findAll({
where,
order: [[sort, direction]],
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
}),
FileOperation.count({
where,
}),
]);
ctx.body = {
pagination: { ...ctx.state.pagination, total },
data: fileOperations.map(presentFileOperation),
policies: presentPolicies(user, fileOperations),
};
}
);
const handleFileOperationsRedirect = async (
ctx: Context<T.FileOperationsRedirectReq>
) => {
const id = (ctx.input.body.id ?? ctx.input.query.id) as string;
const { user } = ctx.auth;
const fileOperation = await FileOperation.findByPk(id, {
userId: user.id,
rejectOnEmpty: true,
});
authorize(user, "read", fileOperation);
if (fileOperation.state !== "complete") {
throw ValidationError(`${fileOperation.type} is not complete yet`);
}
const accessUrl = await FileStorage.getSignedUrl(fileOperation.key);
ctx.redirect(accessUrl);
};
router.register(
"fileOperations.redirect",
["get", "post"],
[
auth(),
validate(T.FileOperationsRedirectSchema),
handleFileOperationsRedirect,
]
);
router.post(
"fileOperations.delete",
auth({ role: UserRole.Admin }),
validate(T.FileOperationsDeleteSchema),
transaction(),
async (ctx: Context<T.FileOperationsDeleteReq>) => {
const { id } = ctx.input.body;
const { user } = ctx.auth;
const { transaction } = ctx;
const fileOperation = await FileOperation.unscoped().findByPk(id, {
rejectOnEmpty: true,
transaction,
lock: transaction.LOCK.NO_KEY_UPDATE,
});
authorize(user, "delete", fileOperation);
await fileOperation.destroyWithCtx(ctx);
ctx.body = {
success: true,
};
}
);
export default router;