import type { Did } from "@atcute/lexicons/syntax"; import { deleteStoredSession, type OAuthUserAgent } from "@atcute/oauth-browser-client"; import { getOrCreateDelegatedSession } from "#lib/api/actAs.ts"; import { canChangeHandle } from "#lib/api/handle.ts"; import { PROFILE_COLLECTION } from "#lib/api/profile.ts"; import type { BobbinContext } from "#lib/api/client.ts"; import { saveAvatar, writeProfile } from "#lib/api/profileSettings.ts"; import type { ProfileRecord } from "#lib/api/records.ts"; export const saveOrganizationProfile = async ( ctx: BobbinContext, controllerAgent: OAuthUserAgent, orgDid: Did, record: ProfileRecord ): Promise => { const written: ProfileRecord = { ...record, isOrganization: true }; const orgAgent = await getOrCreateDelegatedSession(controllerAgent, orgDid); return writeProfile(ctx, orgAgent, written); }; // an organization session made before its owner allowed handle changes keeps the old scope export const organizationHandleAgent = async ( controllerAgent: OAuthUserAgent, orgDid: Did ): Promise => { const orgAgent = await getOrCreateDelegatedSession(controllerAgent, orgDid); if (canChangeHandle(orgAgent) || !canChangeHandle(controllerAgent)) return orgAgent; await orgAgent.signOut().catch(() => deleteStoredSession(orgDid)); return getOrCreateDelegatedSession(controllerAgent, orgDid); }; export const saveOrganizationAvatar = async ( ctx: BobbinContext, controllerAgent: OAuthUserAgent, orgDid: Did, image: Blob | null, current: ProfileRecord | null ): Promise => { const orgAgent = await getOrCreateDelegatedSession(controllerAgent, orgDid); return saveAvatar(ctx, orgAgent, image, { $type: PROFILE_COLLECTION, ...current, isOrganization: true }); };