1
1
import { AppWebsocket , CellId } from '@holochain/client'
2
2
import { WeServices } from '@lightningrodlabs/we-applet'
3
- import { Profile , WhoAmIOutput } from '../types'
3
+ import { Profile , Status , WhoAmIOutput } from '../types'
4
4
import { AgentPubKeyB64 , UpdateInput } from '../types/shared'
5
5
import { WireRecord } from './hdkCrud'
6
6
import { get } from 'svelte/store'
7
7
8
- function weToAcornProfile ( weProfile : WeProfile ) : Profile {
8
+ function weToAcornProfile ( weProfile : WeProfile , weServices : WeServices ) : Profile {
9
9
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 ) => {
11
15
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 ] ) : ''
13
28
}
14
29
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
16
32
}
33
+
17
34
} )
18
35
return acornProfile
19
36
}
20
- function acornToWeProfile ( acornProfile : Profile , nickname : string ) : WeProfile {
21
- let weProfile : WeProfile = { nickname : '' , fields : { } }
37
+ function acornToWeProfile ( acornProfile : Profile , weProfile : WeProfile ) : WeProfile {
22
38
Object . keys ( acornProfile ) . forEach ( ( key ) => {
23
39
if ( key === 'isImported' ) {
24
40
weProfile . fields [ key ] = JSON . stringify ( acornProfile [ key ] )
@@ -40,7 +56,8 @@ export interface WeProfile {
40
56
const WeProfilesApi = ( appWebsocket : AppWebsocket , weServices : WeServices ) => {
41
57
return {
42
58
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 )
44
61
await weServices . profilesStore . updateProfile ( profile )
45
62
return {
46
63
actionHash : null ,
@@ -51,7 +68,8 @@ const WeProfilesApi = (appWebsocket: AppWebsocket, weServices: WeServices) => {
51
68
}
52
69
} ,
53
70
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 )
55
73
await weServices . profilesStore . updateProfile ( profile )
56
74
return {
57
75
actionHash : null ,
@@ -62,7 +80,8 @@ const WeProfilesApi = (appWebsocket: AppWebsocket, weServices: WeServices) => {
62
80
}
63
81
} ,
64
82
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 )
66
85
await weServices . profilesStore . updateProfile ( profile )
67
86
return {
68
87
actionHash : null ,
@@ -77,13 +96,13 @@ const WeProfilesApi = (appWebsocket: AppWebsocket, weServices: WeServices) => {
77
96
return {
78
97
actionHash : null ,
79
98
entryHash : null ,
80
- entry : weToAcornProfile ( myWeProfile ) ,
99
+ entry : weToAcornProfile ( myWeProfile , weServices ) ,
81
100
createdAt : null ,
82
101
updatedAt : null ,
83
102
}
84
103
} ,
85
104
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 ) ) ;
87
106
return profiles
88
107
} ,
89
108
fetchAgentAddress : async ( cellId : CellId ) : Promise < AgentPubKeyB64 > => {
0 commit comments