-
Notifications
You must be signed in to change notification settings - Fork 244
/
recent.go
102 lines (79 loc) · 2.6 KB
/
recent.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
package stickers
import (
"encoding/json"
"github.com/status-im/status-go/multiaccounts/settings"
"github.com/status-im/status-go/services/wallet/bigint"
)
const maxNumberRecentStickers = 24
func (api *API) recentStickers() ([]Sticker, error) {
installedStickersPacksJSON, err := api.accountsDB.GetInstalledStickerPacks()
if err != nil || installedStickersPacksJSON == nil {
return []Sticker{}, nil
}
recentStickersJSON, err := api.accountsDB.GetRecentStickers()
if err != nil || recentStickersJSON == nil {
return []Sticker{}, nil
}
recentStickersList := make([]Sticker, 0)
if err := json.Unmarshal(*recentStickersJSON, &recentStickersList); err != nil {
return []Sticker{}, err
}
var installedStickersPacks map[string]StickerPack
if err := json.Unmarshal(*installedStickersPacksJSON, &installedStickersPacks); err != nil {
return []Sticker{}, err
}
recentStickersListInExistingPacks := make([]Sticker, 0)
existingPackIDs := make(map[string]bool)
for k := range installedStickersPacks {
existingPackIDs[k] = true
}
for _, s := range recentStickersList {
packIDStr := s.PackID.String()
if _, exists := existingPackIDs[packIDStr]; exists {
recentStickersListInExistingPacks = append(recentStickersListInExistingPacks, s)
}
}
return recentStickersListInExistingPacks, nil
}
func (api *API) ClearRecent() error {
var recentStickersList []Sticker
return api.accountsDB.SaveSettingField(settings.StickersRecentStickers, recentStickersList)
}
func (api *API) Recent() ([]Sticker, error) {
recentStickersList, err := api.recentStickers()
if err != nil {
return nil, err
}
for i, sticker := range recentStickersList {
sticker.URL = api.hashToURL(sticker.Hash)
recentStickersList[i] = sticker
}
return recentStickersList, nil
}
func (api *API) AddRecent(packID *bigint.BigInt, hash string) error {
sticker := Sticker{
PackID: packID,
Hash: hash,
}
recentStickersList, err := api.recentStickers()
if err != nil {
return err
}
// Remove duplicated
idx := -1
for i, currSticker := range recentStickersList {
if currSticker.PackID.Cmp(sticker.PackID.Int) == 0 && currSticker.Hash == sticker.Hash {
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...)
}
return api.accountsDB.SaveSettingField(settings.StickersRecentStickers, recentStickersList)
}