105 lines
3.1 KiB
TypeScript
105 lines
3.1 KiB
TypeScript
/**
|
|
* Previews links to this site's own dump, user and playlist pages from the
|
|
* database, for `rich-content-service.ts`. No request goes out, so this works
|
|
* even when the site runs on a private host that `fetchWithTimeout` refuses.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import type { RichContent } from "../../../shared/dump.ts";
|
|
import type { RichContentProvider } from "./provider.ts";
|
|
import { getDump } from "../../services/dump-service.ts";
|
|
import { getUserByUsername } from "../../services/user-service.ts";
|
|
import { getPlaylistById } from "../../services/playlist-service.ts";
|
|
import { PUBLIC_URL, THEME_COLOR } from "../../config.ts";
|
|
import { isSelfUrl } from "../../lib/self-url.ts";
|
|
|
|
const DUMP_RE = /^\/dumps\/([^/]+)$/;
|
|
const USER_RE = /^\/users\/([^/]+)$/;
|
|
const PLAYLIST_RE = /^\/playlists\/([^/]+)$/;
|
|
|
|
/**
|
|
* Gives a link to this site without a thumbnail a placeholder in the site's
|
|
* icon and color.
|
|
*/
|
|
const SELF_BRANDING = {
|
|
siteName: "gerbeur",
|
|
faviconUrl: `${PUBLIC_URL}/apple-touch-icon.png`,
|
|
accentColor: THEME_COLOR,
|
|
} as const;
|
|
|
|
/**
|
|
* Comes before the generic provider, and claims what `isSelfUrl` accepts. Its
|
|
* `fetch` rejects with the services' 404 `APIException` for a missing page or
|
|
* a private dump. A private playlist is previewed all the same.
|
|
*/
|
|
export const selfProvider: RichContentProvider = {
|
|
name: "self",
|
|
|
|
matches(url: string): boolean {
|
|
try {
|
|
return isSelfUrl(new URL(url));
|
|
} catch {
|
|
return false;
|
|
}
|
|
},
|
|
|
|
fetch(url: string): Promise<RichContent> {
|
|
const { pathname } = new URL(url);
|
|
|
|
const dumpMatch = pathname.match(DUMP_RE);
|
|
if (dumpMatch) {
|
|
const dump = getDump(dumpMatch[1]);
|
|
let thumbnailUrl: string | undefined;
|
|
if (dump.kind === "file" && dump.fileMime?.startsWith("image/")) {
|
|
thumbnailUrl = `${PUBLIC_URL}/api/files/${dump.id}`;
|
|
} else if (dump.richContent?.thumbnailUrl) {
|
|
thumbnailUrl = dump.richContent.thumbnailUrl;
|
|
}
|
|
return Promise.resolve({
|
|
type: "generic",
|
|
url,
|
|
...SELF_BRANDING,
|
|
title: dump.title,
|
|
description: dump.comment,
|
|
thumbnailUrl,
|
|
});
|
|
}
|
|
|
|
const userMatch = pathname.match(USER_RE);
|
|
if (userMatch) {
|
|
const user = getUserByUsername(userMatch[1]);
|
|
const thumbnailUrl = user.avatarMime
|
|
? `${PUBLIC_URL}/api/avatars/${user.id}`
|
|
: undefined;
|
|
return Promise.resolve({
|
|
type: "generic",
|
|
url,
|
|
...SELF_BRANDING,
|
|
title: user.username,
|
|
description: user.description,
|
|
thumbnailUrl,
|
|
});
|
|
}
|
|
|
|
const playlistMatch = pathname.match(PLAYLIST_RE);
|
|
if (playlistMatch) {
|
|
const playlist = getPlaylistById(playlistMatch[1]);
|
|
const thumbnailUrl = playlist.imageMime
|
|
? `${PUBLIC_URL}/api/playlists/${playlist.id}/image`
|
|
: undefined;
|
|
return Promise.resolve({
|
|
type: "generic",
|
|
url,
|
|
...SELF_BRANDING,
|
|
title: playlist.title,
|
|
description: playlist.description,
|
|
thumbnailUrl,
|
|
});
|
|
}
|
|
|
|
// Unreachable: isSelfUrl accepts only the three paths above.
|
|
return Promise.resolve({ type: "generic", url });
|
|
},
|
|
};
|