mirror of
https://gitlab.com/PronounsPage/PronounsPage.git
synced 2025-08-03 19:17:07 -04:00
112 lines
3.3 KiB
TypeScript
112 lines
3.3 KiB
TypeScript
/* eslint-disable camelcase */
|
|
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);
|
|
};
|