From 62d0309c810ad8a47f3c577b98b5a3a0e8576a55 Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Thu, 29 Feb 2024 19:19:56 +0800 Subject: [PATCH] Do not sync users when there is no need (#512) --- server/plugin.go | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/server/plugin.go b/server/plugin.go index cf71169f0..b20c85040 100644 --- a/server/plugin.go +++ b/server/plugin.go @@ -694,7 +694,7 @@ func (p *Plugin) syncUsers() { if msUser.Type == msteamsUserTypeGuest { // Check if syncing of MS Teams guest users is disabled. if !syncGuestUsers { - if isUserPresent && isRemoteUser(mmUser) { + if isUserPresent && isRemoteUser(mmUser) && mmUser.DeleteAt == 0 { // Deactivate the Mattermost user corresponding to the MS Teams guest user. p.API.LogInfo("Deactivating the guest user account", "user_id", mmUser.Id, "teams_user_id", msUser.ID) if err := p.API.UpdateUserActive(mmUser.Id, false); err != nil { @@ -767,23 +767,34 @@ func (p *Plugin) syncUsers() { if err = p.store.SetUserInfo(newUser.Id, msUser.ID, nil); err != nil { p.API.LogWarn("Unable to set user info during sync user job", "user_id", newUser.Id, "teams_user_id", msUser.ID, "error", err.Error()) } - } else if (username != mmUser.Username || msUser.DisplayName != mmUser.FirstName) && mmUser.RemoteId != nil { - mmUser.Username = username - mmUser.FirstName = msUser.DisplayName - for { - _, err := p.API.UpdateUser(mmUser) - if err != nil { - if err.Id == "app.user.save.username_exists.app_error" { - mmUser.Username += "-" + fmt.Sprint(userSuffixID) - userSuffixID++ - continue + } else if mmUser.RemoteId != nil { + shouldUpdate := false + if !strings.HasPrefix(mmUser.Username, "msteams_") && username != mmUser.Username { + mmUser.Username = username + shouldUpdate = true + } + + if mmUser.FirstName != msUser.DisplayName { + mmUser.FirstName = msUser.DisplayName + shouldUpdate = true + } + + if shouldUpdate { + for { + _, err := p.API.UpdateUser(mmUser) + if err != nil { + if err.Id == "app.user.save.username_exists.app_error" { + mmUser.Username = username + "-" + fmt.Sprint(userSuffixID) + userSuffixID++ + continue + } + + p.API.LogWarn("Unable to update user during sync user job", "user_id", mmUser.Id, "teams_user_id", msUser.ID, "error", err.Error()) + break } - p.API.LogWarn("Unable to update user during sync user job", "user_id", mmUser.Id, "teams_user_id", msUser.ID, "error", err.Error()) break } - - break } } }