a tool for shared writing and social publishing
1"use server";
2import { refresh } from "next/cache";
3
4import { drizzle } from "drizzle-orm/node-postgres";
5import {
6 entities,
7 permission_tokens,
8 permission_token_rights,
9} from "drizzle/schema";
10import { eq } from "drizzle-orm";
11import { PermissionToken } from "src/replicache";
12import { pool } from "supabase/pool";
13import { getIdentityData } from "./getIdentityData";
14import { supabaseServerClient } from "supabase/serverClient";
15
16export async function deleteLeaflet(permission_token: PermissionToken) {
17 const client = await pool.connect();
18 const db = drizzle(client);
19 await db.transaction(async (tx) => {
20 let [token] = await tx
21 .select()
22 .from(permission_tokens)
23 .leftJoin(
24 permission_token_rights,
25 eq(permission_tokens.id, permission_token_rights.token),
26 )
27 .where(eq(permission_tokens.id, permission_token.id));
28
29 if (!token?.permission_token_rights?.write) return;
30 await tx
31 .delete(entities)
32 .where(eq(entities.set, token.permission_token_rights.entity_set));
33 await tx
34 .delete(permission_tokens)
35 .where(eq(permission_tokens.id, permission_token.id));
36 });
37 client.release();
38
39 refresh();
40 return;
41}
42
43export async function archivePost(token: string) {
44 let identity = await getIdentityData();
45 if (!identity) throw new Error("No Identity");
46
47 // Archive on homepage
48 await supabaseServerClient
49 .from("permission_token_on_homepage")
50 .update({ archived: true })
51 .eq("token", token)
52 .eq("identity", identity.id);
53
54 // Check if leaflet is in any publications where user is the creator
55 let { data: leafletInPubs } = await supabaseServerClient
56 .from("leaflets_in_publications")
57 .select("publication, publications!inner(identity_did)")
58 .eq("leaflet", token);
59
60 if (leafletInPubs) {
61 for (let pub of leafletInPubs) {
62 if (pub.publications.identity_did === identity.atp_did) {
63 await supabaseServerClient
64 .from("leaflets_in_publications")
65 .update({ archived: true })
66 .eq("leaflet", token)
67 .eq("publication", pub.publication);
68 }
69 }
70 }
71
72 refresh();
73 return;
74}
75
76export async function unarchivePost(token: string) {
77 let identity = await getIdentityData();
78 if (!identity) throw new Error("No Identity");
79
80 // Unarchive on homepage
81 await supabaseServerClient
82 .from("permission_token_on_homepage")
83 .update({ archived: false })
84 .eq("token", token)
85 .eq("identity", identity.id);
86
87 // Check if leaflet is in any publications where user is the creator
88 let { data: leafletInPubs } = await supabaseServerClient
89 .from("leaflets_in_publications")
90 .select("publication, publications!inner(identity_did)")
91 .eq("leaflet", token);
92
93 if (leafletInPubs) {
94 for (let pub of leafletInPubs) {
95 if (pub.publications.identity_did === identity.atp_did) {
96 await supabaseServerClient
97 .from("leaflets_in_publications")
98 .update({ archived: false })
99 .eq("leaflet", token)
100 .eq("publication", pub.publication);
101 }
102 }
103 }
104
105 refresh();
106 return;
107}