a tool for shared writing and social publishing
0
fork

Configure Feed

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

remove unused pull/push server actions

-149
-2
src/replicache/index.tsx
··· 1 1 "use client"; 2 - import { Push } from "./push"; 3 - import { Pull } from "./pull"; 4 2 import { createContext, useContext, useEffect, useMemo, useState } from "react"; 5 3 import { useSubscribe } from "replicache-react"; 6 4 import {
-71
src/replicache/pull.ts
··· 1 - "use server"; 2 - 3 - import { createClient } from "@supabase/supabase-js"; 4 - import { 5 - PullRequest, 6 - PullResponseV1, 7 - VersionNotSupportedResponse, 8 - } from "replicache"; 9 - import { Database } from "supabase/database.types"; 10 - import { Fact } from "."; 11 - import postgres from "postgres"; 12 - import { drizzle } from "drizzle-orm/postgres-js"; 13 - import { FactWithIndexes, getClientGroup } from "./utils"; 14 - import { Attributes } from "./attributes"; 15 - import { permission_tokens } from "drizzle/schema"; 16 - import { eq } from "drizzle-orm"; 17 - let supabase = createClient<Database>( 18 - process.env.NEXT_PUBLIC_SUPABASE_API_URL as string, 19 - process.env.SUPABASE_SERVICE_ROLE_KEY as string, 20 - ); 21 - 22 - const client = postgres(process.env.DB_URL as string, { idle_timeout: 5 }); 23 - const db = drizzle(client); 24 - export async function Pull( 25 - body: PullRequest, 26 - token_id: string, 27 - ): Promise<PullResponseV1> { 28 - console.log("Pull"); 29 - if (body.pullVersion === 0) return versionNotSupported; 30 - let [token] = await db 31 - .select({ root_entity: permission_tokens.root_entity }) 32 - .from(permission_tokens) 33 - .where(eq(permission_tokens.id, token_id)); 34 - let facts: { 35 - attribute: string; 36 - created_at: string; 37 - data: any; 38 - entity: string; 39 - id: string; 40 - updated_at: string | null; 41 - version: number; 42 - }[] = []; 43 - let clientGroup = {}; 44 - if (token) { 45 - let { data } = await supabase.rpc("get_facts", { root: token.root_entity }); 46 - 47 - clientGroup = await getClientGroup(db, body.clientGroupID); 48 - facts = data || []; 49 - } 50 - 51 - return { 52 - cookie: Date.now(), 53 - lastMutationIDChanges: clientGroup, 54 - patch: [ 55 - { op: "clear" }, 56 - { op: "put", key: "initialized", value: true }, 57 - ...facts.map((f) => { 58 - return { 59 - op: "put", 60 - key: f.id, 61 - value: FactWithIndexes(f as unknown as Fact<keyof typeof Attributes>), 62 - } as const; 63 - }), 64 - ], 65 - }; 66 - } 67 - 68 - const versionNotSupported: VersionNotSupportedResponse = { 69 - error: "VersionNotSupported", 70 - versionType: "pull", 71 - };
-76
src/replicache/push.ts
··· 1 - "use server"; 2 - import { PushRequest, PushResponse } from "replicache"; 3 - import { serverMutationContext } from "./serverMutationContext"; 4 - import { mutations } from "./mutations"; 5 - import { drizzle } from "drizzle-orm/postgres-js"; 6 - import { eq } from "drizzle-orm"; 7 - import postgres from "postgres"; 8 - import { permission_token_rights, replicache_clients } from "drizzle/schema"; 9 - import { getClientGroup } from "./utils"; 10 - import { createClient } from "@supabase/supabase-js"; 11 - import { Database } from "supabase/database.types"; 12 - 13 - const client = postgres(process.env.DB_URL as string, { idle_timeout: 5 }); 14 - let supabase = createClient<Database>( 15 - process.env.NEXT_PUBLIC_SUPABASE_API_URL as string, 16 - process.env.SUPABASE_SERVICE_ROLE_KEY as string, 17 - ); 18 - const db = drizzle(client); 19 - export async function Push( 20 - pushRequest: PushRequest, 21 - rootEntity: string, 22 - token: { id: string }, 23 - ): Promise<PushResponse | undefined> { 24 - console.log("Push"); 25 - if (pushRequest.pushVersion !== 1) { 26 - return { error: "VersionNotSupported", versionType: "push" }; 27 - } 28 - let clientGroup = await getClientGroup(db, pushRequest.clientGroupID); 29 - let token_rights = await db 30 - .select() 31 - .from(permission_token_rights) 32 - .where(eq(permission_token_rights.token, token.id)); 33 - for (let mutation of pushRequest.mutations) { 34 - let lastMutationID = clientGroup[mutation.clientID] || 0; 35 - if (mutation.id <= lastMutationID) continue; 36 - clientGroup[mutation.clientID] = mutation.id; 37 - let name = mutation.name as keyof typeof mutations; 38 - if (!mutations[name]) { 39 - continue; 40 - } 41 - await db.transaction(async (tx) => { 42 - try { 43 - await mutations[name]( 44 - mutation.args as any, 45 - serverMutationContext(tx, token_rights), 46 - ); 47 - } catch (e) { 48 - console.log( 49 - `Error occured while running mutation: ${name}`, 50 - JSON.stringify(e), 51 - JSON.stringify(mutation, null, 2), 52 - ); 53 - } 54 - await tx 55 - .insert(replicache_clients) 56 - .values({ 57 - client_group: pushRequest.clientGroupID, 58 - client_id: mutation.clientID, 59 - last_mutation: mutation.id, 60 - }) 61 - .onConflictDoUpdate({ 62 - target: replicache_clients.client_id, 63 - set: { last_mutation: mutation.id }, 64 - }); 65 - }); 66 - } 67 - 68 - let channel = supabase.channel(`rootEntity:${rootEntity}`); 69 - await channel.send({ 70 - type: "broadcast", 71 - event: "poke", 72 - payload: { message: "poke" }, 73 - }); 74 - supabase.removeChannel(channel); 75 - return undefined; 76 - }