···
1
+
import type { Did } from "@/lib/types/atproto";
3
+
SystemsGmstnDevelopmentChannelInvite} from "@/lib/types/lexicon/systems.gmstn.development.channel.invite";
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";
11
+
// NOTE: might eventually want to put this into Prism as well.
12
+
export const getInviteRecordsFromPds = async ({
16
+
pdsEndpoint: string;
22
+
value: SystemsGmstnDevelopmentChannelInvite;
27
+
const handler = simpleFetchHandler({ service: pdsEndpoint });
28
+
const client = new Client({ handler });
29
+
const channelRecordsResult = await fetchRecords({
33
+
if (!channelRecordsResult.ok)
34
+
return { ok: false, error: channelRecordsResult.error };
35
+
return { ok: true, data: channelRecordsResult.data };
38
+
const fetchRecords = async ({
48
+
value: SystemsGmstnDevelopmentChannelInvite;
53
+
const allRecords: Array<{
55
+
value: SystemsGmstnDevelopmentChannelInvite;
57
+
let cursor: string | undefined;
59
+
let continueLoop = true;
61
+
while (continueLoop) {
62
+
const results = await client.get("com.atproto.repo.listRecords", {
65
+
collection: "systems.gmstn.development.channel.invite",
73
+
error: "Failed to fetch records. Check the response from PDS.",
75
+
const { records, cursor: nextCursor } = results.data;
86
+
value: systemsGmstnDevelopmentChannelInviteRecordSchema,
89
+
.safeParse(records);
91
+
if (!success) return { ok: false, error: z.treeifyError(error) };
94
+
...responses.map((data) => {
95
+
return { uri: data.uri, value: data.value };
99
+
if (records.length < 100) continueLoop = false;
100
+
cursor = nextCursor;
102
+
return { ok: true, data: allRecords };