2024-09-17 06:50:05 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-09-22 06:04:56 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
2024-11-23 04:16:34 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-09-22 06:04:56 +00:00
|
|
|
"time"
|
2024-09-17 06:50:05 +00:00
|
|
|
|
2024-10-31 12:38:50 +00:00
|
|
|
localCache "git.solsynth.dev/hypernet/passport/pkg/internal/cache"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
|
2024-09-22 06:04:56 +00:00
|
|
|
"github.com/eko/gocache/lib/v4/cache"
|
|
|
|
"github.com/eko/gocache/lib/v4/marshaler"
|
|
|
|
"github.com/eko/gocache/lib/v4/store"
|
2024-09-17 06:50:05 +00:00
|
|
|
"github.com/samber/lo"
|
|
|
|
"gorm.io/datatypes"
|
|
|
|
)
|
|
|
|
|
2024-10-30 16:17:53 +00:00
|
|
|
func GetAuthPreference(account models.Account) (models.PreferenceAuth, error) {
|
2024-10-13 04:36:51 +00:00
|
|
|
var auth models.PreferenceAuth
|
2024-10-30 16:17:53 +00:00
|
|
|
if err := database.C.Where("account_id = ?", account.ID).First(&auth).Error; err != nil {
|
2024-10-13 04:36:51 +00:00
|
|
|
return auth, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return auth, nil
|
|
|
|
}
|
|
|
|
|
2024-10-30 16:17:53 +00:00
|
|
|
func UpdateAuthPreference(account models.Account, config models.AuthConfig) (models.PreferenceAuth, error) {
|
2024-10-13 04:36:51 +00:00
|
|
|
var auth models.PreferenceAuth
|
|
|
|
var err error
|
|
|
|
if auth, err = GetAuthPreference(account); err != nil {
|
|
|
|
auth = models.PreferenceAuth{
|
2024-10-30 16:17:53 +00:00
|
|
|
AccountID: account.ID,
|
2024-10-13 04:36:51 +00:00
|
|
|
Config: datatypes.NewJSONType(config),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auth.Config = datatypes.NewJSONType(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = database.C.Save(&auth).Error
|
|
|
|
return auth, err
|
|
|
|
}
|
|
|
|
|
2024-09-22 06:04:56 +00:00
|
|
|
func GetNotificationPreferenceCacheKey(accountId uint) string {
|
|
|
|
return fmt.Sprintf("notification-preference#%d", accountId)
|
|
|
|
}
|
|
|
|
|
2024-10-30 16:17:53 +00:00
|
|
|
func GetNotificationPreference(account models.Account) (models.PreferenceNotification, error) {
|
2024-09-17 06:50:05 +00:00
|
|
|
var notification models.PreferenceNotification
|
2024-09-22 06:04:56 +00:00
|
|
|
cacheManager := cache.New[any](localCache.S)
|
|
|
|
marshal := marshaler.New(cacheManager)
|
2024-12-08 12:21:40 +00:00
|
|
|
ctx := context.Background()
|
2024-09-22 06:04:56 +00:00
|
|
|
|
2024-12-08 12:21:40 +00:00
|
|
|
if val, err := marshal.Get(ctx, GetNotificationPreferenceCacheKey(account.ID), new(models.PreferenceNotification)); err == nil {
|
|
|
|
notification = *val.(*models.PreferenceNotification)
|
2024-09-22 06:04:56 +00:00
|
|
|
} else {
|
2024-10-30 16:17:53 +00:00
|
|
|
if err := database.C.Where("account_id = ?", account.ID).First(¬ification).Error; err != nil {
|
2024-09-22 06:04:56 +00:00
|
|
|
return notification, err
|
|
|
|
}
|
|
|
|
CacheNotificationPreference(notification)
|
2024-09-17 06:50:05 +00:00
|
|
|
}
|
2024-09-22 06:04:56 +00:00
|
|
|
|
2024-09-17 06:50:05 +00:00
|
|
|
return notification, nil
|
|
|
|
}
|
|
|
|
|
2024-09-22 06:04:56 +00:00
|
|
|
func CacheNotificationPreference(prefs models.PreferenceNotification) {
|
|
|
|
cacheManager := cache.New[any](localCache.S)
|
|
|
|
marshal := marshaler.New(cacheManager)
|
|
|
|
contx := context.Background()
|
|
|
|
|
2024-10-14 15:45:28 +00:00
|
|
|
_ = marshal.Set(
|
2024-09-22 06:04:56 +00:00
|
|
|
contx,
|
|
|
|
GetNotificationPreferenceCacheKey(prefs.AccountID),
|
|
|
|
prefs,
|
|
|
|
store.WithExpiration(60*time.Minute),
|
|
|
|
store.WithTags([]string{"notification-preference", fmt.Sprintf("user#%d", prefs.AccountID)}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-10-30 16:17:53 +00:00
|
|
|
func UpdateNotificationPreference(account models.Account, config map[string]bool) (models.PreferenceNotification, error) {
|
2024-09-17 06:50:05 +00:00
|
|
|
var notification models.PreferenceNotification
|
|
|
|
var err error
|
|
|
|
if notification, err = GetNotificationPreference(account); err != nil {
|
|
|
|
notification = models.PreferenceNotification{
|
2024-10-30 16:17:53 +00:00
|
|
|
AccountID: account.ID,
|
2024-10-14 15:45:28 +00:00
|
|
|
Config: lo.MapValues(config, func(v bool, k string) any { return v }),
|
2024-09-17 06:50:05 +00:00
|
|
|
}
|
2024-09-17 07:56:41 +00:00
|
|
|
} else {
|
2024-10-14 15:45:28 +00:00
|
|
|
notification.Config = lo.MapValues(config, func(v bool, k string) any { return v })
|
2024-09-17 06:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = database.C.Save(¬ification).Error
|
2024-09-22 06:04:56 +00:00
|
|
|
if err == nil {
|
|
|
|
CacheNotificationPreference(notification)
|
|
|
|
}
|
|
|
|
|
2024-09-17 06:50:05 +00:00
|
|
|
return notification, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckNotificationNotifiable(account models.Account, topic string) bool {
|
|
|
|
var notification models.PreferenceNotification
|
2024-09-22 06:04:56 +00:00
|
|
|
cacheManager := cache.New[any](localCache.S)
|
|
|
|
marshal := marshaler.New(cacheManager)
|
|
|
|
contx := context.Background()
|
|
|
|
|
|
|
|
if val, err := marshal.Get(contx, GetNotificationPreferenceCacheKey(account.ID), new(models.PreferenceNotification)); err == nil {
|
|
|
|
notification = val.(models.PreferenceNotification)
|
|
|
|
} else {
|
|
|
|
if err := database.C.Where("account_id = ?", account.ID).First(¬ification).Error; err != nil {
|
2024-10-14 15:45:28 +00:00
|
|
|
return true
|
2024-09-17 06:50:05 +00:00
|
|
|
}
|
2024-09-22 06:04:56 +00:00
|
|
|
CacheNotificationPreference(notification)
|
2024-09-17 06:50:05 +00:00
|
|
|
}
|
2024-09-22 06:04:56 +00:00
|
|
|
|
2024-09-17 06:50:05 +00:00
|
|
|
if val, ok := notification.Config[topic]; ok {
|
2024-09-20 13:55:25 +00:00
|
|
|
if status, ok := val.(bool); ok {
|
|
|
|
return status
|
2024-09-17 06:50:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckNotificationNotifiableBatch(accounts []models.Account, topic string) []bool {
|
2024-09-22 06:04:56 +00:00
|
|
|
cacheManager := cache.New[any](localCache.S)
|
|
|
|
marshal := marshaler.New(cacheManager)
|
2024-11-23 04:16:34 +00:00
|
|
|
ctx := context.Background()
|
2024-09-17 06:50:05 +00:00
|
|
|
|
2024-09-21 14:54:54 +00:00
|
|
|
var notifiable = make([]bool, len(accounts))
|
2024-10-14 15:45:28 +00:00
|
|
|
var queryNeededIdx []uint
|
2024-09-22 06:04:56 +00:00
|
|
|
notificationMap := make(map[uint]models.PreferenceNotification)
|
|
|
|
|
|
|
|
// Check cache for each account
|
2024-11-23 04:16:34 +00:00
|
|
|
for _, account := range accounts {
|
2024-09-22 06:04:56 +00:00
|
|
|
cacheKey := GetNotificationPreferenceCacheKey(account.ID)
|
2024-11-23 04:16:34 +00:00
|
|
|
if val, err := marshal.Get(ctx, cacheKey, new(models.PreferenceNotification)); err == nil {
|
|
|
|
notification := val.(*models.PreferenceNotification)
|
|
|
|
notificationMap[account.ID] = *notification
|
2024-09-22 06:04:56 +00:00
|
|
|
} else {
|
|
|
|
// Add to the list of accounts that need to be queried
|
|
|
|
queryNeededIdx = append(queryNeededIdx, account.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query the database for missing account IDs
|
|
|
|
if len(queryNeededIdx) > 0 {
|
|
|
|
var dbNotifications []models.PreferenceNotification
|
|
|
|
if err := database.C.Where("account_id IN ?", queryNeededIdx).Find(&dbNotifications).Error; err != nil {
|
|
|
|
// Handle error by returning false for accounts without cached notifications
|
|
|
|
return lo.Map(accounts, func(item models.Account, index int) bool {
|
2024-10-14 15:45:28 +00:00
|
|
|
return true
|
2024-09-22 06:04:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the newly fetched notifications and add to the notificationMap
|
|
|
|
for _, notification := range dbNotifications {
|
|
|
|
notificationMap[notification.AccountID] = notification
|
|
|
|
CacheNotificationPreference(notification) // Cache the result
|
|
|
|
}
|
2024-11-23 04:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Any("notifiable", notificationMap).Msg("Fetched notifiable status...")
|
2024-09-22 06:04:56 +00:00
|
|
|
|
2024-11-23 04:16:34 +00:00
|
|
|
// Process the notifiable status for the fetched notifications
|
|
|
|
for idx, account := range accounts {
|
|
|
|
if notification, exists := notificationMap[account.ID]; exists {
|
|
|
|
if val, ok := notification.Config[topic]; ok {
|
|
|
|
if status, ok := val.(bool); ok {
|
|
|
|
notifiable[idx] = status
|
|
|
|
continue
|
2024-09-22 06:04:56 +00:00
|
|
|
}
|
2024-09-17 06:50:05 +00:00
|
|
|
}
|
2024-11-23 04:16:34 +00:00
|
|
|
notifiable[idx] = true
|
2024-11-23 04:31:00 +00:00
|
|
|
} else {
|
|
|
|
notifiable[idx] = true
|
2024-09-17 06:50:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return notifiable
|
|
|
|
}
|