v4: every page and API URL follows one of two rules, written down in API.md — pages are a hierarchy where @name is a user and any other root word a category or an app page, tabs and views are plain segments and a default view is the bare path; API resources are /api/<plural>/<key>, a user's key being an id or @name, the caller's things under /api/me, media a sub-resource, lookups by another key a query filter. ~ is gone: the feed is /, /hot, /new, /journal, /follow and /music/hot, a profile /@bob with its overview and the tabs dumps, upvoted, playlists (her own only), follow (users, and /follow/playlists), invitees and manage (role and name, for user:manage), which replace the separate UserDumps, UserUpvoted and UserPlaylists pages; /settings holds the account (now with the username) and /manage the category manager, both linked from the user menu and one's profile. Dumps and playlists live under their owner, /@bob/dumps/<slug> and /@bob/playlists/<slug>, named by slug alone: migration 0021 re-slugs them to the bare slugify(title), unique per owner and kind (the oldest keeps it, clashes get -2), renames the categories whose slug v4 reserves (hot, follow, settings…), and rewrites this site's v3 links in dump notes, comments, chat, and playlist and profile descriptions. A slug changes only when edited, through the edit forms' new link field, which proposes slugify(title) as the title changes; a username changes from /settings or, for user:manage, from /@name/manage. Every slug or name left behind stays an alias of its item or user for good (dump_slug_aliases, playlist_slug_aliases, user_aliases), so old links redirect without chaining and nobody else can take one; slugs change 3 times a day per item, names once in 30 days, moderators exempt. An alias, or a name in another case, answers the item with its current path: the pages put it in their URL (useCanonicalPath, without fetching again), and the OG middleware answers a 301. shared/paths.ts builds and parses every page path, for the client's links, the emails' (digest-service lost its own builders and its id fallbacks, and links /follow and /settings) and the server's matchers (OG tags, self previews, backlinks, self-url); shared/slug.ts holds slugify and the format categories share. Dumps and playlists carry ownerUsername and a required slug, and notifications join their users' current names and their items' paths each time they are sent or read, so renames reach them and a deleted item's has no link. The API was renamed per the plan's table: POST /api/users and /api/sessions, /api/password-resets, /api/me (with settings, password, avatar, media-token, playlists?contains=, follow, feed?from=, notifications), /api/users/:key/{dumps,upvoted,playlists,follow/users,follow/playlists,invitees,avatar} and by path /api/users/:key/{dumps,playlists}/:slug, /api/dumps?url= and ?category=, /api/dumps/:id/{file,thumbnail,metadata}, /api/playlists/:id/cover, /api/remote/{preview,image,bandcamp,youtube} (one router), and /api/unsubscribe, the v3 POST /api/users/unsubscribe staying for the digests already sent. A stored default feed tab "followed" reads as "follow" once. dumps.slug and playlists.slug stay nullable, since SQLite can't add NOT NULL without rebuilding tables others reference; every writer sets them and unique (user_id, slug) indexes hold them. 254 backend tests (new: migration 0021 on a v3 fixture, slug aliases, swaps and rate limits, username renames, aliases and limits, reads by path and their privacy, the OG 301s, both unsubscribe paths, v4-shaped digest links with no ids, notification joins, shared paths and slugs) and 668 frontend tests (new: the profile's tabs and follow views, Settings, Manage, the link field, canonical URLs on the dump, playlist and profile pages, bare default feed paths) pass, and tsc, eslint, deno check, deno lint, deno fmt and doc:lint are clean; the migration was also run on a copy of the development database.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
khannurien
2026-10-06 16:08:59 +00:00
parent fd0d81fe66
commit 4468438ba7
228 changed files with 7320 additions and 5193 deletions

View File

