A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
fix(notification): use SSO provider for external_id lookup in notifications
Douglas Cortez42f8947e7c4b
diff --git a/internal/repo/user_external_login/user_external_login_repo.go b/internal/repo/user_external_login/user_external_login_repo.goindex c797e461..b5cf85e8 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, userfunc (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.goindex 2f83042b..085b3ec4 100644--- a/internal/service/notification/new_question_notification.go+++ b/internal/service/notification/new_question_notification.go@@ -238,14 +238,17 @@ func (ns *ExternalNotificationService) syncNewQuestionNotificationToPlugin(ctx c}}- userInfo, exist, err := ns.userExternalLoginRepo.GetByUserID(ctx, fn.Info().SlugName, subscriberUserID)+ externalLogins, err := ns.userExternalLoginRepo.GetUserExternalLoginList(ctx, subscriberUserID)if err != nil {- log.Errorf("get user external login info failed: %v", err)- return nil- }- if exist {- newMsg.ReceiverExternalID = userInfo.ExternalID+ 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)+ }}+fn.Notify(newMsg)}return nildiff --git a/internal/service/notification_common/notification.go b/internal/service/notification_common/notification.goindex 0bbd1865..5dbadcb9 100644--- a/internal/service/notification_common/notification.go+++ b/internal/service/notification_common/notification.go@@ -423,15 +423,14 @@ 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 {- log.Errorf("get user external login info failed: %v", err)- return nil- }- if exist {- pluginNotificationMsg.ReceiverExternalID = userInfo.ExternalID- }fn.Notify(pluginNotificationMsg)return nil})