Skip to content

Commit 42f8947

Browse files
IfDougelseSaLinkinStars
authored andcommitted
fix(notification): use SSO provider for external_id lookup in notifications
1 parent 61d9bf3 commit 42f8947

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

internal/repo/user_external_login/user_external_login_repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (ur *userExternalLoginRepo) GetByUserID(ctx context.Context, provider, user
8787
func (ur *userExternalLoginRepo) GetUserExternalLoginList(ctx context.Context, userID string) (
8888
resp []*entity.UserExternalLogin, err error) {
8989
resp = make([]*entity.UserExternalLogin, 0)
90-
err = ur.data.DB.Context(ctx).Where("user_id = ?", userID).Find(&resp)
90+
err = ur.data.DB.Context(ctx).Where("user_id = ?", userID).OrderBy("updated_at DESC").Find(&resp)
9191
if err != nil {
9292
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
9393
}

internal/service/notification/new_question_notification.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,17 @@ func (ns *ExternalNotificationService) syncNewQuestionNotificationToPlugin(ctx c
238238
}
239239
}
240240

241-
userInfo, exist, err := ns.userExternalLoginRepo.GetByUserID(ctx, fn.Info().SlugName, subscriberUserID)
241+
externalLogins, err := ns.userExternalLoginRepo.GetUserExternalLoginList(ctx, subscriberUserID)
242242
if err != nil {
243-
log.Errorf("get user external login info failed: %v", err)
244-
return nil
245-
}
246-
if exist {
247-
newMsg.ReceiverExternalID = userInfo.ExternalID
243+
log.Errorf("get user external login list failed for user %s: %v", subscriberUserID, err)
244+
} else if len(externalLogins) > 0 {
245+
newMsg.ReceiverExternalID = externalLogins[0].ExternalID
246+
if len(externalLogins) > 1 {
247+
log.Debugf("user %s has %d SSO logins, using most recent: provider=%s",
248+
subscriberUserID, len(externalLogins), externalLogins[0].Provider)
249+
}
248250
}
251+
249252
fn.Notify(newMsg)
250253
}
251254
return nil

internal/service/notification_common/notification.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -423,15 +423,14 @@ func (ns *NotificationCommon) syncNotificationToPlugin(ctx context.Context, objI
423423
}
424424
}
425425

426+
externalLogins, err := ns.userExternalLoginRepo.GetUserExternalLoginList(ctx, msg.ReceiverUserID)
427+
if err != nil {
428+
log.Errorf("get user external login list failed for user %s: %v", msg.ReceiverUserID, err)
429+
} else if len(externalLogins) > 0 {
430+
pluginNotificationMsg.ReceiverExternalID = externalLogins[0].ExternalID
431+
}
432+
426433
_ = plugin.CallNotification(func(fn plugin.Notification) error {
427-
userInfo, exist, err := ns.userExternalLoginRepo.GetByUserID(ctx, fn.Info().SlugName, msg.ReceiverUserID)
428-
if err != nil {
429-
log.Errorf("get user external login info failed: %v", err)
430-
return nil
431-
}
432-
if exist {
433-
pluginNotificationMsg.ReceiverExternalID = userInfo.ExternalID
434-
}
435434
fn.Notify(pluginNotificationMsg)
436435
return nil
437436
})

0 commit comments

Comments
 (0)