🐛 Fix notification push batch emitted twice

This commit is contained in:
LittleSheep 2024-11-23 12:43:09 +08:00
parent 3f4fe68105
commit ca9bd7ac14
2 changed files with 19 additions and 21 deletions

13
.idea/workspace.xml generated
View File

@ -4,9 +4,9 @@
<option name="autoReloadType" value="ALL" />
</component>
<component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":loud_sound: Verbose notifying check logging">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Fix notifiable is empty when user do not set">
<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/notifications.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/notifications.go" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -159,7 +159,6 @@
</component>
<component name="VcsManagerConfiguration">
<option name="CHECK_CODE_SMELLS_BEFORE_PROJECT_COMMIT" value="false" />
<MESSAGE value=":recycle: Refactored more modules into nexus" />
<MESSAGE value=":recycle: All parts into nexus" />
<MESSAGE value=":truck: Move http server package" />
<MESSAGE value=":recycle: Move models.Account to sec.UserInfo" />
@ -184,7 +183,8 @@
<MESSAGE value=":boom: Remove deprecated subscription API" />
<MESSAGE value=":loud_sound: Verbose notifying logging" />
<MESSAGE value=":loud_sound: Verbose notifying check logging" />
<option name="LAST_COMMIT_MESSAGE" value=":loud_sound: Verbose notifying check logging" />
<MESSAGE value=":bug: Fix notifiable is empty when user do not set" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Fix notifiable is empty when user do not set" />
<option name="GROUP_MULTIFILE_MERGE_BY_DIRECTORY" value="true" />
</component>
<component name="VgoProject">
@ -208,11 +208,6 @@
<line>73</line>
<option name="timeStamp" value="4" />
</line-breakpoint>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/pkg/internal/services/notifications.go</url>
<line>135</line>
<option name="timeStamp" value="5" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
</component>

View File

@ -145,6 +145,9 @@ func PushNotification(notification models.Notification, skipNotifiableCheck ...b
return err
}
// PushNotificationBatch will push a notification to the user
// The notification should be the same for all users except the account id field
// For the notification push, the method will only use the first notification as template
func PushNotificationBatch(notifications []models.Notification, skipNotifiableCheck ...bool) {
if len(notifications) == 0 {
return
@ -179,8 +182,12 @@ func PushNotificationBatch(notifications []models.Notification, skipNotifiableCh
}
var subscribers []models.NotificationSubscriber
database.C.Where("account_id IN ?", accountIdx).Find(&subscribers)
if err := database.C.Where("account_id IN ?", accountIdx).Find(&subscribers).Error; err != nil {
log.Error().Err(err).Msg("Failed to fetch subscribers, unable to push notifications")
}
var providers []string
var tokens []string
stream := proto.NewStreamServiceClient(gap.Nx.GetNexusGrpcConn())
for _, notification := range notifications {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
@ -198,23 +205,19 @@ func PushNotificationBatch(notifications []models.Notification, skipNotifiableCh
continue
}
var providers []string
var tokens []string
for _, subscriber := range lo.Filter(subscribers, func(item models.NotificationSubscriber, index int) bool {
return item.AccountID == notification.AccountID
}) {
providers = append(providers, subscriber.Provider)
tokens = append(tokens, subscriber.DeviceToken)
}
}
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
if err := gap.Px.PushNotifyBatch(pushkit.NotificationPushBatchRequest{
Providers: providers,
Tokens: tokens,
Notification: notification.EncodeToPushkit(),
Notification: notifications[0].EncodeToPushkit(),
}); err != nil {
log.Warn().Err(err).Str("topic", notification.Topic).Msg("Failed to push notification to Pusher")
}
cancel()
log.Warn().Err(err).Str("topic", notifications[0].Topic).Msg("Failed to push notification to Pusher")
}
}