-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtwitter.go
286 lines (236 loc) · 7.86 KB
/
twitter.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package async_user
import (
"context"
"strings"
"time"
"github.com/mr-tron/base58"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/pkg/errors"
commonpb "github.com/code-payments/code-protobuf-api/generated/go/common/v1"
userpb "github.com/code-payments/code-protobuf-api/generated/go/user/v1"
"github.com/code-payments/code-server/pkg/code/common"
"github.com/code-payments/code-server/pkg/code/data/account"
"github.com/code-payments/code-server/pkg/code/data/twitter"
"github.com/code-payments/code-server/pkg/metrics"
"github.com/code-payments/code-server/pkg/retry"
twitter_lib "github.com/code-payments/code-server/pkg/twitter"
)
const (
tipCardRegistrationPrefix = "CodeAccount:"
maxTweetSearchResults = 100 // maximum allowed
)
var (
errTwitterInvalidRegistrationValue = errors.New("twitter registration value is invalid")
errTwitterRegistrationNotFound = errors.New("twitter registration not found")
)
func (p *service) twitterRegistrationWorker(serviceCtx context.Context, interval time.Duration) error {
log := p.log.WithField("method", "twitterRegistrationWorker")
delay := interval
err := retry.Loop(
func() (err error) {
time.Sleep(delay)
nr := serviceCtx.Value(metrics.NewRelicContextKey).(*newrelic.Application)
m := nr.StartTransaction("async__user_service__handle_twitter_registration")
defer m.End()
tracedCtx := newrelic.NewContext(serviceCtx, m)
err = p.findNewTwitterRegistrations(tracedCtx)
if err != nil {
m.NoticeError(err)
log.WithError(err).Warn("failure processing new twitter registrations")
}
return err
},
retry.NonRetriableErrors(context.Canceled),
)
return err
}
func (p *service) twitterUserInfoUpdateWorker(serviceCtx context.Context, interval time.Duration) error {
log := p.log.WithField("method", "twitterUserInfoUpdateWorker")
delay := interval
err := retry.Loop(
func() (err error) {
time.Sleep(delay)
nr := serviceCtx.Value(metrics.NewRelicContextKey).(*newrelic.Application)
m := nr.StartTransaction("async__user_service__handle_twitter_user_info_update")
defer m.End()
tracedCtx := newrelic.NewContext(serviceCtx, m)
// todo: configurable parameters
records, err := p.data.GetStaleTwitterUsers(tracedCtx, 7*24*time.Hour, 32)
if err == twitter.ErrUserNotFound {
return nil
} else if err != nil {
m.NoticeError(err)
log.WithError(err).Warn("failure getting stale twitter users")
return err
}
for _, record := range records {
err := p.refreshTwitterUserInfo(tracedCtx, record.Username)
if err != nil {
m.NoticeError(err)
log.WithError(err).Warn("failure refreshing twitter user info")
return err
}
}
return nil
},
retry.NonRetriableErrors(context.Canceled),
)
return err
}
func (p *service) findNewTwitterRegistrations(ctx context.Context) error {
var newlyProcessedTweets []string
err := func() error {
var pageToken *string
processedUsernames := make(map[string]any)
for {
tweets, nextPageToken, err := p.twitterClient.SearchRecentTweets(
ctx,
tipCardRegistrationPrefix,
maxTweetSearchResults,
pageToken,
)
if err != nil {
return errors.Wrap(err, "error searching tweets")
}
for _, tweet := range tweets {
if tweet.AdditionalMetadata.Author == nil {
return errors.Errorf("author missing in tweet %s", tweet.ID)
}
isTweetProcessed, err := p.data.IsTweetProcessed(ctx, tweet.ID)
if err != nil {
return errors.Wrap(err, "error checking if tweet is processed")
} else if isTweetProcessed {
// Found a checkpoint, so stop processing
return nil
}
// Oldest tweets go first, so we are guaranteed to checkpoint everything
newlyProcessedTweets = append([]string{tweet.ID}, newlyProcessedTweets...)
// Avoid reprocessing a Twitter user and potentially overriding the
// tip address with something older.
if _, ok := processedUsernames[tweet.AdditionalMetadata.Author.Username]; ok {
continue
}
// Attempt to find a tip account from the registration tweet
tipAccount, err := findTipAccountRegisteredInTweet(tweet)
switch err {
case nil:
case errTwitterInvalidRegistrationValue, errTwitterRegistrationNotFound:
continue
default:
return errors.Wrapf(err, "unexpected error processing tweet %s", tweet.ID)
}
// Validate the new tip account
accountInfoRecord, err := p.data.GetAccountInfoByTokenAddress(ctx, tipAccount.PublicKey().ToBase58())
switch err {
case nil:
// todo: potentially use a relationship account instead
if accountInfoRecord.AccountType != commonpb.AccountType_PRIMARY {
continue
}
case account.ErrAccountInfoNotFound:
continue
default:
return errors.Wrap(err, "error getting account info")
}
processedUsernames[tweet.AdditionalMetadata.Author.Username] = struct{}{}
err = p.updateCachedTwitterUser(ctx, tweet.AdditionalMetadata.Author, tipAccount)
if err != nil {
return errors.Wrap(err, "error updating cached user state")
}
}
if nextPageToken == nil {
return nil
}
pageToken = nextPageToken
}
}()
if err != nil {
return err
}
// Only update the processed tweet cache once we've found another checkpoint,
// or reached the end of the Tweet feed.
//
// todo: add batching
for _, tweetId := range newlyProcessedTweets {
err := p.data.MarkTweetAsProcessed(ctx, tweetId)
if err != nil {
return errors.Wrap(err, "error marking tweet as processed")
}
}
return nil
}
func (p *service) refreshTwitterUserInfo(ctx context.Context, username string) error {
user, err := p.twitterClient.GetUserByUsername(ctx, username)
if err != nil {
return errors.Wrap(err, "error getting user info from twitter")
}
err = p.updateCachedTwitterUser(ctx, user, nil)
if err != nil {
return errors.Wrap(err, "error updating cached user state")
}
return nil
}
func (p *service) updateCachedTwitterUser(ctx context.Context, user *twitter_lib.User, newTipAccount *common.Account) error {
mu := p.userLocks.Get([]byte(user.Username))
mu.Lock()
defer mu.Unlock()
record, err := p.data.GetTwitterUser(ctx, user.Username)
switch err {
case twitter.ErrUserNotFound:
if newTipAccount == nil {
return errors.New("tip account must be present for newly registered twitter users")
}
record = &twitter.Record{
Username: user.Username,
}
fallthrough
case nil:
record.Name = user.Name
record.ProfilePicUrl = user.ProfileImageUrl
record.VerifiedType = toProtoVerifiedType(user.VerifiedType)
record.FollowerCount = uint32(user.PublicMetrics.FollowersCount)
if newTipAccount != nil {
record.TipAddress = newTipAccount.PublicKey().ToBase58()
}
default:
return errors.Wrap(err, "error getting cached twitter user")
}
err = p.data.SaveTwitterUser(ctx, record)
if err != nil {
return errors.Wrap(err, "error updating cached twitter user")
}
return nil
}
func findTipAccountRegisteredInTweet(tweet *twitter_lib.Tweet) (*common.Account, error) {
var depositAccount *common.Account
parts := strings.Fields(tweet.Text)
for _, part := range parts {
if !strings.HasPrefix(part, tipCardRegistrationPrefix) {
continue
}
part = part[len(tipCardRegistrationPrefix):]
part = strings.TrimSuffix(part, ".")
decoded, err := base58.Decode(part)
if err != nil {
return nil, errTwitterInvalidRegistrationValue
}
if len(decoded) != 32 {
return nil, errTwitterInvalidRegistrationValue
}
depositAccount, _ = common.NewAccountFromPublicKeyBytes(decoded)
return depositAccount, nil
}
return nil, errTwitterRegistrationNotFound
}
func toProtoVerifiedType(value string) userpb.GetTwitterUserResponse_VerifiedType {
switch value {
case "blue":
return userpb.GetTwitterUserResponse_BLUE
case "business":
return userpb.GetTwitterUserResponse_BUSINESS
case "government":
return userpb.GetTwitterUserResponse_GOVERNMENT
default:
return userpb.GetTwitterUserResponse_NONE
}
}