Skip to content

Commit

Permalink
Improvements to Twitter registration DB transaction (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffyanta committed Apr 9, 2024
1 parent 617ac80 commit acbd122
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions pkg/code/async/user/twitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,34 +124,38 @@ func (p *service) processNewTwitterRegistrations(ctx context.Context) error {
}

// Save the updated tipping information
var isRegistered bool
err = p.data.ExecuteInTx(ctx, sql.LevelDefault, func(ctx context.Context) error {
var isNonceUsed bool
err = p.data.MarkTwitterNonceAsUsed(ctx, tweet.ID, *registrationNonce)
if err != nil {
return err
}

err = p.updateCachedTwitterUser(ctx, tweet.AdditionalMetadata.Author, tipAccount)
if err != nil {
switch err {
case nil:
case twitter.ErrDuplicateNonce:
isNonceUsed = true
default:
return err
}

err = p.data.MarkTweetAsProcessed(ctx, tweet.ID)
if err != nil {
return err
// Only save updated tip addresses when the nonce isn't used
if !isNonceUsed {
err = p.updateCachedTwitterUser(ctx, tweet.AdditionalMetadata.Author, tipAccount)
switch err {
case nil:
isRegistered = true
case twitter.ErrDuplicateTipAddress:
default:
return err
}
}

return nil
return p.data.MarkTweetAsProcessed(ctx, tweet.ID)
})
if err != nil {
return errors.Wrap(err, "error saving registration details")
}

switch err {
case nil:
if isRegistered {
go push_util.SendTwitterAccountConnectedPushNotification(ctx, p.data, p.pusher, tipAccount)
case twitter.ErrDuplicateTipAddress, twitter.ErrDuplicateNonce:
// Any race conditions with duplicate nonces or tip addresses will are ignored
//
// todo: In the future, support multiple tip address mappings
default:
return errors.Wrap(err, "error saving new registration")
}
}

Expand Down

0 comments on commit acbd122

Please sign in to comment.