users.gno
1.52 Kb ยท 66 lines
1package users
2
3import "gno.land/p/nt/avl/rotree"
4
5// ResolveName returns the latest UserData of a specific user by name or alias
6func ResolveName(name string) (data *UserData, isCurrent bool) {
7 raw, ok := nameStore.Get(name)
8 if !ok {
9 return nil, false
10 }
11
12 data = raw.(*UserData)
13 if data.deleted {
14 return nil, false
15 }
16
17 return data, name == data.username
18}
19
20// ResolveAddress returns the latest UserData of a specific user by address
21func ResolveAddress(addr address) *UserData {
22 raw, ok := addressStore.Get(addr.String())
23 if !ok {
24 return nil
25 }
26
27 data := raw.(*UserData)
28 if data.deleted {
29 return nil
30 }
31
32 return data
33}
34
35// ResolveAny tries to resolve any given string to *UserData
36// If the input is not found in the registry in any form, nil is returned
37func ResolveAny(input string) (*UserData, bool) {
38 addr := address(input)
39 if addr.IsValid() {
40 return ResolveAddress(addr), true
41 }
42
43 return ResolveName(input)
44}
45
46// GetReadonlyAddrStore exposes the address store in readonly mode
47func GetReadonlyAddrStore() *rotree.ReadOnlyTree {
48 return rotree.Wrap(addressStore, makeUserDataSafe)
49}
50
51// GetReadOnlyNameStore exposes the name store in readonly mode
52func GetReadOnlyNameStore() *rotree.ReadOnlyTree {
53 return rotree.Wrap(nameStore, makeUserDataSafe)
54}
55
56func makeUserDataSafe(data any) any {
57 cpy := new(UserData)
58 *cpy = *(data.(*UserData))
59 if cpy.deleted {
60 return nil
61 }
62
63 // Note: when requesting data from this AVL tree, (exists bool) will be true
64 // Even if the data is "deleted". This is currently unavoidable
65 return cpy
66}