2026-01-07 16:07:51 +00:00
|
|
|
package notification
|
2025-11-13 16:46:30 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2025-12-02 00:30:46 +00:00
|
|
|
"strings"
|
2025-11-13 16:46:30 +00:00
|
|
|
"time"
|
|
|
|
|
|
2025-11-24 18:08:24 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
|
|
|
|
enums "github.com/Gleipnir-Technology/nidus-sync/db/enums"
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
2026-01-06 14:46:31 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/debug"
|
2025-11-13 16:46:30 +00:00
|
|
|
"github.com/aarondl/opt/omit"
|
|
|
|
|
"github.com/aarondl/opt/omitnull"
|
2025-12-02 00:30:46 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2025-11-13 16:46:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
NotificationPathOauthReset string = "/oauth/refresh"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Notification struct {
|
|
|
|
|
Link string
|
|
|
|
|
Message string
|
|
|
|
|
Time time.Time
|
|
|
|
|
Type string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear all notifications for a given user with the given path
|
2026-01-07 16:07:51 +00:00
|
|
|
func ClearOauth(ctx context.Context, user *models.User) {
|
2025-11-13 16:46:30 +00:00
|
|
|
setter := models.NotificationSetter{
|
|
|
|
|
ResolvedAt: omitnull.From(time.Now()),
|
|
|
|
|
}
|
|
|
|
|
updater := models.Notifications.Update(
|
|
|
|
|
//models.SelectWhere.Notifications.Link.EQ(NotificationPathOauthReset),
|
|
|
|
|
models.UpdateWhere.Notifications.Link.EQ(NotificationPathOauthReset),
|
|
|
|
|
models.UpdateWhere.Notifications.UserID.EQ(user.ID),
|
|
|
|
|
setter.UpdateMod(),
|
|
|
|
|
)
|
2025-11-24 18:08:24 +00:00
|
|
|
updater.Exec(ctx, db.PGInstance.BobDB)
|
2025-11-13 16:46:30 +00:00
|
|
|
//user.UserNotifications(
|
|
|
|
|
//models.SelectWhere.Notifications.Link.EQ(NotificationPathOauthReset),
|
|
|
|
|
//).UpdateAll()
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 16:07:51 +00:00
|
|
|
func NotifyOauthInvalid(ctx context.Context, user *models.User) {
|
2025-12-02 00:30:46 +00:00
|
|
|
msg := "Oauth token invalidated"
|
2025-11-13 16:46:30 +00:00
|
|
|
notificationSetter := models.NotificationSetter{
|
|
|
|
|
Created: omit.From(time.Now()),
|
2025-12-02 00:30:46 +00:00
|
|
|
Message: omit.From(msg),
|
2025-11-13 16:46:30 +00:00
|
|
|
Link: omit.From(NotificationPathOauthReset),
|
|
|
|
|
Type: omit.From(enums.NotificationtypeOauthTokenInvalidated),
|
|
|
|
|
}
|
2025-11-24 18:08:24 +00:00
|
|
|
err := user.InsertUserNotifications(ctx, db.PGInstance.BobDB, ¬ificationSetter)
|
2025-11-13 16:46:30 +00:00
|
|
|
if err != nil {
|
2025-12-02 00:30:46 +00:00
|
|
|
if strings.HasPrefix(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
|
|
|
|
|
}
|
2026-01-06 14:46:31 +00:00
|
|
|
debug.LogErrorTypeInfo(err)
|
2025-12-02 00:30:46 +00:00
|
|
|
log.Error().Err(err).Msg("Failed to insert new notification. This is a programmer bug.")
|
2025-11-13 16:46:30 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 16:07:51 +00:00
|
|
|
func ForUser(ctx context.Context, u *models.User) ([]Notification, error) {
|
2025-11-13 16:46:30 +00:00
|
|
|
results := make([]Notification, 0)
|
|
|
|
|
notifications, err := u.UserNotifications(
|
|
|
|
|
models.SelectWhere.Notifications.ResolvedAt.IsNull(),
|
2025-11-24 18:08:24 +00:00
|
|
|
).All(ctx, db.PGInstance.BobDB)
|
2025-11-13 16:46:30 +00:00
|
|
|
if err != nil {
|
2025-11-13 20:53:20 +00:00
|
|
|
return results, fmt.Errorf("Failed to get notifications: %w", err)
|
2025-11-13 16:46:30 +00:00
|
|
|
}
|
|
|
|
|
for _, n := range notifications {
|
|
|
|
|
results = append(results, Notification{
|
|
|
|
|
Link: n.Link,
|
|
|
|
|
Message: n.Message,
|
|
|
|
|
Time: n.Created,
|
|
|
|
|
Type: notificationTypeName(n.Type),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return results, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func notificationTypeName(t enums.Notificationtype) string {
|
|
|
|
|
switch t {
|
|
|
|
|
case enums.NotificationtypeOauthTokenInvalidated:
|
|
|
|
|
return "alert"
|
|
|
|
|
default:
|
|
|
|
|
return "unknown-type"
|
|
|
|
|
}
|
|
|
|
|
}
|