🐛 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" /> <option name="autoReloadType" value="ALL" />
</component> </component>
<component name="ChangeListManager"> <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$/.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> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -159,7 +159,6 @@
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<option name="CHECK_CODE_SMELLS_BEFORE_PROJECT_COMMIT" value="false" /> <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=":recycle: All parts into nexus" />
<MESSAGE value=":truck: Move http server package" /> <MESSAGE value=":truck: Move http server package" />
<MESSAGE value=":recycle: Move models.Account to sec.UserInfo" /> <MESSAGE value=":recycle: Move models.Account to sec.UserInfo" />
@ -184,7 +183,8 @@
<MESSAGE value=":boom: Remove deprecated subscription API" /> <MESSAGE value=":boom: Remove deprecated subscription API" />
<MESSAGE value=":loud_sound: Verbose notifying logging" /> <MESSAGE value=":loud_sound: Verbose notifying logging" />
<MESSAGE value=":loud_sound: Verbose notifying check 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" /> <option name="GROUP_MULTIFILE_MERGE_BY_DIRECTORY" value="true" />
</component> </component>
<component name="VgoProject"> <component name="VgoProject">
@ -208,11 +208,6 @@
<line>73</line> <line>73</line>
<option name="timeStamp" value="4" /> <option name="timeStamp" value="4" />
</line-breakpoint> </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> </breakpoints>
</breakpoint-manager> </breakpoint-manager>
</component> </component>

View File

@ -145,6 +145,9 @@ func PushNotification(notification models.Notification, skipNotifiableCheck ...b
return err 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) { func PushNotificationBatch(notifications []models.Notification, skipNotifiableCheck ...bool) {
if len(notifications) == 0 { if len(notifications) == 0 {
return return
@ -179,8 +182,12 @@ func PushNotificationBatch(notifications []models.Notification, skipNotifiableCh
} }
var subscribers []models.NotificationSubscriber 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()) stream := proto.NewStreamServiceClient(gap.Nx.GetNexusGrpcConn())
for _, notification := range notifications { for _, notification := range notifications {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
@ -198,23 +205,19 @@ func PushNotificationBatch(notifications []models.Notification, skipNotifiableCh
continue continue
} }
var providers []string
var tokens []string
for _, subscriber := range lo.Filter(subscribers, func(item models.NotificationSubscriber, index int) bool { for _, subscriber := range lo.Filter(subscribers, func(item models.NotificationSubscriber, index int) bool {
return item.AccountID == notification.AccountID return item.AccountID == notification.AccountID
}) { }) {
providers = append(providers, subscriber.Provider) providers = append(providers, subscriber.Provider)
tokens = append(tokens, subscriber.DeviceToken) tokens = append(tokens, subscriber.DeviceToken)
} }
}
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second) if err := gap.Px.PushNotifyBatch(pushkit.NotificationPushBatchRequest{
if err := gap.Px.PushNotifyBatch(pushkit.NotificationPushBatchRequest{ Providers: providers,
Providers: providers, Tokens: tokens,
Tokens: tokens, Notification: notifications[0].EncodeToPushkit(),
Notification: notification.EncodeToPushkit(), }); err != nil {
}); err != nil { log.Warn().Err(err).Str("topic", notifications[0].Topic).Msg("Failed to push notification to Pusher")
log.Warn().Err(err).Str("topic", notification.Topic).Msg("Failed to push notification to Pusher")
}
cancel()
} }
} }