mirror of
https://github.com/outline/outline.git
synced 2026-11-26 07:33:19 +03:00
* 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>
71 lines
2.0 KiB
TypeScript
71 lines
2.0 KiB
TypeScript
import fractionalIndex from "fractional-index";
|
|
import type { WhereOptions } from "sequelize";
|
|
import { Sequelize, Op } from "sequelize";
|
|
import type { User } from "@server/models";
|
|
import { Pin } from "@server/models";
|
|
import type { Context } from "@server/types";
|
|
|
|
type Props = {
|
|
/** The request context */
|
|
ctx: Context;
|
|
/** The user creating the pin */
|
|
user: User;
|
|
/** The document to pin */
|
|
documentId: string;
|
|
/** The collection to pin the document in. If no collection is provided then it will be pinned to home */
|
|
collectionId?: string | null;
|
|
/** The index to pin the document at. If no index is provided then it will be pinned to the end of the collection */
|
|
index?: string;
|
|
};
|
|
|
|
/**
|
|
* This command creates a "pinned" document via the pin relation. A document can
|
|
* be pinned to a collection or to the home screen.
|
|
*
|
|
* @param Props The properties of the pin to create
|
|
* @returns Pin The pin that was created
|
|
*/
|
|
export default async function pinCreator({
|
|
ctx,
|
|
user,
|
|
documentId,
|
|
collectionId,
|
|
...rest
|
|
}: Props): Promise<Pin> {
|
|
let { index } = rest;
|
|
const { transaction } = ctx;
|
|
const where: WhereOptions<Pin> = {
|
|
teamId: user.teamId,
|
|
...(collectionId ? { collectionId } : { collectionId: { [Op.is]: null } }),
|
|
};
|
|
|
|
if (!index) {
|
|
const pins = await Pin.findAll({
|
|
where,
|
|
attributes: ["id", "index", "updatedAt"],
|
|
transaction,
|
|
limit: 1,
|
|
order: [
|
|
// using LC_COLLATE:"C" because we need byte order to drive the sorting
|
|
// find only the last pin so we can create an index after it
|
|
Sequelize.literal('"pin"."index" collate "C" DESC'),
|
|
["updatedAt", "ASC"],
|
|
],
|
|
});
|
|
|
|
// create a pin at the end of the list
|
|
index = fractionalIndex(pins.length ? pins[0].index : null, null);
|
|
}
|
|
|
|
const [pin] = await Pin.findOrCreateWithCtx(ctx, {
|
|
where: {
|
|
collectionId: collectionId ?? null,
|
|
documentId,
|
|
teamId: user.teamId,
|
|
},
|
|
defaults: { index, createdById: user.id },
|
|
});
|
|
|
|
return pin;
|
|
}
|