Files
outline/server/routes/api/developer/developer.ts
T
Tom Moor 754672e9dc Centralize document access rules for list queries (#14783)
* wip

* Route document list queries through findAllWithCtx

Add findAllWithCtx and countWithCtx on Document and Template, unsafe* methods
for system reads, and a lint rule that bans direct list queries. Remove the
cached membership list, accessibleSql and findByIds in favour of one gate.

* Remove test

* Include drafts when loading starred documents

* Scope the access gate to the user's team, move whereToSql to QueryHelper

* Move collection access to Collection.findAllWithCtx

Collection.accessibleIds keeps the cached id list inside the collection gate, and user.collectionIds() is a shortcut to it. List queries use findAllWithCtx and countWithCtx, system reads use the shared unsafe methods, and the admin listing in collections.list is explicit.

* Check permanentDelete for each document in empty_trash
2026-11-27 15:30:43 -05:00

128 lines
3.5 KiB
TypeScript

import type { Next } from "koa";
import Router from "@koa/router";
import { Op } from "sequelize";
import { randomString, randomElement } from "@shared/random";
import { NotificationEventType } from "@shared/types";
import type { Invite } from "@server/commands/userInviter";
import userInviter from "@server/commands/userInviter";
import env from "@server/env";
import Logger from "@server/logging/Logger";
import auth from "@server/middlewares/authentication";
import validate from "@server/middlewares/validate";
import { Document, Notification, User } from "@server/models";
import presentNotification from "@server/presenters/notification";
import { presentUser } from "@server/presenters";
import type { Context, AppContext } from "@server/types";
import * as T from "./schema";
const router = new Router();
function dev() {
return async function checkDevelopmentMiddleware(
ctx: AppContext,
next: Next
) {
if (env.ENVIRONMENT !== "development") {
throw new Error("Attempted to access development route in production");
}
return next();
};
}
router.post(
"developer.create_test_users",
dev(),
auth(),
validate(T.CreateTestUsersSchema),
async (ctx: Context<T.CreateTestUsersReq>) => {
const { count } = ctx.input.body;
const invites = Array(Math.min(count, 100))
.fill(0)
.map(() => {
const rando = randomString(10);
return {
email: `${rando}@example.com`,
name: `${rando.slice(0, 5)} Tester`,
role: "member",
} as Invite;
});
Logger.info("utils", `Creating ${count} test users`, invites);
// Generate a bunch of invites
const response = await userInviter(ctx, { invites });
// Convert from invites to active users by marking as active
await Promise.all(
response.users.map((user) => user.updateActiveAt(ctx, true))
);
ctx.body = {
data: {
users: response.users.map((user) => presentUser(user)),
},
};
}
);
router.post(
"developer.create_test_notifications",
dev(),
auth(),
validate(T.CreateTestNotificationsSchema),
async (ctx: Context<T.CreateTestNotificationsReq>) => {
const { count } = ctx.input.body;
const { user } = ctx.auth;
const events = [
NotificationEventType.UpdateDocument,
NotificationEventType.CreateComment,
NotificationEventType.MentionedInDocument,
NotificationEventType.MentionedInComment,
NotificationEventType.AddUserToDocument,
];
const [documents, actors] = await Promise.all([
Document.findAllWithCtx(ctx, { limit: 25 }),
User.findAll({
where: { teamId: user.teamId, id: { [Op.ne]: user.id } },
limit: 25,
}),
]);
const notifications = await Promise.all(
Array(Math.min(count, 100))
.fill(0)
.map(() => {
const document = documents.length
? randomElement(documents)
: undefined;
return Notification.create({
event: randomElement(events),
userId: user.id,
actorId: actors.length ? randomElement(actors).id : user.id,
teamId: user.teamId,
documentId: document?.id,
});
})
);
Logger.info("utils", `Creating ${count} test notifications`);
ctx.body = {
data: {
notifications: await Promise.all(
notifications.map((notification) =>
presentNotification(ctx, notification)
)
),
},
};
}
);
export default router;