···1616export async function deleteLeaflet(permission_token: PermissionToken) {
1717 const client = await pool.connect();
1818 const db = drizzle(client);
1919+2020+ // Get the current user's identity
2121+ let identity = await getIdentityData();
2222+2323+ // Check publication and document ownership in one query
2424+ let { data: tokenData } = await supabaseServerClient
2525+ .from("permission_tokens")
2626+ .select(
2727+ `
2828+ id,
2929+ leaflets_in_publications(publication, publications!inner(identity_did)),
3030+ leaflets_to_documents(document, documents!inner(uri))
3131+ `,
3232+ )
3333+ .eq("id", permission_token.id)
3434+ .single();
3535+3636+ if (tokenData) {
3737+ // Check if leaflet is in a publication
3838+ const leafletInPubs = tokenData.leaflets_in_publications || [];
3939+ if (leafletInPubs.length > 0) {
4040+ if (!identity) {
4141+ throw new Error(
4242+ "Unauthorized: You must be logged in to delete a leaflet in a publication",
4343+ );
4444+ }
4545+ const isOwner = leafletInPubs.some(
4646+ (pub: any) => pub.publications.identity_did === identity.atp_did,
4747+ );
4848+ if (!isOwner) {
4949+ throw new Error(
5050+ "Unauthorized: You must own the publication to delete this leaflet",
5151+ );
5252+ }
5353+ }
5454+5555+ // Check if there's a standalone published document
5656+ const leafletDocs = tokenData.leaflets_to_documents || [];
5757+ if (leafletDocs.length > 0) {
5858+ if (!identity) {
5959+ throw new Error(
6060+ "Unauthorized: You must be logged in to delete a published leaflet",
6161+ );
6262+ }
6363+ for (let leafletDoc of leafletDocs) {
6464+ const docUri = leafletDoc.documents?.uri;
6565+ // Extract the DID from the document URI (format: at://did:plc:xxx/...)
6666+ if (docUri && identity.atp_did && !docUri.includes(identity.atp_did)) {
6767+ throw new Error(
6868+ "Unauthorized: You must own the published document to delete this leaflet",
6969+ );
7070+ }
7171+ }
7272+ }
7373+ }
7474+1975 await db.transaction(async (tx) => {
2076 let [token] = await tx
2177 .select()
···11+-- Create GIN index on the tags array in the JSONB data field
22+-- This allows efficient querying of documents by tag
33+CREATE INDEX IF NOT EXISTS idx_documents_tags
44+ ON "public"."documents" USING gin ((data->'tags'));
55+66+-- Function to search and aggregate tags from documents
77+-- This does the aggregation in the database rather than fetching all documents
88+CREATE OR REPLACE FUNCTION search_tags(search_query text)
99+RETURNS TABLE (name text, document_count bigint) AS $$
1010+BEGIN
1111+ RETURN QUERY
1212+ SELECT
1313+ LOWER(tag::text) as name,
1414+ COUNT(DISTINCT d.uri) as document_count
1515+ FROM
1616+ "public"."documents" d,
1717+ jsonb_array_elements_text(d.data->'tags') as tag
1818+ WHERE
1919+ CASE
2020+ WHEN search_query = '' THEN true
2121+ ELSE LOWER(tag::text) LIKE '%' || search_query || '%'
2222+ END
2323+ GROUP BY
2424+ LOWER(tag::text)
2525+ ORDER BY
2626+ COUNT(DISTINCT d.uri) DESC,
2727+ LOWER(tag::text) ASC
2828+ LIMIT 20;
2929+END;
3030+$$ LANGUAGE plpgsql STABLE;
···11+set check_function_bodies = off;
22+33+CREATE OR REPLACE FUNCTION public.pull_data(token_id uuid, client_group_id text)
44+ RETURNS pull_result
55+ LANGUAGE plpgsql
66+AS $function$DECLARE
77+ result pull_result;
88+BEGIN
99+ -- Get client group data as JSON array
1010+ SELECT json_agg(row_to_json(rc))
1111+ FROM replicache_clients rc
1212+ WHERE rc.client_group = client_group_id
1313+ INTO result.client_groups;
1414+1515+ -- Get facts as JSON array
1616+ SELECT json_agg(row_to_json(f))
1717+ FROM permission_tokens pt,
1818+ get_facts(pt.root_entity) f
1919+ WHERE pt.id = token_id
2020+ INTO result.facts;
2121+2222+ -- Get publication data - try leaflets_in_publications first, then leaflets_to_documents
2323+ SELECT json_agg(row_to_json(lip))
2424+ FROM leaflets_in_publications lip
2525+ WHERE lip.leaflet = token_id
2626+ INTO result.publications;
2727+2828+ -- If no publication data found, try leaflets_to_documents (for standalone documents)
2929+ IF result.publications IS NULL THEN
3030+ SELECT json_agg(row_to_json(ltd))
3131+ FROM leaflets_to_documents ltd
3232+ WHERE ltd.leaflet = token_id
3333+ INTO result.publications;
3434+ END IF;
3535+3636+ RETURN result;
3737+END;$function$
3838+;