import "server-only"; import { db } from "@/db"; import { cdn, shareTexts } from "@/db/schema"; import type { ShareSource } from "@/db/schema/share"; import type { ValidatedImage } from "@/lib/share/validation"; export type ShareAuthor = { userId: string; discordId: string; displayName: string; avatarUrl: string | null; }; export async function createShare({ content, description, image, source, author, }: { content: string; description: string | null; image: ValidatedImage | null; source: ShareSource; author: ShareAuthor; }) { return db.transaction(async (tx) => { let imageCdnId: string | null = null; if (image) { const [record] = await tx .insert(cdn) .values({ data: image.bytes, name: image.name, type: image.type, size: image.size, ownerDiscordId: author.discordId, }) .returning({ id: cdn.id }); imageCdnId = record.id; } const [share] = await tx .insert(shareTexts) .values({ content, description, imageCdnId, source, authorId: author.userId, authorDiscordId: author.discordId, authorName: author.displayName, authorAvatarUrl: author.avatarUrl, }) .returning({ id: shareTexts.id, imageCdnId: shareTexts.imageCdnId, createdAt: shareTexts.createdAt, }); return share; }); }