Make it possible to resolve notifications
This commit is contained in:
parent
20186f65bf
commit
fc40309dd0
8 changed files with 266 additions and 96 deletions
83
notification.go
Normal file
83
notification.go
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
enums "github.com/Gleipnir-Technology/nidus-sync/enums"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/models"
|
||||
"github.com/aarondl/opt/omit"
|
||||
"github.com/aarondl/opt/omitnull"
|
||||
)
|
||||
|
||||
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
|
||||
func clearNotificationsOauth(ctx context.Context, user *models.User) {
|
||||
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(),
|
||||
)
|
||||
updater.Exec(ctx, PGInstance.BobDB)
|
||||
//user.UserNotifications(
|
||||
//models.SelectWhere.Notifications.Link.EQ(NotificationPathOauthReset),
|
||||
//).UpdateAll()
|
||||
}
|
||||
|
||||
func notifyOauthInvalid(ctx context.Context, user *models.User) {
|
||||
notificationSetter := models.NotificationSetter{
|
||||
Created: omit.From(time.Now()),
|
||||
Message: omit.From("Oauth token invalidated"),
|
||||
Link: omit.From(NotificationPathOauthReset),
|
||||
Type: omit.From(enums.NotificationtypeOauthTokenInvalidated),
|
||||
}
|
||||
err := user.InsertUserNotifications(ctx, PGInstance.BobDB, ¬ificationSetter)
|
||||
if err != nil {
|
||||
slog.Error("Failed to get oauth user", slog.String("err", err.Error()))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func notificationsForUser(ctx context.Context, u *models.User) ([]Notification, error) {
|
||||
results := make([]Notification, 0)
|
||||
notifications, err := u.UserNotifications(
|
||||
models.SelectWhere.Notifications.ResolvedAt.IsNull(),
|
||||
).All(ctx, PGInstance.BobDB)
|
||||
if err != nil {
|
||||
return results, fmt.Errorf("Failed to get notifications: %v", err)
|
||||
}
|
||||
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"
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue