(refactor) migrate /api/sources/** from express to h3

This commit is contained in:
Valentyne Stigloher 2025-03-20 13:01:10 +01:00
parent b93ddf2f2b
commit ad8ee97954
19 changed files with 285 additions and 244 deletions

View File

@ -17,7 +17,7 @@ const { data: sourcesRaw } = useAsyncData(async () => {
});
const sources = computed(() => {
if (!key) {
if (!key || !sourcesRaw.value) {
return;
}

View File

@ -80,7 +80,7 @@ const edit = (source: Source): void => {
defineExpose({ edit });
const pronounLibrary = await loadPronounLibrary(config);
const { data: keys } = await useFetch('/api/sources/keys', { lazy: true, default: () => [] });
const { data: keys } = await useFetch('/api/sources/keys', { lazy: true, default: () => ({}) });
</script>
<template>

View File

@ -97,7 +97,7 @@ const focus = (editable = true): void => {
defineExpose({ edit, focus });
const abbreviations = await loadNounAbbreviations();
const { data: sourcesKeys } = await useFetch('/api/sources/keys', { lazy: true, default: () => [] });
const { data: sourcesKeys } = await useFetch('/api/sources/keys', { lazy: true, default: () => ({}) });
</script>
<template>

View File

@ -207,7 +207,7 @@ const generatorResult = computed(() => {
const sources = ref<Record<string, Source[] | undefined>>();
onMounted(async () => {
const rawSources = await $fetch('/api/sources?pronoun=dukatywy');
const rawSources = await $fetch('/api/sources', { query: { pronoun: 'dukatywy' } });
sources.value = {
'': new SourceLibrary(config, rawSources).getForPronoun('dukatywy'),
};

View File

@ -149,7 +149,7 @@ const generatorResult = computed(() => {
const sources = ref<Record<string, Source[] | undefined>>();
onMounted(async () => {
const rawSources = await $fetch('/api/sources?pronoun=iksatywy');
const rawSources = await $fetch('/api/sources', { query: { pronoun: 'iksatywy' } });
sources.value = {
'': new SourceLibrary(config, rawSources).getForPronoun('iksatywy'),
};

View File

@ -64,7 +64,7 @@ const personNouns = [
const sources = ref<Record<string, Source[] | undefined>>();
onMounted(async () => {
const rawSources = await $fetch('/api/sources?pronoun=osobatywy');
const rawSources = await $fetch('/api/sources', { query: { pronoun: 'osobatywy' } });
sources.value = {
'': new SourceLibrary(config, rawSources).getForPronoun('osobatywy'),
};

View File

@ -1,7 +1,6 @@
<script setup lang="ts">
import type SourceSubmitForm from '~/components/SourceSubmitForm.vue';
import { Source, SourceLibrary } from '~/src/classes.ts';
import type { SourceRaw } from '~/src/classes.ts';
import { loadPronounLibrary } from '~/src/data.ts';
import { changeSourceInjectionKey } from '~/src/injectionKeys.ts';
@ -22,7 +21,7 @@ useSimpleHead({
const filter = useFilterWithCategory();
const { data: rawSources } = await useFetch<SourceRaw[]>('/api/sources', { lazy: true });
const { data: rawSources } = await useFetch('/api/sources', { lazy: true });
const sourceLibrary = computed(() => {
if (rawSources.value === null) {
return undefined;

View File

@ -11,10 +11,10 @@ import localeDescriptions from '~/locale/locales.ts';
import { getPosts } from '~/server/blog.ts';
import { getLocale, loadCalendar, loadConfig, loadPronounLibrary, loadTranslator } from '~/server/data.ts';
import { getInclusiveEntries } from '~/server/express/inclusive.ts';
import { getSourcesEntries } from '~/server/express/sources.ts';
import { getTermsEntries } from '~/server/express/terms.ts';
import { getNounEntries } from '~/server/nouns.ts';
import { rootDir } from '~/server/paths.ts';
import { getSourcesEntries } from '~/server/sources.ts';
import { shortForVariant } from '~/src/buildPronoun.ts';
import { Day } from '~/src/calendar/helpers.ts';
import { getUrlForLocale } from '~/src/domain.ts';

View File

@ -0,0 +1,21 @@
import SQL from 'sql-template-strings';
import { getLocale } from '~/server/data.ts';
import { linkOtherVersions } from '~/server/sources.ts';
export default defineEventHandler(async (event) => {
const { isGranted } = await useAuthentication(event);
const locale = getLocale(event);
const id = getRouterParam(event, 'id');
const db = useDatabase();
return await linkOtherVersions(db, isGranted, locale, await db.all(SQL`
SELECT s.*, u.username AS submitter FROM sources s
LEFT JOIN users u ON s.submitter_id = u.id
WHERE s.locale = ${locale}
AND s.deleted = 0
AND s.approved >= ${isGranted('sources') ? 0 : 1}
AND s.id = ${id}
`));
});

View File

@ -0,0 +1,20 @@
import { auditLog } from '~/server/audit.ts';
import { getLocale } from '~/server/data.ts';
import { approveSourceEntry } from '~/server/sources.ts';
export default defineEventHandler(async (event) => {
const { user, isGranted } = await useAuthentication(event);
if (!isGranted('sources')) {
throw createError({
status: 401,
statusMessage: 'Unauthorised',
});
}
const id = getRouterParam(event, 'id')!;
const db = useDatabase();
await approveSourceEntry(db, id, getLocale(event));
await auditLog({ user }, 'sources/approved', { id });
});

View File

@ -0,0 +1,27 @@
import SQL from 'sql-template-strings';
import { auditLog } from '~/server/audit.ts';
import { getLocale } from '~/server/data.ts';
export default defineEventHandler(async (event) => {
const { user, isGranted } = await useAuthentication(event);
if (!isGranted('sources')) {
throw createError({
status: 401,
statusMessage: 'Unauthorised',
});
}
const id = getRouterParam(event, 'id');
const db = useDatabase();
await db.get(SQL`
UPDATE sources
SET approved = 0
WHERE id = ${id}
`);
await invalidateCacheKind('sources', getLocale(event));
await auditLog({ user }, 'sources/hidden', { id });
});

View File

@ -0,0 +1,11 @@
import { getLocale } from '~/server/data.ts';
import { getSourcesEntries } from '~/server/sources.ts';
export default defineEventHandler(async (event) => {
const locale = getLocale(event);
const pronounsFilter = getQuery(event).pronoun as string | undefined;
const { isGranted } = await useAuthentication(event);
const db = useDatabase();
return await getSourcesEntries(db, isGranted, locale, pronounsFilter);
});

View File

@ -0,0 +1,17 @@
import SQL from 'sql-template-strings';
export default defineEventHandler(async (): Promise<Record<string, string>> => {
const db = useDatabase();
const keys = await db.all<{ key: string }>(SQL`
SELECT trim(key) AS key
FROM sources
WHERE key IS NOT NULL
AND deleted = 0
AND approved = 1
GROUP BY key
ORDER BY key
`);
return Object.fromEntries(keys.map((k) => [k.key, k.key]));
});

View File

@ -0,0 +1,27 @@
import SQL from 'sql-template-strings';
import { auditLog } from '~/server/audit.ts';
import { getLocale } from '~/server/data.ts';
export default defineEventHandler(async (event) => {
const { user, isGranted } = await useAuthentication(event);
if (!isGranted('sources')) {
throw createError({
status: 401,
statusMessage: 'Unauthorised',
});
}
const id = getRouterParam(event, 'id');
const db = useDatabase();
await db.get(SQL`
UPDATE sources
SET deleted=1
WHERE id = ${id}
`);
await invalidateCacheKind('sources', getLocale(event));
await auditLog({ user }, 'sources/removed', { id });
});

View File

@ -0,0 +1,43 @@
import SQL from 'sql-template-strings';
import { ulid } from 'ulid';
import { auditLog } from '~/server/audit.ts';
import { getLocale } from '~/server/data.ts';
import { approveSourceEntry } from '~/server/sources.ts';
import { isAllowedToPost } from '~/server/user.ts';
import { clearKey } from '~/src/helpers.ts';
export default defineEventHandler(async (event) => {
const { user, isGranted } = await useAuthentication(event);
const db = useDatabase();
if (!user || !await isAllowedToPost(db, user)) {
throw createError({
status: 401,
statusMessage: 'Unauthorised',
});
}
const locale = getLocale(event);
const body = await readBody(event);
const id = ulid();
await db.get(SQL`
INSERT INTO sources (id, locale, pronouns, type, author, title, extra, year, fragments, comment, link, key, images, spoiler, submitter_id, base_id)
VALUES (
${id}, ${locale}, ${body.pronouns.join(';')},
${body.type}, ${body.author}, ${body.title}, ${body.extra}, ${body.year},
${body.fragments.join('@').replace(/\n/g, '|')}, ${body.comment}, ${body.link},
${clearKey(body.key)}, ${body.images ? body.images.join(',') : null}, ${body.spoiler ? 1 : 0},
${user.id}, ${body.base}
)
`);
await auditLog({ user }, 'sources/submitted', body);
if (isGranted('sources')) {
await approveSourceEntry(db, id, locale);
await auditLog({ user }, 'sources/approved', { id });
}
setResponseStatus(event, 201, 'Created');
});

View File

@ -1,232 +0,0 @@
import { Router } from 'express';
import type { Request } from 'express';
import { getH3Event } from 'h3-express';
import SQL from 'sql-template-strings';
import { ulid } from 'ulid';
import { clearKey, handleErrorAsync } from '../../src/helpers.ts';
import { auditLog } from '../audit.ts';
import type { Database } from '../db.ts';
import { getLocale } from '~/server/data.ts';
import type { SourceType } from '~/src/classes.ts';
export interface SourceRow {
id: string;
locale: string;
pronouns: string;
type: SourceType;
author: string | null;
title: string;
extra: string | null;
year: number | null;
fragments: string;
comment: string | null;
link: string | null;
submitter_id: string | null;
approved: boolean;
deleted: number | null;
base_id: string | null;
key: string | null;
images: string | null;
spoiler: boolean;
}
const approve = async (db: Database, id: string, locale: string) => {
const { base_id } = (await db.get<Pick<SourceRow, 'base_id'>>(SQL`SELECT base_id FROM sources WHERE id=${id}`))!;
if (base_id) {
await db.get(SQL`
UPDATE sources
SET deleted=1
WHERE id = ${base_id}
`);
}
await db.get(SQL`
UPDATE sources
SET approved = 1, base_id = NULL
WHERE id = ${id}
`);
await invalidateCacheKind('sources', locale);
};
const keyGetMain = (key: string): string => {
return key.split('/')[0];
};
const linkOtherVersions = async (
db: Database,
isGranted: Request['isGranted'],
locale: string,
sources: SourceRow[],
) => {
const keys = new Set(sources.filter((s) => !!s && s.key).map((s) => keyGetMain(clearKey(s.key)!)));
let sql = SQL`
SELECT s.*, u.username AS submitter FROM sources s
LEFT JOIN users u ON s.submitter_id = u.id
WHERE s.locale != ${locale}
AND s.deleted = 0
AND s.approved >= ${isGranted('sources') ? 0 : 1}
AND (`;
for (const key of keys) {
sql = sql.append(SQL`s.key = ${key} OR s.key LIKE ${`${key}/%`} OR `);
}
sql = sql.append(SQL`0=1)`);
const otherVersions = await db.all<SourceRow>(sql);
const otherVersionsMap: Record<string, SourceRow[]> = {};
otherVersions.forEach((version) => {
const k = keyGetMain(version.key!);
if (otherVersionsMap[k] === undefined) {
otherVersionsMap[k] = [];
}
otherVersionsMap[k].push(version);
});
return sources.map((s) => ({
...s,
versions: s.key ? otherVersionsMap[keyGetMain(s.key)] || [] : [],
}));
};
const router = Router();
export const getSourcesEntries = defineCachedFunction(async (
db: Database,
isGranted: Request['isGranted'],
locale: string,
pronounsFilter: string | undefined,
) => {
const sql = SQL`
SELECT s.*, u.username AS submitter FROM sources s
LEFT JOIN users u ON s.submitter_id = u.id
WHERE s.locale = ${locale}
AND s.deleted = 0
AND s.approved >= ${isGranted('sources') ? 0 : 1}
`;
if (pronounsFilter) {
sql.append(SQL`AND s.pronouns LIKE ${`%${pronounsFilter}%`}`);
}
return await linkOtherVersions(db, isGranted, locale, await db.all(sql));
}, {
name: 'sources',
getKey: (db, isGranted, locale, pronounsFilter) => {
return pronounsFilter ? `${locale}:pronouns:${pronounsFilter}` : `${locale}:default`;
},
shouldBypassCache: (db, isGranted) => isGranted('sources'),
maxAge: 24 * 60 * 60,
});
router.get('/sources', handleErrorAsync(async (req, res) => {
const locale = getLocale(getH3Event(req));
const pronounsFilter = req.query.pronoun as string | undefined;
return res.json(await getSourcesEntries(req.db, req.isGranted, locale, pronounsFilter));
}));
router.post('/sources/submit', handleErrorAsync(async (req, res) => {
if (!req.user || !await req.isUserAllowedToPost()) {
return res.status(401).json({ error: 'Unauthorised' });
}
const locale = getLocale(getH3Event(req));
const id = ulid();
await req.db.get(SQL`
INSERT INTO sources (id, locale, pronouns, type, author, title, extra, year, fragments, comment, link, key, images, spoiler, submitter_id, base_id)
VALUES (
${id}, ${locale}, ${req.body.pronouns.join(';')},
${req.body.type}, ${req.body.author}, ${req.body.title}, ${req.body.extra}, ${req.body.year},
${req.body.fragments.join('@').replace(/\n/g, '|')}, ${req.body.comment}, ${req.body.link},
${clearKey(req.body.key)}, ${req.body.images ? req.body.images.join(',') : null}, ${req.body.spoiler ? 1 : 0},
${req.user ? req.user.id : null}, ${req.body.base}
)
`);
await auditLog(req, 'sources/submitted', { ...req.body });
if (req.isGranted('sources')) {
await approve(req.db, id, locale);
await auditLog(req, 'sources/approved', { id });
}
return res.json('ok');
}));
router.post('/sources/hide/:id', handleErrorAsync(async (req, res) => {
if (!req.isGranted('sources')) {
return res.status(401).json({ error: 'Unauthorised' });
}
await req.db.get(SQL`
UPDATE sources
SET approved = 0
WHERE id = ${req.params.id}
`);
await invalidateCacheKind('sources', getLocale(getH3Event(req)));
await auditLog(req, 'sources/hidden', { id: req.params.id });
return res.json('ok');
}));
router.post('/sources/approve/:id', handleErrorAsync(async (req, res) => {
if (!req.isGranted('sources')) {
return res.status(401).json({ error: 'Unauthorised' });
}
await approve(req.db, req.params.id, getLocale(getH3Event(req)));
await auditLog(req, 'sources/approved', { id: req.params.id });
return res.json('ok');
}));
router.post('/sources/remove/:id', handleErrorAsync(async (req, res) => {
if (!req.isGranted('sources')) {
return res.status(401).json({ error: 'Unauthorised' });
}
await req.db.get(SQL`
UPDATE sources
SET deleted=1
WHERE id = ${req.params.id}
`);
await invalidateCacheKind('sources', getLocale(getH3Event(req)));
await auditLog(req, 'sources/removed', { id: req.params.id });
return res.json('ok');
}));
router.get('/sources/keys', handleErrorAsync(async (req, res) => {
const keys = await req.db.all<{ key: string }>(SQL`
SELECT trim(key) AS key
FROM sources
WHERE key IS NOT NULL
AND deleted = 0
AND approved = 1
GROUP BY key
ORDER BY key
`);
return res.json(
Object.fromEntries(keys.map((k) => [k.key, k.key])),
);
}));
router.get('/sources/:id', handleErrorAsync(async (req, res) => {
const locale = getLocale(getH3Event(req));
return res.json(await linkOtherVersions(req.db, req.isGranted, locale, await req.db.all(SQL`
SELECT s.*, u.username AS submitter FROM sources s
LEFT JOIN users u ON s.submitter_id = u.id
WHERE s.locale = ${locale}
AND s.deleted = 0
AND s.approved >= ${req.isGranted('sources') ? 0 : 1}
AND s.id = ${req.params.id}
`)));
}));
export default router;

View File

@ -27,7 +27,6 @@ import namesRoute from './express/names.ts';
import profileRoute from './express/profile.ts';
import pronounceRoute from './express/pronounce.ts';
import sentryRoute from './express/sentry.ts';
import sourcesRoute from './express/sources.ts';
import subscriptionRoute from './express/subscription.ts';
import termsRoute from './express/terms.ts';
import translationsRoute from './express/translations.ts';
@ -160,7 +159,6 @@ router.use(userRoute);
router.use(profileRoute);
router.use(adminRoute);
router.use(mfaRoute);
router.use(sourcesRoute);
router.use(inclusiveRoute);
router.use(termsRoute);
router.use(pronounceRoute);

View File

@ -1,8 +1,8 @@
import SQL from 'sql-template-strings';
import type { Database } from '~/server/db.ts';
import type { SourceRow } from '~/server/express/sources.ts';
import type { UserRow } from '~/server/express/user.ts';
import type { SourceRow } from '~/server/sources.ts';
import type { IsGrantedFn } from '~/server/utils/useAuthentication.ts';
import { clearKey } from '~/src/helpers.ts';
import type { User } from '~/src/user.ts';

110
server/sources.ts Normal file
View File

@ -0,0 +1,110 @@
import SQL from 'sql-template-strings';
import type { Database } from '~/server/db.ts';
import type { SourceType } from '~/src/classes.ts';
import { clearKey } from '~/src/helpers.ts';
export interface SourceRow {
id: string;
locale: string;
pronouns: string;
type: SourceType;
author: string | null;
title: string;
extra: string | null;
year: number | null;
fragments: string;
comment: string | null;
link: string | null;
submitter_id: string | null;
approved: boolean;
deleted: number | null;
base_id: string | null;
key: string | null;
images: string | null;
spoiler: boolean;
}
const keyGetMain = (key: string): string => {
return key.split('/')[0];
};
export const linkOtherVersions = async (
db: Database,
isGranted: IsGrantedFn,
locale: string,
sources: SourceRow[],
) => {
const keys = new Set(sources.filter((s) => !!s && s.key).map((s) => keyGetMain(clearKey(s.key)!)));
let sql = SQL`
SELECT s.*, u.username AS submitter FROM sources s
LEFT JOIN users u ON s.submitter_id = u.id
WHERE s.locale != ${locale}
AND s.deleted = 0
AND s.approved >= ${isGranted('sources') ? 0 : 1}
AND (`;
for (const key of keys) {
sql = sql.append(SQL`s.key = ${key} OR s.key LIKE ${`${key}/%`} OR `);
}
sql = sql.append(SQL`0=1)`);
const otherVersions = await db.all<SourceRow>(sql);
const otherVersionsMap: Record<string, SourceRow[]> = {};
otherVersions.forEach((version) => {
const k = keyGetMain(version.key!);
if (otherVersionsMap[k] === undefined) {
otherVersionsMap[k] = [];
}
otherVersionsMap[k].push(version);
});
return sources.map((s) => ({
...s,
versions: s.key ? otherVersionsMap[keyGetMain(s.key)] || [] : [],
}));
};
export const getSourcesEntries = defineCachedFunction(async (
db: Database,
isGranted: IsGrantedFn,
locale: string,
pronounsFilter: string | undefined,
) => {
const sql = SQL`
SELECT s.*, u.username AS submitter FROM sources s
LEFT JOIN users u ON s.submitter_id = u.id
WHERE s.locale = ${locale}
AND s.deleted = 0
AND s.approved >= ${isGranted('sources') ? 0 : 1}
`;
if (pronounsFilter) {
sql.append(SQL`AND s.pronouns LIKE ${`%${pronounsFilter}%`}`);
}
return await linkOtherVersions(db, isGranted, locale, await db.all(sql));
}, {
name: 'sources',
getKey: (db, isGranted, locale, pronounsFilter) => {
return pronounsFilter ? `${locale}:pronouns:${pronounsFilter}` : `${locale}:default`;
},
shouldBypassCache: (db, isGranted) => isGranted('sources'),
maxAge: 24 * 60 * 60,
});
export const approveSourceEntry = async (db: Database, id: string, locale: string) => {
const { base_id } = (await db.get<Pick<SourceRow, 'base_id'>>(SQL`SELECT base_id FROM sources WHERE id=${id}`))!;
if (base_id) {
await db.get(SQL`
UPDATE sources
SET deleted=1
WHERE id = ${base_id}
`);
}
await db.get(SQL`
UPDATE sources
SET approved = 1, base_id = NULL
WHERE id = ${id}
`);
await invalidateCacheKind('sources', locale);
};