Skip to content

Commit 7c951fd

Browse files
authored
In many cases user avatar link should be an absolute URL with http host (#17420)
1 parent 3676faf commit 7c951fd

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

integrations/user_avatar_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestUserAvatar(t *testing.T) {
7373

7474
user2 = db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo3, is an org
7575

76-
req = NewRequest(t, "GET", user2.AvatarLink())
76+
req = NewRequest(t, "GET", user2.AvatarLinkWithSize(0))
7777
_ = session.MakeRequest(t, req, http.StatusOK)
7878

7979
// Can't test if the response matches because the image is re-generated on upload but checking that this at least doesn't give a 404 should be enough.

models/user_avatar.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"image/png"
1111
"io"
12+
"strings"
1213

1314
"code.gitea.io/gitea/models/avatars"
1415
"code.gitea.io/gitea/models/db"
@@ -91,9 +92,13 @@ func (u *User) AvatarLinkWithSize(size int) string {
9192
return avatars.GenerateEmailAvatarFastLink(u.AvatarEmail, size)
9293
}
9394

94-
// AvatarLink returns a avatar link with default size
95+
// AvatarLink returns the full avatar link with http host
9596
func (u *User) AvatarLink() string {
96-
return u.AvatarLinkWithSize(0)
97+
link := u.AvatarLinkWithSize(0)
98+
if !strings.HasPrefix(link, "//") && !strings.Contains(link, "://") {
99+
return setting.AppURL + strings.TrimPrefix(link, setting.AppSubURL+"/")
100+
}
101+
return link
97102
}
98103

99104
// UploadAvatar saves custom avatar for user.

modules/convert/user_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func TestUser_ToUser(t *testing.T) {
2121

2222
apiUser := toUser(user1, true, true)
2323
assert.True(t, apiUser.IsAdmin)
24+
assert.Contains(t, apiUser.AvatarURL, "://")
2425

2526
user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2, IsAdmin: false}).(*models.User)
2627

0 commit comments

Comments
 (0)