mirror of
https://gitlab.com/PronounsPage/PronounsPage.git
synced 2025-09-28 23:42:58 -04:00

the #shared alias used by Nuxt cannot be easily disabled and to prevent breackage with jiti, we make use of it
47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
import SQL from 'sql-template-strings';
|
|
import { ulid } from 'ulid';
|
|
|
|
import { reactionOptions } from '#shared/blog/reactions.ts';
|
|
import { getPostReactions } from '~~/server/blog.ts';
|
|
import { getLocale, loadConfig } from '~~/server/data.ts';
|
|
import useAuthentication from '~~/server/utils/useAuthentication.ts';
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const config = await loadConfig(getLocale(event));
|
|
if (!config.links.enabled || !config.links.blog) {
|
|
throw createError({
|
|
status: 404,
|
|
statusMessage: 'Not Found',
|
|
});
|
|
}
|
|
|
|
const { user } = await useAuthentication(event);
|
|
if (!user) {
|
|
throw createError({
|
|
status: 401,
|
|
statusMessage: 'Unauthorised',
|
|
});
|
|
}
|
|
|
|
const locale = getLocale(event);
|
|
|
|
const body = await readBody(event);
|
|
const emoji = body.emoji;
|
|
if (!reactionOptions.includes(emoji)) {
|
|
throw createError({
|
|
status: 400,
|
|
statusMessage: 'Bad Request',
|
|
message: 'Invalid emoji',
|
|
});
|
|
}
|
|
const slug = getRouterParam(event, 'slug')!;
|
|
const db = useDatabase();
|
|
await db.get(SQL`DELETE FROM blog_reactions
|
|
WHERE locale = ${locale} AND slug = ${slug} AND user_id = ${user.id} AND emoji = ${emoji}`);
|
|
if (body.reaction) {
|
|
await db.get(SQL`INSERT INTO blog_reactions (id, locale, slug, emoji, user_id)
|
|
VALUES (${ulid()}, ${locale}, ${slug}, ${emoji}, ${user.id})`);
|
|
}
|
|
return await getPostReactions(db, locale, slug, user.id);
|
|
});
|