@@ -18,6 +18,14 @@ import (
1818 "code.gitea.io/gitea/modules/setting"
1919)
2020
21+ type AvatarUtils struct {
22+ ctx context.Context
23+ }
24+
25+ func NewAvatarUtils (ctx context.Context ) * AvatarUtils {
26+ return & AvatarUtils {ctx : ctx }
27+ }
28+
2129// AvatarHTML creates the HTML for an avatar
2230func AvatarHTML (src string , size int , class , name string ) template.HTML {
2331 sizeStr := fmt .Sprintf (`%d` , size )
@@ -30,44 +38,44 @@ func AvatarHTML(src string, size int, class, name string) template.HTML {
3038}
3139
3240// Avatar renders user avatars. args: user, size (int), class (string)
33- func Avatar ( ctx context. Context , item any , others ... any ) template.HTML {
41+ func ( au * AvatarUtils ) Avatar ( item any , others ... any ) template.HTML {
3442 size , class := gitea_html .ParseSizeAndClass (avatars .DefaultAvatarPixelSize , avatars .DefaultAvatarClass , others ... )
3543
3644 switch t := item .(type ) {
3745 case * user_model.User :
38- src := t .AvatarLinkWithSize (ctx , size * setting .Avatar .RenderedSizeFactor )
46+ src := t .AvatarLinkWithSize (au . ctx , size * setting .Avatar .RenderedSizeFactor )
3947 if src != "" {
4048 return AvatarHTML (src , size , class , t .DisplayName ())
4149 }
4250 case * repo_model.Collaborator :
43- src := t .AvatarLinkWithSize (ctx , size * setting .Avatar .RenderedSizeFactor )
51+ src := t .AvatarLinkWithSize (au . ctx , size * setting .Avatar .RenderedSizeFactor )
4452 if src != "" {
4553 return AvatarHTML (src , size , class , t .DisplayName ())
4654 }
4755 case * organization.Organization :
48- src := t .AsUser ().AvatarLinkWithSize (ctx , size * setting .Avatar .RenderedSizeFactor )
56+ src := t .AsUser ().AvatarLinkWithSize (au . ctx , size * setting .Avatar .RenderedSizeFactor )
4957 if src != "" {
5058 return AvatarHTML (src , size , class , t .AsUser ().DisplayName ())
5159 }
5260 }
5361
54- return template . HTML ( "" )
62+ return ""
5563}
5664
5765// AvatarByAction renders user avatars from action. args: action, size (int), class (string)
58- func AvatarByAction ( ctx context. Context , action * activities_model.Action , others ... any ) template.HTML {
59- action .LoadActUser (ctx )
60- return Avatar (ctx , action .ActUser , others ... )
66+ func ( au * AvatarUtils ) AvatarByAction ( action * activities_model.Action , others ... any ) template.HTML {
67+ action .LoadActUser (au . ctx )
68+ return au . Avatar (action .ActUser , others ... )
6169}
6270
6371// AvatarByEmail renders avatars by email address. args: email, name, size (int), class (string)
64- func AvatarByEmail ( ctx context. Context , email , name string , others ... any ) template.HTML {
72+ func ( au * AvatarUtils ) AvatarByEmail ( email , name string , others ... any ) template.HTML {
6573 size , class := gitea_html .ParseSizeAndClass (avatars .DefaultAvatarPixelSize , avatars .DefaultAvatarClass , others ... )
66- src := avatars .GenerateEmailAvatarFastLink (ctx , email , size * setting .Avatar .RenderedSizeFactor )
74+ src := avatars .GenerateEmailAvatarFastLink (au . ctx , email , size * setting .Avatar .RenderedSizeFactor )
6775
6876 if src != "" {
6977 return AvatarHTML (src , size , class , name )
7078 }
7179
72- return template . HTML ( "" )
80+ return ""
7381}
0 commit comments