-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.go
204 lines (165 loc) · 5.35 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package profiles
import (
"context"
"fmt"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/forbole/juno/v5/node/remote"
profilestypes "github.com/desmos-labs/desmos/v7/x/profiles/types"
"github.com/desmos-labs/athena/v2/types"
)
// GetDisplayName returns the name to be displayed for the user having the given address
func (m *Module) GetDisplayName(userAddress string) string {
profile, err := m.GetUserProfile(userAddress)
if err != nil || profile == nil {
return fmt.Sprintf("%[1]s...%[2]s", userAddress[:9], userAddress[len(userAddress)-5:])
}
switch {
case profile.Nickname != "":
return fmt.Sprintf("%[1]s (@%[2]s)", profile.Nickname, profile.DTag)
default:
return fmt.Sprintf("@%[1]s", profile.DTag)
}
}
// --------------------------------------------------------------------------------------------------------------------
// updateUserChainLinks updates the chain links for the given address, if any
func (m *Module) updateUserChainLinks(height int64, address string) error {
chainLinks, err := m.queryAllUserChainLinks(height, address)
if err != nil {
return err
}
for _, chainLink := range chainLinks {
err = m.db.SaveChainLink(chainLink)
if err != nil {
return err
}
}
return err
}
// queryAllUserChainLinks queries all the chain links for the given address
func (m *Module) queryAllUserChainLinks(height int64, address string) ([]types.ChainLink, error) {
var chainLinks []types.ChainLink
var nextKey []byte
var stop = false
for !stop {
res, err := m.profilesClient.ChainLinks(
remote.GetHeightRequestContext(context.Background(), height),
&profilestypes.QueryChainLinksRequest{
User: address,
Pagination: &query.PageRequest{
Key: nextKey,
},
},
)
if err != nil {
return nil, err
}
for _, link := range res.Links {
chainLinks = append(chainLinks, types.NewChainLink(link, height))
}
nextKey = res.Pagination.NextKey
stop = nextKey == nil
}
return chainLinks, nil
}
// --------------------------------------------------------------------------------------------------------------------
// updateUserDefaultChainLinks updates the default chain links associated with the given address, if any
func (m *Module) updateUserDefaultChainLinks(height int64, address string) error {
chainLinks, err := m.queryAllUserDefaultChainLinks(height, address)
if err != nil {
return err
}
for _, chainLink := range chainLinks {
err = m.db.SaveDefaultChainLink(chainLink)
if err != nil {
return err
}
}
return nil
}
// queryAllUserDefaultChainLinks queries all the default chain links for the given address
func (m *Module) queryAllUserDefaultChainLinks(height int64, address string) ([]types.ChainLink, error) {
var chainLinks []types.ChainLink
var nextKey []byte
var stop = false
for !stop {
res, err := m.profilesClient.DefaultExternalAddresses(
remote.GetHeightRequestContext(context.Background(), height),
&profilestypes.QueryDefaultExternalAddressesRequest{
Owner: address,
Pagination: &query.PageRequest{
Key: nextKey,
},
},
)
if err != nil {
return nil, err
}
for _, link := range res.Links {
chainLinks = append(chainLinks, types.NewChainLink(link, height))
}
nextKey = res.Pagination.NextKey
stop = nextKey == nil
}
return chainLinks, nil
}
// --------------------------------------------------------------------------------------------------------------------
// updateUserApplicationLinks updates the application links associated with the given address, if any
func (m *Module) updateUserApplicationLinks(height int64, address string) error {
applicationLinks, err := m.queryAllUserApplicationLinks(height, address)
if err != nil {
return err
}
for _, applicationLink := range applicationLinks {
err = m.db.SaveApplicationLink(applicationLink)
if err != nil {
return err
}
}
return nil
}
// queryAllUserApplicationLinks queries all the application links for the given address
func (m *Module) queryAllUserApplicationLinks(height int64, address string) ([]types.ApplicationLink, error) {
var chainLinks []types.ApplicationLink
var nextKey []byte
var stop = false
for !stop {
res, err := m.profilesClient.ApplicationLinks(
remote.GetHeightRequestContext(context.Background(), height),
&profilestypes.QueryApplicationLinksRequest{
User: address,
Pagination: &query.PageRequest{
Key: nextKey,
},
},
)
if err != nil {
return nil, err
}
for _, link := range res.Links {
chainLinks = append(chainLinks, types.NewApplicationLink(link, height))
}
nextKey = res.Pagination.NextKey
stop = nextKey == nil
}
return chainLinks, nil
}
// --------------------------------------------------------------------------------------------------------------------
// updateParams allows to update the profiles params by fetching them from the chain
func (m *Module) updateParams() error {
height, err := m.node.LatestHeight()
if err != nil {
return fmt.Errorf("error while getting latest block height: %s", err)
}
res, err := m.profilesClient.Params(
remote.GetHeightRequestContext(context.Background(), height),
&profilestypes.QueryParamsRequest{},
)
if err != nil {
return fmt.Errorf("error while getting params: %s", err)
}
err = m.db.SaveProfilesParams(types.NewProfilesParams(res.Params, height))
if err != nil {
return fmt.Errorf("error while storing profiles params: %s", err)
}
return nil
}