Skip to content

Commit 4cef334

Browse files
committed
weProfilesApi handles defaults when profile fields don't exist and if other fields exist from other applets
1 parent 13ad415 commit 4cef334

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

Diff for: web/src/api/weProfilesApi.ts

+31-12
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
11
import { AppWebsocket, CellId } from '@holochain/client'
22
import { WeServices } from '@lightningrodlabs/we-applet'
3-
import { Profile, WhoAmIOutput } from '../types'
3+
import { Profile, Status, WhoAmIOutput } from '../types'
44
import { AgentPubKeyB64, UpdateInput } from '../types/shared'
55
import { WireRecord } from './hdkCrud'
66
import { get } from 'svelte/store'
77

8-
function weToAcornProfile(weProfile: WeProfile): Profile {
8+
function weToAcornProfile(weProfile: WeProfile, weServices: WeServices): Profile {
99
let acornProfile: Profile
10-
Object.keys(weProfile.fields).forEach((key) => {
10+
// constrains the fields only to what acorn wants/expects (in case other fields have been added by other applets)
11+
const acornFields = ['firstName', 'lastName', 'handle', 'status', 'avatarUrl', 'agentPubKey', 'isImported']
12+
13+
// field each acorn profile field, check if it exists in the we Profile, if it does, use that, if not, use defaults
14+
acornFields.forEach((key) => {
1115
if (key === 'isImported') {
12-
acornProfile[key] = JSON.parse(weProfile.fields[key])
16+
// default to false, unless field exists, then parse into bool from string
17+
acornProfile[key] = weProfile.fields[key] ? JSON.parse(weProfile.fields[key]) : false
18+
}
19+
else if (key === 'status') {
20+
// default to offline until the fields exists
21+
acornProfile[key] = weProfile.fields[key] ? weProfile.fields[key] as Status : "Offline" as Status
22+
}
23+
else if (key === 'agentPubKey') {
24+
acornProfile[key] = weProfile.fields[key] ? weProfile.fields[key] : new TextDecoder().decode(weServices.profilesStore.myAgentPubKey)
25+
}
26+
else if (key === 'avatarUrl') {
27+
acornProfile[key] = weProfile.fields[key] ? JSON.parse(weProfile.fields[key]) : ''
1328
}
1429
else {
15-
acornProfile[key] = weProfile.fields[key]
30+
// if no first and last name and handle are given, use the nickname
31+
acornProfile[key] = weProfile.fields[key] ? weProfile.fields[key] : weProfile.nickname
1632
}
33+
1734
})
1835
return acornProfile
1936
}
20-
function acornToWeProfile(acornProfile: Profile, nickname: string): WeProfile {
21-
let weProfile: WeProfile = { nickname: '', fields: {} }
37+
function acornToWeProfile(acornProfile: Profile, weProfile: WeProfile): WeProfile {
2238
Object.keys(acornProfile).forEach((key) => {
2339
if (key === 'isImported') {
2440
weProfile.fields[key] = JSON.stringify(acornProfile[key])
@@ -40,7 +56,8 @@ export interface WeProfile {
4056
const WeProfilesApi = (appWebsocket: AppWebsocket, weServices: WeServices) => {
4157
return {
4258
createWhoami: async (cellId: CellId, payload: Profile): Promise<WireRecord<Profile>> => {
43-
const profile = acornToWeProfile(payload, await getMyNickname(weServices))
59+
let myWeProfile = get(await weServices.profilesStore.fetchMyProfile());
60+
const profile = acornToWeProfile(payload, myWeProfile)
4461
await weServices.profilesStore.updateProfile(profile)
4562
return {
4663
actionHash: null,
@@ -51,7 +68,8 @@ const WeProfilesApi = (appWebsocket: AppWebsocket, weServices: WeServices) => {
5168
}
5269
},
5370
createImportedProfile: async (cellId: CellId, payload: Profile): Promise<WireRecord<Profile>> => {
54-
const profile = acornToWeProfile(payload, await getMyNickname(weServices))
71+
let myWeProfile = get(await weServices.profilesStore.fetchMyProfile());
72+
const profile = acornToWeProfile(payload, myWeProfile)
5573
await weServices.profilesStore.updateProfile(profile)
5674
return {
5775
actionHash: null,
@@ -62,7 +80,8 @@ const WeProfilesApi = (appWebsocket: AppWebsocket, weServices: WeServices) => {
6280
}
6381
},
6482
updateWhoami: async (cellId: CellId, payload: UpdateInput<Profile>): Promise<WireRecord<Profile>> => {
65-
const profile = acornToWeProfile(payload.entry, await getMyNickname(weServices))
83+
let myWeProfile = get(await weServices.profilesStore.fetchMyProfile());
84+
const profile = acornToWeProfile(payload.entry, myWeProfile)
6685
await weServices.profilesStore.updateProfile(profile)
6786
return {
6887
actionHash: null,
@@ -77,13 +96,13 @@ const WeProfilesApi = (appWebsocket: AppWebsocket, weServices: WeServices) => {
7796
return {
7897
actionHash: null,
7998
entryHash: null,
80-
entry: weToAcornProfile(myWeProfile),
99+
entry: weToAcornProfile(myWeProfile, weServices),
81100
createdAt: null,
82101
updatedAt: null,
83102
}
84103
},
85104
fetchAgents: async (cellId: CellId): Promise<Array<Profile>> => {
86-
let profiles: Array<Profile> = get(await weServices.profilesStore.fetchAllProfiles()).values().map((weProfile) => weToAcornProfile(weProfile));
105+
let profiles: Array<Profile> = get(await weServices.profilesStore.fetchAllProfiles()).values().map((weProfile) => weToAcornProfile(weProfile, weServices));
87106
return profiles
88107
},
89108
fetchAgentAddress: async (cellId: CellId): Promise<AgentPubKeyB64> => {

Diff for: web/src/types/profile.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AgentPubKeyB64, ActionHashB64 } from './shared'
22

3-
type Status = "Online" | "Offline" | "Away"
3+
export type Status = "Online" | "Offline" | "Away"
44

55
export interface Profile {
66
firstName: string,

0 commit comments

Comments
 (0)