v3: added opengraph support to the app, wrote README instructions incl. a Docker image
This commit is contained in:
@@ -34,7 +34,7 @@ const PLAYLIST_SELECT = `p.*, u.username as owner_username,
|
||||
(SELECT COUNT(*) FROM playlist_dumps pd WHERE pd.playlist_id = p.id) as dump_count
|
||||
FROM playlists p LEFT JOIN users u ON u.id = p.user_id`;
|
||||
|
||||
function getPlaylistById(idOrSlug: string): Playlist {
|
||||
export function getPlaylistById(idOrSlug: string): Playlist {
|
||||
const row = UUID_RE.test(idOrSlug)
|
||||
? db.prepare(`SELECT ${PLAYLIST_SELECT} WHERE p.id = ?;`).get(idOrSlug)
|
||||
: db.prepare(`SELECT ${PLAYLIST_SELECT} WHERE p.slug = ?;`).get(idOrSlug);
|
||||
|
||||
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 });
|
||||
},
|
||||
};
|
||||
@@ -2,6 +2,7 @@ import type { RichContent } from "../model/interfaces.ts";
|
||||
import { youtubeProvider } from "./providers/youtube.ts";
|
||||
import { bandcampProvider } from "./providers/bandcamp.ts";
|
||||
import { soundcloudProvider } from "./providers/soundcloud.ts";
|
||||
import { selfProvider } from "./providers/self.ts";
|
||||
import { genericProvider } from "./providers/generic.ts";
|
||||
|
||||
export interface RichContentProvider {
|
||||
@@ -12,12 +13,14 @@ export interface RichContentProvider {
|
||||
|
||||
/**
|
||||
* Register providers in priority order. The first match wins.
|
||||
* `selfProvider` resolves gerbeur URLs directly from the DB (no HTTP round-trip).
|
||||
* `genericProvider` must stay last — it always matches.
|
||||
*/
|
||||
const providers: RichContentProvider[] = [
|
||||
youtubeProvider,
|
||||
bandcampProvider,
|
||||
soundcloudProvider,
|
||||
selfProvider,
|
||||
genericProvider,
|
||||
];
|
||||
|
||||
@@ -86,11 +89,17 @@ function isPrivateHost(hostname: string): boolean {
|
||||
return /^(127\.|10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.)/.test(hostname);
|
||||
}
|
||||
|
||||
const SELF_PATH_RE = /^\/(dumps|users|playlists)\/[^/]+$/;
|
||||
|
||||
export function isValidHttpUrl(raw: string): boolean {
|
||||
try {
|
||||
const u = new URL(raw);
|
||||
if (u.protocol !== "http:" && u.protocol !== "https:") return false;
|
||||
if (isPrivateHost(u.hostname)) return false;
|
||||
// Allow private hosts for self-referential gerbeur URLs — they are
|
||||
// resolved directly from the DB by selfProvider, no outbound HTTP needed.
|
||||
if (isPrivateHost(u.hostname) && !SELF_PATH_RE.test(u.pathname)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
type User,
|
||||
} from "../model/interfaces.ts";
|
||||
import { db, isUserRow, userApiToRow, userRowToApi } from "../model/db.ts";
|
||||
import { disconnectUser } from "./ws-service.ts";
|
||||
|
||||
import { hashPassword } from "../lib/jwt.ts";
|
||||
|
||||
@@ -160,6 +161,8 @@ export function updateUserAvatar(userId: string, mime: string): void {
|
||||
}
|
||||
|
||||
export function deleteUser(userId: string): void {
|
||||
disconnectUser(userId);
|
||||
|
||||
const result = db.prepare(
|
||||
`DELETE FROM users WHERE id = ?;`,
|
||||
).run(userId);
|
||||
|
||||
@@ -66,6 +66,15 @@ export function sendToUser(userId: string, data: ServerToClientMessage): void {
|
||||
}
|
||||
}
|
||||
|
||||
export function disconnectUser(userId: string): void {
|
||||
for (const client of clients) {
|
||||
if (client.userId === userId) {
|
||||
send(client.socket, { type: "force_logout" });
|
||||
client.socket.close(1000, "Account deleted");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function broadcastPresence(): void {
|
||||
const users = getOnlineUsers();
|
||||
for (const client of clients) {
|
||||
|
||||
Reference in New Issue
Block a user