Something went wrong. Try again.
a tool for shared writing and social publishing
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164"use server";import { refresh } from "next/cache";
import { drizzle } from "drizzle-orm/node-postgres";import { entities, permission_tokens, permission_token_rights,} from "drizzle/schema";import { eq } from "drizzle-orm";import { PermissionToken } from "src/replicache";import { pool } from "supabase/pool";import { getIdentityData } from "./getIdentityData";import { supabaseServerClient } from "supabase/serverClient";
export async function deleteLeaflet(permission_token: PermissionToken) { const client = await pool.connect(); const db = drizzle(client);
// Get the current user's identity let identity = await getIdentityData();
// Check publication and document ownership in one query let { data: tokenData } = await supabaseServerClient .from("permission_tokens") .select( ` id, leaflets_in_publications(publication, publications!inner(identity_did)), leaflets_to_documents(document, documents!inner(uri)) `, ) .eq("id", permission_token.id) .single();
if (tokenData) { // Check if leaflet is in a publication const leafletInPubs = tokenData.leaflets_in_publications || []; if (leafletInPubs.length > 0) { if (!identity) { throw new Error( "Unauthorized: You must be logged in to delete a leaflet in a publication", ); } const isOwner = leafletInPubs.some( (pub: any) => pub.publications.identity_did === identity.atp_did, ); if (!isOwner) { throw new Error( "Unauthorized: You must own the publication to delete this leaflet", ); } }
// Check if there's a standalone published document const leafletDocs = tokenData.leaflets_to_documents || []; if (leafletDocs.length > 0) { if (!identity) { throw new Error( "Unauthorized: You must be logged in to delete a published leaflet", ); } for (let leafletDoc of leafletDocs) { const docUri = leafletDoc.documents?.uri; // Extract the DID from the document URI (format: at://did:plc:xxx/...) if (docUri && identity.atp_did && !docUri.includes(identity.atp_did)) { throw new Error( "Unauthorized: You must own the published document to delete this leaflet", ); } } } }
await db.transaction(async (tx) => { let [token] = await tx .select() .from(permission_tokens) .leftJoin( permission_token_rights, eq(permission_tokens.id, permission_token_rights.token), ) .where(eq(permission_tokens.id, permission_token.id));
if (!token?.permission_token_rights?.write) return; await tx .delete(entities) .where(eq(entities.set, token.permission_token_rights.entity_set)); await tx .delete(permission_tokens) .where(eq(permission_tokens.id, permission_token.id)); }); client.release();
refresh(); return;}
export async function archivePost(token: string) { let identity = await getIdentityData(); if (!identity) throw new Error("No Identity");
// Archive on homepage await supabaseServerClient .from("permission_token_on_homepage") .update({ archived: true }) .eq("token", token) .eq("identity", identity.id);
// Check if leaflet is in any publications where user is the creator let { data: leafletInPubs } = await supabaseServerClient .from("leaflets_in_publications") .select("publication, publications!inner(identity_did)") .eq("leaflet", token);
if (leafletInPubs) { for (let pub of leafletInPubs) { if (pub.publications.identity_did === identity.atp_did) { await supabaseServerClient .from("leaflets_in_publications") .update({ archived: true }) .eq("leaflet", token) .eq("publication", pub.publication); } } }
refresh(); return;}
export async function unarchivePost(token: string) { let identity = await getIdentityData(); if (!identity) throw new Error("No Identity");
// Unarchive on homepage await supabaseServerClient .from("permission_token_on_homepage") .update({ archived: false }) .eq("token", token) .eq("identity", identity.id);
// Check if leaflet is in any publications where user is the creator let { data: leafletInPubs } = await supabaseServerClient .from("leaflets_in_publications") .select("publication, publications!inner(identity_did)") .eq("leaflet", token);
if (leafletInPubs) { for (let pub of leafletInPubs) { if (pub.publications.identity_did === identity.atp_did) { await supabaseServerClient .from("leaflets_in_publications") .update({ archived: false }) .eq("leaflet", token) .eq("publication", pub.publication); } } }
refresh(); return;}