import { AtProtoClient } from "../generated_client.ts"; import { logger } from "./logger.ts"; import { SYSTEM_SLICE_URI, SYSTEM_SLICE_DID, DEFAULT_API_URL } from "./constants.ts"; export interface WaitlistCheckResult { hasAccess: boolean; isOnWaitlist: boolean; } export async function checkUserWaitlistAccess( userDid: string, apiUrl = DEFAULT_API_URL ): Promise { try { // Create a public client for checking waitlist status (no auth needed) const client = new AtProtoClient(apiUrl, SYSTEM_SLICE_URI); // Query for invites for this DID - using json field to query the record content const invitesResult = await client.network.slices.waitlist.invite.getRecords({ where: { did: { eq: SYSTEM_SLICE_DID }, slice: { eq: SYSTEM_SLICE_URI }, json: { contains: userDid }, }, limit: 1, }); // Check if user has a valid invite if (invitesResult.records && invitesResult.records.length > 0) { const invite = invitesResult.records[0]; // Check if invite has expired if (invite.value.expiresAt) { const expiresAt = new Date(invite.value.expiresAt); const now = new Date(); if (expiresAt < now) { return { hasAccess: false, isOnWaitlist: false }; // Invite has expired } } return { hasAccess: true, isOnWaitlist: false }; // Valid invite found } // Check if user is already on the waitlist - requests are created by the user so record.did is correct const requestsResult = await client.network.slices.waitlist.request.getRecords({ where: { slice: { eq: SYSTEM_SLICE_URI }, json: { eq: userDid }, }, limit: 1, }); const isOnWaitlist = requestsResult.records && requestsResult.records.length > 0; return { hasAccess: false, isOnWaitlist }; } catch (error) { logger.error("Error checking user waitlist access:", error); return { hasAccess: false, isOnWaitlist: false }; // Default to blocking access on error } } export function showWaitlistError( userDid: string, isOnWaitlist: boolean ): void { console.error("\nāŒ Access Denied"); console.error("─".repeat(50)); if (isOnWaitlist) { console.error("You are already on the waitlist for Slices."); console.error("Please wait for an invitation to be sent to your account."); } else { console.error("You need an invitation to use Slices."); console.error("Please visit https://slices.network to request access."); } console.error(`\nYour DID: ${userDid}`); console.error("─".repeat(50)); console.error( "If you believe this is an error, please DM @slices.network on Bsky.\n" ); }