-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathpending.go
133 lines (110 loc) · 3.25 KB
/
pending.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
package stickers
import (
"encoding/json"
"errors"
"math/big"
"github.com/status-im/status-go/multiaccounts/settings"
"github.com/status-im/status-go/services/wallet/bigint"
)
func (api *API) AddPending(chainID uint64, packID *bigint.BigInt) error {
pendingPacks, err := api.pendingStickerPacks()
if err != nil {
return err
}
if _, exists := pendingPacks[uint(packID.Uint64())]; exists {
return errors.New("sticker pack is already pending")
}
stickerType, err := api.contractMaker.NewStickerType(chainID)
if err != nil {
return err
}
stickerPack, err := api.fetchPackData(stickerType, packID.Int, false)
if err != nil {
return err
}
pendingPacks[uint(packID.Uint64())] = *stickerPack
return api.accountsDB.SaveSettingField(settings.StickersPacksPending, pendingPacks)
}
func (api *API) pendingStickerPacks() (StickerPackCollection, error) {
stickerPacks := make(StickerPackCollection)
pendingStickersJSON, err := api.accountsDB.GetPendingStickerPacks()
if err != nil {
return nil, err
}
if pendingStickersJSON == nil {
return stickerPacks, nil
}
err = json.Unmarshal(*pendingStickersJSON, &stickerPacks)
if err != nil {
return nil, err
}
return stickerPacks, nil
}
func (api *API) Pending() (StickerPackCollection, error) {
stickerPacks, err := api.pendingStickerPacks()
if err != nil {
return nil, err
}
for packID, stickerPack := range stickerPacks {
stickerPack.Status = statusPending
stickerPack.Preview = api.hashToURL(stickerPack.Preview)
stickerPack.Thumbnail = api.hashToURL(stickerPack.Thumbnail)
for i, sticker := range stickerPack.Stickers {
sticker.URL = api.hashToURL(sticker.Hash)
stickerPack.Stickers[i] = sticker
}
stickerPacks[packID] = stickerPack
}
return stickerPacks, nil
}
func (api *API) ProcessPending(chainID uint64) (pendingChanged StickerPackCollection, err error) {
pendingStickerPacks, err := api.pendingStickerPacks()
if err != nil {
return nil, err
}
accs, err := api.accountsDB.GetAccounts()
if err != nil {
return nil, err
}
purchasedPacks := make(map[uint]struct{})
purchasedPackChan := make(chan *big.Int)
errChan := make(chan error)
doneChan := make(chan struct{}, 1)
go api.getAccountsPurchasedPack(chainID, accs, purchasedPackChan, errChan, doneChan)
for {
select {
case err := <-errChan:
if err != nil {
return nil, err
}
case packID := <-purchasedPackChan:
if packID != nil {
purchasedPacks[uint(packID.Uint64())] = struct{}{}
}
case <-doneChan:
result := make(StickerPackCollection)
for _, stickerPack := range pendingStickerPacks {
packID := uint(stickerPack.ID.Uint64())
if _, exists := purchasedPacks[packID]; !exists {
continue
}
delete(pendingStickerPacks, packID)
stickerPack.Status = statusPurchased
result[packID] = stickerPack
}
err = api.accountsDB.SaveSettingField(settings.StickersPacksPending, pendingStickerPacks)
return result, err
}
}
}
func (api *API) RemovePending(packID *bigint.BigInt) error {
pendingPacks, err := api.pendingStickerPacks()
if err != nil {
return err
}
if _, exists := pendingPacks[uint(packID.Uint64())]; !exists {
return nil
}
delete(pendingPacks, uint(packID.Uint64()))
return api.accountsDB.SaveSettingField(settings.StickersPacksPending, pendingPacks)
}