mirror of
https://github.com/outline/outline.git
synced 2026-11-25 23:24:06 +03:00
Add attachments.list API endpoint (#9259)
* Add attachments.list endpoint to retrieve user's attachments * Fix TypeScript error in attachments.list endpoint * Address review comments: restore tests, properly type WhereOptions, remove comments * fix: Pagination, user filtering --------- Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com> Co-authored-by: Codegen Bot <codegen@example.com> Co-authored-by: Tom Moor <tom@getoutline.com>
This commit is contained in:
4 files changed
+178
-1
No files matched your search
@@ -2,6 +2,7 @@ import { Attachment } from "@server/models";
|
||||
|
||||
export default function presentAttachment(attachment: Attachment) {
|
||||
return {
|
||||
userId: attachment.userId,
|
||||
documentId: attachment.documentId,
|
||||
contentType: attachment.contentType,
|
||||
name: attachment.name,
|
||||
|
||||
@@ -15,6 +15,120 @@ jest.mock("@server/storage/files");
|
||||
|
||||
const server = getTestServer();
|
||||
|
||||
describe("#attachments.list", () => {
|
||||
it("should return attachments for user", async () => {
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
const attachment = await buildAttachment({
|
||||
teamId: user.teamId,
|
||||
userId: user.id,
|
||||
documentId: document.id,
|
||||
});
|
||||
const attachment2 = await buildAttachment({
|
||||
teamId: user.teamId,
|
||||
userId: user.id,
|
||||
});
|
||||
|
||||
const res = await server.post("/api/attachments.list", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.pagination.total).toEqual(2);
|
||||
expect(body.data.length).toEqual(2);
|
||||
expect(body.data[0].id).toEqual(attachment.id);
|
||||
expect(body.data[1].id).toEqual(attachment2.id);
|
||||
});
|
||||
|
||||
it("should allow filtering by userId when user is an admin", async () => {
|
||||
const admin = await buildAdmin();
|
||||
const user = await buildUser({ teamId: admin.teamId });
|
||||
// Attachments for user
|
||||
const attachment1 = await buildAttachment({
|
||||
teamId: admin.teamId,
|
||||
userId: user.id,
|
||||
});
|
||||
// Attachment for admin
|
||||
await buildAttachment({
|
||||
teamId: admin.teamId,
|
||||
userId: admin.id,
|
||||
});
|
||||
|
||||
const res = await server.post("/api/attachments.list", {
|
||||
body: {
|
||||
userId: user.id,
|
||||
token: admin.getJwtToken(),
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.length).toEqual(1);
|
||||
expect(body.data[0].id).toEqual(attachment1.id);
|
||||
});
|
||||
|
||||
it("should filter by documentId", async () => {
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
const attachment = await buildAttachment({
|
||||
teamId: user.teamId,
|
||||
userId: user.id,
|
||||
documentId: document.id,
|
||||
});
|
||||
await buildAttachment({
|
||||
teamId: user.teamId,
|
||||
userId: user.id,
|
||||
});
|
||||
|
||||
const res = await server.post("/api/attachments.list", {
|
||||
body: {
|
||||
documentId: document.id,
|
||||
token: user.getJwtToken(),
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.length).toEqual(1);
|
||||
expect(body.data[0].id).toEqual(attachment.id);
|
||||
});
|
||||
|
||||
it("should not return attachments created by other users", async () => {
|
||||
const user = await buildUser();
|
||||
const anotherUser = await buildUser({
|
||||
teamId: user.teamId,
|
||||
});
|
||||
await buildAttachment({
|
||||
teamId: user.teamId,
|
||||
userId: anotherUser.id,
|
||||
});
|
||||
|
||||
const res = await server.post("/api/attachments.list", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.length).toEqual(0);
|
||||
});
|
||||
|
||||
it("should require authentication", async () => {
|
||||
const res = await server.post("/api/attachments.list");
|
||||
expect(res.status).toEqual(401);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#attachments.create", () => {
|
||||
it("should require authentication", async () => {
|
||||
const res = await server.post("/api/attachments.create");
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import Router from "koa-router";
|
||||
import { WhereOptions } from "sequelize";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { AttachmentPreset } from "@shared/types";
|
||||
import { bytesToHumanReadable, getFileNameFromUrl } from "@shared/utils/files";
|
||||
@@ -16,8 +17,9 @@ import validate from "@server/middlewares/validate";
|
||||
import { Attachment, Document } from "@server/models";
|
||||
import AttachmentHelper from "@server/models/helpers/AttachmentHelper";
|
||||
import { authorize } from "@server/policies";
|
||||
import { presentAttachment } from "@server/presenters";
|
||||
import { presentAttachment, presentPolicies } from "@server/presenters";
|
||||
import UploadAttachmentFromUrlTask from "@server/queues/tasks/UploadAttachmentFromUrlTask";
|
||||
import pagination from "@server/routes/api/middlewares/pagination";
|
||||
import { sequelize } from "@server/storage/database";
|
||||
import FileStorage from "@server/storage/files";
|
||||
import BaseStorage from "@server/storage/files/BaseStorage";
|
||||
@@ -28,6 +30,55 @@ import * as T from "./schema";
|
||||
|
||||
const router = new Router();
|
||||
|
||||
router.post(
|
||||
"attachments.list",
|
||||
auth(),
|
||||
pagination(),
|
||||
validate(T.AttachmentsListSchema),
|
||||
async (ctx: APIContext<T.AttachmentsListReq>) => {
|
||||
const { documentId, userId } = ctx.input.body;
|
||||
const { user } = ctx.state.auth;
|
||||
|
||||
const where: WhereOptions<Attachment> = {
|
||||
teamId: user.teamId,
|
||||
};
|
||||
|
||||
// If a specific user is passed then add to filters
|
||||
if (userId && user.isAdmin) {
|
||||
where.userId = userId;
|
||||
} else {
|
||||
where.userId = user.id;
|
||||
}
|
||||
|
||||
// If a specific document is passed then add to filters
|
||||
if (documentId) {
|
||||
const document = await Document.findByPk(documentId, {
|
||||
userId: user.id,
|
||||
});
|
||||
authorize(user, "read", document);
|
||||
where.documentId = documentId;
|
||||
}
|
||||
|
||||
const [attachments, total] = await Promise.all([
|
||||
Attachment.findAll({
|
||||
where,
|
||||
order: [["createdAt", "DESC"]],
|
||||
offset: ctx.state.pagination.offset,
|
||||
limit: ctx.state.pagination.limit,
|
||||
}),
|
||||
Attachment.count({
|
||||
where,
|
||||
}),
|
||||
]);
|
||||
|
||||
ctx.body = {
|
||||
pagination: { ...ctx.state.pagination, total },
|
||||
data: attachments.map(presentAttachment),
|
||||
policies: presentPolicies(user, attachments),
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
router.post(
|
||||
"attachments.create",
|
||||
rateLimiter(RateLimiterStrategy.TenPerMinute),
|
||||
|
||||
@@ -3,6 +3,17 @@ import { z } from "zod";
|
||||
import { AttachmentPreset } from "@shared/types";
|
||||
import { BaseSchema } from "@server/routes/api/schema";
|
||||
|
||||
export const AttachmentsListSchema = BaseSchema.extend({
|
||||
body: z.object({
|
||||
/** Id of the document to which the Attachment belongs */
|
||||
documentId: z.string().uuid().optional(),
|
||||
/** Id of the user that uploaded the Attachment */
|
||||
userId: z.string().uuid().optional(),
|
||||
}),
|
||||
});
|
||||
|
||||
export type AttachmentsListReq = z.infer<typeof AttachmentsListSchema>;
|
||||
|
||||
export const AttachmentsCreateSchema = BaseSchema.extend({
|
||||
body: z.object({
|
||||
/** Attachment name */
|
||||
|
||||
Reference in new issue
Block a user