grain.social is a photo sharing platform built on atproto.
at main 1.2 kB view raw
1import { ProfileView } from "$lexicon/types/social/grain/actor/defs.ts"; 2import { NotificationView } from "$lexicon/types/social/grain/notification/defs.ts"; 3import { Un$Typed } from "$lexicon/util.ts"; 4import { BffMiddleware } from "@bigmoves/bff"; 5import { MetaDescriptor } from "@bigmoves/bff/components"; 6import { getActorProfile } from "./lib/actor.ts"; 7import { getNotifications } from "./lib/notifications.ts"; 8 9export type State = { 10 profile?: ProfileView; 11 scripts?: string[]; 12 meta?: MetaDescriptor[]; 13 notifications?: Un$Typed<NotificationView>[]; 14}; 15 16export const appStateMiddleware: BffMiddleware = (req, ctx) => { 17 if (ctx.currentUser) { 18 const url = new URL(req.url); 19 // ignore routes prefixed with actions and dialogs (no need to resolve profile) 20 if ( 21 url.pathname.includes("actions") || 22 (url.pathname.includes("dialogs") && 23 !url.pathname.includes("/dialogs/profile")) 24 ) { 25 return ctx.next(); 26 } 27 const profile = getActorProfile(ctx.currentUser.did, ctx); 28 if (profile) { 29 ctx.state.profile = profile; 30 } 31 const notifications = getNotifications(ctx); 32 ctx.state.notifications = notifications; 33 return ctx.next(); 34 } 35 return ctx.next(); 36};