a tool for shared writing and social publishing
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

add template route

+122
+122
app/template/[template_id]/route.ts
··· 1 + import { createServerClient } from "@supabase/ssr"; 2 + import { drizzle } from "drizzle-orm/postgres-js"; 3 + import { NextRequest } from "next/server"; 4 + import postgres from "postgres"; 5 + import { Fact } from "src/replicache"; 6 + import { Attributes } from "src/replicache/attributes"; 7 + import { Database } from "supabase/database.types"; 8 + import { v7 } from "uuid"; 9 + 10 + import { 11 + entities, 12 + permission_tokens, 13 + permission_token_rights, 14 + entity_sets, 15 + facts, 16 + } from "drizzle/schema"; 17 + import { sql } from "drizzle-orm"; 18 + import { redirect } from "next/navigation"; 19 + 20 + let supabase = createServerClient<Database>( 21 + process.env.NEXT_PUBLIC_SUPABASE_API_URL as string, 22 + process.env.SUPABASE_SERVICE_ROLE_KEY as string, 23 + { cookies: {} }, 24 + ); 25 + export async function GET( 26 + request: NextRequest, 27 + { params }: { params: { template_id: string } }, 28 + ) { 29 + let res = await supabase 30 + .from("permission_tokens") 31 + .select("*, permission_token_rights(*)") 32 + .eq("id", params.template_id) 33 + .single(); 34 + let rootEntity = res.data?.root_entity; 35 + if (!rootEntity || !res.data) return { title: "Leaflet not found" }; 36 + let { data } = await supabase.rpc("get_facts", { 37 + root: rootEntity, 38 + }); 39 + let initialFacts = (data as unknown as Fact<keyof typeof Attributes>[]) || []; 40 + 41 + let oldEntityIDToNewID = {} as { [k: string]: string }; 42 + let oldEntities = initialFacts.reduce((acc, f) => { 43 + if (!acc.includes(f.entity)) acc.push(f.entity); 44 + return acc; 45 + }, [] as string[]); 46 + let newEntities = [] as string[]; 47 + 48 + for (let oldEntity of oldEntities) { 49 + let newEntity = v7(); 50 + oldEntityIDToNewID[oldEntity] = newEntity; 51 + newEntities.push(newEntity); 52 + } 53 + 54 + let newFacts = [] as Array<Pick<Fact<any>, "entity" | "attribute" | "data">>; 55 + for (let fact of initialFacts) { 56 + let entity = oldEntityIDToNewID[fact.entity]; 57 + let data = fact.data; 58 + if ( 59 + data.type === "ordered-reference" || 60 + data.type == "spatial-reference" || 61 + data.type === "reference" 62 + ) { 63 + data.value = oldEntityIDToNewID[data.value]; 64 + } 65 + if (data.type === "image") { 66 + let url = data.src.split("?"); 67 + let paths = url[0].split("/"); 68 + let newID = v7(); 69 + await supabase.storage 70 + .from("minilink-user-assets") 71 + .copy(paths[paths.length - 1], newID); 72 + let newPath = [...paths]; 73 + newPath[newPath.length - 1] = newID; 74 + let newURL = newPath.join("/"); 75 + if (url[1]) newURL += `?${url[1]}`; 76 + data.src = newURL; 77 + } 78 + newFacts.push({ entity, attribute: fact.attribute, data }); 79 + } 80 + 81 + const client = postgres(process.env.DB_URL as string, { idle_timeout: 5 }); 82 + const db = drizzle(client); 83 + 84 + let { permissionToken } = await db.transaction(async (tx) => { 85 + // Create a new entity set 86 + let [entity_set] = await tx.insert(entity_sets).values({}).returning(); 87 + await tx 88 + .insert(entities) 89 + .values(newEntities.map((e) => ({ id: e, set: entity_set.id }))); 90 + await tx.insert(facts).values( 91 + newFacts.map((f) => ({ 92 + id: v7(), 93 + entity: f.entity, 94 + attribute: f.attribute, 95 + data: sql`${f.data}`, 96 + })), 97 + ); 98 + 99 + let [permissionToken] = await tx 100 + .insert(permission_tokens) 101 + .values({ root_entity: oldEntityIDToNewID[rootEntity] }) 102 + .returning(); 103 + 104 + //and give it all the permission on that entity set 105 + let [rights] = await tx 106 + .insert(permission_token_rights) 107 + .values({ 108 + token: permissionToken.id, 109 + entity_set: entity_set.id, 110 + read: true, 111 + write: true, 112 + create_token: true, 113 + change_entity_set: true, 114 + }) 115 + .returning(); 116 + 117 + return { permissionToken, rights, entity_set }; 118 + }); 119 + 120 + client.end(); 121 + return redirect(`/${permissionToken.id}`); 122 + }