populate account last message ID on upsert

This commit is contained in:
Andre Medeiros 2022-11-05 15:53:35 -04:00
parent ea45065dac
commit 735dc558df
2 changed files with 24 additions and 13 deletions

View file

@ -177,6 +177,16 @@ func (a *api) upsertAccountsHandler(w http.ResponseWriter, r *http.Request) {
// Set account ID from Reddit // Set account ID from Reddit
acc.AccountID = me.ID acc.AccountID = me.ID
mi, err := rac.MessageInbox(ctx)
if err != nil {
a.errorResponse(w, r, 500, err)
return
}
if mi.Count > 0 {
acc.LastMessageID = mi.Children[0].FullName()
}
if err := a.accountRepo.CreateOrUpdate(ctx, &acc); err != nil { if err := a.accountRepo.CreateOrUpdate(ctx, &acc); err != nil {
a.errorResponse(w, r, 422, err) a.errorResponse(w, r, 422, err)
return return
@ -210,8 +220,8 @@ func (a *api) upsertAccountHandler(w http.ResponseWriter, r *http.Request) {
} }
// Here we check whether the account is supplied with a valid token. // Here we check whether the account is supplied with a valid token.
ac := a.reddit.NewAuthenticatedClient(reddit.SkipRateLimiting, acct.RefreshToken, acct.AccessToken) rac := a.reddit.NewAuthenticatedClient(reddit.SkipRateLimiting, acct.RefreshToken, acct.AccessToken)
tokens, err := ac.RefreshTokens(ctx) tokens, err := rac.RefreshTokens(ctx)
if err != nil { if err != nil {
a.logger.Error("failed to refresh token", zap.Error(err)) a.logger.Error("failed to refresh token", zap.Error(err))
a.errorResponse(w, r, 422, err) a.errorResponse(w, r, 422, err)
@ -223,8 +233,8 @@ func (a *api) upsertAccountHandler(w http.ResponseWriter, r *http.Request) {
acct.RefreshToken = tokens.RefreshToken acct.RefreshToken = tokens.RefreshToken
acct.AccessToken = tokens.AccessToken acct.AccessToken = tokens.AccessToken
ac = a.reddit.NewAuthenticatedClient(reddit.SkipRateLimiting, acct.RefreshToken, acct.AccessToken) rac = a.reddit.NewAuthenticatedClient(reddit.SkipRateLimiting, acct.RefreshToken, acct.AccessToken)
me, err := ac.Me(ctx) me, err := rac.Me(ctx)
if err != nil { if err != nil {
a.logger.Error("failed to grab user details from reddit", zap.Error(err)) a.logger.Error("failed to grab user details from reddit", zap.Error(err))
@ -242,6 +252,16 @@ func (a *api) upsertAccountHandler(w http.ResponseWriter, r *http.Request) {
// Set account ID from Reddit // Set account ID from Reddit
acct.AccountID = me.ID acct.AccountID = me.ID
mi, err := rac.MessageInbox(ctx)
if err != nil {
a.errorResponse(w, r, 500, err)
return
}
if mi.Count > 0 {
acct.LastMessageID = mi.Children[0].FullName()
}
// Associate // Associate
dev, err := a.deviceRepo.GetByAPNSToken(ctx, vars["apns"]) dev, err := a.deviceRepo.GetByAPNSToken(ctx, vars["apns"])
if err != nil { if err != nil {

View file

@ -263,15 +263,6 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) {
} }
} }
// Let's populate this with the latest message so we don't flood users with stuff
if account.CheckCount == 0 {
logger.Debug("populating first message id to prevent spamming")
account.CheckCount = 1
_ = nc.accountRepo.Update(ctx, &account)
return
}
devices, err := nc.deviceRepo.GetInboxNotifiableByAccountID(ctx, account.ID) devices, err := nc.deviceRepo.GetInboxNotifiableByAccountID(ctx, account.ID)
if err != nil { if err != nil {
logger.Error("failed to fetch account devices", zap.Error(err)) logger.Error("failed to fetch account devices", zap.Error(err))