Skip to content

Commit 0338edc

Browse files
committed
WIP: weProfileApi makes zome calls using WeServices
1 parent d4ca461 commit 0338edc

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

web/src/api/weProfilesApi.ts

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import { AppWebsocket, CellId } from '@holochain/client'
2+
import { WeServices } from '@lightningrodlabs/we-applet'
3+
import { ObjectFlags } from 'typescript'
4+
import { PROFILES_ZOME_NAME } from '../holochainConfig'
5+
import { Profile, WhoAmIOutput } from '../types'
6+
import { AgentPubKeyB64, UpdateInput } from '../types/shared'
7+
import callZome from './callZome'
8+
import { WireRecord } from './hdkCrud'
9+
10+
const ZOME_FN_NAMES = {
11+
CREATE_WHOAMI: 'create_whoami',
12+
CREATE_IMPORTED_PROFILE: 'create_imported_profile',
13+
UPDATE_WHOAMI: 'update_whoami',
14+
WHOAMI: 'whoami',
15+
FETCH_AGENTS: 'fetch_agents',
16+
FETCH_AGENT_ADDRESS: 'fetch_agent_address',
17+
}
18+
function weToAcornProfile(weProfile: WeProfile): Profile {
19+
let acornProfile: Profile
20+
Object.keys(weProfile.fields).forEach((key) => {
21+
if (key === 'isImported') {
22+
acornProfile[key] = JSON.parse(weProfile.fields[key])
23+
}
24+
else {
25+
acornProfile[key] = weProfile.fields[key]
26+
}
27+
})
28+
return acornProfile
29+
}
30+
function acornToWeProfile(acornProfile: Profile, nickname: string): WeProfile {
31+
let weProfile: WeProfile = { nickname: '', fields: {} }
32+
Object.keys(acornProfile).forEach((key) => {
33+
if (key === 'isImported') {
34+
weProfile.fields[key] = JSON.stringify(acornProfile[key])
35+
}
36+
else {
37+
weProfile.fields[key] = acornProfile[key]
38+
}
39+
})
40+
return weProfile
41+
}
42+
async function getMyNickname(weServices: WeServices): Promise<string> {
43+
let myWeProfile: WeProfile
44+
(await weServices.profilesStore.fetchMyProfile()).subscribe((profile) => {myWeProfile = profile})
45+
return myWeProfile.nickname
46+
}
47+
export interface WeProfile {
48+
nickname: string;
49+
fields: Record<string, string>;
50+
}
51+
const WeProfilesApi = (appWebsocket: AppWebsocket, weServices: WeServices) => {
52+
return {
53+
createWhoami: async (cellId: CellId, payload: Profile): Promise<WireRecord<Profile>> => {
54+
const profile = acornToWeProfile(payload, await getMyNickname(weServices))
55+
await weServices.profilesStore.updateProfile(profile)
56+
return {
57+
actionHash: null,
58+
entryHash: null,
59+
entry: payload,
60+
createdAt: null,
61+
updatedAt: null,
62+
}
63+
},
64+
createImportedProfile: async (cellId: CellId, payload: Profile): Promise<WireRecord<Profile>> => {
65+
const profile = acornToWeProfile(payload, await getMyNickname(weServices))
66+
await weServices.profilesStore.updateProfile(profile)
67+
return {
68+
actionHash: null,
69+
entryHash: null,
70+
entry: payload,
71+
createdAt: null,
72+
updatedAt: null,
73+
}
74+
},
75+
updateWhoami: async (cellId: CellId, payload: UpdateInput<Profile>): Promise<WireRecord<Profile>> => {
76+
const profile = acornToWeProfile(payload.entry, await getMyNickname(weServices))
77+
await weServices.profilesStore.updateProfile(profile)
78+
return {
79+
actionHash: null,
80+
entryHash: null,
81+
entry: payload.entry,
82+
createdAt: null,
83+
updatedAt: null,
84+
}
85+
},
86+
whoami: async (cellId: CellId): Promise<WhoAmIOutput> => {
87+
let myWeProfile
88+
(await weServices.profilesStore.fetchMyProfile()).subscribe((profile) => {myWeProfile = profile})
89+
return {
90+
actionHash: null,
91+
entryHash: null,
92+
entry: weToAcornProfile(myWeProfile),
93+
createdAt: null,
94+
updatedAt: null,
95+
}
96+
},
97+
fetchAgents: async (cellId: CellId): Promise<Array<Profile>> => {
98+
return callZome(
99+
appWebsocket,
100+
cellId,
101+
PROFILES_ZOME_NAME,
102+
ZOME_FN_NAMES.FETCH_AGENTS,
103+
null
104+
)
105+
},
106+
fetchAgentAddress: async (cellId: CellId): Promise<AgentPubKeyB64> => {
107+
return callZome(
108+
appWebsocket,
109+
cellId,
110+
PROFILES_ZOME_NAME,
111+
ZOME_FN_NAMES.FETCH_AGENT_ADDRESS,
112+
null
113+
)
114+
},
115+
}
116+
}
117+
118+
export default class WeProfilesZomeApi {
119+
appWebsocket: AppWebsocket
120+
profile: ReturnType<typeof WeProfilesApi>
121+
constructor(appWebsocket: AppWebsocket, weServices: WeServices) {
122+
this.appWebsocket = appWebsocket
123+
this.profile = WeProfilesApi(appWebsocket, weServices)
124+
}
125+
}

0 commit comments

Comments
 (0)