2022-02-02 22:50:55 +00:00
|
|
|
package stickers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-03-23 18:47:00 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/multiaccounts/settings"
|
2022-05-09 13:07:57 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/bigint"
|
2022-02-02 22:50:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const maxNumberRecentStickers = 24
|
|
|
|
|
|
|
|
func (api *API) recentStickers() ([]Sticker, error) {
|
2022-02-28 00:12:34 +00:00
|
|
|
recentStickersList := make([]Sticker, 0)
|
2022-02-02 22:50:55 +00:00
|
|
|
|
|
|
|
recentStickersJSON, err := api.accountsDB.GetRecentStickers()
|
|
|
|
if err != nil {
|
2022-02-28 00:12:34 +00:00
|
|
|
return recentStickersList, err
|
2022-02-02 22:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if recentStickersJSON == nil {
|
2022-02-28 00:12:34 +00:00
|
|
|
return recentStickersList, nil
|
2022-02-02 22:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(*recentStickersJSON, &recentStickersList)
|
|
|
|
if err != nil {
|
2022-02-28 00:12:34 +00:00
|
|
|
return recentStickersList, err
|
2022-02-02 22:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return recentStickersList, nil
|
|
|
|
}
|
|
|
|
|
2022-02-28 00:12:34 +00:00
|
|
|
func (api *API) ClearRecent() error {
|
2022-03-23 18:47:00 +00:00
|
|
|
var recentStickersList []Sticker
|
|
|
|
return api.accountsDB.SaveSettingField(settings.StickersRecentStickers, recentStickersList)
|
2022-02-28 00:12:34 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 22:50:55 +00:00
|
|
|
func (api *API) Recent() ([]Sticker, error) {
|
|
|
|
recentStickersList, err := api.recentStickers()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, sticker := range recentStickersList {
|
2022-05-09 13:07:57 +00:00
|
|
|
sticker.URL = api.hashToURL(sticker.Hash)
|
2022-02-02 22:50:55 +00:00
|
|
|
recentStickersList[i] = sticker
|
|
|
|
}
|
|
|
|
|
|
|
|
return recentStickersList, nil
|
|
|
|
}
|
|
|
|
|
2022-05-09 13:07:57 +00:00
|
|
|
func (api *API) AddRecent(packID *bigint.BigInt, hash string) error {
|
|
|
|
sticker := Sticker{
|
|
|
|
PackID: packID,
|
|
|
|
Hash: hash,
|
|
|
|
}
|
|
|
|
|
2022-02-02 22:50:55 +00:00
|
|
|
recentStickersList, err := api.recentStickers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove duplicated
|
|
|
|
idx := -1
|
|
|
|
for i, currSticker := range recentStickersList {
|
2022-02-14 23:25:56 +00:00
|
|
|
if currSticker.PackID.Cmp(sticker.PackID.Int) == 0 && currSticker.Hash == sticker.Hash {
|
2022-02-02 22:50:55 +00:00
|
|
|
idx = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if idx > -1 {
|
|
|
|
recentStickersList = append(recentStickersList[:idx], recentStickersList[idx+1:]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
sticker.URL = ""
|
|
|
|
|
|
|
|
if len(recentStickersList) >= maxNumberRecentStickers {
|
|
|
|
recentStickersList = append([]Sticker{sticker}, recentStickersList[:maxNumberRecentStickers-1]...)
|
|
|
|
} else {
|
|
|
|
recentStickersList = append([]Sticker{sticker}, recentStickersList...)
|
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
return api.accountsDB.SaveSettingField(settings.StickersRecentStickers, recentStickersList)
|
2022-02-02 22:50:55 +00:00
|
|
|
}
|