|
| 1 | +import type { InboxContext } from "@fedify/fedify"; |
| 2 | +import { Accept, Reject } from "@fedify/vocab"; |
| 3 | +import { and, eq } from "drizzle-orm"; |
| 4 | +import { beforeEach, describe, expect, it } from "vitest"; |
| 5 | +import { cleanDatabase } from "../../tests/helpers"; |
| 6 | +import { createAccount } from "../../tests/helpers/oauth"; |
| 7 | +import db from "../db"; |
| 8 | +import { accounts, follows } from "../schema"; |
| 9 | +import type { Uuid } from "../uuid"; |
| 10 | +import { onFollowAccepted, onFollowRejected } from "./inbox"; |
| 11 | + |
| 12 | +type SeededFollow = { |
| 13 | + followerId: Uuid; |
| 14 | + followingId: Uuid; |
| 15 | + followerIri: string; |
| 16 | + followingIri: string; |
| 17 | +}; |
| 18 | + |
| 19 | +async function seedFollow(): Promise<SeededFollow> { |
| 20 | + const followerOwner = await createAccount({ username: "follower" }); |
| 21 | + const followingOwner = await createAccount({ username: "following" }); |
| 22 | + const follower = await db.query.accounts.findFirst({ |
| 23 | + where: eq(accounts.id, followerOwner.id as Uuid), |
| 24 | + }); |
| 25 | + const following = await db.query.accounts.findFirst({ |
| 26 | + where: eq(accounts.id, followingOwner.id as Uuid), |
| 27 | + }); |
| 28 | + if (follower == null || following == null) { |
| 29 | + throw new Error("Failed to seed accounts"); |
| 30 | + } |
| 31 | + const followIri = `${follower.iri}#follows/${crypto.randomUUID()}`; |
| 32 | + await db.insert(follows).values({ |
| 33 | + iri: followIri, |
| 34 | + followerId: follower.id, |
| 35 | + followingId: following.id, |
| 36 | + approved: null, |
| 37 | + }); |
| 38 | + return { |
| 39 | + followerId: follower.id, |
| 40 | + followingId: following.id, |
| 41 | + followerIri: follower.iri, |
| 42 | + followingIri: following.iri, |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +const ctx = { |
| 47 | + origin: "https://hollo.test", |
| 48 | + recipient: "follower", |
| 49 | +} as InboxContext<void>; |
| 50 | + |
| 51 | +describe("onFollowAccepted", () => { |
| 52 | + beforeEach(async () => { |
| 53 | + await cleanDatabase(); |
| 54 | + }); |
| 55 | + |
| 56 | + it("approves a pending follow from embedded Follow object", async () => { |
| 57 | + expect.assertions(2); |
| 58 | + |
| 59 | + const seeded = await seedFollow(); |
| 60 | + const accept = await Accept.fromJsonLd({ |
| 61 | + "@context": ["https://www.w3.org/ns/activitystreams"], |
| 62 | + id: `${seeded.followingIri}#accepts/${crypto.randomUUID()}`, |
| 63 | + type: "Accept", |
| 64 | + actor: { |
| 65 | + id: seeded.followingIri, |
| 66 | + type: "Person", |
| 67 | + preferredUsername: "following", |
| 68 | + inbox: `${seeded.followingIri}/inbox`, |
| 69 | + }, |
| 70 | + object: { |
| 71 | + id: `${seeded.followerIri}#follows/${crypto.randomUUID()}`, |
| 72 | + type: "Follow", |
| 73 | + actor: seeded.followerIri, |
| 74 | + object: seeded.followingIri, |
| 75 | + }, |
| 76 | + }); |
| 77 | + |
| 78 | + await onFollowAccepted(ctx, accept); |
| 79 | + |
| 80 | + const follow = await db.query.follows.findFirst({ |
| 81 | + where: and( |
| 82 | + eq(follows.followerId, seeded.followerId), |
| 83 | + eq(follows.followingId, seeded.followingId), |
| 84 | + ), |
| 85 | + }); |
| 86 | + expect(follow).toBeDefined(); |
| 87 | + expect(follow?.approved).not.toBeNull(); |
| 88 | + }); |
| 89 | + |
| 90 | + it("updates the follower's followingCount when approved via embedded Follow object (Path B)", async () => { |
| 91 | + expect.assertions(2); |
| 92 | + |
| 93 | + const seeded = await seedFollow(); |
| 94 | + |
| 95 | + const followerBefore = await db.query.accounts.findFirst({ |
| 96 | + where: eq(accounts.id, seeded.followerId), |
| 97 | + }); |
| 98 | + expect(followerBefore?.followingCount).toBe(0); |
| 99 | + |
| 100 | + // Path B: Accept wraps a Follow object whose id does NOT match any stored |
| 101 | + // follow IRI, so the objectId-based lookup (Path A) finds nothing and falls |
| 102 | + // through to the embedded-object fallback. |
| 103 | + const accept = await Accept.fromJsonLd({ |
| 104 | + "@context": ["https://www.w3.org/ns/activitystreams"], |
| 105 | + id: `${seeded.followingIri}#accepts/${crypto.randomUUID()}`, |
| 106 | + type: "Accept", |
| 107 | + actor: { |
| 108 | + id: seeded.followingIri, |
| 109 | + type: "Person", |
| 110 | + preferredUsername: "following", |
| 111 | + inbox: `${seeded.followingIri}/inbox`, |
| 112 | + }, |
| 113 | + object: { |
| 114 | + id: `${seeded.followerIri}#follows/${crypto.randomUUID()}`, |
| 115 | + type: "Follow", |
| 116 | + actor: seeded.followerIri, |
| 117 | + object: seeded.followingIri, |
| 118 | + }, |
| 119 | + }); |
| 120 | + |
| 121 | + await onFollowAccepted(ctx, accept); |
| 122 | + |
| 123 | + const followerAfter = await db.query.accounts.findFirst({ |
| 124 | + where: eq(accounts.id, seeded.followerId), |
| 125 | + }); |
| 126 | + expect(followerAfter?.followingCount).toBe(1); |
| 127 | + }); |
| 128 | +}); |
| 129 | + |
| 130 | +describe("onFollowRejected", () => { |
| 131 | + beforeEach(async () => { |
| 132 | + await cleanDatabase(); |
| 133 | + }); |
| 134 | + |
| 135 | + it("deletes a pending follow from embedded Follow object", async () => { |
| 136 | + expect.assertions(1); |
| 137 | + |
| 138 | + const seeded = await seedFollow(); |
| 139 | + const reject = await Reject.fromJsonLd({ |
| 140 | + "@context": ["https://www.w3.org/ns/activitystreams"], |
| 141 | + id: `${seeded.followingIri}#rejects/${crypto.randomUUID()}`, |
| 142 | + type: "Reject", |
| 143 | + actor: { |
| 144 | + id: seeded.followingIri, |
| 145 | + type: "Person", |
| 146 | + preferredUsername: "following", |
| 147 | + inbox: `${seeded.followingIri}/inbox`, |
| 148 | + }, |
| 149 | + object: { |
| 150 | + id: `${seeded.followerIri}#follows/${crypto.randomUUID()}`, |
| 151 | + type: "Follow", |
| 152 | + actor: seeded.followerIri, |
| 153 | + object: seeded.followingIri, |
| 154 | + }, |
| 155 | + }); |
| 156 | + |
| 157 | + await onFollowRejected(ctx, reject); |
| 158 | + |
| 159 | + const follow = await db.query.follows.findFirst({ |
| 160 | + where: and( |
| 161 | + eq(follows.followerId, seeded.followerId), |
| 162 | + eq(follows.followingId, seeded.followingId), |
| 163 | + ), |
| 164 | + }); |
| 165 | + expect(follow).toBeUndefined(); |
| 166 | + }); |
| 167 | +}); |
0 commit comments