🐛 Fix notifiable check

This commit is contained in:
LittleSheep 2024-09-21 22:54:54 +08:00
parent 53c6b2a636
commit 9174767d80
2 changed files with 36 additions and 32 deletions

View File

@ -74,34 +74,28 @@ func NewNotificationBatch(notifications []models.Notification) error {
notifiable := CheckNotificationNotifiableBatch(lo.Map(notifications, func(item models.Notification, index int) models.Account {
return item.Account
}), notifications[0].Topic)
accountIdx := lo.Map(
lo.Filter(notifications, func(item models.Notification, index int) bool {
return notifiable[index]
}),
func(item models.Notification, index int) uint {
return item.AccountID
},
)
if len(accountIdx) == 0 {
return nil
}
notifications = lo.Filter(notifications, func(item models.Notification, index int) bool {
return notifiable[index]
})
if err := database.C.CreateInBatches(notifications, 1000).Error; err != nil {
return err
}
PushNotificationBatch(notifications)
PushNotificationBatch(notifications, true)
return nil
}
// PushNotification will push a notification to the user, via websocket, firebase, or APNs
// Please provide the notification with the account field is not empty
func PushNotification(notification models.Notification) error {
func PushNotification(notification models.Notification, skipNotifiableCheck ...bool) error {
if len(skipNotifiableCheck) == 0 || !skipNotifiableCheck[0] {
if ok := CheckNotificationNotifiable(notification.Account, notification.Topic); !ok {
log.Info().Str("topic", notification.Topic).Uint("uid", notification.AccountID).Msg("Notification dismissed by user...")
return nil
}
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
@ -158,15 +152,17 @@ func PushNotification(notification models.Notification) error {
return err
}
func PushNotificationBatch(notifications []models.Notification) {
func PushNotificationBatch(notifications []models.Notification, skipNotifiableCheck ...bool) {
if len(notifications) == 0 {
return
}
var accountIdx []uint
if len(skipNotifiableCheck) == 0 || !skipNotifiableCheck[0] {
notifiable := CheckNotificationNotifiableBatch(lo.Map(notifications, func(item models.Notification, index int) models.Account {
return item.Account
}), notifications[0].Topic)
accountIdx := lo.Map(
accountIdx = lo.Map(
lo.Filter(notifications, func(item models.Notification, index int) bool {
return notifiable[index]
}),
@ -174,6 +170,14 @@ func PushNotificationBatch(notifications []models.Notification) {
return item.AccountID
},
)
} else {
accountIdx = lo.Map(
notifications,
func(item models.Notification, index int) uint {
return item.AccountID
},
)
}
if len(accountIdx) == 0 {
return

View File

@ -62,15 +62,15 @@ func CheckNotificationNotifiableBatch(accounts []models.Account, topic string) [
})
}
var notifiable []bool
for _, notification := range notifications {
var notifiable = make([]bool, len(accounts))
for idx, notification := range notifications {
if val, ok := notification.Config[topic]; ok {
if status, ok := val.(bool); ok {
notifiable = append(notifiable, status)
notifiable[idx] = status
continue
}
}
notifiable = append(notifiable, true)
notifiable[idx] = true
}
return notifiable