Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In many cases user avatar link should be an absolute URL with http host #17420

Merged
merged 4 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion integrations/user_avatar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestUserAvatar(t *testing.T) {

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

req = NewRequest(t, "GET", user2.AvatarLink())
req = NewRequest(t, "GET", user2.AvatarLinkWithSize(0))
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
_ = session.MakeRequest(t, req, http.StatusOK)

// 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.
Expand Down
9 changes: 7 additions & 2 deletions models/user_avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"image/png"
"io"
"strings"

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

// AvatarLink returns a avatar link with default size
// AvatarLink returns the full avatar link with http host
func (u *User) AvatarLink() string {
return u.AvatarLinkWithSize(0)
link := u.AvatarLinkWithSize(0)
if !strings.HasPrefix(link, "//") && !strings.Contains(link, "://") {
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
return setting.AppURL + strings.TrimPrefix(link, setting.AppSubURL+"/")
}
return link
}

// UploadAvatar saves custom avatar for user.
Expand Down
1 change: 1 addition & 0 deletions modules/convert/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestUser_ToUser(t *testing.T) {

apiUser := toUser(user1, true, true)
assert.True(t, apiUser.IsAdmin)
assert.Contains(t, apiUser.AvatarURL, "://")

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

Expand Down