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.

util_avatar.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package templates
  4. import (
  5. "context"
  6. "fmt"
  7. "html"
  8. "html/template"
  9. activities_model "code.gitea.io/gitea/models/activities"
  10. "code.gitea.io/gitea/models/avatars"
  11. "code.gitea.io/gitea/models/organization"
  12. repo_model "code.gitea.io/gitea/models/repo"
  13. user_model "code.gitea.io/gitea/models/user"
  14. gitea_html "code.gitea.io/gitea/modules/html"
  15. "code.gitea.io/gitea/modules/setting"
  16. )
  17. // AvatarHTML creates the HTML for an avatar
  18. func AvatarHTML(src string, size int, class, name string) template.HTML {
  19. sizeStr := fmt.Sprintf(`%d`, size)
  20. if name == "" {
  21. name = "avatar"
  22. }
  23. return template.HTML(`<img class="` + class + `" src="` + src + `" title="` + html.EscapeString(name) + `" width="` + sizeStr + `" height="` + sizeStr + `"/>`)
  24. }
  25. // Avatar renders user avatars. args: user, size (int), class (string)
  26. func Avatar(ctx context.Context, item interface{}, others ...interface{}) template.HTML {
  27. size, class := gitea_html.ParseSizeAndClass(avatars.DefaultAvatarPixelSize, avatars.DefaultAvatarClass, others...)
  28. switch t := item.(type) {
  29. case *user_model.User:
  30. src := t.AvatarLinkWithSize(ctx, size*setting.Avatar.RenderedSizeFactor)
  31. if src != "" {
  32. return AvatarHTML(src, size, class, t.DisplayName())
  33. }
  34. case *repo_model.Collaborator:
  35. src := t.AvatarLinkWithSize(ctx, size*setting.Avatar.RenderedSizeFactor)
  36. if src != "" {
  37. return AvatarHTML(src, size, class, t.DisplayName())
  38. }
  39. case *organization.Organization:
  40. src := t.AsUser().AvatarLinkWithSize(ctx, size*setting.Avatar.RenderedSizeFactor)
  41. if src != "" {
  42. return AvatarHTML(src, size, class, t.AsUser().DisplayName())
  43. }
  44. }
  45. return template.HTML("")
  46. }
  47. // AvatarByAction renders user avatars from action. args: action, size (int), class (string)
  48. func AvatarByAction(ctx context.Context, action *activities_model.Action, others ...interface{}) template.HTML {
  49. action.LoadActUser(ctx)
  50. return Avatar(ctx, action.ActUser, others...)
  51. }
  52. // RepoAvatar renders repo avatars. args: repo, size(int), class (string)
  53. func RepoAvatar(repo *repo_model.Repository, others ...interface{}) template.HTML {
  54. size, class := gitea_html.ParseSizeAndClass(avatars.DefaultAvatarPixelSize, avatars.DefaultAvatarClass, others...)
  55. src := repo.RelAvatarLink()
  56. if src != "" {
  57. return AvatarHTML(src, size, class, repo.FullName())
  58. }
  59. return template.HTML("")
  60. }
  61. // AvatarByEmail renders avatars by email address. args: email, name, size (int), class (string)
  62. func AvatarByEmail(ctx context.Context, email, name string, others ...interface{}) template.HTML {
  63. size, class := gitea_html.ParseSizeAndClass(avatars.DefaultAvatarPixelSize, avatars.DefaultAvatarClass, others...)
  64. src := avatars.GenerateEmailAvatarFastLink(ctx, email, size*setting.Avatar.RenderedSizeFactor)
  65. if src != "" {
  66. return AvatarHTML(src, size, class, name)
  67. }
  68. return template.HTML("")
  69. }