summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorsilverwind <me@silverwind.io>2020-12-03 19:46:11 +0100
committerGitHub <noreply@github.com>2020-12-03 19:46:11 +0100
commit9269a038a4e904bdeaf5470e76e3a4f8a2a4685b (patch)
tree59730c0dbe7b011a8cb751d135e44152e7feeec0 /routers
parent0d35ef5b439623774d1adddf7071d744b1116809 (diff)
downloadgitea-9269a038a4e904bdeaf5470e76e3a4f8a2a4685b.tar.gz
gitea-9269a038a4e904bdeaf5470e76e3a4f8a2a4685b.zip
Direct avatar rendering (#13649)
* Direct avatar rendering This adds new template helpers for avatar rendering which output image elements with direct links to avatars which makes them cacheable by the browsers. This should be a major performance improvment for pages with many avatars. * fix avatars of other user's profile pages * fix top border on user avatar name * uncircle avatars * remove old incomplete avatar selector * use title attribute for name and add it back on blame * minor refactor * tweak comments * fix url path join and adjust test to new result * dedupe functions
Diffstat (limited to 'routers')
-rw-r--r--routers/repo/blame.go15
-rw-r--r--routers/user/avatar.go25
2 files changed, 26 insertions, 14 deletions
diff --git a/routers/repo/blame.go b/routers/repo/blame.go
index 812c55ea4d..bd11310988 100644
--- a/routers/repo/blame.go
+++ b/routers/repo/blame.go
@@ -10,7 +10,6 @@ import (
"fmt"
"html"
gotemplate "html/template"
- "net/url"
"strings"
"code.gitea.io/gitea/models"
@@ -19,7 +18,7 @@ import (
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/highlight"
"code.gitea.io/gitea/modules/log"
- "code.gitea.io/gitea/modules/setting"
+ "code.gitea.io/gitea/modules/templates"
"code.gitea.io/gitea/modules/timeutil"
)
@@ -209,17 +208,15 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m
commit := commitNames[part.Sha]
if index == 0 {
// User avatar image
- avatar := ""
commitSince := timeutil.TimeSinceUnix(timeutil.TimeStamp(commit.Author.When.Unix()), ctx.Data["Lang"].(string))
+
+ var avatar string
if commit.User != nil {
- authorName := commit.Author.Name
- if len(commit.User.FullName) > 0 {
- authorName = commit.User.FullName
- }
- avatar = fmt.Sprintf(`<a href="%s/%s"><img class="ui avatar image" src="%s" title="%s" alt=""/></a>`, setting.AppSubURL, url.PathEscape(commit.User.Name), commit.User.RelAvatarLink(), html.EscapeString(authorName))
+ avatar = string(templates.Avatar(commit.User, 18, "mr-3"))
} else {
- avatar = fmt.Sprintf(`<img class="ui avatar image" src="%s" title="%s"/>`, html.EscapeString(models.AvatarLink(commit.Author.Email)), html.EscapeString(commit.Author.Name))
+ avatar = string(templates.AvatarByEmail(commit.Author.Email, commit.Author.Name, 18, "mr-3"))
}
+
commitInfo.WriteString(fmt.Sprintf(`<div class="blame-info%s"><div class="blame-data"><div class="blame-avatar">%s</div><div class="blame-message"><a href="%s/commit/%s" title="%[5]s">%[5]s</a></div><div class="blame-time">%s</div></div></div>`, attr, avatar, repoLink, part.Sha, html.EscapeString(commit.CommitMessage), commitSince))
} else {
commitInfo.WriteString(fmt.Sprintf(`<div class="blame-info%s">&#8203;</div>`, attr))
diff --git a/routers/user/avatar.go b/routers/user/avatar.go
index 32d05f03cc..c3ece49089 100644
--- a/routers/user/avatar.go
+++ b/routers/user/avatar.go
@@ -6,11 +6,11 @@ package user
import (
"errors"
+ "net/url"
"strconv"
"strings"
"code.gitea.io/gitea/models"
- "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
)
@@ -46,23 +46,38 @@ func Avatar(ctx *context.Context) {
// AvatarByEmailHash redirects the browser to the appropriate Avatar link
func AvatarByEmailHash(ctx *context.Context) {
+ var err error
+
hash := ctx.Params(":hash")
if len(hash) == 0 {
ctx.ServerError("invalid avatar hash", errors.New("hash cannot be empty"))
return
}
- email, err := models.GetEmailForHash(hash)
+
+ var email string
+ email, err = models.GetEmailForHash(hash)
if err != nil {
ctx.ServerError("invalid avatar hash", err)
return
}
if len(email) == 0 {
- ctx.Redirect(base.DefaultAvatarLink())
+ ctx.Redirect(models.DefaultAvatarLink())
return
}
size := ctx.QueryInt("size")
if size == 0 {
- size = base.DefaultAvatarSize
+ size = models.DefaultAvatarSize
}
- ctx.Redirect(base.SizedAvatarLinkWithDomain(email, size))
+
+ var avatarURL *url.URL
+ avatarURL, err = models.LibravatarURL(email)
+ if err != nil {
+ avatarURL, err = url.Parse(models.DefaultAvatarLink())
+ if err != nil {
+ ctx.ServerError("invalid default avatar url", err)
+ return
+ }
+ }
+
+ ctx.Redirect(models.MakeFinalAvatarURL(avatarURL, size))
}