From 4da043b79882d254984b9685c6355b722f112c57 Mon Sep 17 00:00:00 2001 From: Anirudh Oppiliappan Date: Wed, 9 Jul 2025 23:03:43 +0300 Subject: [PATCH] appview/pages: show mini avatar next to user handle Change-Id: ytpxvpknlxunvyokkrnktynuouxzqzpl Signed-off-by: Anirudh Oppiliappan --- appview/pages/funcmap.go | 16 ++++++- appview/pages/pages.go | 8 ++-- .../pages/templates/repo/issues/issue.html | 44 +++++++++---------- .../pages/templates/repo/issues/issues.html | 10 ++--- .../repo/pulls/fragments/pullHeader.html | 6 +-- appview/pages/templates/repo/pulls/pulls.html | 10 ++--- appview/pages/templates/timeline.html | 34 ++++---------- .../templates/user/fragments/picHandle.html | 10 +++++ 8 files changed, 72 insertions(+), 66 deletions(-) create mode 100644 appview/pages/templates/user/fragments/picHandle.html diff --git a/appview/pages/funcmap.go b/appview/pages/funcmap.go index 3a29490..abf2252 100644 --- a/appview/pages/funcmap.go +++ b/appview/pages/funcmap.go @@ -1,6 +1,9 @@ package pages import ( + "crypto/hmac" + "crypto/sha256" + "encoding/hex" "errors" "fmt" "html" @@ -19,7 +22,7 @@ import ( "tangled.sh/tangled.sh/core/appview/pages/markup" ) -func funcMap() template.FuncMap { +func (p *Pages) funcMap() template.FuncMap { return template.FuncMap{ "split": func(s string) []string { return strings.Split(s, "\n") @@ -246,9 +249,20 @@ func funcMap() template.FuncMap { u, _ := url.PathUnescape(s) return u }, + + "tinyAvatar": p.tinyAvatar, } } +func (p *Pages) tinyAvatar(handle string) string { + handle = strings.TrimPrefix(handle, "@") + secret := p.avatar.SharedSecret + h := hmac.New(sha256.New, []byte(secret)) + h.Write([]byte(handle)) + signature := hex.EncodeToString(h.Sum(nil)) + return fmt.Sprintf("%s/%s/%s?size=tiny", p.avatar.Host, signature, handle) +} + func icon(name string, classes []string) (template.HTML, error) { iconPath := filepath.Join("static", "icons", name) diff --git a/appview/pages/pages.go b/appview/pages/pages.go index dba91ed..4b1b7f6 100644 --- a/appview/pages/pages.go +++ b/appview/pages/pages.go @@ -40,6 +40,7 @@ var Files embed.FS type Pages struct { t map[string]*template.Template + avatar config.AvatarConfig dev bool embedFS embed.FS templateDir string // Path to templates on disk for dev mode @@ -57,6 +58,7 @@ func NewPages(config *config.Config) *Pages { p := &Pages{ t: make(map[string]*template.Template), dev: config.Core.Dev, + avatar: config.Avatar, embedFS: Files, rctx: rctx, templateDir: "appview/pages", @@ -90,7 +92,7 @@ func (p *Pages) loadAllTemplates() { name := strings.TrimPrefix(path, "templates/") name = strings.TrimSuffix(name, ".html") tmpl, err := template.New(name). - Funcs(funcMap()). + Funcs(p.funcMap()). ParseFS(p.embedFS, path) if err != nil { log.Fatalf("setting up fragment: %v", err) @@ -131,7 +133,7 @@ func (p *Pages) loadAllTemplates() { allPaths = append(allPaths, fragmentPaths...) allPaths = append(allPaths, path) tmpl, err := template.New(name). - Funcs(funcMap()). + Funcs(p.funcMap()). ParseFS(p.embedFS, allPaths...) if err != nil { return fmt.Errorf("setting up template: %w", err) @@ -185,7 +187,7 @@ func (p *Pages) loadTemplateFromDisk(name string) error { } // Create a new template - tmpl := template.New(name).Funcs(funcMap()) + tmpl := template.New(name).Funcs(p.funcMap()) // Parse layouts layoutGlob := filepath.Join(p.templateDir, "templates", "layouts", "*.html") diff --git a/appview/pages/templates/repo/issues/issue.html b/appview/pages/templates/repo/issues/issue.html index c4d1882..f26a536 100644 --- a/appview/pages/templates/repo/issues/issue.html +++ b/appview/pages/templates/repo/issues/issue.html @@ -4,7 +4,7 @@ {{ define "extrameta" }} {{ $title := printf "%s · issue #%d · %s" .Issue.Title .Issue.IssueId .RepoInfo.FullName }} {{ $url := printf "https://tangled.sh/%s/issues/%d" .RepoInfo.FullName .Issue.IssueId }} - + {{ template "repo/fragments/og" (dict "RepoInfo" .RepoInfo "Title" $title "Url" $url) }} {{ end }} @@ -30,13 +30,11 @@ {{ i $icon "w-4 h-4 mr-1.5 text-white" }} {{ .State }} - + opened by {{ $owner := didOrHandle .Issue.OwnerDid .IssueOwnerHandle }} - {{ $owner }} - + {{ template "user/fragments/picHandle" $owner }} + @@ -71,8 +69,8 @@ {{ define "newComment" }} {{ if .LoggedInUser }} -
@@ -90,9 +88,9 @@
- +
- -
-
{{ else if and (or $isIssueAuthor $isRepoCollaborator $isRepoOwner) (eq .State "closed") }} -
- + {{ else }}
login to join the discussion diff --git a/appview/pages/templates/repo/issues/issues.html b/appview/pages/templates/repo/issues/issues.html index 2dfb47c..74dd2e2 100644 --- a/appview/pages/templates/repo/issues/issues.html +++ b/appview/pages/templates/repo/issues/issues.html @@ -3,7 +3,7 @@ {{ define "extrameta" }} {{ $title := "issues"}} {{ $url := printf "https://tangled.sh/%s/issues" .RepoInfo.FullName }} - + {{ template "repo/fragments/og" (dict "RepoInfo" .RepoInfo "Title" $title "Url" $url) }} {{ end }} @@ -49,7 +49,7 @@ #{{ .IssueId }}
-

+

{{ $bgColor := "bg-gray-800 dark:bg-gray-700" }} {{ $icon := "ban" }} {{ $state := "closed" }} @@ -64,9 +64,9 @@ {{ $state }} - - {{ $owner := index $.DidHandleMap .OwnerDid }} - {{ $owner }} + + {{ $owner := index $.DidHandleMap .OwnerDid }} + {{ template "user/fragments/picHandle" $owner }} diff --git a/appview/pages/templates/repo/pulls/fragments/pullHeader.html b/appview/pages/templates/repo/pulls/fragments/pullHeader.html index 4e1b90e..24cb25a 100644 --- a/appview/pages/templates/repo/pulls/fragments/pullHeader.html +++ b/appview/pages/templates/repo/pulls/fragments/pullHeader.html @@ -26,12 +26,10 @@ {{ i $icon "w-4 h-4 mr-1.5 text-white" }} {{ .Pull.State.String }}

- + opened by {{ $owner := index $.DidHandleMap .Pull.OwnerDid }} - {{ $owner }} + {{ template "user/fragments/picHandle" $owner }} diff --git a/appview/pages/templates/repo/pulls/pulls.html b/appview/pages/templates/repo/pulls/pulls.html index f9afe60..fe6d31c 100644 --- a/appview/pages/templates/repo/pulls/pulls.html +++ b/appview/pages/templates/repo/pulls/pulls.html @@ -3,7 +3,7 @@ {{ define "extrameta" }} {{ $title := "pulls"}} {{ $url := printf "https://tangled.sh/%s/pulls" .RepoInfo.FullName }} - + {{ template "repo/fragments/og" (dict "RepoInfo" .RepoInfo "Title" $title "Url" $url) }} {{ end }} @@ -54,7 +54,7 @@ #{{ .PullId }} -

+

{{ $owner := index $.DidHandleMap .OwnerDid }} {{ $bgColor := "bg-gray-800 dark:bg-gray-700" }} {{ $icon := "ban" }} @@ -75,11 +75,11 @@ {{ .State.String }} - - {{ $owner }} + + {{ template "user/fragments/picHandle" $owner }} - + diff --git a/appview/pages/templates/timeline.html b/appview/pages/templates/timeline.html index effae89..763e87f 100644 --- a/appview/pages/templates/timeline.html +++ b/appview/pages/templates/timeline.html @@ -60,20 +60,16 @@ {{ if .Repo }} {{ $userHandle := index $.DidHandleMap .Repo.Did }}

-

- {{ $userHandle | truncateAt30 }} +

+ {{ template "user/fragments/picHandle" $userHandle }} {{ if .Source }} forked - {{ index $.DidHandleMap .Source.Did }}/{{ .Source.Name }} - + {{ index $.DidHandleMap .Source.Did }}/{{ .Source.Name }} to -

- {{ $userHandle | truncateAt30 }} +

+ {{ template "user/fragments/picHandle" $userHandle }} followed - {{ $subjectHandle | truncateAt30 }} + {{ template "user/fragments/picHandle" $subjectHandle }} -

- {{ $starrerHandle | truncateAt30 }} + {{ template "user/fragments/picHandle" $starrerHandle }} +

starred + {{ . }} + {{ . | truncateAt30 }} + +{{ end }} -- 2.43.0