users.gno

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