status-go/services/stickers/pending.go

102 lines
2.1 KiB
Go
Raw Normal View History

2022-02-02 22:50:55 +00:00
package stickers
import (
"encoding/json"
"errors"
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
return api.accountsDB.SaveSetting("stickers/packs-pending", pendingPacks)
}
func (api *API) pendingStickerPacks() (map[uint]StickerPack, error) {
stickerPacks := make(map[uint]StickerPack)
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() (map[uint]StickerPack, error) {
stickerPacks, err := api.pendingStickerPacks()
if err != nil {
return nil, err
}
for packID, stickerPack := range stickerPacks {
stickerPack.Status = statusPending
stickerPack.Preview, err = decodeStringHash(stickerPack.Preview)
if err != nil {
return nil, err
}
stickerPack.Thumbnail, err = decodeStringHash(stickerPack.Thumbnail)
if err != nil {
return nil, err
}
for i, sticker := range stickerPack.Stickers {
sticker.URL, err = decodeStringHash(sticker.Hash)
if err != nil {
return nil, err
}
stickerPack.Stickers[i] = sticker
}
2022-02-02 22:50:55 +00:00
stickerPacks[packID] = stickerPack
}
return stickerPacks, nil
}
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 {
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
return api.accountsDB.SaveSetting("stickers/packs-pending", pendingPacks)
}