From ae08958b80391b06bcb7c38b4e750c1980604640 Mon Sep 17 00:00:00 2001 From: dawn Date: Fri, 11 Sep 2026 04:20:06 +0300 Subject: [PATCH] web: use awaitRecord on key and profile writes Signed-off-by: dawn --- web/src/lib/api/profile.ts | 9 ++-- web/src/lib/api/settings.ts | 21 +++++++--- .../components/profile/ProfileEditForm.svelte | 42 +++++++++---------- .../components/settings/tabs/KeysTab.svelte | 5 ++- web/src/routes/welcome/+page.svelte | 16 +++++-- 5 files changed, 59 insertions(+), 34 deletions(-) diff --git a/web/src/lib/api/profile.ts b/web/src/lib/api/profile.ts index 87abd1966..e43db86e7 100644 --- a/web/src/lib/api/profile.ts +++ b/web/src/lib/api/profile.ts @@ -7,7 +7,7 @@ import { FOLLOW_VIEWER, STAR_VIEWER } from "$lib/api/descriptors"; import { enrich, target } from "$lib/api/enrich"; import { jsonGet } from "$lib/api/_request"; import type { BobbinContext, XrpcRequestInit } from "$lib/api/client"; -import { putRecord } from "$lib/api/write"; +import { putRecord, type WrittenRecord } from "$lib/api/write"; import type * as ShTangledActorGetTrending from "$lib/api/lexicons/types/sh/tangled/actor/getTrending"; import type { ProfileRecord } from "$lib/api/records"; @@ -47,6 +47,9 @@ export const uploadProfileAvatar = async ( }; // the profile record lives at the fixed rkey `self`; put upserts it. -export const putProfile = async (agent: OAuthUserAgent, record: ProfileRecord): Promise => { - await putRecord(agent, PROFILE_COLLECTION, "self", record); +export const putProfile = async ( + agent: OAuthUserAgent, + record: ProfileRecord +): Promise => { + return putRecord(agent, PROFILE_COLLECTION, "self", record); }; diff --git a/web/src/lib/api/settings.ts b/web/src/lib/api/settings.ts index a2bc493cc..f682c7b70 100644 --- a/web/src/lib/api/settings.ts +++ b/web/src/lib/api/settings.ts @@ -1,5 +1,5 @@ import { now as tidNow } from "@atcute/tid"; -import type { Nsid } from "@atcute/lexicons/syntax"; +import type { Cid, Nsid, ResourceUri } from "@atcute/lexicons/syntax"; import type { OAuthUserAgent } from "@atcute/oauth-browser-client"; import { jsonGet } from "$lib/api/_request"; import type { BobbinContext, XrpcRequestInit } from "$lib/api/client"; @@ -52,14 +52,20 @@ const listRecords = async ( export const listPubKeys = (ctx: BobbinContext, did: string, init?: XrpcRequestInit) => listRecords(ctx, "sh.tangled.publicKey.listKeys", did, init); +export interface CreatedPubKey { + uri: ResourceUri; + cid: Cid; + rkey: string; +} + export const createPubKey = async ( agent: OAuthUserAgent, name: string, key: string -): Promise => { +): Promise => { // the rkey is a tid the client mints, rather than one the pds assigns const rkey = tidNow(); - await createRecord( + const { uri, cid } = await createRecord( agent, PUBLIC_KEY_COLLECTION, { @@ -70,9 +76,14 @@ export const createPubKey = async ( }, rkey ); - return rkey; + return { uri, cid, rkey }; }; -export const deletePubKey = async (agent: OAuthUserAgent, rkey: string): Promise => { +export const deletePubKey = async ( + agent: OAuthUserAgent, + rkey: string +): Promise<{ uri: ResourceUri }> => { + const uri = `at://${agent.sub}/${PUBLIC_KEY_COLLECTION}/${rkey}` as ResourceUri; await deleteRecord(agent, PUBLIC_KEY_COLLECTION, rkey); + return { uri }; }; diff --git a/web/src/lib/components/profile/ProfileEditForm.svelte b/web/src/lib/components/profile/ProfileEditForm.svelte index 3753ddb57..c711554db 100644 --- a/web/src/lib/components/profile/ProfileEditForm.svelte +++ b/web/src/lib/components/profile/ProfileEditForm.svelte @@ -11,6 +11,8 @@ import Textarea from "$lib/components/ui/Textarea.svelte"; import { getAuth } from "$lib/auth.svelte"; import { putProfile } from "$lib/api/profile"; + import { awaitIndexedOrSaved } from "$lib/api/awaitRecord"; + import { createBobbinClient } from "$lib/api/client"; import type { ProfileRecord } from "$lib/api/records"; import { createAction } from "$lib/action.svelte"; @@ -35,27 +37,25 @@ value: (seed?.links?.[index] as string | undefined) ?? "" })) ); - const save = createAction( - async (event: SubmitEvent) => { - event.preventDefault(); - const agent = auth.agent; - if (!agent) return; - const cleanLinks = links.map((link) => link.value.trim()).filter(Boolean); - // put replaces the whole record, so carry unedited fields (avatar, pins, stats). - const record: ProfileRecord = { - ...profile, - $type: "sh.tangled.actor.profile", - bluesky, - description: description.trim() || undefined, - pronouns: pronouns.trim() || undefined, - location: location.trim() || undefined, - links: cleanLinks.length > 0 ? (cleanLinks as ProfileRecord["links"]) : undefined - }; - await putProfile(agent, record); - onSaved(record); - }, - () => "Could not save profile. Try again." - ); + const save = createAction(async (event: SubmitEvent) => { + event.preventDefault(); + const agent = auth.agent; + if (!agent) return; + const cleanLinks = links.map((link) => link.value.trim()).filter(Boolean); + const record: ProfileRecord = { + ...profile, + $type: "sh.tangled.actor.profile", + bluesky, + description: description.trim() || undefined, + pronouns: pronouns.trim() || undefined, + location: location.trim() || undefined, + links: cleanLinks.length > 0 ? (cleanLinks as ProfileRecord["links"]) : undefined + }; + const written = await putProfile(agent, record); + const ctx = createBobbinClient({ serviceUrl: auth.bobbinUrl }); + await awaitIndexedOrSaved(ctx, written); + onSaved(record); + });
diff --git a/web/src/lib/components/settings/tabs/KeysTab.svelte b/web/src/lib/components/settings/tabs/KeysTab.svelte index 013b67212..226d145d0 100644 --- a/web/src/lib/components/settings/tabs/KeysTab.svelte +++ b/web/src/lib/components/settings/tabs/KeysTab.svelte @@ -1,6 +1,7 @@