-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnj_tweet.go
390 lines (331 loc) · 10.5 KB
/
nj_tweet.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package database
import (
"cloud.google.com/go/firestore"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/ninjahome/web-bridge/util"
"google.golang.org/api/iterator"
"strings"
)
type TxStatus int8
const (
TxStNotPay TxStatus = iota
TxStPending
TxStSuccess
TxStFailed
)
func (ts TxStatus) String() string {
switch ts {
case TxStNotPay:
return "not paid"
case TxStPending:
return "pending"
case TxStSuccess:
return "success"
case TxStFailed:
return "failed"
default:
return "unknown"
}
}
type NinjaTweet struct {
Txt string `json:"text" firestore:"text"`
TxtList []string `json:"text_list" firestore:"-"`
ImageThumb [][]string `json:"-" firestore:"-"`
ImageHash [][]string `json:"-" firestore:"-"`
ImageRaw [][]string `json:"-" firestore:"-"`
CreateAt int64 `json:"create_time" firestore:"create_time"`
Web3ID string `json:"web3_id" firestore:"web3_id"`
TweetUsrId string `json:"twitter_id" firestore:"twitter_id"`
TweetId string `json:"tweet_id,omitempty" firestore:"tweet_id"`
Signature string `json:"signature,omitempty" firestore:"signature"`
PrefixedHash string `json:"prefixed_hash" firestore:"prefixed_hash"`
PaymentStatus TxStatus `json:"payment_status" firestore:"payment_status"`
VoteCount int `json:"vote_count" firestore:"vote_count"`
}
type TweetQueryParam struct {
StartID int64 `json:"start_id"`
Web3ID string `json:"web3_id"`
VotedIDs []int64 `json:"voted_ids"`
HashArr []string `json:"hash_arr"`
IsOwner bool `json:"is_owner"`
}
type TweetImgRaw struct {
Raw string `json:"raw" firestore:"raw"`
Hash string `json:"hash" firestore:"hash"`
}
func (p *TweetQueryParam) String() string {
bts, _ := json.Marshal(p)
return string(bts)
}
type TweetPaymentStatus struct {
CreateTime int64 `json:"create_time" firestore:"create_time"`
TxHash string `json:"tx_hash" firestore:"tx_hash"`
Status TxStatus `json:"status,omitempty" firestore:"status"`
}
func (p *TweetQueryParam) createFilter(pageSize int, doc *firestore.CollectionRef, opCtx context.Context) *firestore.DocumentIterator {
if len(p.VotedIDs) > 0 {
return doc.Where("create_time", "in", p.VotedIDs).OrderBy("create_time", firestore.Desc).Documents(opCtx)
}
if len(p.HashArr) > 0 {
return doc.Where("prefixed_hash", "in", p.HashArr).Documents(opCtx)
}
var query = doc.Limit(pageSize)
if len(p.Web3ID) == 0 {
query = query.Where("payment_status", "==", TxStSuccess)
} else {
query = query.Where("web3_id", "==", p.Web3ID)
if p.IsOwner == false {
query = query.Where("payment_status", "==", TxStSuccess)
}
}
if p.StartID == 0 {
query = query.OrderBy("create_time", firestore.Desc)
} else {
query = query.Where("create_time", "<", p.StartID).OrderBy("create_time", firestore.Desc)
}
return query.Documents(opCtx)
}
type TweetsOfUser struct {
Tweets map[string]struct{} `json:"tweets" firestore:"tweets"`
}
func (nt *NinjaTweet) IsValid() bool {
return nt.CreateAt > 0 && len(nt.TxtList) >= 0 &&
len(nt.TweetUsrId) > 0 && len(nt.Web3ID) > 0
}
func (nt *NinjaTweet) String() string {
bts, _ := json.Marshal(nt)
return string(bts)
}
func (dm *DbManager) SaveRawImg(hash, raw string) error {
opCtx, cancel := context.WithTimeout(dm.ctx, DefaultDBTimeOut)
defer cancel()
imgDoc := dm.fileCli.Collection(DBTableTweetsImages).Doc(hash)
var obj = TweetImgRaw{
raw,
hash,
}
_, err := imgDoc.Set(opCtx, obj)
return err
}
func (dm *DbManager) GetRawImg(hash string) (*TweetImgRaw, error) {
opCtx, cancel := context.WithTimeout(dm.ctx, DefaultDBTimeOut)
defer cancel()
imgDoc := dm.fileCli.Collection(DBTableTweetsImages).Doc(hash)
docSnapshot, err := imgDoc.Get(opCtx)
if err != nil {
util.LogInst().Err(err).Msg("not found image raw obj :" + hash)
return nil, err
}
var imgRaw TweetImgRaw
err = docSnapshot.DataTo(&imgRaw)
if err != nil {
util.LogInst().Err(err).Msg("parse image raw obj failed:" + hash)
return nil, err
}
return &imgRaw, nil // 返回TweetImgRaw结构体中的Raw字段
}
func (dm *DbManager) SaveThumbImg(hash, raw string) error {
opCtx, cancel := context.WithTimeout(dm.ctx, DefaultDBTimeOut)
defer cancel()
imgDoc := dm.fileCli.Collection(DBTableTweetsThumbImages).Doc(hash)
var obj = TweetImgRaw{
raw,
hash,
}
_, err := imgDoc.Set(opCtx, obj)
return err
}
func (dm *DbManager) GetThumbImg(hash string) (*TweetImgRaw, error) {
opCtx, cancel := context.WithTimeout(dm.ctx, DefaultDBTimeOut)
defer cancel()
imgDoc := dm.fileCli.Collection(DBTableTweetsThumbImages).Doc(hash)
docSnapshot, err := imgDoc.Get(opCtx)
if err != nil {
util.LogInst().Err(err).Msg("not found image raw obj :" + hash)
return nil, err
}
var imgRaw TweetImgRaw
err = docSnapshot.DataTo(&imgRaw)
if err != nil {
util.LogInst().Err(err).Msg("parse image raw obj failed:" + hash)
return nil, err
}
return &imgRaw, nil
}
func (dm *DbManager) updateNjUserForTweet(web3ID string, opCtx context.Context) error {
docRef := dm.fileCli.Collection(DBTableNJUser).Doc(strings.ToLower(web3ID))
var nu NinjaUsrInfo
doc, err := docRef.Get(opCtx)
if err != nil {
util.LogInst().Err(err).Str("web3-id", web3ID).Msg("query nj user failed")
return err
}
err = doc.DataTo(&nu)
if err != nil {
util.LogInst().Err(err).Str("web3-id", web3ID).Msg("parse nj user failed")
return err
}
nu.TweetCount += 1
_, err = docRef.Update(opCtx, []firestore.Update{
{Path: "tweet_count", Value: nu.TweetCount},
})
go dm.ProcSystemPoints(web3ID, func(sp *SysPoints, _ bool) {
pointsWithReferrerBonus(sp, __dbConf.PointForPost)
})
return err
}
func (dm *DbManager) SaveTweet(content *NinjaTweet) error {
opCtx, cancel := context.WithTimeout(dm.ctx, DefaultDBTimeOut)
defer cancel()
var createTime = fmt.Sprintf("%d", content.CreateAt)
tweetsDoc := dm.fileCli.Collection(DBTableTweetsPosted).Doc(createTime)
_, err := tweetsDoc.Set(opCtx, content)
if err != nil {
util.LogInst().Err(err).Msg("save ninja tweet failed:" + content.String())
return err
}
return err
}
func (dm *DbManager) QueryTweetsByFilter(pageSize int, param *TweetQueryParam) ([]*NinjaTweet, error) {
opCtx, cancel := context.WithTimeout(dm.ctx, DefaultDBTimeOut)
defer cancel()
var doc = dm.fileCli.Collection(DBTableTweetsPosted)
var iter = param.createFilter(pageSize, doc, opCtx)
defer iter.Stop()
var tweets = make([]*NinjaTweet, 0)
for {
doc, err := iter.Next()
if errors.Is(err, iterator.Done) {
return tweets, nil
}
if err != nil {
util.LogInst().Err(err).Msgf("Failed to iterate: %v", err)
return nil, err
}
var tweet NinjaTweet
err = doc.DataTo(&tweet)
if err != nil {
util.LogInst().Err(err).Msgf("Failed to convert document to NinjaUsrInfo: %v", err)
return nil, err
}
tweets = append(tweets, &tweet)
}
}
func (dm *DbManager) NjTweetDetails(createAt int64) (*NinjaTweet, error) {
opCtx, cancel := context.WithTimeout(dm.ctx, DefaultDBTimeOut)
defer cancel()
var doc = dm.fileCli.Collection(DBTableTweetsPosted).Doc(fmt.Sprintf("%d", createAt))
data, err := doc.Get(opCtx)
if err != nil {
util.LogInst().Err(err).Int64("create_time", createAt).Msg("query ninja tweet detail failed")
return nil, err
}
var obj NinjaTweet
err = data.DataTo(&obj)
return &obj, err
}
func (dm *DbManager) UpdateTweetPaymentStatus(status *TweetPaymentStatus, web3ID string) error {
opCtx, cancel := context.WithTimeout(dm.ctx, DefaultDBTimeOut*2)
defer cancel()
tweetId := fmt.Sprintf("%d", status.CreateTime)
tweetsDoc := dm.fileCli.Collection(DBTableTweetsPosted).Doc(tweetId)
_, err := tweetsDoc.Update(opCtx, []firestore.Update{
{Path: "payment_status", Value: status.Status},
})
if err != nil {
util.LogInst().Err(err).Str("tweet", tweetId).Msg("update tweet status failed")
return err
}
if status.Status != TxStSuccess {
return nil
}
if err := dm.checkTransactionStatus(opCtx, status); err != nil {
return err
}
return dm.updateNjUserForTweet(web3ID, opCtx)
}
func (dm *DbManager) checkTransactionStatus(opCtx context.Context, status *TweetPaymentStatus) error {
statusDoc := dm.fileCli.Collection(DBTableTweetsStatus).Doc(status.TxHash)
_, err := statusDoc.Get(opCtx)
if err == nil {
return fmt.Errorf("duplicate update tweet:%d with transaction:%s", status.CreateTime, status.TxHash)
}
_, err = statusDoc.Set(opCtx, status)
if err != nil {
util.LogInst().Err(err).Int64("tweet", status.CreateTime).Str("tx", status.TxHash).Msg("add tweet update failed")
return err
}
return nil
}
func (dm *DbManager) DelUnpaidTweet(createTime int64, addr string) error {
opCtx, cancel := context.WithTimeout(dm.ctx, DefaultDBTimeOut)
defer cancel()
query := dm.fileCli.Collection(DBTableTweetsPosted).
Where("payment_status", "==", TxStNotPay).
Where("create_time", "==", createTime).
Where("web3_id", "==", addr)
iter := query.Documents(opCtx)
defer iter.Stop()
doc, err := iter.Next()
if err != nil {
util.LogInst().Err(err).Msg("no such item to delete")
return err
}
_, err = doc.Ref.Delete(opCtx)
return err
}
func (dm *DbManager) QueryTwUserByTweetHash(tHash string) (*TWUserInfo, error) {
opCtx, cancel := context.WithTimeout(dm.ctx, DefaultDBTimeOut)
defer cancel()
query := dm.fileCli.Collection(DBTableTweetsPosted).
Where("prefixed_hash", "==", tHash)
iter := query.Documents(opCtx)
defer iter.Stop()
doc, err := iter.Next()
if err != nil {
util.LogInst().Err(err).Str("tweet-hash", tHash).Msg("no such tweet")
return nil, err
}
var obj NinjaTweet
err = doc.DataTo(&obj)
if err != nil {
util.LogInst().Err(err).Msg("parse nj tweet failed")
return nil, err
}
twitterDoc := dm.fileCli.Collection(DBTableTWUser).Doc(obj.TweetUsrId)
doc, err = twitterDoc.Get(opCtx)
if err != nil {
util.LogInst().Err(err).Str("twitter-id", obj.TweetUsrId).Msg("twitterDoc get failed")
return nil, err
}
tu := &TWUserInfo{}
err = doc.DataTo(tu)
if err != nil {
util.LogInst().Err(err).Str("twitter-id", obj.TweetUsrId).Msg("twitter Doc to TWUserInfo failed")
return nil, err
}
return tu, nil
}
func (dm *DbManager) NjTweetDetailsByHash(tHash string) (*NinjaTweet, error) {
opCtx, cancel := context.WithTimeout(dm.ctx, DefaultDBTimeOut)
defer cancel()
query := dm.fileCli.Collection(DBTableTweetsPosted).
Where("prefixed_hash", "==", tHash)
iter := query.Documents(opCtx)
defer iter.Stop()
doc, err := iter.Next()
if err != nil {
util.LogInst().Err(err).Str("tweet-hash", tHash).Msg("no such tweet")
return nil, err
}
var obj NinjaTweet
err = doc.DataTo(&obj)
if err != nil {
util.LogInst().Err(err).Msg("parse nj tweet failed")
return nil, err
}
return &obj, nil
}