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>
161 lines
3.6 KiB
TypeScript
161 lines
3.6 KiB
TypeScript
import { cloneDeep, uniq } from "es-toolkit/compat";
|
|
import type {
|
|
Attributes,
|
|
CreationAttributes,
|
|
FindOrCreateOptions,
|
|
InferAttributes,
|
|
InferCreationAttributes,
|
|
InstanceDestroyOptions,
|
|
} from "sequelize";
|
|
import {
|
|
AfterCreate,
|
|
AfterDestroy,
|
|
BelongsTo,
|
|
Column,
|
|
DataType,
|
|
ForeignKey,
|
|
Table,
|
|
} from "sequelize-typescript";
|
|
import { createContext } from "@server/context";
|
|
import type { HookContextFields } from "@server/context";
|
|
import Comment from "./Comment";
|
|
import User from "./User";
|
|
import IdModel from "./base/IdModel";
|
|
import Length from "./validators/Length";
|
|
|
|
@Table({ tableName: "reactions", modelName: "reaction" })
|
|
class Reaction extends IdModel<
|
|
InferAttributes<Reaction>,
|
|
Partial<InferCreationAttributes<Reaction>>
|
|
> {
|
|
@Length({
|
|
max: 50,
|
|
msg: `emoji must be 50 characters or less`,
|
|
})
|
|
@Column(DataType.STRING)
|
|
emoji: string;
|
|
|
|
// associations
|
|
|
|
@BelongsTo(() => User)
|
|
user: User;
|
|
|
|
@ForeignKey(() => User)
|
|
@Column(DataType.UUID)
|
|
userId: string;
|
|
|
|
@BelongsTo(() => Comment)
|
|
comment: Comment;
|
|
|
|
@ForeignKey(() => Comment)
|
|
@Column(DataType.UUID)
|
|
commentId: string;
|
|
|
|
@AfterCreate
|
|
public static async addReactionToCommentCache(
|
|
model: Reaction,
|
|
ctx: HookContextFields &
|
|
FindOrCreateOptions<Attributes<Reaction>, CreationAttributes<Reaction>>
|
|
) {
|
|
const { transaction } = ctx;
|
|
|
|
const lock = transaction
|
|
? {
|
|
level: transaction.LOCK.NO_KEY_UPDATE,
|
|
of: Comment,
|
|
}
|
|
: undefined;
|
|
|
|
const comment = await Comment.findByPk(model.commentId, {
|
|
transaction,
|
|
lock,
|
|
});
|
|
|
|
if (!comment) {
|
|
return;
|
|
}
|
|
|
|
const reactions = cloneDeep(comment.reactions) ?? [];
|
|
const reaction = reactions.find((r) => r.emoji === model.emoji);
|
|
|
|
if (!reaction) {
|
|
reactions.push({ emoji: model.emoji, userIds: [model.userId] });
|
|
} else {
|
|
reaction.userIds = uniq([...reaction.userIds, model.userId]);
|
|
}
|
|
|
|
comment.reactions = reactions;
|
|
|
|
// Pass only the fields needed in Context; otherwise sequelize props will be overwritten.
|
|
const context = createContext({
|
|
user: ctx.auth.user,
|
|
authType: ctx.auth.type,
|
|
...ctx,
|
|
});
|
|
|
|
await comment.saveWithCtx(
|
|
context,
|
|
{
|
|
fields: ["reactions"],
|
|
silent: true,
|
|
},
|
|
{ name: "add_reaction", data: { emoji: model.emoji } }
|
|
);
|
|
}
|
|
|
|
@AfterDestroy
|
|
public static async removeReactionFromCommentCache(
|
|
model: Reaction,
|
|
ctx: HookContextFields & InstanceDestroyOptions
|
|
) {
|
|
const { transaction } = ctx;
|
|
|
|
const lock = transaction
|
|
? {
|
|
level: transaction.LOCK.NO_KEY_UPDATE,
|
|
of: Comment,
|
|
}
|
|
: undefined;
|
|
|
|
const comment = await Comment.findByPk(model.commentId, {
|
|
transaction,
|
|
lock,
|
|
});
|
|
|
|
if (!comment) {
|
|
return;
|
|
}
|
|
|
|
let reactions = cloneDeep(comment.reactions) ?? [];
|
|
const reaction = reactions.find((r) => r.emoji === model.emoji);
|
|
|
|
if (reaction) {
|
|
reaction.userIds = reaction.userIds.filter((id) => id !== model.userId);
|
|
|
|
if (reaction.userIds.length === 0) {
|
|
reactions = reactions.filter((r) => r.emoji !== model.emoji);
|
|
}
|
|
}
|
|
|
|
comment.reactions = reactions;
|
|
|
|
// Pass only the fields needed in Context; otherwise sequelize props will be overwritten.
|
|
const context = createContext({
|
|
user: ctx.auth.user,
|
|
authType: ctx.auth.type,
|
|
...ctx,
|
|
});
|
|
|
|
await comment.saveWithCtx(
|
|
context,
|
|
{
|
|
fields: ["reactions"],
|
|
silent: true,
|
|
},
|
|
{ name: "remove_reaction", data: { emoji: model.emoji } }
|
|
);
|
|
}
|
|
}
|
|
|
|
export default Reaction;
|