diff --git a/internal/repo/user_external_login/user_external_login_repo.go b/internal/repo/user_external_login/user_external_login_repo.go index c797e461d..b5cf85e86 100644 --- a/internal/repo/user_external_login/user_external_login_repo.go +++ b/internal/repo/user_external_login/user_external_login_repo.go @@ -87,7 +87,7 @@ func (ur *userExternalLoginRepo) GetByUserID(ctx context.Context, provider, user func (ur *userExternalLoginRepo) GetUserExternalLoginList(ctx context.Context, userID string) ( resp []*entity.UserExternalLogin, err error) { resp = make([]*entity.UserExternalLogin, 0) - err = ur.data.DB.Context(ctx).Where("user_id = ?", userID).Find(&resp) + err = ur.data.DB.Context(ctx).Where("user_id = ?", userID).OrderBy("updated_at DESC").Find(&resp) if err != nil { err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() } diff --git a/internal/service/notification/new_question_notification.go b/internal/service/notification/new_question_notification.go index 2f83042b2..0a5471873 100644 --- a/internal/service/notification/new_question_notification.go +++ b/internal/service/notification/new_question_notification.go @@ -238,6 +238,19 @@ func (ns *ExternalNotificationService) syncNewQuestionNotificationToPlugin(ctx c } } + // Get all external logins as fallback + externalLogins, err := ns.userExternalLoginRepo.GetUserExternalLoginList(ctx, subscriberUserID) + if err != nil { + log.Errorf("get user external login list failed for user %s: %v", subscriberUserID, err) + } else if len(externalLogins) > 0 { + newMsg.ReceiverExternalID = externalLogins[0].ExternalID + if len(externalLogins) > 1 { + log.Debugf("user %s has %d SSO logins, using most recent: provider=%s", + subscriberUserID, len(externalLogins), externalLogins[0].Provider) + } + } + + // Try to get external login specific to this plugin (takes precedence over fallback) userInfo, exist, err := ns.userExternalLoginRepo.GetByUserID(ctx, fn.Info().SlugName, subscriberUserID) if err != nil { log.Errorf("get user external login info failed: %v", err) diff --git a/internal/service/notification_common/notification.go b/internal/service/notification_common/notification.go index 0bbd1865f..94ce86b16 100644 --- a/internal/service/notification_common/notification.go +++ b/internal/service/notification_common/notification.go @@ -423,6 +423,13 @@ func (ns *NotificationCommon) syncNotificationToPlugin(ctx context.Context, objI } } + externalLogins, err := ns.userExternalLoginRepo.GetUserExternalLoginList(ctx, msg.ReceiverUserID) + if err != nil { + log.Errorf("get user external login list failed for user %s: %v", msg.ReceiverUserID, err) + } else if len(externalLogins) > 0 { + pluginNotificationMsg.ReceiverExternalID = externalLogins[0].ExternalID + } + _ = plugin.CallNotification(func(fn plugin.Notification) error { userInfo, exist, err := ns.userExternalLoginRepo.GetByUserID(ctx, fn.Info().SlugName, msg.ReceiverUserID) if err != nil {