import type { Did } from "@atcute/lexicons/syntax"; import type { OAuthUserAgent } from "@atcute/oauth-browser-client"; import { getOrCreateDelegatedSession } from "$lib/api/actAs"; import { organizationRole, type OrganizationRole } from "$lib/api/organizations"; import { addController, getScopePresets, listControllers, removeController } from "$lib/api/tranquil"; export interface OrganizationMember { did: Did; handle?: string; role: OrganizationRole; grantedAt: string; } export const listOrganizationMembers = async ( controllerAgent: OAuthUserAgent, orgDid: Did ): Promise => { const orgAgent = await getOrCreateDelegatedSession(controllerAgent, orgDid); const controllers = await listControllers(orgAgent); return controllers .filter((controller) => controller.isActive) .map((controller) => ({ did: controller.did, handle: controller.handle, role: organizationRole(controller.grantedScopes), grantedAt: controller.grantedAt })); }; export const inviteOrganizationMembers = async ( controllerAgent: OAuthUserAgent, orgDid: Did, dids: readonly Did[] ): Promise => { const orgAgent = await getOrCreateDelegatedSession(controllerAgent, orgDid); const editor = (await getScopePresets(orgAgent)).find((preset) => preset.name === "editor"); if (!editor) throw new Error("The organization's PDS does not offer an editor role."); for (const did of dids) { await addController(orgAgent, { controllerDid: did, grantedScopes: editor.scopes }); } }; export const removeOrganizationMember = async ( controllerAgent: OAuthUserAgent, orgDid: Did, memberDid: Did ): Promise => { const orgAgent = await getOrCreateDelegatedSession(controllerAgent, orgDid); await removeController(orgAgent, memberDid); };