github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/services/sharedchannel/util.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package sharedchannel 5 6 import ( 7 "fmt" 8 "strings" 9 10 "github.com/masterhung0112/hk_server/v5/model" 11 ) 12 13 // fixMention replaces any mentions in a post for the user with the user's real username. 14 func fixMention(post *model.Post, mentionMap model.UserMentionMap, user *model.User) { 15 if post == nil || len(mentionMap) == 0 { 16 return 17 } 18 19 realUsername, ok := user.GetProp(KeyRemoteUsername) 20 if !ok { 21 return 22 } 23 24 // there may be more than one mention for each user so we have to walk the whole map. 25 for mention, id := range mentionMap { 26 if id == user.Id && strings.Contains(mention, ":") { 27 post.Message = strings.ReplaceAll(post.Message, "@"+mention, "@"+realUsername) 28 } 29 } 30 } 31 32 func sanitizeUserForSync(user *model.User) *model.User { 33 user.Password = model.NewId() 34 user.AuthData = nil 35 user.AuthService = "" 36 user.Roles = "system_user" 37 user.AllowMarketing = false 38 user.NotifyProps = model.StringMap{} 39 user.LastPasswordUpdate = 0 40 user.LastPictureUpdate = 0 41 user.FailedAttempts = 0 42 user.MfaActive = false 43 user.MfaSecret = "" 44 45 return user 46 } 47 48 // mungUsername creates a new username by combining username and remote cluster name, plus 49 // a suffix to create uniqueness. If the resulting username exceeds the max length then 50 // it is truncated and ellipses added. 51 func mungUsername(username string, remotename string, suffix string, maxLen int) string { 52 if suffix != "" { 53 suffix = "~" + suffix 54 } 55 56 // If the username already contains a colon then another server already munged it. 57 // In that case we can split on the colon and use the existing remote name. 58 // We still need to re-mung with suffix in case of collision. 59 comps := strings.Split(username, ":") 60 if len(comps) >= 2 { 61 username = comps[0] 62 remotename = strings.Join(comps[1:], "") 63 } 64 65 var userEllipses string 66 var remoteEllipses string 67 68 // The remotename is allowed to use up to half the maxLen, and the username gets the remaining space. 69 // Username might have a suffix to account for, and remotename always has a preceding colon. 70 half := maxLen / 2 71 72 // If the remotename is less than half the maxLen, then the left over space can be given to 73 // the username. 74 extra := half - (len(remotename) + 1) 75 if extra < 0 { 76 extra = 0 77 } 78 79 truncUser := (len(username) + len(suffix)) - (half + extra) 80 if truncUser > 0 { 81 username = username[:len(username)-truncUser-3] 82 userEllipses = "..." 83 } 84 85 truncRemote := (len(remotename) + 1) - (maxLen - (len(username) + len(userEllipses) + len(suffix))) 86 if truncRemote > 0 { 87 remotename = remotename[:len(remotename)-truncRemote-3] 88 remoteEllipses = "..." 89 } 90 91 return fmt.Sprintf("%s%s%s:%s%s", username, suffix, userEllipses, remotename, remoteEllipses) 92 } 93 94 // mungEmail creates a unique email address using a UID and remote name. 95 func mungEmail(remotename string, maxLen int) string { 96 s := fmt.Sprintf("%s@%s", model.NewId(), remotename) 97 if len(s) > maxLen { 98 s = s[:maxLen] 99 } 100 return s 101 }