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:
@@ -21,13 +21,7 @@ import type { Dump, DumpUrlMatch } from "../../../shared/dump.ts";
|
||||
import type { PaginatedData } from "../../../shared/api.ts";
|
||||
import type { Playlist } from "../../../shared/playlist.ts";
|
||||
import type { PublicUser } from "../../../shared/user.ts";
|
||||
import {
|
||||
assertEquals,
|
||||
assertExists,
|
||||
assertFalse,
|
||||
assertMatch,
|
||||
assertNotEquals,
|
||||
} from "@std/assert";
|
||||
import { assertEquals, assertExists, assertFalse } from "@std/assert";
|
||||
|
||||
const PAGE = html(`<title>Scraped title</title>
|
||||
<meta property="og:image" content="https://example.com/cover.jpg">`);
|
||||
@@ -53,8 +47,9 @@ test("POST /api/dumps: a URL dump takes its title from the page", async () => {
|
||||
const dump = res.data;
|
||||
assertEquals(dump.kind, "url");
|
||||
assertEquals(dump.title, "Scraped title");
|
||||
assertMatch(dump.slug ?? "", /^scraped-title-[0-9a-f]{8}$/);
|
||||
assertEquals(dump.slug, "scraped-title");
|
||||
assertEquals(dump.userId, user.id);
|
||||
assertEquals(dump.ownerUsername, user.username);
|
||||
assertEquals(dump.isPrivate, false);
|
||||
assertEquals(dump.richContent?.thumbnailUrl, "https://example.com/cover.jpg");
|
||||
const row = db.prepare(`SELECT url, url_canonical FROM dumps WHERE id = ?;`)
|
||||
@@ -149,7 +144,7 @@ test("POST /api/dumps: a file dump is stored and served back", async () => {
|
||||
assertEquals(dump.fileSize, PNG.byteLength);
|
||||
assertEquals(dump.comment, "tiny");
|
||||
assertEquals(readFile(`dumps/${dump.id}`), PNG);
|
||||
const file = await get(`/api/files/${dump.id}`);
|
||||
const file = await get(`/api/dumps/${dump.id}/file`);
|
||||
assertEquals(file.status, 200);
|
||||
assertEquals(file.bytes, PNG);
|
||||
});
|
||||
@@ -221,11 +216,12 @@ test("POST /api/dumps: followers are notified of public dumps, not private ones"
|
||||
|
||||
// ── Read ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
test("GET /api/dumps/:id works by id and by slug", async () => {
|
||||
test("GET /api/dumps/:id works by id only", async () => {
|
||||
const user = await createUser();
|
||||
const { id, slug } = insertDump({ userId: user.id, title: "Hello" });
|
||||
assertEquals((await get<Dump>(`/api/dumps/${id}`)).data.id, id);
|
||||
assertEquals((await get<Dump>(`/api/dumps/${slug}`)).data.id, id);
|
||||
// A slug is only unique under its owner: it is read by page path.
|
||||
assertEquals((await get(`/api/dumps/${slug}`)).status, 404);
|
||||
assertEquals((await get(`/api/dumps/nope`)).status, 404);
|
||||
assertEquals((await get(`/api/dumps/${crypto.randomUUID()}`)).status, 404);
|
||||
});
|
||||
@@ -293,7 +289,7 @@ test("GET /api/dumps: newest first, paginated, private ones for the owner only",
|
||||
);
|
||||
});
|
||||
|
||||
test("GET /api/dumps/by-url: matches on the canonical key, respects privacy", async () => {
|
||||
test("GET /api/dumps?url=: matches on the canonical key, respects privacy", async () => {
|
||||
const owner = await createUser();
|
||||
const other = await createUser();
|
||||
insertDump({
|
||||
@@ -308,24 +304,24 @@ test("GET /api/dumps/by-url: matches on the canonical key, respects privacy", as
|
||||
isPrivate: true,
|
||||
});
|
||||
const q = encodeURIComponent("https://m.youtube.com/watch?v=abc&t=10");
|
||||
const anon = await get<DumpUrlMatch[]>(`/api/dumps/by-url?url=${q}`);
|
||||
const anon = await get<DumpUrlMatch[]>(`/api/dumps?url=${q}`);
|
||||
assertEquals(anon.data.map((d) => d.title), [
|
||||
"public",
|
||||
]);
|
||||
assertEquals(anon.data[0].username, owner.username);
|
||||
const mine = await get<DumpUrlMatch[]>(`/api/dumps/by-url?url=${q}`, {
|
||||
assertEquals(anon.data[0].ownerUsername, owner.username);
|
||||
const mine = await get<DumpUrlMatch[]>(`/api/dumps?url=${q}`, {
|
||||
auth: owner.auth,
|
||||
});
|
||||
assertEquals(mine.data.length, 2);
|
||||
assertEquals(
|
||||
(await get<DumpUrlMatch[]>(`/api/dumps/by-url?url=${q}`, {
|
||||
(await get<DumpUrlMatch[]>(`/api/dumps?url=${q}`, {
|
||||
auth: other.auth,
|
||||
})).data
|
||||
.length,
|
||||
1,
|
||||
);
|
||||
assertEquals(
|
||||
(await get<DumpUrlMatch[]>(`/api/dumps/by-url?url=garbage`)).data,
|
||||
(await get<DumpUrlMatch[]>(`/api/dumps?url=garbage`)).data,
|
||||
[],
|
||||
);
|
||||
});
|
||||
@@ -365,7 +361,7 @@ test("GET /api/dumps/:id/related: links in both directions, hiding private ones"
|
||||
json: {
|
||||
url: "https://example.com/src",
|
||||
comment:
|
||||
`see https://www.x.test/t/?utm_source=feed and http://anywhere/dumps/${secret.slug} and http://gerbeur.test/dumps/self`,
|
||||
`see https://www.x.test/t/?utm_source=feed and http://anywhere${secret.path}#comment-x and http://gerbeur.test/@${other.username}/dumps/scraped-title`,
|
||||
},
|
||||
});
|
||||
const srcId = src.data.id;
|
||||
@@ -416,6 +412,48 @@ test("GET /api/dumps/:id/playlists: public playlists, plus the viewer's private
|
||||
|
||||
// ── Update ───────────────────────────────────────────────────────────────────
|
||||
|
||||
test("PUT /api/dumps/:id: a slug changes when sent, the old one stays an alias, 3 a day", async () => {
|
||||
const owner = await createUser();
|
||||
const mod = await createUser({ role: "moderator" });
|
||||
const { id } = insertDump({ userId: owner.id, title: "First" });
|
||||
const other = insertDump({ userId: owner.id, title: "Taken" });
|
||||
const byPath = (slug: string) =>
|
||||
get<Dump>(`/api/users/@${owner.username}/dumps/${slug}`, {
|
||||
auth: owner.auth,
|
||||
});
|
||||
const setSlug = (slug: string, auth = owner.auth) =>
|
||||
put<Dump>(`/api/dumps/${id}`, { auth, json: { slug } });
|
||||
|
||||
assertEquals((await setSlug("Not A Slug")).status, 400);
|
||||
// Another dump's slug, current or former, is refused.
|
||||
assertEquals((await setSlug(other.slug)).status, 409);
|
||||
assertEquals((await setSlug("second")).data.slug, "second");
|
||||
// The old slug answers the dump, which carries its current one.
|
||||
assertEquals((await byPath("first")).data.slug, "second");
|
||||
assertEquals((await byPath("second")).data.id, id);
|
||||
// Taking back its own former slug swaps them; the limit counts the aliases
|
||||
// left, so a swap doesn't add to it.
|
||||
assertEquals((await setSlug("first")).data.slug, "first");
|
||||
assertEquals((await byPath("second")).data.slug, "first");
|
||||
assertEquals((await setSlug("third")).data.slug, "third");
|
||||
assertEquals((await setSlug("fourth")).data.slug, "fourth");
|
||||
// A fourth alias in a day is refused, but not to a moderator.
|
||||
assertEquals((await setSlug("fifth")).status, 429);
|
||||
assertEquals((await setSlug("fifth", mod.auth)).data.slug, "fifth");
|
||||
// Every former slug still leads to it, without chaining.
|
||||
for (const slug of ["first", "second", "third", "fourth", "fifth"]) {
|
||||
assertEquals((await byPath(slug)).data.slug, "fifth", slug);
|
||||
}
|
||||
// Deleting the dump frees its aliases.
|
||||
await del(`/api/dumps/${id}`, { auth: owner.auth });
|
||||
assertEquals(
|
||||
count(`SELECT COUNT(*) AS n FROM dump_slug_aliases WHERE dump_id = ?;`, id),
|
||||
0,
|
||||
);
|
||||
const reborn = insertDump({ userId: owner.id, title: "Second" });
|
||||
assertEquals(reborn.slug, "second");
|
||||
});
|
||||
|
||||
test("PUT /api/dumps/:id: owner and moderator may edit, others may not", async () => {
|
||||
const owner = await createUser();
|
||||
const other = await createUser();
|
||||
@@ -430,14 +468,14 @@ test("PUT /api/dumps/:id: owner and moderator may edit, others may not", async (
|
||||
(await put(`/api/dumps/${id}`, { json: { title: "X" } })).status,
|
||||
401,
|
||||
);
|
||||
const res = await put<Dump>(`/api/dumps/${slug}`, {
|
||||
const res = await put<Dump>(`/api/dumps/${id}`, {
|
||||
auth: owner.auth,
|
||||
json: { title: "New title", comment: "hello" },
|
||||
});
|
||||
assertEquals(res.status, 200);
|
||||
assertEquals(res.data.title, "New title");
|
||||
assertMatch(res.data.slug ?? "", /^new-title-/);
|
||||
assertNotEquals(res.data.slug, slug);
|
||||
// A new title never changes the slug by itself.
|
||||
assertEquals(res.data.slug, slug);
|
||||
assertExists(res.data.updatedAt);
|
||||
const byMod = await put(`/api/dumps/${id}`, {
|
||||
auth: mod.auth,
|
||||
@@ -635,7 +673,7 @@ test("PUT /api/dumps/:id/file: visibility changes with the file, or stays", asyn
|
||||
let res = await replace({ isPrivate: "true" });
|
||||
assertEquals(res.data.isPrivate, true);
|
||||
assertEquals(await visible(), false);
|
||||
assertEquals((await get(`/api/files/${id}`)).status, 404);
|
||||
assertEquals((await get(`/api/dumps/${id}/file`)).status, 404);
|
||||
// Left out, it stays private.
|
||||
res = await replace({});
|
||||
assertEquals(res.data.isPrivate, true);
|
||||
@@ -677,7 +715,7 @@ test("custom thumbnails: set, served, removed", async () => {
|
||||
});
|
||||
assertEquals(set.status, 200);
|
||||
assertEquals(set.data.thumbnailMime, "image/png");
|
||||
const thumb = await get(`/api/thumbnails/${id}`);
|
||||
const thumb = await get(`/api/dumps/${id}/thumbnail`);
|
||||
assertEquals(thumb.status, 200);
|
||||
assertEquals(thumb.headers.get("content-type"), "image/png");
|
||||
assertEquals(thumb.bytes, PNG);
|
||||
@@ -688,10 +726,10 @@ test("custom thumbnails: set, served, removed", async () => {
|
||||
assertEquals(removed.data.thumbnailMime, undefined);
|
||||
assertEquals(readFile(`thumbnails/${id}-custom`), null);
|
||||
// A URL dump without its own thumbnail has none to serve.
|
||||
assertEquals((await get(`/api/thumbnails/${id}`)).status, 404);
|
||||
assertEquals((await get(`/api/dumps/${id}/thumbnail`)).status, 404);
|
||||
});
|
||||
|
||||
test("POST /api/dumps/:id/refresh-metadata: updates, or keeps what it had on failure", async () => {
|
||||
test("POST /api/dumps/:id/metadata: updates, or keeps what it had on failure", async () => {
|
||||
const owner = await createUser();
|
||||
const { id } = insertDump({
|
||||
userId: owner.id,
|
||||
@@ -707,7 +745,7 @@ test("POST /api/dumps/:id/refresh-metadata: updates, or keeps what it had on fai
|
||||
using _ = stubNetwork(() => {
|
||||
throw new TypeError("down");
|
||||
});
|
||||
const res = await post<Dump>(`/api/dumps/${id}/refresh-metadata`, {
|
||||
const res = await post<Dump>(`/api/dumps/${id}/metadata`, {
|
||||
auth: owner.auth,
|
||||
});
|
||||
assertEquals(res.status, 502);
|
||||
@@ -715,7 +753,7 @@ test("POST /api/dumps/:id/refresh-metadata: updates, or keeps what it had on fai
|
||||
}
|
||||
{
|
||||
using _ = stubNetwork(() => PAGE.clone());
|
||||
const res = await post<Dump>(`/api/dumps/${id}/refresh-metadata`, {
|
||||
const res = await post<Dump>(`/api/dumps/${id}/metadata`, {
|
||||
auth: owner.auth,
|
||||
});
|
||||
assertEquals(res.status, 200);
|
||||
@@ -723,7 +761,7 @@ test("POST /api/dumps/:id/refresh-metadata: updates, or keeps what it had on fai
|
||||
}
|
||||
const file = insertDump({ userId: owner.id, fileName: "f.txt" });
|
||||
assertEquals(
|
||||
(await post(`/api/dumps/${file.id}/refresh-metadata`, { auth: owner.auth }))
|
||||
(await post(`/api/dumps/${file.id}/metadata`, { auth: owner.auth }))
|
||||
.status,
|
||||
400,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user