Highly ambitious ATProtocol AppView service and sdks
1import { AtProtoClient } from "../generated_client.ts";
2import { logger } from "./logger.ts";
3import { SYSTEM_SLICE_URI, SYSTEM_SLICE_DID, DEFAULT_API_URL } from "./constants.ts";
4
5export interface WaitlistCheckResult {
6 hasAccess: boolean;
7 isOnWaitlist: boolean;
8}
9
10export async function checkUserWaitlistAccess(
11 userDid: string,
12 apiUrl = DEFAULT_API_URL
13): Promise<WaitlistCheckResult> {
14 try {
15 // Create a public client for checking waitlist status (no auth needed)
16 const client = new AtProtoClient(apiUrl, SYSTEM_SLICE_URI);
17
18 // Query for invites for this DID - using json field to query the record content
19 const invitesResult =
20 await client.network.slices.waitlist.invite.getRecords({
21 where: {
22 did: { eq: SYSTEM_SLICE_DID },
23 slice: { eq: SYSTEM_SLICE_URI },
24 json: { contains: userDid },
25 },
26 limit: 1,
27 });
28
29 // Check if user has a valid invite
30 if (invitesResult.records && invitesResult.records.length > 0) {
31 const invite = invitesResult.records[0];
32
33 // Check if invite has expired
34 if (invite.value.expiresAt) {
35 const expiresAt = new Date(invite.value.expiresAt);
36 const now = new Date();
37 if (expiresAt < now) {
38 return { hasAccess: false, isOnWaitlist: false }; // Invite has expired
39 }
40 }
41
42 return { hasAccess: true, isOnWaitlist: false }; // Valid invite found
43 }
44
45 // Check if user is already on the waitlist - requests are created by the user so record.did is correct
46 const requestsResult =
47 await client.network.slices.waitlist.request.getRecords({
48 where: {
49 slice: { eq: SYSTEM_SLICE_URI },
50 json: { eq: userDid },
51 },
52 limit: 1,
53 });
54
55 const isOnWaitlist =
56 requestsResult.records && requestsResult.records.length > 0;
57
58 return { hasAccess: false, isOnWaitlist };
59 } catch (error) {
60 logger.error("Error checking user waitlist access:", error);
61 return { hasAccess: false, isOnWaitlist: false }; // Default to blocking access on error
62 }
63}
64
65export function showWaitlistError(
66 userDid: string,
67 isOnWaitlist: boolean
68): void {
69 console.error("\n❌ Access Denied");
70 console.error("─".repeat(50));
71
72 if (isOnWaitlist) {
73 console.error("You are already on the waitlist for Slices.");
74 console.error("Please wait for an invitation to be sent to your account.");
75 } else {
76 console.error("You need an invitation to use Slices.");
77 console.error("Please visit https://slices.network to request access.");
78 }
79
80 console.error(`\nYour DID: ${userDid}`);
81 console.error("─".repeat(50));
82 console.error(
83 "If you believe this is an error, please DM @slices.network on Bsky.\n"
84 );
85}