From aad3ee78d17bd58383e2978e0a987160d8163fb2 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Wed, 13 Sep 2023 20:12:54 -1000 Subject: [PATCH] Test for Update support --- server/api/[username]/inbox/index.ts | 19 +++++--- tests/inbox.test.ts | 65 ++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/server/api/[username]/inbox/index.ts b/server/api/[username]/inbox/index.ts index b4968442..ff373a95 100644 --- a/server/api/[username]/inbox/index.ts +++ b/server/api/[username]/inbox/index.ts @@ -78,17 +78,26 @@ export default async ( // Replace the object in database with the new provided object // TODO: Add authentication - const object = await RawObject.findOneBy({ - data: { - id: (body.object as RawObject).id, - }, - }); + const object = await RawObject.createQueryBuilder("object") + .where("object.data->>'id' = :id", { + id: (body.object as APObject).id, + }) + .getOne(); if (!object) return errorResponse("Object not found", 404); object.data = body.object as APObject; + // Store the Update event in database + const activity = new RawActivity(); + activity.data = { + ...body, + object: undefined, + }; + activity.objects = [object]; + await object.save(); + await activity.save(); break; } case "Delete" as APDelete: { diff --git a/tests/inbox.test.ts b/tests/inbox.test.ts index 63cbe8b4..882dad6b 100644 --- a/tests/inbox.test.ts +++ b/tests/inbox.test.ts @@ -85,6 +85,71 @@ describe("POST /@test/inbox", () => { published: "2021-01-01T00:00:00.000Z", }); }); + + test("should try to update that Note object", async () => { + const response = await fetch( + `${config.http.base_url}:${config.http.port}/@test/inbox/`, + { + method: "POST", + headers: { + "Content-Type": "application/activity+json", + }, + body: JSON.stringify({ + "@context": "https://www.w3.org/ns/activitystreams", + type: "Update", + id: "https://example.com/notes/1/activity", + actor: `${config.http.base_url}:${config.http.port}/@test`, + to: ["https://www.w3.org/ns/activitystreams#Public"], + cc: [], + published: "2021-01-02T00:00:00.000Z", + object: { + "@context": "https://www.w3.org/ns/activitystreams", + id: "https://example.com/notes/1", + type: "Note", + content: "This note has been edited!", + summary: null, + inReplyTo: null, + published: "2021-01-01T00:00:00.000Z", + }, + }), + } + ); + + expect(response.status).toBe(200); + expect(response.headers.get("content-type")).toBe("application/json"); + + const activity = await RawActivity.createQueryBuilder("activity") + // Where id is part of the jsonb column 'data' + .where("activity.data->>'id' = :id", { + id: "https://example.com/notes/1/activity", + }) + .leftJoinAndSelect("activity.objects", "objects") + // Sort by most recent + .orderBy("activity.data->>'published'", "DESC") + .getOne(); + + expect(activity).not.toBeUndefined(); + expect(activity?.data).toEqual({ + "@context": "https://www.w3.org/ns/activitystreams", + type: "Update", + id: "https://example.com/notes/1/activity", + actor: `${config.http.base_url}:${config.http.port}/@test`, + to: ["https://www.w3.org/ns/activitystreams#Public"], + cc: [], + published: "2021-01-02T00:00:00.000Z", + }); + + expect(activity?.objects).toHaveLength(1); + expect(activity?.objects[0].data).toEqual({ + "@context": "https://www.w3.org/ns/activitystreams", + id: "https://example.com/notes/1", + type: "Note", + content: "This note has been edited!", + summary: null, + inReplyTo: null, + published: "2021-01-01T00:00:00.000Z", + }); + }); }); afterAll(async () => {