~james/core

core/web/src/lib/api/orgProfile.ts -rw-r--r-- 1.7 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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<ProfileRecord> => {
	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<OAuthUserAgent> => {
	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<ProfileRecord> => {
	const orgAgent = await getOrCreateDelegatedSession(controllerAgent, orgDid);
	return saveAvatar(ctx, orgAgent, image, {
		$type: PROFILE_COLLECTION,
		...current,
		isOrganization: true
	});
};