···11+import { Response } from "@atproto/api/dist/client/types/com/atproto/repo/getRecord";
12import { PlaceStreamChatProfile } from "streamplace";
23import {
34 getStreamplaceStoreFromContext,
···1617 if (!did || !pdsAgent) {
1718 throw new Error("No DID or PDS agent");
1819 }
1919- let res;
2020+ let res: Response | undefined;
2121+ let notFound = false;
2022 try {
2123 res = await pdsAgent.com.atproto.repo.getRecord({
2224 repo: did,
···2426 rkey: "self",
2527 });
2628 } catch (e) {
2727- console.error(
2828- "Failed to get chat profile record, attempting creation",
2929- e,
3030- );
2929+ // if the record is a 400 with "Record not found", then we want to create an empty chat profile
3030+ if (
3131+ e.error.status === 400 &&
3232+ e.error.data?.error.includes("Record not found")
3333+ ) {
3434+ notFound = true;
3535+ } else {
3636+ console.error("Failed to get chat profile record", e);
3737+ }
3138 }
3232- if (!res || !res.success) {
3939+ if (notFound || (res && !res.success)) {
3340 try {
3441 await createEmptyChatProfile();
3542 res = await pdsAgent.com.atproto.repo.getRecord({
···4047 } catch (e) {
4148 console.error("Failed to create empty chat profile record", e);
4249 }
5050+ }
5151+5252+ // if no response, assume some fluke happened and just return without setting state
5353+ if (res === undefined) {
5454+ console.error("No response from get or create chat profile");
5555+ return;
4356 }
44574558 if (PlaceStreamChatProfile.isRecord(res.data.value)) {