🐛 Bug fixes in non-cached notification preferences
This commit is contained in:
		
							
								
								
									
										7
									
								
								.idea/workspace.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										7
									
								
								.idea/workspace.xml
									
									
									
										generated
									
									
									
								
							| @@ -4,8 +4,9 @@ | ||||
|     <option name="autoReloadType" value="ALL" /> | ||||
|   </component> | ||||
|   <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$/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" /> | ||||
|     </list> | ||||
|     <option name="SHOW_DIALOG" value="false" /> | ||||
| @@ -153,7 +154,6 @@ | ||||
|     </option> | ||||
|   </component> | ||||
|   <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 missing account id" /> | ||||
|     <MESSAGE value=":sparkles: Preload api key's ticket" /> | ||||
| @@ -178,7 +178,8 @@ | ||||
|     <MESSAGE value=":bug: Bug fixes on settings auth preferences" /> | ||||
|     <MESSAGE value=":sparkles: Implement event recorder grpc" /> | ||||
|     <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 name="VgoProject"> | ||||
|     <settings-migrated>true</settings-migrated> | ||||
|   | ||||
| @@ -2,7 +2,6 @@ package services | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"time" | ||||
|  | ||||
| @@ -14,7 +13,6 @@ import ( | ||||
| 	"github.com/eko/gocache/lib/v4/store" | ||||
| 	"github.com/samber/lo" | ||||
| 	"gorm.io/datatypes" | ||||
| 	"gorm.io/gorm" | ||||
| ) | ||||
|  | ||||
| func GetAuthPreference(account models.Account) (models.PreferenceAuth, error) { | ||||
| @@ -69,7 +67,7 @@ func CacheNotificationPreference(prefs models.PreferenceNotification) { | ||||
| 	marshal := marshaler.New(cacheManager) | ||||
| 	contx := context.Background() | ||||
|  | ||||
| 	marshal.Set( | ||||
| 	_ = marshal.Set( | ||||
| 		contx, | ||||
| 		GetNotificationPreferenceCacheKey(prefs.AccountID), | ||||
| 		prefs, | ||||
| @@ -84,14 +82,10 @@ func UpdateNotificationPreference(account models.Account, config map[string]bool | ||||
| 	if notification, err = GetNotificationPreference(account); err != nil { | ||||
| 		notification = models.PreferenceNotification{ | ||||
| 			AccountID: account.ID, | ||||
| 			Config: datatypes.JSONMap( | ||||
| 				lo.MapValues(config, func(v bool, k string) any { return v }), | ||||
| 			), | ||||
| 			Config:    lo.MapValues(config, func(v bool, k string) any { return v }), | ||||
| 		} | ||||
| 	} else { | ||||
| 		notification.Config = datatypes.JSONMap( | ||||
| 			lo.MapValues(config, func(v bool, k string) any { return v }), | ||||
| 		) | ||||
| 		notification.Config = lo.MapValues(config, func(v bool, k string) any { return v }) | ||||
| 	} | ||||
|  | ||||
| 	err = database.C.Save(¬ification).Error | ||||
| @@ -112,11 +106,8 @@ func CheckNotificationNotifiable(account models.Account, topic string) bool { | ||||
| 		notification = val.(models.PreferenceNotification) | ||||
| 	} else { | ||||
| 		if err := database.C.Where("account_id = ?", account.ID).First(¬ification).Error; err != nil { | ||||
| 			if errors.Is(err, gorm.ErrRecordNotFound) { | ||||
| 			return true | ||||
| 		} | ||||
| 			return false | ||||
| 		} | ||||
| 		CacheNotificationPreference(notification) | ||||
| 	} | ||||
|  | ||||
| @@ -134,7 +125,7 @@ func CheckNotificationNotifiableBatch(accounts []models.Account, topic string) [ | ||||
| 	contx := context.Background() | ||||
|  | ||||
| 	var notifiable = make([]bool, len(accounts)) | ||||
| 	queryNeededIdx := []uint{} | ||||
| 	var queryNeededIdx []uint | ||||
| 	notificationMap := make(map[uint]models.PreferenceNotification) | ||||
|  | ||||
| 	// 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 { | ||||
| 			// Handle error by returning false for accounts without cached notifications | ||||
| 			return lo.Map(accounts, func(item models.Account, index int) bool { | ||||
| 				return false | ||||
| 				return true | ||||
| 			}) | ||||
| 		} | ||||
|  | ||||
|   | ||||
| @@ -26,7 +26,7 @@ func DetectRisk(user models.Account, ip, ua string) int { | ||||
| 		Model(models.AuthTicket{}). | ||||
| 		Count(&clue).Error; err == nil { | ||||
| 		if clue >= 1 { | ||||
| 			return 3 | ||||
| 			return 1 | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user