v3: added opengraph support to the app, wrote README instructions incl. a Docker image
This commit is contained in:
83
api/services/providers/self.ts
Normal file
83
api/services/providers/self.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import type { RichContent } from "../../model/interfaces.ts";
|
||||
import type { RichContentProvider } from "../rich-content-service.ts";
|
||||
import { getDump } from "../dump-service.ts";
|
||||
import { getUserByUsername } from "../user-service.ts";
|
||||
import { getPlaylistById } from "../playlist-service.ts";
|
||||
import { BASE_URL } from "../../config.ts";
|
||||
|
||||
const DUMP_RE = /^\/dumps\/([^/]+)$/;
|
||||
const USER_RE = /^\/users\/([^/]+)$/;
|
||||
const PLAYLIST_RE = /^\/playlists\/([^/]+)$/;
|
||||
|
||||
export const selfProvider: RichContentProvider = {
|
||||
name: "self",
|
||||
|
||||
matches(url: string): boolean {
|
||||
try {
|
||||
const { pathname } = new URL(url);
|
||||
return DUMP_RE.test(pathname) ||
|
||||
USER_RE.test(pathname) ||
|
||||
PLAYLIST_RE.test(pathname);
|
||||
} 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 = `${BASE_URL}/api/files/${dump.id}`;
|
||||
} else if (dump.richContent?.thumbnailUrl) {
|
||||
thumbnailUrl = dump.richContent.thumbnailUrl;
|
||||
}
|
||||
return Promise.resolve({
|
||||
type: "generic",
|
||||
url,
|
||||
siteName: "gerbeur",
|
||||
title: dump.title,
|
||||
description: dump.comment,
|
||||
thumbnailUrl,
|
||||
});
|
||||
}
|
||||
|
||||
const userMatch = pathname.match(USER_RE);
|
||||
if (userMatch) {
|
||||
const user = getUserByUsername(userMatch[1]);
|
||||
const thumbnailUrl = user.avatarMime
|
||||
? `${BASE_URL}/api/avatars/${user.id}`
|
||||
: undefined;
|
||||
return Promise.resolve({
|
||||
type: "generic",
|
||||
url,
|
||||
siteName: "gerbeur",
|
||||
title: user.username,
|
||||
description: user.description,
|
||||
thumbnailUrl,
|
||||
});
|
||||
}
|
||||
|
||||
const playlistMatch = pathname.match(PLAYLIST_RE);
|
||||
if (playlistMatch) {
|
||||
const playlist = getPlaylistById(playlistMatch[1]);
|
||||
const thumbnailUrl = playlist.imageMime
|
||||
? `${BASE_URL}/api/playlists/${playlist.id}/image`
|
||||
: undefined;
|
||||
return Promise.resolve({
|
||||
type: "generic",
|
||||
url,
|
||||
siteName: "gerbeur",
|
||||
title: playlist.title,
|
||||
description: playlist.description,
|
||||
thumbnailUrl,
|
||||
});
|
||||
}
|
||||
|
||||
// Should not reach here if matches() is correct
|
||||
return Promise.resolve({ type: "generic", url });
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user