🐛 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,33 +74,27 @@ 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 ok := CheckNotificationNotifiable(notification.Account, notification.Topic); !ok { if len(skipNotifiableCheck) == 0 || !skipNotifiableCheck[0] {
log.Info().Str("topic", notification.Topic).Uint("uid", notification.AccountID).Msg("Notification dismissed by user...") if ok := CheckNotificationNotifiable(notification.Account, notification.Topic); !ok {
return nil 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) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
@ -158,22 +152,32 @@ 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
} }
notifiable := CheckNotificationNotifiableBatch(lo.Map(notifications, func(item models.Notification, index int) models.Account { var accountIdx []uint
return item.Account if len(skipNotifiableCheck) == 0 || !skipNotifiableCheck[0] {
}), notifications[0].Topic) notifiable := CheckNotificationNotifiableBatch(lo.Map(notifications, func(item models.Notification, index int) models.Account {
accountIdx := lo.Map( return item.Account
lo.Filter(notifications, func(item models.Notification, index int) bool { }), notifications[0].Topic)
return notifiable[index] accountIdx = lo.Map(
}), lo.Filter(notifications, func(item models.Notification, index int) bool {
func(item models.Notification, index int) uint { return notifiable[index]
return item.AccountID }),
}, func(item models.Notification, index int) uint {
) 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