You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.7 KiB

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