/
external.go
296 lines (249 loc) · 7.48 KB
/
external.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package posts
import (
"context"
"encoding/hex"
"fmt"
"sort"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
"github.com/desmos-labs/athena/utils"
"github.com/rs/zerolog/log"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/forbole/juno/v5/node/remote"
poststypes "github.com/desmos-labs/desmos/v6/x/posts/types"
"github.com/desmos-labs/athena/types"
)
// RefreshPostsData refreshes all the posts' data for the given subspace
func (m *Module) RefreshPostsData(height int64, subspaceID uint64) error {
postsTxs, err := m.QueryPostsTxs(height, subspaceID)
if err != nil {
return err
}
posts, err := m.QuerySubspacePosts(height, subspaceID)
if err != nil {
return err
}
err = m.db.DeleteAllPosts(height, subspaceID)
if err != nil {
return fmt.Errorf("error while deleting subspace posts: %s", err)
}
// Refresh posts
for _, post := range posts {
// Get the post transactions
postTxs := m.GetPostTxHashes(postsTxs, post)
// Refresh the post data
err = m.RefreshPostData(height, postTxs, post)
if err != nil {
return err
}
}
return nil
}
// RefreshPostData refreshes the data associated with the given post at the specified height
func (m *Module) RefreshPostData(height int64, postTxs []types.PostTransaction, post types.Post) error {
// Make sure the post conversation was stored properly
if post.ConversationID != 0 {
conversationExists, err := m.db.HasPost(height, post.SubspaceID, post.ConversationID)
if err != nil {
return err
}
// Skip posts for which we have not stored the conversation yet
if !conversationExists {
return nil
}
}
// Make sure the post reference all exists properly
for _, reference := range post.ReferencedPosts {
referenceExists, err := m.db.HasPost(height, post.SubspaceID, reference.PostID)
if err != nil {
return err
}
// Skip posts for which we have not stored the reference yet
if !referenceExists {
return nil
}
}
log.Debug().Uint64("subspace", post.SubspaceID).Uint64("post", post.ID).Msg("refreshing post")
err := m.db.SavePost(post)
if err != nil {
return fmt.Errorf("error while saving post: %s", err)
}
// Refresh transactions
for _, tx := range postTxs {
err = m.db.SavePostTx(tx)
if err != nil {
return fmt.Errorf("error while saving post transaction: %s", tx.Hash)
}
}
attachments, err := m.QueryPostAttachments(height, post.SubspaceID, post.ID)
if err != nil {
return err
}
// Refresh attachments
for _, attachment := range attachments {
log.Debug().Uint64("subspace", attachment.SubspaceID).Uint64("post", attachment.PostID).
Uint32("attachment", attachment.ID).Msg("refreshing attachment")
err = m.db.SavePostAttachment(attachment)
if err != nil {
return fmt.Errorf("error while saving post attachment: %s", err)
}
// Refresh poll answers
if _, isPoll := attachment.Content.GetCachedValue().(*poststypes.Poll); isPoll {
log.Debug().Uint64("subspace", attachment.SubspaceID).Uint64("post", attachment.PostID).
Uint32("poll", attachment.ID).Msg("refreshing poll answers")
answers, err := m.queryPollAnswers(height, attachment.SubspaceID, attachment.PostID, attachment.ID)
if err != nil {
return err
}
for _, answer := range answers {
err = m.db.SavePollAnswer(answer)
if err != nil {
return fmt.Errorf("errror while saving poll answer: %s", err)
}
}
}
}
return nil
}
// QuerySubspacePosts queries all the posts present inside the given subspace at the provided height
func (m *Module) QuerySubspacePosts(height int64, subspaceID uint64) ([]types.Post, error) {
var posts []types.Post
var nextKey []byte
var stop = false
for !stop {
res, err := m.client.SubspacePosts(
remote.GetHeightRequestContext(context.Background(), height),
&poststypes.QuerySubspacePostsRequest{
SubspaceId: subspaceID,
Pagination: &query.PageRequest{
Key: nextKey,
},
},
)
if err != nil {
return nil, err
}
for _, post := range res.Posts {
posts = append(posts, types.NewPost(post, height))
}
nextKey = res.Pagination.NextKey
stop = nextKey == nil
}
return posts, nil
}
// QueryPostsTxs queries all the posts transactions made inside the given subspace
func (m *Module) QueryPostsTxs(height int64, subspaceID uint64) ([]*coretypes.ResultTx, error) {
queries := []string{
// MsgCreatePost
fmt.Sprintf("%s.%s='%d' AND tx.height <= %d",
poststypes.EventTypeCreatePost,
poststypes.AttributeKeySubspaceID,
subspaceID,
height,
),
// MsgEditPost
fmt.Sprintf("%s.%s='%d' AND tx.height <= %d",
poststypes.EventTypeEditPost,
poststypes.AttributeKeySubspaceID,
subspaceID,
height,
),
// MsgAddPostAttachment
fmt.Sprintf("%s.%s='%d' AND tx.height <= %d",
poststypes.EventTypeAddPostAttachment,
poststypes.AttributeKeySubspaceID,
subspaceID,
height,
),
// MsgRemovePostAttachment
fmt.Sprintf("%s.%s='%d' AND tx.height <= %d",
poststypes.EventTypeRemovePostAttachment,
poststypes.AttributeKeySubspaceID,
subspaceID,
height,
),
}
var txs []*coretypes.ResultTx
for _, qry := range queries {
resultTxs, err := utils.QueryTxs(m.node, qry)
if err != nil {
return nil, err
}
txs = append(txs, resultTxs...)
}
// Sort the txs based on their ascending height
sort.Slice(txs, func(i, j int) bool {
return txs[i].Height < txs[j].Height
})
return txs, nil
}
// GetPostTxHashes filters the given transactions and returns only the ones related to the given post
func (m *Module) GetPostTxHashes(txs []*coretypes.ResultTx, post types.Post) []types.PostTransaction {
var postTxs []types.PostTransaction
for _, tx := range txs {
for _, event := range tx.TxResult.Events {
if utils.HasSubspaceIDAndPostIDAttributes(event, post.SubspaceID, post.ID) {
postTxs = append(postTxs, types.NewPostTransaction(
post.SubspaceID,
post.ID,
hex.EncodeToString(tx.Hash),
))
}
}
}
return postTxs
}
// QueryPostAttachments queries all the attachments for the given post at the provided height
func (m *Module) QueryPostAttachments(height int64, subspaceID uint64, postID uint64) ([]types.PostAttachment, error) {
var attachments []types.PostAttachment
var nextKey []byte
var stop = false
for !stop {
res, err := m.client.PostAttachments(
remote.GetHeightRequestContext(context.Background(), height),
&poststypes.QueryPostAttachmentsRequest{
SubspaceId: subspaceID,
PostId: postID,
Pagination: &query.PageRequest{
Key: nextKey,
},
},
)
if err != nil {
return nil, err
}
for _, attachment := range res.Attachments {
attachments = append(attachments, types.NewPostAttachment(attachment, height))
}
nextKey = res.Pagination.NextKey
stop = nextKey == nil
}
return attachments, nil
}
// QuerySubspacePosts queries all the posts present inside the given subspace at the provided height
func (m *Module) queryPollAnswers(height int64, subspaceID uint64, postID uint64, pollID uint32) ([]types.PollAnswer, error) {
var answers []types.PollAnswer
var nextKey []byte
var stop = false
for !stop {
res, err := m.client.PollAnswers(
remote.GetHeightRequestContext(context.Background(), height),
&poststypes.QueryPollAnswersRequest{
SubspaceId: subspaceID,
PostId: postID,
PollId: pollID,
Pagination: &query.PageRequest{
Key: nextKey,
},
},
)
if err != nil {
return nil, err
}
for _, answer := range res.Answers {
answers = append(answers, types.NewPollAnswer(answer, height))
}
nextKey = res.Pagination.NextKey
stop = nextKey == nil
}
return answers, nil
}