Gracefully handle creating duplicate notifications

This commit is contained in:
Eli Ribble 2026-02-12 21:06:08 +00:00
parent 8e68230f4a
commit a82231859d
No known key found for this signature in database

View file

@ -6,11 +6,13 @@ import (
"strings"
"time"
"github.com/Gleipnir-Technology/bob/dialect/psql"
"github.com/Gleipnir-Technology/bob/dialect/psql/im"
"github.com/Gleipnir-Technology/nidus-sync/db"
enums "github.com/Gleipnir-Technology/nidus-sync/db/enums"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/Gleipnir-Technology/nidus-sync/debug"
"github.com/aarondl/opt/omit"
//"github.com/aarondl/opt/omit"
"github.com/aarondl/opt/omitnull"
"github.com/rs/zerolog/log"
)
@ -45,6 +47,22 @@ func ClearOauth(ctx context.Context, user *models.User) {
func NotifyOauthInvalid(ctx context.Context, user *models.User) {
msg := "Oauth token invalidated"
_, err := psql.Insert(
im.Into("notification", "created", "id", "link", "message", "resolved_at", "type", "user_id"),
im.Values(
psql.Arg(time.Now()),
psql.Raw("DEFAULT"),
psql.Arg(NotificationPathOauthReset),
psql.Arg(msg),
psql.Raw("NULL"),
psql.Arg(enums.NotificationtypeOauthTokenInvalidated),
psql.Arg(user.ID),
),
//im.OnConflict("user_id", "link").DoNothing(),
//im.OnConflictOnConstraint("unique_user_link_not_resolved").DoNothing(),
im.OnConflict("user_id", "link").Where("resolved_at IS NULL").DoNothing(),
).Exec(ctx, db.PGInstance.BobDB)
/*
notificationSetter := models.NotificationSetter{
Created: omit.From(time.Now()),
Message: omit.From(msg),
@ -52,8 +70,9 @@ func NotifyOauthInvalid(ctx context.Context, user *models.User) {
Type: omit.From(enums.NotificationtypeOauthTokenInvalidated),
}
err := user.InsertUserNotifications(ctx, db.PGInstance.BobDB, &notificationSetter)
*/
if err != nil {
if strings.HasPrefix(err.Error(), "ERROR: duplicate key value violates unique constraint") {
if strings.Contains(err.Error(), "ERROR: duplicate key value violates unique constraint") {
log.Info().Str("msg", msg).Int("user_id", int(user.ID)).Msg("Refusing to add another notification with the same type")
return
}