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

View File

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