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>
261 lines
6.7 KiB
TypeScript
261 lines
6.7 KiB
TypeScript
import Router from "@koa/router";
|
|
import type { WhereOptions } from "sequelize";
|
|
import { Transaction } from "sequelize";
|
|
import { QueryNotices, SubscriptionType } from "@shared/types";
|
|
import subscriptionCreator from "@server/commands/subscriptionCreator";
|
|
import { createContext } from "@server/context";
|
|
import env from "@server/env";
|
|
import auth from "@server/middlewares/authentication";
|
|
import { rateLimiter } from "@server/middlewares/rateLimiter";
|
|
import { transaction } from "@server/middlewares/transaction";
|
|
import validate from "@server/middlewares/validate";
|
|
import { Subscription, Document, User, Collection } from "@server/models";
|
|
import SubscriptionHelper from "@server/models/helpers/SubscriptionHelper";
|
|
import { authorize } from "@server/policies";
|
|
import { presentSubscription } from "@server/presenters";
|
|
import type { Context } from "@server/types";
|
|
import { RateLimiterStrategy } from "@server/utils/RateLimiter";
|
|
import { safeEqual } from "@server/utils/crypto";
|
|
import pagination from "../middlewares/pagination";
|
|
import * as T from "./schema";
|
|
|
|
const router = new Router();
|
|
|
|
router.post(
|
|
"subscriptions.list",
|
|
auth(),
|
|
pagination(),
|
|
validate(T.SubscriptionsListSchema),
|
|
async (ctx: Context<T.SubscriptionsListReq>) => {
|
|
const { user } = ctx.auth;
|
|
const { event, collectionId, documentId } = ctx.input.body;
|
|
|
|
const where: WhereOptions<Subscription> = {
|
|
userId: user.id,
|
|
event,
|
|
};
|
|
|
|
if (collectionId) {
|
|
const collection = await Collection.findByPk(collectionId, {
|
|
userId: user.id,
|
|
transaction: ctx.transaction,
|
|
});
|
|
authorize(user, "read", collection);
|
|
|
|
where.collectionId = collectionId;
|
|
} else {
|
|
// documentId will be available here
|
|
const document = await Document.findByPk(documentId!, {
|
|
userId: user.id,
|
|
includeContent: false,
|
|
});
|
|
authorize(user, "read", document);
|
|
|
|
where.documentId = documentId;
|
|
}
|
|
|
|
const subscriptions = await Subscription.findAll({
|
|
where,
|
|
order: [["createdAt", "DESC"]],
|
|
offset: ctx.state.pagination.offset,
|
|
limit: ctx.state.pagination.limit,
|
|
});
|
|
|
|
ctx.body = {
|
|
pagination: ctx.state.pagination,
|
|
data: subscriptions.map(presentSubscription),
|
|
};
|
|
}
|
|
);
|
|
|
|
router.post(
|
|
"subscriptions.info",
|
|
auth(),
|
|
validate(T.SubscriptionsInfoSchema),
|
|
async (ctx: Context<T.SubscriptionsInfoReq>) => {
|
|
const { user } = ctx.auth;
|
|
const { event, collectionId, documentId } = ctx.input.body;
|
|
|
|
const where: WhereOptions<Subscription> = {
|
|
userId: user.id,
|
|
event,
|
|
};
|
|
|
|
if (collectionId) {
|
|
const collection = await Collection.findByPk(collectionId, {
|
|
userId: user.id,
|
|
});
|
|
authorize(user, "read", collection);
|
|
|
|
where.collectionId = collectionId;
|
|
} else {
|
|
// documentId will be available here
|
|
const document = await Document.findByPk(documentId!, {
|
|
userId: user.id,
|
|
includeContent: false,
|
|
});
|
|
authorize(user, "read", document);
|
|
|
|
where.documentId = documentId;
|
|
}
|
|
|
|
// There can be only one subscription with these props.
|
|
const subscription = await Subscription.findOne({
|
|
where,
|
|
});
|
|
|
|
if (!subscription) {
|
|
ctx.response.status = 204;
|
|
return;
|
|
}
|
|
|
|
ctx.body = {
|
|
data: presentSubscription(subscription),
|
|
};
|
|
}
|
|
);
|
|
|
|
router.post(
|
|
"subscriptions.create",
|
|
rateLimiter(RateLimiterStrategy.TwentyFivePerMinute),
|
|
auth(),
|
|
validate(T.SubscriptionsCreateSchema),
|
|
transaction(),
|
|
async (ctx: Context<T.SubscriptionsCreateReq>) => {
|
|
const { user } = ctx.auth;
|
|
const { event, collectionId, documentId } = ctx.input.body;
|
|
|
|
if (documentId) {
|
|
const document = await Document.findByPk(documentId, {
|
|
userId: user.id,
|
|
});
|
|
|
|
authorize(user, "subscribe", document);
|
|
}
|
|
|
|
if (collectionId) {
|
|
const collection = await Collection.findByPk(collectionId, {
|
|
userId: user.id,
|
|
});
|
|
|
|
authorize(user, "subscribe", collection);
|
|
}
|
|
|
|
const subscription = await subscriptionCreator({
|
|
ctx,
|
|
documentId,
|
|
collectionId,
|
|
event,
|
|
});
|
|
|
|
ctx.body = {
|
|
data: presentSubscription(subscription),
|
|
};
|
|
}
|
|
);
|
|
|
|
router.get(
|
|
"subscriptions.delete",
|
|
validate(T.SubscriptionsDeleteTokenSchema),
|
|
rateLimiter(RateLimiterStrategy.FivePerMinute),
|
|
transaction(),
|
|
async (ctx: Context<T.SubscriptionsDeleteTokenReq>) => {
|
|
const { transaction } = ctx;
|
|
const { follow, userId, documentId, token } = ctx.input.query;
|
|
|
|
// The link in the email does not include the follow query param, this
|
|
// is to help prevent anti-virus, and email clients from pre-fetching the link
|
|
if (!follow) {
|
|
return ctx.redirectOnClient(ctx.request.href + "&follow=true");
|
|
}
|
|
|
|
const unsubscribeToken = SubscriptionHelper.unsubscribeToken(
|
|
userId,
|
|
documentId
|
|
);
|
|
|
|
if (!safeEqual(unsubscribeToken, token)) {
|
|
ctx.redirect(`${env.URL}?notice=invalid-auth`);
|
|
return;
|
|
}
|
|
|
|
const user = await User.scope("withTeam").findByPk(userId, {
|
|
rejectOnEmpty: true,
|
|
transaction,
|
|
});
|
|
|
|
// Always record a fresh opt-out, also when no active subscription exists.
|
|
const documentSubscription = await Subscription.findOne({
|
|
where: {
|
|
userId,
|
|
documentId,
|
|
event: SubscriptionType.Document,
|
|
},
|
|
paranoid: false,
|
|
lock: Transaction.LOCK.UPDATE,
|
|
transaction,
|
|
});
|
|
|
|
if (!documentSubscription) {
|
|
await Subscription.create(
|
|
{
|
|
userId,
|
|
documentId,
|
|
event: SubscriptionType.Document,
|
|
deletedAt: new Date(),
|
|
},
|
|
{ transaction }
|
|
);
|
|
} else if (documentSubscription.deletedAt) {
|
|
await Subscription.update(
|
|
{ deletedAt: new Date() },
|
|
{
|
|
where: { id: documentSubscription.id },
|
|
paranoid: false,
|
|
transaction,
|
|
}
|
|
);
|
|
} else {
|
|
authorize(user, "delete", documentSubscription);
|
|
await documentSubscription.destroyWithCtx(
|
|
createContext({
|
|
user,
|
|
ip: ctx.request.ip,
|
|
transaction,
|
|
})
|
|
);
|
|
}
|
|
|
|
ctx.redirect(
|
|
`${user.team.url}/home?notice=${QueryNotices.UnsubscribeDocument}`
|
|
);
|
|
}
|
|
);
|
|
|
|
router.post(
|
|
"subscriptions.delete",
|
|
auth(),
|
|
validate(T.SubscriptionsDeleteSchema),
|
|
transaction(),
|
|
async (ctx: Context<T.SubscriptionsDeleteReq>) => {
|
|
const { transaction } = ctx;
|
|
const { user } = ctx.auth;
|
|
const { id } = ctx.input.body;
|
|
|
|
const subscription = await Subscription.findByPk(id, {
|
|
rejectOnEmpty: true,
|
|
transaction,
|
|
});
|
|
|
|
authorize(user, "delete", subscription);
|
|
|
|
await subscription.destroyWithCtx(ctx);
|
|
|
|
ctx.body = {
|
|
success: true,
|
|
};
|
|
}
|
|
);
|
|
|
|
export default router;
|