🐛 Bug fixes in non-cached notification preferences

This commit is contained in:
LittleSheep 2024-10-14 23:45:28 +08:00
parent 8e3ce09734
commit be32ad1522
3 changed files with 11 additions and 19 deletions

7
.idea/workspace.xml generated
View File

@ -4,8 +4,9 @@
<option name="autoReloadType" value="ALL" /> <option name="autoReloadType" value="ALL" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":sparkles: More event logs point"> <list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":sparkles: New login alert">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/preferences.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/preferences.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/ticket.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/ticket.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/pkg/internal/services/ticket.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/ticket.go" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@ -153,7 +154,6 @@
</option> </option>
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<MESSAGE value=":bug: Fix path parameters misplaced" />
<MESSAGE value=":bug: Fix api key wasn't in auto maintain range" /> <MESSAGE value=":bug: Fix api key wasn't in auto maintain range" />
<MESSAGE value=":bug: Fix api key missing account id" /> <MESSAGE value=":bug: Fix api key missing account id" />
<MESSAGE value=":sparkles: Preload api key's ticket" /> <MESSAGE value=":sparkles: Preload api key's ticket" />
@ -178,7 +178,8 @@
<MESSAGE value=":bug: Bug fixes on settings auth preferences" /> <MESSAGE value=":bug: Bug fixes on settings auth preferences" />
<MESSAGE value=":sparkles: Implement event recorder grpc" /> <MESSAGE value=":sparkles: Implement event recorder grpc" />
<MESSAGE value=":sparkles: More event logs point" /> <MESSAGE value=":sparkles: More event logs point" />
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: More event logs point" /> <MESSAGE value=":sparkles: New login alert" />
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: New login alert" />
</component> </component>
<component name="VgoProject"> <component name="VgoProject">
<settings-migrated>true</settings-migrated> <settings-migrated>true</settings-migrated>

View File

@ -2,7 +2,6 @@ package services
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"time" "time"
@ -14,7 +13,6 @@ import (
"github.com/eko/gocache/lib/v4/store" "github.com/eko/gocache/lib/v4/store"
"github.com/samber/lo" "github.com/samber/lo"
"gorm.io/datatypes" "gorm.io/datatypes"
"gorm.io/gorm"
) )
func GetAuthPreference(account models.Account) (models.PreferenceAuth, error) { func GetAuthPreference(account models.Account) (models.PreferenceAuth, error) {
@ -69,7 +67,7 @@ func CacheNotificationPreference(prefs models.PreferenceNotification) {
marshal := marshaler.New(cacheManager) marshal := marshaler.New(cacheManager)
contx := context.Background() contx := context.Background()
marshal.Set( _ = marshal.Set(
contx, contx,
GetNotificationPreferenceCacheKey(prefs.AccountID), GetNotificationPreferenceCacheKey(prefs.AccountID),
prefs, prefs,
@ -84,14 +82,10 @@ func UpdateNotificationPreference(account models.Account, config map[string]bool
if notification, err = GetNotificationPreference(account); err != nil { if notification, err = GetNotificationPreference(account); err != nil {
notification = models.PreferenceNotification{ notification = models.PreferenceNotification{
AccountID: account.ID, AccountID: account.ID,
Config: datatypes.JSONMap( Config: lo.MapValues(config, func(v bool, k string) any { return v }),
lo.MapValues(config, func(v bool, k string) any { return v }),
),
} }
} else { } else {
notification.Config = datatypes.JSONMap( notification.Config = lo.MapValues(config, func(v bool, k string) any { return v })
lo.MapValues(config, func(v bool, k string) any { return v }),
)
} }
err = database.C.Save(&notification).Error err = database.C.Save(&notification).Error
@ -112,10 +106,7 @@ func CheckNotificationNotifiable(account models.Account, topic string) bool {
notification = val.(models.PreferenceNotification) notification = val.(models.PreferenceNotification)
} else { } else {
if err := database.C.Where("account_id = ?", account.ID).First(&notification).Error; err != nil { if err := database.C.Where("account_id = ?", account.ID).First(&notification).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { return true
return true
}
return false
} }
CacheNotificationPreference(notification) CacheNotificationPreference(notification)
} }
@ -134,7 +125,7 @@ func CheckNotificationNotifiableBatch(accounts []models.Account, topic string) [
contx := context.Background() contx := context.Background()
var notifiable = make([]bool, len(accounts)) var notifiable = make([]bool, len(accounts))
queryNeededIdx := []uint{} var queryNeededIdx []uint
notificationMap := make(map[uint]models.PreferenceNotification) notificationMap := make(map[uint]models.PreferenceNotification)
// Check cache for each account // Check cache for each account
@ -163,7 +154,7 @@ func CheckNotificationNotifiableBatch(accounts []models.Account, topic string) [
if err := database.C.Where("account_id IN ?", queryNeededIdx).Find(&dbNotifications).Error; err != nil { if err := database.C.Where("account_id IN ?", queryNeededIdx).Find(&dbNotifications).Error; err != nil {
// Handle error by returning false for accounts without cached notifications // Handle error by returning false for accounts without cached notifications
return lo.Map(accounts, func(item models.Account, index int) bool { return lo.Map(accounts, func(item models.Account, index int) bool {
return false return true
}) })
} }

View File

@ -26,7 +26,7 @@ func DetectRisk(user models.Account, ip, ua string) int {
Model(models.AuthTicket{}). Model(models.AuthTicket{}).
Count(&clue).Error; err == nil { Count(&clue).Error; err == nil {
if clue >= 1 { if clue >= 1 {
return 3 return 1
} }
} }