frontend client for gemstone. decentralised workplace app
1
fork

Configure Feed

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

feat: get invites query

serenity dcebd7e0 b8dd6754

+103
+103
src/queries/get-invites-from-pds.ts
··· 1 + import type { Did } from "@/lib/types/atproto"; 2 + import type { 3 + SystemsGmstnDevelopmentChannelInvite} from "@/lib/types/lexicon/systems.gmstn.development.channel.invite"; 4 + import { 5 + systemsGmstnDevelopmentChannelInviteRecordSchema, 6 + } from "@/lib/types/lexicon/systems.gmstn.development.channel.invite"; 7 + import type { Result } from "@/lib/utils/result"; 8 + import { Client, simpleFetchHandler } from "@atcute/client"; 9 + import { z } from "zod"; 10 + 11 + // NOTE: might eventually want to put this into Prism as well. 12 + export const getInviteRecordsFromPds = async ({ 13 + pdsEndpoint, 14 + did, 15 + }: { 16 + pdsEndpoint: string; 17 + did: Did; 18 + }): Promise< 19 + Result< 20 + Array<{ 21 + uri: string; 22 + value: SystemsGmstnDevelopmentChannelInvite; 23 + }>, 24 + unknown 25 + > 26 + > => { 27 + const handler = simpleFetchHandler({ service: pdsEndpoint }); 28 + const client = new Client({ handler }); 29 + const channelRecordsResult = await fetchRecords({ 30 + client, 31 + did, 32 + }); 33 + if (!channelRecordsResult.ok) 34 + return { ok: false, error: channelRecordsResult.error }; 35 + return { ok: true, data: channelRecordsResult.data }; 36 + }; 37 + 38 + const fetchRecords = async ({ 39 + client, 40 + did, 41 + }: { 42 + client: Client; 43 + did: Did; 44 + }): Promise< 45 + Result< 46 + Array<{ 47 + uri: string; 48 + value: SystemsGmstnDevelopmentChannelInvite; 49 + }>, 50 + unknown 51 + > 52 + > => { 53 + const allRecords: Array<{ 54 + uri: string; 55 + value: SystemsGmstnDevelopmentChannelInvite; 56 + }> = []; 57 + let cursor: string | undefined; 58 + 59 + let continueLoop = true; 60 + 61 + while (continueLoop) { 62 + const results = await client.get("com.atproto.repo.listRecords", { 63 + params: { 64 + repo: did, 65 + collection: "systems.gmstn.development.channel.invite", 66 + limit: 100, 67 + cursor, 68 + }, 69 + }); 70 + if (!results.ok) 71 + return { 72 + ok: false, 73 + error: "Failed to fetch records. Check the response from PDS.", 74 + }; 75 + const { records, cursor: nextCursor } = results.data; 76 + 77 + const { 78 + success, 79 + error, 80 + data: responses, 81 + } = z 82 + .array( 83 + z.object({ 84 + cid: z.string(), 85 + uri: z.string(), 86 + value: systemsGmstnDevelopmentChannelInviteRecordSchema, 87 + }), 88 + ) 89 + .safeParse(records); 90 + 91 + if (!success) return { ok: false, error: z.treeifyError(error) }; 92 + 93 + allRecords.push( 94 + ...responses.map((data) => { 95 + return { uri: data.uri, value: data.value }; 96 + }), 97 + ); 98 + 99 + if (records.length < 100) continueLoop = false; 100 + cursor = nextCursor; 101 + } 102 + return { ok: true, data: allRecords }; 103 + };