mirror of
https://github.com/outline/outline.git
synced 2026-11-25 23:24:06 +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>
189 lines
4.5 KiB
TypeScript
189 lines
4.5 KiB
TypeScript
import type {
|
||
CreateOptions,
|
||
InferAttributes,
|
||
InferCreationAttributes,
|
||
SaveOptions,
|
||
WhereOptions,
|
||
} from "sequelize";
|
||
import {
|
||
ForeignKey,
|
||
AfterSave,
|
||
BeforeCreate,
|
||
BelongsTo,
|
||
Column,
|
||
IsIP,
|
||
IsUUID,
|
||
Table,
|
||
DataType,
|
||
Length,
|
||
} from "sequelize-typescript";
|
||
import { globalEventQueue } from "../queues";
|
||
import { afterCommit } from "../storage/transaction";
|
||
import type { Context } from "../types";
|
||
import { AuthenticationType } from "../types";
|
||
import { normalizeIp } from "../utils/ip";
|
||
import Collection from "./Collection";
|
||
import Document from "./Document";
|
||
import Team from "./Team";
|
||
import User from "./User";
|
||
import IdModel from "./base/IdModel";
|
||
import type { AppContext } from "@server/types";
|
||
|
||
@Table({ tableName: "events", modelName: "event", updatedAt: false })
|
||
class Event extends IdModel<
|
||
InferAttributes<Event>,
|
||
Partial<InferCreationAttributes<Event>>
|
||
> {
|
||
@IsUUID(4)
|
||
@Column(DataType.UUID)
|
||
modelId: string | null;
|
||
|
||
/** The name of the event. */
|
||
@Length({
|
||
max: 255,
|
||
msg: "name must be 255 characters or less",
|
||
})
|
||
@Column(DataType.STRING)
|
||
name: string;
|
||
|
||
/** The originating IP address of the event. */
|
||
@IsIP
|
||
@Column(DataType.STRING)
|
||
ip: string | null;
|
||
|
||
/** The type of authentication used to create the event. */
|
||
@Column(DataType.ENUM(...Object.values(AuthenticationType)))
|
||
authType: AuthenticationType | null;
|
||
|
||
/**
|
||
* Metadata associated with the event, previously used for storing some changed attributes.
|
||
* Note that the `data` column will be visible to the client and API requests.
|
||
*/
|
||
@Column(DataType.JSONB)
|
||
// oxlint-disable-next-line @typescript-eslint/no-explicit-any
|
||
data: Record<string, any> | null;
|
||
|
||
/**
|
||
* The changes made to the model – gradually moving to this column away from `data` which can be
|
||
* used for arbitrary data associated with the event.
|
||
*/
|
||
@Column(DataType.JSONB)
|
||
// oxlint-disable-next-line @typescript-eslint/no-explicit-any
|
||
changes: Record<string, any> | null;
|
||
|
||
// hooks
|
||
|
||
@BeforeCreate
|
||
static cleanupIp(model: Event) {
|
||
model.ip = normalizeIp(model.ip);
|
||
}
|
||
|
||
@AfterSave
|
||
static async enqueue(
|
||
model: Event,
|
||
options: SaveOptions<InferAttributes<Event>>
|
||
) {
|
||
await afterCommit(
|
||
options.transaction,
|
||
() => void globalEventQueue().add(model)
|
||
);
|
||
}
|
||
|
||
// associations
|
||
|
||
@BelongsTo(() => User, "userId")
|
||
user: User | null;
|
||
|
||
@ForeignKey(() => User)
|
||
@Column(DataType.UUID)
|
||
userId: string | null;
|
||
|
||
@BelongsTo(() => Document, "documentId")
|
||
document: Document | null;
|
||
|
||
@ForeignKey(() => Document)
|
||
@Column(DataType.UUID)
|
||
documentId: string | null;
|
||
|
||
@BelongsTo(() => User, "actorId")
|
||
actor: User;
|
||
|
||
@ForeignKey(() => User)
|
||
@Column(DataType.UUID)
|
||
actorId: string | null;
|
||
|
||
@BelongsTo(() => Collection, "collectionId")
|
||
collection: Collection | null;
|
||
|
||
@ForeignKey(() => Collection)
|
||
@Column(DataType.UUID)
|
||
collectionId: string | null;
|
||
|
||
@BelongsTo(() => Team, "teamId")
|
||
team: Team;
|
||
|
||
@ForeignKey(() => Team)
|
||
@Column(DataType.UUID)
|
||
teamId: string;
|
||
|
||
/*
|
||
* Schedule can be used to send events into the event system without recording
|
||
* them in the database or audit trail – consider using a task instead.
|
||
*/
|
||
static schedule(event: Partial<Event>) {
|
||
const now = new Date();
|
||
return globalEventQueue().add(
|
||
this.build({
|
||
createdAt: now,
|
||
...event,
|
||
})
|
||
);
|
||
}
|
||
|
||
/**
|
||
* Find the latest event matching the where clause
|
||
*
|
||
* @param where The options to match against
|
||
* @returns A promise resolving to the latest event or null
|
||
*/
|
||
static findLatest(where: WhereOptions) {
|
||
return this.findOne({
|
||
where,
|
||
order: [["createdAt", "DESC"]],
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Create and persist new event from request context
|
||
*
|
||
* @param ctx The request context to use
|
||
* @param attributes The event attributes
|
||
* @returns A promise resolving to the new event
|
||
*/
|
||
static createFromContext(
|
||
ctx: AppContext | Context,
|
||
attributes: Omit<Partial<Event>, "ip" | "teamId" | "actorId"> = {},
|
||
defaultAttributes: Pick<Partial<Event>, "ip" | "teamId" | "actorId"> = {},
|
||
options?: CreateOptions<InferAttributes<Event>>
|
||
) {
|
||
const user = ctx.auth?.user;
|
||
const authType = ctx.auth?.type;
|
||
|
||
return this.create(
|
||
{
|
||
...attributes,
|
||
actorId: user?.id || defaultAttributes.actorId,
|
||
teamId: user?.teamId || defaultAttributes.teamId,
|
||
ip: ctx.request?.ip || defaultAttributes.ip,
|
||
authType,
|
||
},
|
||
{
|
||
transaction: ctx.transaction,
|
||
...options,
|
||
}
|
||
);
|
||
}
|
||
}
|
||
|
||
export default Event;
|