···23 // Check publication and document ownership in one query
24 let { data: tokenData } = await supabaseServerClient
25 .from("permission_tokens")
26- .select(`
027 id,
28 leaflets_in_publications(publication, publications!inner(identity_did)),
29 leaflets_to_documents(document, documents!inner(uri))
30- `)
031 .eq("id", permission_token.id)
32 .single();
33···36 const leafletInPubs = tokenData.leaflets_in_publications || [];
37 if (leafletInPubs.length > 0) {
38 if (!identity) {
39- throw new Error("Unauthorized: You must be logged in to delete a leaflet in a publication");
0040 }
41 const isOwner = leafletInPubs.some(
42- (pub: any) => pub.publications.identity_did === identity.atp_did
43 );
44 if (!isOwner) {
45- throw new Error("Unauthorized: You must own the publication to delete this leaflet");
0046 }
47 }
48···50 const leafletDocs = tokenData.leaflets_to_documents || [];
51 if (leafletDocs.length > 0) {
52 if (!identity) {
53- throw new Error("Unauthorized: You must be logged in to delete a published leaflet");
0054 }
55 for (let leafletDoc of leafletDocs) {
56 const docUri = leafletDoc.documents?.uri;
57 // Extract the DID from the document URI (format: at://did:plc:xxx/...)
58- if (docUri && !docUri.includes(identity.atp_did)) {
59- throw new Error("Unauthorized: You must own the published document to delete this leaflet");
0060 }
61 }
62 }
···23 // Check publication and document ownership in one query
24 let { data: tokenData } = await supabaseServerClient
25 .from("permission_tokens")
26+ .select(
27+ `
28 id,
29 leaflets_in_publications(publication, publications!inner(identity_did)),
30 leaflets_to_documents(document, documents!inner(uri))
31+ `,
32+ )
33 .eq("id", permission_token.id)
34 .single();
35···38 const leafletInPubs = tokenData.leaflets_in_publications || [];
39 if (leafletInPubs.length > 0) {
40 if (!identity) {
41+ throw new Error(
42+ "Unauthorized: You must be logged in to delete a leaflet in a publication",
43+ );
44 }
45 const isOwner = leafletInPubs.some(
46+ (pub: any) => pub.publications.identity_did === identity.atp_did,
47 );
48 if (!isOwner) {
49+ throw new Error(
50+ "Unauthorized: You must own the publication to delete this leaflet",
51+ );
52 }
53 }
54···56 const leafletDocs = tokenData.leaflets_to_documents || [];
57 if (leafletDocs.length > 0) {
58 if (!identity) {
59+ throw new Error(
60+ "Unauthorized: You must be logged in to delete a published leaflet",
61+ );
62 }
63 for (let leafletDoc of leafletDocs) {
64 const docUri = leafletDoc.documents?.uri;
65 // Extract the DID from the document URI (format: at://did:plc:xxx/...)
66+ if (docUri && identity.atp_did && !docUri.includes(identity.atp_did)) {
67+ throw new Error(
68+ "Unauthorized: You must own the published document to delete this leaflet",
69+ );
70 }
71 }
72 }