Highly ambitious ATProtocol AppView service and sdks
1import { AtProtoClient } from "../client.ts";
2import { createOAuthClient } from "../config.ts";
3import { buildAtUri } from "./at-uri.ts";
4
5interface AuthContext {
6 currentUser: {
7 sessionId?: string;
8 sub?: string;
9 };
10}
11
12// Helper function to get a slice-specific AT Protocol client
13export function getSliceClient(
14 context: AuthContext,
15 sliceId: string,
16 sliceOwnerDid?: string,
17): AtProtoClient {
18 const API_URL = Deno.env.get("API_URL")!;
19
20 // Use provided sliceOwnerDid or fall back to current user's DID
21 const ownerDid = sliceOwnerDid || context.currentUser.sub;
22
23 if (!ownerDid) {
24 throw new Error("Owner DID is required to create slice client");
25 }
26
27 const sliceUri = buildAtUri({
28 did: ownerDid,
29 collection: "network.slices.slice",
30 rkey: sliceId,
31 });
32
33 // Use authenticated client if user is authenticated, otherwise public client
34 if (context.currentUser.sessionId) {
35 const sessionOAuthClient = createOAuthClient(context.currentUser.sessionId);
36 return new AtProtoClient(API_URL, sliceUri, sessionOAuthClient);
37 } else {
38 return new AtProtoClient(API_URL, sliceUri);
39 }
40}