🐛 Trying to prevent send same notification to the same user in batch

This commit is contained in:
LittleSheep 2024-10-13 12:46:14 +08:00
parent 39c3799d82
commit 821e0c3e60
2 changed files with 23 additions and 10 deletions

14
.idea/workspace.xml generated
View File

@ -4,15 +4,9 @@
<option name="autoReloadType" value="ALL" />
</component>
<component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":sparkles: Auth config to limit auth steps">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":recycle: Single table to store auth preferences">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/database/migrator.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/database/migrator.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/models/accounts.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/models/accounts.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/models/preferences.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/models/preferences.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/index.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/index.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/preferences_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/preferences_api.go" 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/ticket.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/ticket.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/grpc/notifier.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/grpc/notifier.go" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -159,7 +153,6 @@
</option>
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value=":sparkles: Bots aka. automated accounts" />
<MESSAGE value=":sparkles: Return affiliated to and automated by in userinfo grpc call" />
<MESSAGE value=":sparkles: Pagination bots api" />
<MESSAGE value=":bug: Fix query issue" />
@ -184,7 +177,8 @@
<MESSAGE value=":sparkles: Realm avatar, banner and access policy" />
<MESSAGE value=":sparkles: Account deletion" />
<MESSAGE value=":sparkles: Auth config to limit auth steps" />
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: Auth config to limit auth steps" />
<MESSAGE value=":recycle: Single table to store auth preferences" />
<option name="LAST_COMMIT_MESSAGE" value=":recycle: Single table to store auth preferences" />
</component>
<component name="VgoProject">
<settings-migrated>true</settings-migrated>

View File

@ -3,6 +3,7 @@ package grpc
import (
"context"
"fmt"
"github.com/rs/zerolog/log"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
jsoniter "github.com/json-iterator/go"
@ -37,6 +38,8 @@ func (v *Server) NotifyUser(_ context.Context, in *proto.NotifyUserRequest) (*pr
AccountID: user.ID,
}
log.Debug().Str("topic", notification.Topic).Uint("uid", notification.AccountID).Msg("Notifying user...")
if notification.IsRealtime {
if err := services.PushNotification(notification); err != nil {
return nil, err
@ -64,8 +67,13 @@ func (v *Server) NotifyUserBatch(_ context.Context, in *proto.NotifyUserBatchReq
var metadata map[string]any
_ = jsoniter.Unmarshal(in.GetNotify().GetMetadata(), &metadata)
var checklist = make(map[uint]bool, len(users))
var notifications []models.Notification
for _, user := range users {
if checklist[user.ID] {
continue
}
notification := models.Notification{
Topic: in.GetNotify().GetTopic(),
Title: in.GetNotify().GetTitle(),
@ -79,10 +87,13 @@ func (v *Server) NotifyUserBatch(_ context.Context, in *proto.NotifyUserBatchReq
Account: user,
AccountID: user.ID,
}
checklist[user.ID] = true
notifications = append(notifications, notification)
}
log.Debug().Str("topic", notifications[0].Topic).Any("uid", lo.Keys(checklist)).Msg("Notifying users...")
if in.GetNotify().GetIsRealtime() {
services.PushNotificationBatch(notifications)
} else {
@ -105,8 +116,13 @@ func (v *Server) NotifyAllUser(_ context.Context, in *proto.NotifyRequest) (*pro
var metadata map[string]any
_ = jsoniter.Unmarshal(in.GetMetadata(), &metadata)
var checklist = make(map[uint]bool, len(users))
var notifications []models.Notification
for _, user := range users {
if checklist[user.ID] {
continue
}
notification := models.Notification{
Topic: in.GetTopic(),
Title: in.GetTitle(),
@ -120,10 +136,13 @@ func (v *Server) NotifyAllUser(_ context.Context, in *proto.NotifyRequest) (*pro
Account: user,
AccountID: user.ID,
}
checklist[user.ID] = true
notifications = append(notifications, notification)
}
log.Debug().Str("topic", notifications[0].Topic).Any("uid", lo.Keys(checklist)).Msg("Notifying users...")
if in.GetIsRealtime() {
services.PushNotificationBatch(notifications)
} else {