a tool for shared writing and social publishing
1"use server";
2
3import { drizzle } from "drizzle-orm/node-postgres";
4import { sql } from "drizzle-orm";
5import { cookies } from "next/headers";
6import { pool } from "supabase/pool";
7
8export async function addLeafletToHome(leaflet: string) {
9 let auth_token = (await cookies()).get("auth_token")?.value;
10 const client = await pool.connect();
11 const db = drizzle(client);
12 await db.transaction(async (tx) => {
13 if (auth_token) {
14 await tx.execute(sql`
15 WITH auth_token AS (
16 SELECT identities.id as identity_id
17 FROM email_auth_tokens
18 LEFT JOIN identities ON email_auth_tokens.identity = identities.id
19 WHERE email_auth_tokens.id = ${auth_token}
20 AND email_auth_tokens.confirmed = true
21 AND identities.id IS NOT NULL
22 )
23 INSERT INTO permission_token_on_homepage (token, identity)
24 SELECT ${leaflet}, identity_id
25 FROM auth_token
26 `);
27 }
28
29 return;
30 });
31 client.release();
32 return;
33}