2022-02-02 22:50:55 +00:00
|
|
|
package stickers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-05-09 13:07:57 +00:00
|
|
|
"math/big"
|
2022-02-13 23:43:08 +00:00
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/settings"
|
2022-02-13 23:43:08 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/bigint"
|
2022-02-02 22:50:55 +00:00
|
|
|
)
|
|
|
|
|
2022-02-13 23:43:08 +00:00
|
|
|
func (api *API) AddPending(chainID uint64, packID *bigint.BigInt) error {
|
2022-02-02 22:50:55 +00:00
|
|
|
pendingPacks, err := api.pendingStickerPacks()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-13 23:43:08 +00:00
|
|
|
if _, exists := pendingPacks[uint(packID.Uint64())]; exists {
|
2022-02-02 22:50:55 +00:00
|
|
|
return errors.New("sticker pack is already pending")
|
|
|
|
}
|
|
|
|
|
|
|
|
stickerType, err := api.contractMaker.NewStickerType(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-13 23:43:08 +00:00
|
|
|
stickerPack, err := api.fetchPackData(stickerType, packID.Int, false)
|
2022-02-02 22:50:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-13 23:43:08 +00:00
|
|
|
pendingPacks[uint(packID.Uint64())] = *stickerPack
|
2022-02-02 22:50:55 +00:00
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
return api.accountsDB.SaveSettingField(settings.StickersPacksPending, pendingPacks)
|
2022-02-02 22:50:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
func (api *API) pendingStickerPacks() (StickerPackCollection, error) {
|
|
|
|
stickerPacks := make(StickerPackCollection)
|
2022-02-02 22:50:55 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
func (api *API) Pending() (StickerPackCollection, error) {
|
2022-02-02 22:50:55 +00:00
|
|
|
stickerPacks, err := api.pendingStickerPacks()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for packID, stickerPack := range stickerPacks {
|
|
|
|
stickerPack.Status = statusPending
|
2022-05-09 13:07:57 +00:00
|
|
|
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
|
2022-02-28 00:12:34 +00:00
|
|
|
}
|
2022-05-09 13:07:57 +00:00
|
|
|
stickerPacks[packID] = stickerPack
|
|
|
|
}
|
2022-02-28 00:12:34 +00:00
|
|
|
|
2022-05-09 13:07:57 +00:00
|
|
|
return stickerPacks, nil
|
|
|
|
}
|
2022-02-28 00:12:34 +00:00
|
|
|
|
2022-05-09 13:07:57 +00:00
|
|
|
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:
|
2022-02-28 00:12:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-09 13:07:57 +00:00
|
|
|
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
|
2022-02-28 00:12:34 +00:00
|
|
|
}
|
2022-02-02 22:50:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 23:43:08 +00:00
|
|
|
func (api *API) RemovePending(packID *bigint.BigInt) error {
|
2022-02-02 22:50:55 +00:00
|
|
|
pendingPacks, err := api.pendingStickerPacks()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-13 23:43:08 +00:00
|
|
|
if _, exists := pendingPacks[uint(packID.Uint64())]; !exists {
|
2022-02-28 00:12:34 +00:00
|
|
|
return nil
|
2022-02-02 22:50:55 +00:00
|
|
|
}
|
|
|
|
|
2022-02-13 23:43:08 +00:00
|
|
|
delete(pendingPacks, uint(packID.Uint64()))
|
2022-02-02 22:50:55 +00:00
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
return api.accountsDB.SaveSettingField(settings.StickersPacksPending, pendingPacks)
|
2022-02-02 22:50:55 +00:00
|
|
|
}
|