v3: search engine, responsive header with compact user menu

This commit is contained in:
khannurien
2026-03-29 11:56:31 +00:00
parent f0f6472db6
commit cbb3505139
31 changed files with 1206 additions and 178 deletions

View File

@@ -17,28 +17,17 @@ import {
notifyUserFollowersNewDump,
} from "./notification-service.ts";
import { makeSlug, UUID_RE } from "../lib/slugify.ts";
import { DUMPS_DIR } from "../lib/upload.ts";
import {
DUMP_ALLOWED_MIME_PREFIXES,
DUMP_ALLOWED_MIME_TYPES,
DUMP_MAX_FILE_SIZE_BYTES,
DUMPS_DIR,
} from "../config.ts";
import { linkAttachments } from "./attachment-service.ts";
const MAX_FILE_SIZE = 50 * 1024 * 1024; // 50 MB
const ALLOWED_MIME_PREFIXES = ["text/", "image/", "video/", "audio/"];
const ALLOWED_MIME_TYPES = new Set([
"application/pdf",
"application/json",
"application/zip",
"application/x-zip-compressed",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
"application/msword",
"application/vnd.ms-excel",
"application/vnd.ms-powerpoint",
]);
function isAllowedMime(mime: string): boolean {
return ALLOWED_MIME_PREFIXES.some((p) => mime.startsWith(p)) ||
ALLOWED_MIME_TYPES.has(mime);
return DUMP_ALLOWED_MIME_PREFIXES.some((p) => mime.startsWith(p)) ||
DUMP_ALLOWED_MIME_TYPES.has(mime);
}
function titleFromUrl(url: string): string {
@@ -128,7 +117,7 @@ export async function createFileDump(
`File type '${file.type}' is not allowed`,
);
}
if (file.size > MAX_FILE_SIZE) {
if (file.size > DUMP_MAX_FILE_SIZE_BYTES) {
throw new APIException(
APIErrorCode.BAD_REQUEST,
400,
@@ -216,6 +205,39 @@ export function getDump(dumpId: string, requestingUserId?: string): Dump {
return dump;
}
export function searchDumps(
query: string,
page: number,
limit: number,
requestingUserId?: string,
): { items: Dump[]; total: number } {
if (!query.trim()) return { items: [], total: 0 };
const pattern = `%${query}%`;
const offset = (page - 1) * limit;
const searchClause =
`(title LIKE ? OR comment LIKE ? OR json_extract(rich_content,'$.title') LIKE ? OR json_extract(rich_content,'$.description') LIKE ?)`;
const searchParams = [pattern, pattern, pattern, pattern] as const;
if (requestingUserId) {
const rows = db.prepare(
`SELECT ${SELECT_COLS} FROM dumps WHERE (is_private = 0 OR user_id = ?) AND ${searchClause} ORDER BY created_at DESC LIMIT ? OFFSET ?;`,
).all(requestingUserId, ...searchParams, limit, offset);
const totalRow = db.prepare(
`SELECT COUNT(*) as count FROM dumps WHERE (is_private = 0 OR user_id = ?) AND ${searchClause};`,
).get(requestingUserId, ...searchParams) as { count: number } | undefined;
return { items: rows.filter(isDumpRow).map(dumpRowToApi), total: totalRow?.count ?? 0 };
} else {
const rows = db.prepare(
`SELECT ${SELECT_COLS} FROM dumps WHERE is_private = 0 AND ${searchClause} ORDER BY created_at DESC LIMIT ? OFFSET ?;`,
).all(...searchParams, limit, offset);
const totalRow = db.prepare(
`SELECT COUNT(*) as count FROM dumps WHERE is_private = 0 AND ${searchClause};`,
).get(...searchParams) as { count: number } | undefined;
return { items: rows.filter(isDumpRow).map(dumpRowToApi), total: totalRow?.count ?? 0 };
}
}
export function listDumps(
page: number,
limit: number,
@@ -367,7 +389,7 @@ export async function replaceFileDump(
`File type '${file.type}' is not allowed`,
);
}
if (file.size > MAX_FILE_SIZE) {
if (file.size > DUMP_MAX_FILE_SIZE_BYTES) {
throw new APIException(
APIErrorCode.BAD_REQUEST,
400,

View File

@@ -0,0 +1,93 @@
import nodemailer, { type SendMailOptions, type Transporter } from "nodemailer";
import { SMTPS_URL } from "../config.ts";
export type EmailRecipient = string | string[];
export interface EmailMessage {
from: string;
to: EmailRecipient;
subject: string;
text?: string;
html?: string;
cc?: EmailRecipient;
bcc?: EmailRecipient;
replyTo?: string;
}
export interface EmailSendResult {
messageId: string;
accepted: string[];
rejected: string[];
}
let transporter: Transporter | null = null;
export function isEmailEnabled(): boolean {
return SMTPS_URL.length > 0;
}
function normalizeRecipients(
recipient: string | readonly (string | { address?: string })[] | undefined,
): string[] {
if (!recipient) return [];
if (typeof recipient === "string") {
return recipient.split(",").map((s) => s.trim()).filter(Boolean);
}
return recipient.flatMap((entry) =>
typeof entry === "string" ? entry.trim() : (entry.address?.trim() ?? "")
).filter(Boolean);
}
function getTransporter(): Transporter {
if (!isEmailEnabled()) {
throw new Error(
"GERBEUR_SMTPS_URL environment variable is required to send emails.",
);
}
if (!transporter) {
transporter = nodemailer.createTransport(SMTPS_URL);
}
return transporter;
}
export async function verifyEmailTransport(): Promise<boolean> {
if (!isEmailEnabled()) return false;
await getTransporter().verify();
return true;
}
export async function sendEmail(message: EmailMessage): Promise<EmailSendResult> {
if (normalizeRecipients(message.to).length === 0) {
throw new Error("Email recipient is required.");
}
if (!message.subject.trim()) {
throw new Error("Email subject is required.");
}
if (!message.text?.trim() && !message.html?.trim()) {
throw new Error("Email body is required (text and/or html).");
}
const options: SendMailOptions = {
from: message.from,
to: message.to,
cc: message.cc,
bcc: message.bcc,
subject: message.subject,
text: message.text,
html: message.html,
replyTo: message.replyTo,
};
const info = await getTransporter().sendMail(options);
return {
messageId: info.messageId,
accepted: normalizeRecipients(info.accepted),
rejected: normalizeRecipients(info.rejected),
};
}

View File

@@ -112,6 +112,26 @@ export function getPlaylist(
return { ...playlist, dumps: visibleDumps };
}
export function searchPlaylists(
query: string,
limit: number,
requestingUserId?: string,
): Playlist[] {
if (!query.trim()) return [];
const pattern = `%${query}%`;
const searchClause = `(p.title LIKE ? OR p.description LIKE ?)`;
const rows = requestingUserId
? db.prepare(
`SELECT ${PLAYLIST_SELECT} WHERE (p.is_public = 1 OR p.user_id = ?) AND ${searchClause} ORDER BY p.created_at DESC LIMIT ?;`,
).all(requestingUserId, pattern, pattern, limit)
: db.prepare(
`SELECT ${PLAYLIST_SELECT} WHERE p.is_public = 1 AND ${searchClause} ORDER BY p.created_at DESC LIMIT ?;`,
).all(pattern, pattern, limit);
return rows.filter(isPlaylistRow).map(playlistRowToApi);
}
export function listPlaylistsByUser(
userId: string,
requestingUserId: string | null,

View File

@@ -1,6 +1,6 @@
import type { RichContent } from "../../model/interfaces.ts";
import type { RichContentProvider } from "../rich-content-service.ts";
import { fetchWithTimeout } from "../rich-content-service.ts";
import { extractOgTag, fetchWithTimeout } from "../rich-content-service.ts";
function extractVideoId(url: string): string | null {
try {
@@ -24,38 +24,123 @@ function extractVideoId(url: string): string | null {
return null;
}
function extractPlaylistId(url: string): string | null {
try {
return new URL(url).searchParams.get("list");
} catch {
return null;
}
}
/** Matches /channel/UC…, /@handle, /c/name, /user/name */
function extractChannelPath(url: string): string | null {
try {
const u = new URL(url);
if (u.hostname === "youtube.com" || u.hostname === "www.youtube.com") {
if (
u.pathname.startsWith("/channel/") ||
u.pathname.startsWith("/@") ||
u.pathname.startsWith("/c/") ||
u.pathname.startsWith("/user/")
) {
return u.pathname;
}
}
} catch {
// invalid URL
}
return null;
}
async function fetchOEmbed(
url: string,
): Promise<{ title?: string; thumbnailUrl?: string }> {
try {
const oembedUrl =
`https://www.youtube.com/oembed?url=${encodeURIComponent(url)}&format=json`;
const res = await fetchWithTimeout(oembedUrl);
if (res.ok) {
const data = await res.json() as {
title?: string;
thumbnail_url?: string;
};
return { title: data.title, thumbnailUrl: data.thumbnail_url };
}
} catch {
// oembed failed — carry on
}
return {};
}
export const youtubeProvider: RichContentProvider = {
name: "youtube",
matches(url: string): boolean {
return extractVideoId(url) !== null;
return (
extractVideoId(url) !== null ||
extractPlaylistId(url) !== null ||
extractChannelPath(url) !== null
);
},
async fetch(url: string): Promise<RichContent> {
const videoId = extractVideoId(url)!;
const thumbnailUrl = `https://img.youtube.com/vi/${videoId}/hqdefault.jpg`;
let title: string | undefined;
const videoId = extractVideoId(url);
const listId = extractPlaylistId(url);
const channelPath = extractChannelPath(url);
try {
const oembedUrl =
`https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=${videoId}&format=json`;
const res = await fetchWithTimeout(oembedUrl);
if (res.ok) {
const data = await res.json() as { title?: string };
title = data.title;
// ── Channel ───────────────────────────────────────────────────────────────
if (channelPath && !videoId) {
// oEmbed doesn't support channel URLs — scrape og:image from the page instead
let title: string | undefined;
let thumbnailUrl: string | undefined;
try {
const res = await fetchWithTimeout(url);
if (res.ok) {
const html = await res.text();
title = extractOgTag(html, "title");
thumbnailUrl = extractOgTag(html, "image");
}
} catch {
// scrape failed — carry on with undefined values
}
} catch {
// oembed failed — thumbnail still works
return {
type: "youtube",
siteName: "YouTube",
url,
title,
thumbnailUrl,
// channels are not embeddable as a player
};
}
// ── Playlist (no specific video) ──────────────────────────────────────────
if (listId && !videoId) {
const { title, thumbnailUrl } = await fetchOEmbed(url);
return {
type: "youtube",
siteName: "YouTube",
url,
title,
thumbnailUrl,
embedUrl: `https://www.youtube.com/embed/videoseries?list=${listId}&rel=0`,
};
}
// ── Video (with optional playlist context) ────────────────────────────────
const thumbnailUrl = `https://img.youtube.com/vi/${videoId}/hqdefault.jpg`;
const { title } = await fetchOEmbed(url);
const embedParams = new URLSearchParams({ rel: "0" });
if (listId) embedParams.set("list", listId);
return {
type: "youtube",
siteName: "YouTube",
url,
videoId,
videoId: videoId!,
title,
thumbnailUrl,
embedUrl: `https://www.youtube.com/embed/${videoId}?rel=0`,
embedUrl: `https://www.youtube.com/embed/${videoId}?${embedParams}`,
};
},
};

View File

@@ -102,6 +102,15 @@ export function searchUsers(
}));
}
export function searchUsersGlobal(query: string, limit: number): User[] {
if (!query.trim()) return [];
const pattern = `%${query}%`;
const rows = db.prepare(
`${USER_SELECT} WHERE (u.username LIKE ? OR u.description LIKE ?) ORDER BY u.username LIMIT ?;`,
).all(pattern, pattern, limit);
return rows.filter(isUserRow).map(userRowToApi);
}
export function listUsers(): User[] {
const userRows = db.prepare(
`${USER_SELECT}`,