From c05701dd7d72aea75c67fbb07df928c192474686 Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 9 Dec 2020 01:12:15 +0100 Subject: [PATCH] Fix frontpage avatars (#13853) The frontpage uses a rather strange method to obtain the commit's avatar which I've overlooked earlier. I don't exactly understand how it works but this change fixes the wrong default avatars by using the function that was in previous use. Also introduced a few constants for size an size increase factor. Fixes: https://github.com/go-gitea/gitea/issues/13844 --- models/avatar.go | 6 ++++++ modules/repository/commits.go | 6 ++++-- modules/repository/commits_test.go | 6 ++++-- modules/templates/helper.go | 20 +++++++++++--------- templates/admin/hook_new.tmpl | 18 +++++++++--------- templates/org/settings/hook_new.tmpl | 18 +++++++++--------- templates/repo/release/list.tmpl | 4 ++-- templates/repo/settings/webhook/list.tmpl | 18 +++++++++--------- templates/repo/settings/webhook/new.tmpl | 18 +++++++++--------- templates/repo/view_list.tmpl | 4 ++-- templates/user/dashboard/feeds.tmpl | 2 +- web_src/less/_base.less | 21 +++++++++++---------- web_src/less/helpers.less | 2 +- 13 files changed, 78 insertions(+), 65 deletions(-) diff --git a/models/avatar.go b/models/avatar.go index ac260fbd93..166ca337ca 100644 --- a/models/avatar.go +++ b/models/avatar.go @@ -40,6 +40,12 @@ func DefaultAvatarLink() string { // determined by the avatar-hosting service. const DefaultAvatarSize = -1 +// DefaultAvatarPixelSize is the default size in pixels of a rendered avatar +const DefaultAvatarPixelSize = 28 + +// AvatarRenderedSizeFactor is the factor by which the default size is increased for finer rendering +const AvatarRenderedSizeFactor = 2 + // HashEmail hashes email address to MD5 string. // https://en.gravatar.com/site/implement/hash/ func HashEmail(email string) string { diff --git a/modules/repository/commits.go b/modules/repository/commits.go index fd8b8d927a..6b67c2c262 100644 --- a/modules/repository/commits.go +++ b/modules/repository/commits.go @@ -118,12 +118,14 @@ func (pc *PushCommits) AvatarLink(email string) string { return avatar } + size := models.DefaultAvatarPixelSize * models.AvatarRenderedSizeFactor + u, ok := pc.emailUsers[email] if !ok { var err error u, err = models.GetUserByEmail(email) if err != nil { - pc.avatars[email] = models.HashedAvatarLink(email) + pc.avatars[email] = models.SizedAvatarLink(email, size) if !models.IsErrUserNotExist(err) { log.Error("GetUserByEmail: %v", err) return "" @@ -133,7 +135,7 @@ func (pc *PushCommits) AvatarLink(email string) string { } } if u != nil { - pc.avatars[email] = u.RelAvatarLink() + pc.avatars[email] = u.RealSizedAvatarLink(size) } return pc.avatars[email] diff --git a/modules/repository/commits_test.go b/modules/repository/commits_test.go index cb00e19c2e..16677fe8a6 100644 --- a/modules/repository/commits_test.go +++ b/modules/repository/commits_test.go @@ -112,11 +112,13 @@ func TestPushCommits_AvatarLink(t *testing.T) { pushCommits.Len = len(pushCommits.Commits) assert.Equal(t, - "/user/avatar/user2/-1", + "https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon&s=56", pushCommits.AvatarLink("user2@example.com")) assert.Equal(t, - "/avatar/"+fmt.Sprintf("%x", md5.Sum([]byte("nonexistent@example.com"))), + "https://secure.gravatar.com/avatar/"+ + fmt.Sprintf("%x", md5.Sum([]byte("nonexistent@example.com")))+ + "?d=identicon&s=56", pushCommits.AvatarLink("nonexistent@example.com")) } diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 0b5ae0f013..f7c10c3698 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -340,6 +340,7 @@ func NewFuncMap() []template.FuncMap { }, "svg": SVG, "avatar": Avatar, + "avatarHTML": AvatarHTML, "avatarByEmail": AvatarByEmail, "repoAvatar": RepoAvatar, "SortArrow": func(normSort, revSort, urlSort string, isDefault bool) template.HTML { @@ -519,7 +520,8 @@ func parseOthers(defaultSize int, defaultClass string, others ...interface{}) (i return size, class } -func avatarHTML(src string, size int, class string, name string) template.HTML { +// AvatarHTML creates the HTML for an avatar +func AvatarHTML(src string, size int, class string, name string) template.HTML { sizeStr := fmt.Sprintf(`%d`, size) if name == "" { @@ -548,33 +550,33 @@ func SVG(icon string, others ...interface{}) template.HTML { // Avatar renders user avatars. args: user, size (int), class (string) func Avatar(user *models.User, others ...interface{}) template.HTML { - size, class := parseOthers(28, "ui avatar image", others...) + size, class := parseOthers(models.DefaultAvatarPixelSize, "ui avatar image", others...) - src := user.RealSizedAvatarLink(size * 2) // request double size for finer rendering + src := user.RealSizedAvatarLink(size * models.AvatarRenderedSizeFactor) if src != "" { - return avatarHTML(src, size, class, user.DisplayName()) + return AvatarHTML(src, size, class, user.DisplayName()) } return template.HTML("") } // RepoAvatar renders repo avatars. args: repo, size(int), class (string) func RepoAvatar(repo *models.Repository, others ...interface{}) template.HTML { - size, class := parseOthers(28, "ui avatar image", others...) + size, class := parseOthers(models.DefaultAvatarPixelSize, "ui avatar image", others...) src := repo.RelAvatarLink() if src != "" { - return avatarHTML(src, size, class, repo.FullName()) + return AvatarHTML(src, size, class, repo.FullName()) } return template.HTML("") } // AvatarByEmail renders avatars by email address. args: email, name, size (int), class (string) func AvatarByEmail(email string, name string, others ...interface{}) template.HTML { - size, class := parseOthers(28, "ui avatar image", others...) - src := models.SizedAvatarLink(email, size*2) // request double size for finer rendering + size, class := parseOthers(models.DefaultAvatarPixelSize, "ui avatar image", others...) + src := models.SizedAvatarLink(email, size*models.AvatarRenderedSizeFactor) if src != "" { - return avatarHTML(src, size, class, name) + return AvatarHTML(src, size, class, name) } return template.HTML("") diff --git a/templates/admin/hook_new.tmpl b/templates/admin/hook_new.tmpl index 8eeb0e5264..dabd568f93 100644 --- a/templates/admin/hook_new.tmpl +++ b/templates/admin/hook_new.tmpl @@ -11,23 +11,23 @@ {{end}}
{{if eq .HookType "gitea"}} - + {{else if eq .HookType "gogs"}} - + {{else if eq .HookType "slack"}} - + {{else if eq .HookType "discord"}} - + {{else if eq .HookType "dingtalk"}} - + {{else if eq .HookType "telegram"}} - + {{else if eq .HookType "msteams"}} - + {{else if eq .HookType "feishu"}} - + {{else if eq .HookType "matrix"}} - + {{end}}
diff --git a/templates/org/settings/hook_new.tmpl b/templates/org/settings/hook_new.tmpl index 96769c7446..2fdf2662d6 100644 --- a/templates/org/settings/hook_new.tmpl +++ b/templates/org/settings/hook_new.tmpl @@ -10,23 +10,23 @@ {{if .PageIsSettingsHooksNew}}{{.i18n.Tr "repo.settings.add_webhook"}}{{else}}{{.i18n.Tr "repo.settings.update_webhook"}}{{end}}
{{if eq .HookType "gitea"}} - + {{else if eq .HookType "gogs"}} - + {{else if eq .HookType "slack"}} - + {{else if eq .HookType "discord"}} - + {{else if eq .HookType "dingtalk"}} - + {{else if eq .HookType "telegram"}} - + {{else if eq .HookType "msteams"}} - + {{else if eq .HookType "feishu"}} - + {{else if eq .HookType "matrix"}} - + {{end}}
diff --git a/templates/repo/release/list.tmpl b/templates/repo/release/list.tmpl index 70a614ff72..780a75ad89 100644 --- a/templates/repo/release/list.tmpl +++ b/templates/repo/release/list.tmpl @@ -90,7 +90,7 @@

{{ if gt .Publisher.ID 0 }} - {{avatar .Publisher 28 "img-10"}} + {{avatar .Publisher 20}} {{.Publisher.Name}} {{ end }} @@ -117,7 +117,7 @@ {{if .OriginalAuthor}} {{svg "octicon-mark-github" 16 "mr-2"}}{{.OriginalAuthor}} {{else if .Publisher}} - {{avatar .Publisher 28 "img-10"}} + {{avatar .Publisher 20}} {{.Publisher.GetDisplayName}} {{else}} Ghost diff --git a/templates/repo/settings/webhook/list.tmpl b/templates/repo/settings/webhook/list.tmpl index 29994d8197..47ed37cc33 100644 --- a/templates/repo/settings/webhook/list.tmpl +++ b/templates/repo/settings/webhook/list.tmpl @@ -6,31 +6,31 @@

{{.i18n.Tr "repo.settings.add_webhook"}}
diff --git a/templates/repo/settings/webhook/new.tmpl b/templates/repo/settings/webhook/new.tmpl index fc78db8648..ac5d1886b2 100644 --- a/templates/repo/settings/webhook/new.tmpl +++ b/templates/repo/settings/webhook/new.tmpl @@ -8,23 +8,23 @@ {{if .PageIsSettingsHooksNew}}{{.i18n.Tr "repo.settings.add_webhook"}}{{else}}{{.i18n.Tr "repo.settings.update_webhook"}}{{end}}
{{if eq .HookType "gitea"}} - + {{else if eq .HookType "gogs"}} - + {{else if eq .HookType "slack"}} - + {{else if eq .HookType "discord"}} - + {{else if eq .HookType "dingtalk"}} - + {{else if eq .HookType "telegram"}} - + {{else if eq .HookType "msteams"}} - + {{else if eq .HookType "feishu"}} - + {{else if eq .HookType "matrix"}} - + {{end}}
diff --git a/templates/repo/view_list.tmpl b/templates/repo/view_list.tmpl index 651747f20a..3266a813e3 100644 --- a/templates/repo/view_list.tmpl +++ b/templates/repo/view_list.tmpl @@ -3,7 +3,7 @@ {{if .LatestCommitUser}} - {{avatar .LatestCommitUser 28 "img-12"}} + {{avatar .LatestCommitUser 24}} {{if .LatestCommitUser.FullName}} {{.LatestCommitUser.FullName}} {{else}} @@ -11,7 +11,7 @@ {{end}} {{else}} {{if .LatestCommit.Author}} - {{avatarByEmail .LatestCommit.Author.Email .LatestCommit.Author.Name 28 "img-12"}} + {{avatarByEmail .LatestCommit.Author.Email .LatestCommit.Author.Name 24}} {{.LatestCommit.Author.Name}} {{end}} {{end}} diff --git a/templates/user/dashboard/feeds.tmpl b/templates/user/dashboard/feeds.tmpl index 76c751de8a..0d9f7de5a7 100644 --- a/templates/user/dashboard/feeds.tmpl +++ b/templates/user/dashboard/feeds.tmpl @@ -85,7 +85,7 @@ {{range $push.Commits}} {{ $commitLink := printf "%s/commit/%s" $repoLink .Sha1}}
  • - {{avatarByEmail .AuthorEmail .AuthorName 28 "img-8 mr-2"}} + {{avatarHTML ($push.AvatarLink .AuthorEmail) 16 "mr-2" .AuthorName}} {{ShortSha .Sha1}} {{RenderCommitMessage .Message $repoLink $.ComposeMetas}} diff --git a/web_src/less/_base.less b/web_src/less/_base.less index 81c6a310f5..0e555daf9f 100644 --- a/web_src/less/_base.less +++ b/web_src/less/_base.less @@ -312,6 +312,12 @@ a.muted:hover, font-weight: normal; } +/* fix misaligned images in webhook dropdown */ +.ui.dropdown .menu > .item > img { + margin-top: -.25rem; + margin-bottom: -.25rem; +} + .ui.selection.dropdown .menu > .item { border-color: var(--color-secondary); } @@ -1101,16 +1107,6 @@ footer { text-align: center; } -.generate-img(16); -.generate-img(@n, @i: 1) when (@i =< @n) { - .img-@{i} { - width: (2px * @i) !important; - height: (2px * @i) !important; - } - - .generate-img(@n, (@i + 1)); -} - // Conditional display @media @mediaMdAndUp { .mobile-only, @@ -1660,6 +1656,11 @@ a.ui.basic.label:hover { visibility: hidden; } +/* prevent stacking context issue on webhook dropdown */ +.ui.segment { + position: static; +} + .ui.segment, .ui.segments, .ui.attached.segment { diff --git a/web_src/less/helpers.less b/web_src/less/helpers.less index 7e299339f8..c611076f48 100644 --- a/web_src/less/helpers.less +++ b/web_src/less/helpers.less @@ -22,7 +22,7 @@ } .full-screen-width { width: 100vw !important; } -.full-screen-height { height: 100vw !important; } +.full-screen-height { height: 100vh !important; } .rounded { border-radius: var(--border-radius) !important; } .rounded-top { border-radius: var(--border-radius) var(--border-radius) 0 0 !important; }