github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/setting/utils.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 package setting 7 8 import ( 9 "html" 10 "net/url" 11 "strings" 12 ) 13 14 // RemoveUsernameParameterSuffix returns the username parameter without the (fullname) suffix - leaving just the username 15 func RemoveUsernameParameterSuffix(name string) string { 16 if index := strings.Index(name, " ("); index >= 0 { 17 name = name[:index] 18 } 19 return name 20 } 21 22 // IsValidSlackChannel validates a channel name conforms to what slack expects. 23 // It makes sure a channel name cannot be empty and invalid ( only an # ) 24 func IsValidSlackChannel(channelName string) bool { 25 switch len(strings.TrimSpace(channelName)) { 26 case 0: 27 return false 28 case 1: 29 // Keep default behaviour where a channel name is still 30 // valid without an # 31 // But if it contains only an #, it should be regarded as 32 // invalid 33 if channelName[0] == '#' { 34 return false 35 } 36 } 37 38 return true 39 } 40 41 // SanitizeFlashErrorString will sanitize a flash error string 42 func SanitizeFlashErrorString(x string) string { 43 return strings.ReplaceAll(html.EscapeString(x), "\n", "<br>") 44 } 45 46 // IsExternalURL checks if rawURL points to an external URL like http://example.com 47 func IsExternalURL(rawURL string) bool { 48 parsed, err := url.Parse(rawURL) 49 if err != nil { 50 return true 51 } 52 appURL, _ := url.Parse(AppURL) 53 if len(parsed.Host) != 0 && strings.Replace(parsed.Host, "www.", "", 1) != strings.Replace(appURL.Host, "www.", "", 1) { 54 return true 55 } 56 return false 57 }