Skip to content

Commit b489b99

Browse files
committed
Always return local url for users avatar
Avoids having to wait for DNS lookups when libravatar is activated fixing #6046
1 parent 63ff616 commit b489b99

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

models/action_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func TestPushCommits_AvatarLink(t *testing.T) {
173173
pushCommits.Len = len(pushCommits.Commits)
174174

175175
assert.Equal(t,
176-
"https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon",
176+
"/user/avatar/user2/-1",
177177
pushCommits.AvatarLink("[email protected]"))
178178

179179
assert.Equal(t,

models/user.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"image/png"
1818
"os"
1919
"path/filepath"
20+
"strconv"
2021
"strings"
2122
"time"
2223
"unicode/utf8"
@@ -373,9 +374,20 @@ func (u *User) generateRandomAvatar(e Engine) error {
373374
return nil
374375
}
375376

376-
// SizedRelAvatarLink returns a relative link to the user's avatar. When
377-
// applicable, the link is for an avatar of the indicated size (in pixels).
377+
// SizedRelAvatarLink returns a link to the user's avatar via
378+
// the local explore page. Function returns immediately.
379+
// When applicable, the link is for an avatar of the indicated size (in pixels).
378380
func (u *User) SizedRelAvatarLink(size int) string {
381+
return setting.AppSubURL + "/user/avatar/" + u.Name + "/" + strconv.Itoa(size)
382+
}
383+
384+
// RealSizedAvatarLink returns a link to the user's avatar. When
385+
// applicable, the link is for an avatar of the indicated size (in pixels).
386+
//
387+
// This function make take time to return when federated avatars
388+
// are in use, due to a DNS lookup need
389+
//
390+
func (u *User) RealSizedAvatarLink(size int) string {
379391
if u.ID == -1 {
380392
return base.DefaultAvatarLink()
381393
}

routers/routes/routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ func RegisterRoutes(m *macaron.Macaron) {
278278

279279
// ***** START: User *****
280280
m.Group("/user", func() {
281+
m.Get("/avatar/:username/:size", user.Avatar)
281282
m.Get("/login", user.SignIn)
282283
m.Post("/login", bindIgnErr(auth.SignInForm{}), user.SignInPost)
283284
m.Group("", func() {

routers/user/avatar.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2019 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+
5+
package user
6+
7+
import (
8+
"strconv"
9+
10+
"code.gitea.io/gitea/models"
11+
"code.gitea.io/gitea/modules/context"
12+
"code.gitea.io/gitea/modules/log"
13+
)
14+
15+
// Avatar redirect browser to user avatar of requested size
16+
func Avatar(ctx *context.Context) {
17+
userName := ctx.Params(":username")
18+
size, err := strconv.Atoi(ctx.Params(":size"))
19+
if err != nil {
20+
ctx.ServerError("Invalid avatar size", err)
21+
return
22+
}
23+
24+
log.Debug("Asked avatar for user %v and size %v", userName, size)
25+
26+
user, err := models.GetUserByName(userName)
27+
if err != nil {
28+
if models.IsErrUserNotExist(err) {
29+
ctx.ServerError("Requested avatar for invalid user", err)
30+
} else {
31+
ctx.ServerError("Retrieving user by name", err)
32+
}
33+
return
34+
}
35+
36+
ctx.Redirect(user.RealSizedAvatarLink(size))
37+
}

0 commit comments

Comments
 (0)