오늘의 서커스 내용 세션의 정보를 가져오는데 세션의 앱은 ApplicationGrants를 받아야 하고 세션의 프로필 ID는 ApplicationGrantProfiles으로 프로필별 승인을 받거나 전체 프로필 승인(ApplicationGrantProfiles.profileId == null)을 받야아 가져올 수 있는데 세션의 프로필을 헤더로 오버라이드할 수 있고 오버라이드된 프로필 역시 위 조건을 지키는 경우만 리턴되게 하는 쿼리를 짰어요

const headerProfileId = c.req.header('X-Actor-Profile-Id');
    const session = await db
      .select({
        id: Sessions.id,
        applicationId: Sessions.applicationId,
        accountId: Sessions.accountId,
        scopes: Sessions.scopes,
        languages: Accounts.languages,
        profileId: Profiles.id,
      })
      .from(Sessions)
      .innerJoin(Accounts, eq(Sessions.accountId, Accounts.id))
      .innerJoin(ApplicationGrants, eq(Sessions.applicationId, ApplicationGrants.applicationId))
      .leftJoin(
        ApplicationGrantProfiles,
        eq(ApplicationGrants.id, ApplicationGrantProfiles.applicationGrantId),
      )
      .leftJoin(
        Profiles,
        and(
          eq(Profiles.id, headerProfileId ?? Sessions.profileId),
          eq(Profiles.state, ProfileState.ACTIVE),
          isNotNull(ApplicationGrantProfiles.id),
          or(
            eq(ApplicationGrantProfiles.profileId, Profiles.id),
            isNull(ApplicationGrantProfiles.profileId),
          ),
        ),
      )
      .where(eq(Sessions.token, accessToken))
      .limit(1)
      .then(first);
1

If you have a fediverse account, you can quote this note from your own instance. Search https://hackers.pub/ap/notes/0198cbcc-b4a9-7bb2-8e0f-f7d0cca91a13 on your instance and quote it. (Note that quoting is not supported in Mastodon.)