a tool for shared writing and social publishing
1"use server";
2
3import { cookies } from "next/headers";
4import { supabaseServerClient } from "supabase/serverClient";
5import { cache } from "react";
6export const getIdentityData = cache(uncachedGetIdentityData);
7export async function uncachedGetIdentityData() {
8 let cookieStore = await cookies();
9 let auth_token =
10 cookieStore.get("auth_token")?.value ||
11 cookieStore.get("external_auth_token")?.value;
12 let auth_res = auth_token
13 ? await supabaseServerClient
14 .from("email_auth_tokens")
15 .select(
16 `*,
17 identities(
18 *,
19 bsky_profiles(*),
20 notifications(count),
21 publication_subscriptions(*),
22 custom_domains!custom_domains_identity_id_fkey(publication_domains(*), *),
23 home_leaflet:permission_tokens!identities_home_page_fkey(*, permission_token_rights(*,
24 entity_sets(entities(facts(*)))
25 )),
26 permission_token_on_homepage(
27 archived,
28 created_at,
29 permission_tokens!inner(
30 id,
31 root_entity,
32 permission_token_rights(*),
33 leaflets_to_documents(*, documents(*)),
34 leaflets_in_publications(*, publications(*), documents(*))
35 )
36 )
37 )`,
38 )
39 .eq("identities.notifications.read", false)
40 .eq("id", auth_token)
41 .eq("confirmed", true)
42 .single()
43 : null;
44 if (!auth_res?.data?.identities) return null;
45 if (auth_res.data.identities.atp_did) {
46 //I should create a relationship table so I can do this in the above query
47 let { data: publications } = await supabaseServerClient
48 .from("publications")
49 .select("*")
50 .eq("identity_did", auth_res.data.identities.atp_did);
51 return {
52 ...auth_res.data.identities,
53 publications: publications || [],
54 };
55 }
56
57 return { ...auth_res.data.identities, publications: [] };
58}