ATlast — you'll never need to find your favorites on another platform again. Find your favs in the ATmosphere.
atproto
1import { SimpleHandler } from "./core/types/api.types";
2import { SessionService } from "./services/SessionService";
3import { extractSessionId } from "./core/middleware";
4import { successResponse } from "./utils";
5import { withErrorHandling } from "./core/middleware";
6import { AuthenticationError, ERROR_MESSAGES } from "./core/errors";
7import { profileCache } from "./infrastructure/cache/CacheService";
8
9const sessionHandler: SimpleHandler = async (event) => {
10 const sessionId =
11 event.queryStringParameters?.session || extractSessionId(event);
12
13 if (!sessionId) {
14 throw new AuthenticationError(ERROR_MESSAGES.NO_SESSION_COOKIE);
15 }
16
17 const isValid = await SessionService.verifySession(sessionId);
18 if (!isValid) {
19 throw new AuthenticationError(ERROR_MESSAGES.INVALID_SESSION);
20 }
21
22 const did = await SessionService.getDIDForSession(sessionId);
23 if (!did) {
24 throw new AuthenticationError(ERROR_MESSAGES.INVALID_SESSION);
25 }
26
27 const cached = profileCache.get<any>(did);
28 if (cached) {
29 console.log("Returning cached profile for", did);
30 return successResponse(cached, 200, {
31 "Cache-Control": "private, max-age=300",
32 "X-Cache-Status": "HIT",
33 }, event);
34 }
35
36 const { agent } = await SessionService.getAgentForSession(sessionId, event);
37
38 const profile = await agent.getProfile({ actor: did });
39
40 const profileData = {
41 did: did,
42 handle: profile.data.handle,
43 displayName: profile.data.displayName,
44 avatar: profile.data.avatar,
45 description: profile.data.description,
46 };
47
48 profileCache.set(did, profileData);
49
50 return successResponse(profileData, 200, {
51 "Cache-Control": "private, max-age=300",
52 "X-Cache-Status": "MISS",
53 }, event);
54};
55
56export const handler = withErrorHandling(sessionHandler);