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
This commit is contained in:
silverwind 2020-12-09 01:12:15 +01:00 committed by GitHub
parent 658e90a114
commit c05701dd7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 78 additions and 65 deletions

View file

@ -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]

View file

@ -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"))
}

View file

@ -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("")