-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
216 lines (173 loc) · 4.71 KB
/
store.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package memory
import (
"context"
"sort"
"sync"
"time"
"github.com/google/uuid"
"github.com/code-payments/code-server/pkg/code/data/twitter"
)
type ByLastUpdatedAt []*twitter.Record
func (a ByLastUpdatedAt) Len() int { return len(a) }
func (a ByLastUpdatedAt) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByLastUpdatedAt) Less(i, j int) bool { return a[i].LastUpdatedAt.Before(a[j].LastUpdatedAt) }
type store struct {
mu sync.Mutex
userRecords []*twitter.Record
processedTweets map[string]any
usedNonces map[string]any
last uint64
}
// New returns a new in memory twitter.Store
func New() twitter.Store {
return &store{
processedTweets: make(map[string]any),
usedNonces: make(map[string]any),
}
}
// SaveUser implements twitter.Store.SaveUser
func (s *store) SaveUser(_ context.Context, data *twitter.Record) error {
if err := data.Validate(); err != nil {
return err
}
s.mu.Lock()
defer s.mu.Unlock()
s.last++
itemByTipAddress := s.findUserByTipAddress(data.TipAddress)
if itemByTipAddress != nil && data.Username != itemByTipAddress.Username {
return twitter.ErrDuplicateTipAddress
}
if item := s.findUser(data); item != nil {
data.LastUpdatedAt = time.Now()
item.Name = data.Name
item.ProfilePicUrl = data.ProfilePicUrl
item.VerifiedType = data.VerifiedType
item.FollowerCount = data.FollowerCount
item.TipAddress = data.TipAddress
item.LastUpdatedAt = data.LastUpdatedAt
} else {
if data.Id == 0 {
data.Id = s.last
}
if data.CreatedAt.IsZero() {
data.CreatedAt = time.Now()
}
data.LastUpdatedAt = time.Now()
c := data.Clone()
s.userRecords = append(s.userRecords, &c)
}
return nil
}
// GetUserByUsername implements twitter.Store.GetUserByUsername
func (s *store) GetUserByUsername(_ context.Context, username string) (*twitter.Record, error) {
s.mu.Lock()
defer s.mu.Unlock()
item := s.findUserByUsername(username)
if item == nil {
return nil, twitter.ErrUserNotFound
}
cloned := item.Clone()
return &cloned, nil
}
// GetUserByTipAddress implements twitter.Store.GetUserByTipAddress
func (s *store) GetUserByTipAddress(ctx context.Context, tipAddress string) (*twitter.Record, error) {
s.mu.Lock()
defer s.mu.Unlock()
item := s.findUserByTipAddress(tipAddress)
if item == nil {
return nil, twitter.ErrUserNotFound
}
cloned := item.Clone()
return &cloned, nil
}
// GetStaleUsers implements twitter.Store.GetStaleUsers
func (s *store) GetStaleUsers(ctx context.Context, minAge time.Duration, limit int) ([]*twitter.Record, error) {
s.mu.Lock()
defer s.mu.Unlock()
items := s.findStaleUsers(minAge)
sorted := ByLastUpdatedAt(items)
sort.Sort(sorted)
if len(items) > limit {
sorted = sorted[:limit]
}
if len(sorted) == 0 {
return nil, twitter.ErrUserNotFound
}
return userSliceCopy(sorted), nil
}
// MarkTweetAsProcessed implements twitter.Store.MarkTweetAsProcessed
func (s *store) MarkTweetAsProcessed(_ context.Context, tweetId string) error {
s.mu.Lock()
defer s.mu.Unlock()
s.processedTweets[tweetId] = struct{}{}
return nil
}
// IsTweetProcessed implements twitter.Store.IsTweetProcessed
func (s *store) IsTweetProcessed(_ context.Context, tweetId string) (bool, error) {
s.mu.Lock()
defer s.mu.Unlock()
_, ok := s.processedTweets[tweetId]
return ok, nil
}
func (s *store) MarkNonceAsUsed(_ context.Context, _ string, nonce uuid.UUID) error {
s.mu.Lock()
defer s.mu.Unlock()
_, ok := s.usedNonces[nonce.String()]
if ok {
return twitter.ErrDuplicateNonce
}
s.usedNonces[nonce.String()] = struct{}{}
return nil
}
func (s *store) findUser(data *twitter.Record) *twitter.Record {
for _, item := range s.userRecords {
if item.Id == data.Id {
return item
}
if data.Username == item.Username {
return item
}
}
return nil
}
func (s *store) findUserByUsername(username string) *twitter.Record {
for _, item := range s.userRecords {
if username == item.Username {
return item
}
}
return nil
}
func (s *store) findUserByTipAddress(tipAddress string) *twitter.Record {
for _, item := range s.userRecords {
if tipAddress == item.TipAddress {
return item
}
}
return nil
}
func (s *store) findStaleUsers(minAge time.Duration) []*twitter.Record {
var res []*twitter.Record
for _, item := range s.userRecords {
if time.Since(item.LastUpdatedAt) > minAge {
res = append(res, item)
}
}
return res
}
func (s *store) reset() {
s.mu.Lock()
defer s.mu.Unlock()
s.userRecords = nil
s.processedTweets = make(map[string]any)
s.usedNonces = make(map[string]any)
s.last = 0
}
func userSliceCopy(items []*twitter.Record) []*twitter.Record {
res := make([]*twitter.Record, len(items))
for i, item := range items {
cloned := item.Clone()
res[i] = &cloned
}
return res
}