@@ -13,13 +13,13 @@ const DAY = 86_400_000;
function open(items: Notification[], { hasMore = false } = {}) {
route(
"GET /api/notifications",
"GET /api/me/notifications",
(req) =>
req.url.searchParams.get("page") === "1"
? page(items, hasMore)
: page([]),
);
route("POST /api/notifications/read-all", () => ({}));
route("POST /api/me/notifications/read", () => ({}));
const user = userEvent.setup();
renderApp(<Notifications />, { session: me, route: "/notifications" });
const socket = FakeWebSocket.last;
@@ -40,6 +40,8 @@ const n = {
followerId: "f",
followerUsername: "fan",
playlistId: "p1",
playlistSlug: "p1",
playlistOwnerUsername: "bob",
playlistTitle: "Mix",
},
}),
@@ -49,6 +51,8 @@ const n = {
dumperId: "a",
dumperUsername: "ann",
dumpId: "d1",
dumpSlug: "d1",
dumpOwnerUsername: "bob",
dumpTitle: "Song",
},
}),
@@ -56,8 +60,12 @@ const n = {
type: "playlist_dump_added",
data: {
dumpId: "d2",
dumpSlug: "d2",
dumpOwnerUsername: "bob",
dumpTitle: "Tune",
playlistId: "p",
playlistSlug: "p",
playlistOwnerUsername: "bob",
playlistTitle: "Mix",
},
}),
@@ -67,6 +75,8 @@ const n = {
voterId: "v",
voterUsername: "bob",
dumpId: "d3",
dumpSlug: "d3",
dumpOwnerUsername: "bob",
dumpTitle: "Pic",
},
}),
@@ -77,6 +87,8 @@ const n = {
commenterUsername: "cy",
commentId: "c1",
dumpId: "d4",
dumpSlug: "d4",
dumpOwnerUsername: "bob",
dumpTitle: "Post",
},
}),
@@ -87,6 +99,8 @@ const n = {
likerUsername: "lu",
commentId: "c2",
dumpId: "d5",
dumpSlug: "d5",
dumpOwnerUsername: "bob",
dumpTitle: "Thing",
},
}),
@@ -99,6 +113,8 @@ const n = {
contextId: "c3",
contextTitle: "",
dumpId: "d6",
dumpSlug: "d6",
dumpOwnerUsername: "bob",
},
}),
mentionChat: makeNotification({
@@ -118,6 +134,8 @@ const n = {
mentionerUsername: "mo",
contextType: "playlist",
contextId: "p9",
playlistSlug: "faves",
playlistOwnerUsername: "bob",
contextTitle: "Faves",
},
}),
@@ -128,26 +146,45 @@ describe("Notifications", () => {
open(Object.values(n));
const link = async (text: RegExp) =>
(await screen.findByText(text)).closest("a")!.getAttribute("href");
expect(await link(/started following you/)).toBe("/users/fan");
expect(await link(/followed your playlist/)).toBe("/playlists/p1");
expect(await link(/^posted$/)).toBe("/dumps/d1");
expect(await link(/was added to/)).toBe("/dumps/d2");
expect(await link(/^upvoted$/)).toBe("/dumps/d3");
expect(await link(/commented on/)).toBe("/dumps/d4#comment-c1");
expect(await link(/liked your comment on/)).toBe("/dumps/d5#comment-c2");
expect(await link(/started following you/)).toBe("/@fan");
expect(await link(/followed your playlist/)).toBe("/@bob/playlists/p1");
expect(await link(/^posted$/)).toBe("/@bob/dumps/d1");
expect(await link(/was added to/)).toBe("/@bob/dumps/d2");
expect(await link(/^upvoted$/)).toBe("/@bob/dumps/d3");
expect(await link(/commented on/)).toBe("/@bob/dumps/d4#comment-c1");
expect(await link(/liked your comment on/)).toBe(
"/@bob/dumps/d5#comment-c2",
);
const mentions = screen.getAllByText(/mentioned you in/).map((el) =>
el.closest("a")!.getAttribute("href")
);
expect(mentions).toEqual([
"/dumps/d6#comment-c3",
"/@bob/dumps/d6#comment-c3",
"/notifications?chat=open",
"/playlists/p9",
"/@bob/playlists/faves",
]);
expect(screen.getByText("a comment")).toBeInTheDocument();
expect(screen.getByText("the chat")).toBeInTheDocument();
expect(screen.getByText("Faves")).toBeInTheDocument();
});
test("one about something since deleted reads, but links nowhere", async () => {
open([
makeNotification({
type: "dump_upvoted",
data: {
voterId: "v",
voterUsername: "vic",
dumpId: "gone",
dumpTitle: "Gone dump",
},
}),
]);
const item = (await screen.findByText(/^upvoted$/)).closest("li")!;
expect(item.querySelector("a.notification-item-link")).toBeNull();
expect(item).toHaveTextContent("Gone dump");
});
test("grouped by day", async () => {
open([
makeNotification({ createdAt: new Date() }),
@@ -164,7 +201,7 @@ describe("Notifications", () => {
open([n.follow, { ...n.upvoted, read: true }]);
await screen.findByText(/started following you/);
await waitFor(() =>
expect(calls("POST /api/notifications/read-all")).toHaveLength(1)
expect(calls("POST /api/me/notifications/read")).toHaveLength(1)
);
await waitFor(() =>
expect(screen.getByRole("button", { name: "Notifications" }))
@@ -178,24 +215,24 @@ describe("Notifications", () => {
});
test("a list that loaded stays shown if marking it read fails", async () => {
route("POST /api/notifications/read-all", () => new TypeError("offline"));
route("POST /api/me/notifications/read", () => new TypeError("offline"));
open([n.follow]);
expect(await screen.findByText(/started following you/))
.toBeInTheDocument();
await waitFor(() =>
expect(calls("POST /api/notifications/read-all")).toHaveLength(1)
expect(calls("POST /api/me/notifications/read")).toHaveLength(1)
);
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
expect(screen.getByText(/started following you/)).toBeInTheDocument();
});
test("a failed load shows the server's reason", async () => {
route("GET /api/notifications", () => fail("Database is locked", 500));
route("GET /api/me/notifications", () => fail("Database is locked", 500));
renderApp(<Notifications />, { session: me });
const alert = await screen.findByRole("alert");
expect(alert).toHaveTextContent("Failed to load");
expect(alert).toHaveTextContent("Database is locked");
expect(calls("POST /api/notifications/read-all")).toHaveLength(0);
expect(calls("POST /api/me/notifications/read")).toHaveLength(0);
});
test("empty", async () => {
@@ -215,7 +252,7 @@ describe("Notifications", () => {
expect(items[0]).toHaveClass("notification-item--unread");
// Seen as it arrives: marked read, so the bell doesn't count it.
await waitFor(() =>
expect(calls("POST /api/notifications/read-all")).toHaveLength(2)
expect(calls("POST /api/me/notifications/read")).toHaveLength(2)
);
await waitFor(() =>
expect(screen.getByRole("button", { name: "Notifications" }))
@@ -225,13 +262,13 @@ describe("Notifications", () => {
test("one that arrives while the list is loading isn't lost", async () => {
const list = deferred<unknown>();
route("GET /api/notifications", () => list.promise);
route("POST /api/notifications/read-all", () => ({}));
route("GET /api/me/notifications", () => list.promise);
route("POST /api/me/notifications/read", () => ({}));
renderApp(<Notifications />, { session: me, route: "/notifications" });
const socket = FakeWebSocket.last;
socket.welcome({ unreadNotificationCount: 1 });
await waitFor(() =>
expect(calls("GET /api/notifications")).toHaveLength(1)
expect(calls("GET /api/me/notifications")).toHaveLength(1)
);
// Created after the list was read, so the list won't have it — but the
// read-all after the load will mark it read.
@@ -252,21 +289,21 @@ describe("Notifications", () => {
expect(items[0]).toHaveClass("notification-item--unread");
// The read-all after the load covered them: none is sent again.
await waitFor(() =>
expect(calls("POST /api/notifications/read-all")).toHaveLength(1)
expect(calls("POST /api/me/notifications/read")).toHaveLength(1)
);
await new Promise((r) => setTimeout(r, 50));
expect(calls("POST /api/notifications/read-all")).toHaveLength(1);
expect(calls("POST /api/me/notifications/read")).toHaveLength(1);
});
test("load more", async () => {
route(
"GET /api/notifications",
"GET /api/me/notifications",
(req) =>
req.url.searchParams.get("page") === "1"
? page([n.follow], true)
: page([n.upvoted], false),
);
route("POST /api/notifications/read-all", () => ({}));
route("POST /api/me/notifications/read", () => ({}));
const user = userEvent.setup();
renderApp(<Notifications />, { session: me });
await user.click(await screen.findByRole("button", { name: "Load more" }));