forked from cwtch.im/cwtch
Implementing #117 - Profile Custom Attribute Map
This commit is contained in:
parent
291f717e7e
commit
2197134758
|
@ -30,6 +30,7 @@ type Profile struct {
|
|||
Ed25519PrivateKey ed25519.PrivateKey
|
||||
OnionPrivateKey *rsa.PrivateKey
|
||||
Groups map[string]*Group
|
||||
Custom map[string]string
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
|
@ -52,6 +53,7 @@ func GenerateNewProfile(name string) *Profile {
|
|||
p.Contacts = make(map[string]*PublicProfile)
|
||||
p.Contacts[p.Onion] = &p.PublicProfile
|
||||
p.Groups = make(map[string]*Group)
|
||||
p.Custom = make(map[string]string)
|
||||
return p
|
||||
}
|
||||
|
||||
|
@ -113,6 +115,21 @@ func (p *Profile) GetGroups() []string {
|
|||
return keys
|
||||
}
|
||||
|
||||
// SetCustomAttribute allows applications to store arbitrary configuration info at the profile level.
|
||||
func (p *Profile) SetCustomAttribute(name string, value string) {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
p.Custom[name] = value
|
||||
}
|
||||
|
||||
// GetCustomAttribute returns the value of a value set with SetCustomAttribute. If no such value has been set exists is set to false.
|
||||
func (p *Profile) GetCustomAttribute(name string) (value string, exists bool) {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
value, exists = p.Custom[name]
|
||||
return
|
||||
}
|
||||
|
||||
// GetContacts returns an unordered list of contact onions associated with this profile.
|
||||
func (p *Profile) GetContacts() []string {
|
||||
p.lock.Lock()
|
||||
|
|
|
@ -26,6 +26,12 @@ func TestProfileIdentity(t *testing.T) {
|
|||
t.Errorf("alice should be only contact: %v", alice.GetContacts())
|
||||
}
|
||||
|
||||
alice.SetCustomAttribute("test", "hello world")
|
||||
value, _ := alice.GetCustomAttribute("test")
|
||||
if value != "hello world" {
|
||||
t.Errorf("value from custom attribute should have been 'hello world', instead was: %v", value)
|
||||
}
|
||||
|
||||
t.Logf("%v", alice)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue