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.

48 lines
1.2 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. "strings"
  8. )
  9. // RemoveUsernameParameterSuffix returns the username parameter without the (fullname) suffix - leaving just the username
  10. func RemoveUsernameParameterSuffix(name string) string {
  11. if index := strings.Index(name, " ("); index >= 0 {
  12. name = name[:index]
  13. }
  14. return name
  15. }
  16. // IsValidSlackChannel validates a channel name conforms to what slack expects.
  17. // It makes sure a channel name cannot be empty and invalid ( only an # )
  18. func IsValidSlackChannel(channelName string) bool {
  19. switch len(strings.TrimSpace(channelName)) {
  20. case 0:
  21. return false
  22. case 1:
  23. // Keep default behaviour where a channel name is still
  24. // valid without an #
  25. // But if it contains only an #, it should be regarded as
  26. // invalid
  27. if channelName[0] == '#' {
  28. return false
  29. }
  30. }
  31. return true
  32. }
  33. // SanitizeFlashErrorString will sanitize a flash error string
  34. func SanitizeFlashErrorString(x string) string {
  35. runes := []rune(x)
  36. if len(runes) > 512 {
  37. x = "..." + string(runes[len(runes)-512:])
  38. }
  39. return strings.Replace(html.EscapeString(x), "\n", "<br>", -1)
  40. }