Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
},
"dependencies": {
"@drfed/models": "workspace:*",
"@faker-js/faker": "catalog:",
"@fedify/uri-template": "^2.3.1",
"@fedify/fedify": "catalog:",
"@fedify/vocab": "catalog:",
Expand Down
22 changes: 21 additions & 1 deletion packages/graphql/src/actor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import assert from "node:assert/strict";

import { schema } from "@drfed/models";
import { type Uuid, uuidV7 as uuid } from "@drfed/models/uuid";
import { faker } from "@faker-js/faker";
import { describe, it } from "@logtape/testing-node/autoload";
import { and, eq } from "drizzle-orm";

Expand Down Expand Up @@ -143,7 +144,14 @@ describe("Mutation.generateActors", () => {
});
});

it("creates local actors", async () => {
it("creates local actors with normalized usernames and retries collisions", async (t) => {
const username = t.mock.method(
faker.internet,
"username",
() => "Dr.FED42",
);
username.mock.mockImplementationOnce(() => "Dr-Fed43", 2);

await withTestHarness(async ({ db, post }) => {
const auth = await seedAuthenticatedLocalInstance(db);

Expand Down Expand Up @@ -189,6 +197,18 @@ describe("Mutation.generateActors", () => {

const actors = await db.select().from(schema.actors);
assert.equal(actors.length, 2);
assert.equal(username.mock.callCount(), 3);
assert.deepEqual(
new Set(actors.map((actor) => actor.username)),
new Set(["dr_fed42", "dr_fed43"]),
);
for (const actor of actors) {
assert.match(actor.username, /^[a-z0-9_]+$/u);
assert.equal(
actor.profileUrl,
`https://test-instance.drfed.org/@${actor.username}`,
);
}
assert.equal(
actors.every(
(actor) =>
Expand Down
30 changes: 20 additions & 10 deletions packages/graphql/src/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import { type Database, promoteResource, schema } from "@drfed/models";
import { type CollectionRole, actorTypeEnum } from "@drfed/models/schema";
import { type Uuid, uuidV7 as uuid } from "@drfed/models/uuid";
import { faker } from "@faker-js/faker";
import type { Context } from "@fedify/fedify";
import { drizzleConnectionHelpers } from "@pothos/plugin-drizzle";
import type { PgInsertValue } from "drizzle-orm/pg-core";
Expand Down Expand Up @@ -358,14 +359,19 @@ builder.mutationFields((t) => ({
fedCtx.getActorUri(id).href,
"actor",
async (inner, resource) => {
const [createdActor] = await inner
.insert(schema.actors)
.values(generateActor(resource.id, targetInstanceId, fedCtx))
.returning();
if (createdActor == null) {
throw new Error("Actor insertion returned no row.");
for (let attempt = 0; attempt < 10; attempt += 1) {
const [createdActor] = await inner
.insert(schema.actors)
.values(generateActor(resource.id, targetInstanceId, fedCtx))
.onConflictDoNothing({
target: [schema.actors.username, schema.actors.instanceId],
})
.returning();
if (createdActor != null) {
return createdActor;
}
}
return createdActor;
throw new Error("Actor insertion returned no row.");
},
id,
);
Expand Down Expand Up @@ -406,15 +412,19 @@ function generateActor(
instanceId: Uuid,
fedCtx: Context<unknown>,
): PgInsertValue<typeof schema.actors> {
const username = faker.internet
.username()
.toLowerCase()
.replace(/[^a-z0-9_]/gu, "_");

return {
id,
localId: id,
// FIXME: https://github.com/fedify-dev/drfed/issues/85
username: id,
username,
instanceId,
type: "Person",
inboxUrl: fedCtx.getInboxUri(id).href,
profileUrl: new URL(`/@${id}`, fedCtx.origin).href,
profileUrl: new URL(`/@${username}`, fedCtx.origin).href,
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"preview": "vite preview"
},
"dependencies": {
"@faker-js/faker": "catalog:",
"@formisch/solid": "1.0.0",
"@kobalte/core": "^0.13.13",
"@solidjs/meta": "^0.29.4",
Expand All @@ -60,7 +61,6 @@
"vite": "^8.2.0"
},
"devDependencies": {
"@faker-js/faker": "^10.5.0",
"@types/relay-runtime": "^20.1.1",
"eslint-plugin-solid": "^0.14.5",
"relay-compiler": "^21.0.1",
Expand Down
12 changes: 9 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ allowBuilds:

catalog:
"@electric-sql/pglite": ^0.5.3
"@faker-js/faker": ^10.5.0
"@fedify/fedify": 2.4.0-pr.1020.43+41cebe9c
"@fedify/postgres": 2.4.0-pr.1020.43+41cebe9c
"@fedify/pglite": 2.4.0-pr.1020.43+41cebe9c
Expand Down
Loading