reduce account updates

This commit is contained in:
Andre Medeiros 2022-07-13 17:28:46 -04:00
parent fa8b75d728
commit 3bdd348d50

View file

@ -165,9 +165,6 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) {
return return
} }
account.CheckCount++
newAccount := account.CheckCount == 1
defer func(acc *domain.Account) { defer func(acc *domain.Account) {
if err = nc.accountRepo.Update(nc, acc); err != nil { if err = nc.accountRepo.Update(nc, acc); err != nil {
nc.logger.Error("failed to update account", nc.logger.Error("failed to update account",
@ -212,6 +209,7 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) {
account.AccessToken = tokens.AccessToken account.AccessToken = tokens.AccessToken
account.RefreshToken = tokens.RefreshToken account.RefreshToken = tokens.RefreshToken
account.TokenExpiresAt = now.Add(tokens.Expiry) account.TokenExpiresAt = now.Add(tokens.Expiry)
_ = nc.accountRepo.Update(nc, &account)
// Refresh client // Refresh client
rac = nc.reddit.NewAuthenticatedClient(account.AccountID, tokens.RefreshToken, tokens.AccessToken) rac = nc.reddit.NewAuthenticatedClient(account.AccountID, tokens.RefreshToken, tokens.AccessToken)
@ -270,16 +268,20 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) {
for _, msg := range msgs.Children { for _, msg := range msgs.Children {
if !msg.IsDeleted() { if !msg.IsDeleted() {
account.LastMessageID = msg.FullName() account.LastMessageID = msg.FullName()
_ = nc.accountRepo.Update(nc, &account)
break break
} }
} }
// Let's populate this with the latest message so we don't flood users with stuff // Let's populate this with the latest message so we don't flood users with stuff
if newAccount { if account.CheckCount == 0 {
nc.logger.Debug("populating first message id to prevent spamming", nc.logger.Debug("populating first message id to prevent spamming",
zap.String("account#reddit_account_id", id), zap.String("account#reddit_account_id", id),
zap.String("account#username", account.NormalizedUsername()), zap.String("account#username", account.NormalizedUsername()),
) )
account.CheckCount = 1
_ = nc.accountRepo.Update(nc, &account)
return return
} }