diff --git a/appview/models/notifications.go b/appview/models/notifications.go index faaf6188..0943994a 100644 --- a/appview/models/notifications.go +++ b/appview/models/notifications.go @@ -1,9 +1,12 @@ package models import ( + "context" + "fmt" "time" "github.com/bluesky-social/indigo/atproto/syntax" + "tangled.org/core/idresolver" ) type NotificationType string @@ -108,6 +111,36 @@ type NotificationWithEntity struct { Pull *Pull } +// URL returns the navigable path for the notification, resolving DIDs to +// handles via the provided resolver. +func (n *NotificationWithEntity) URL(res *idresolver.Resolver) string { + resolve := func(did string) string { + if id, err := res.ResolveIdent(context.Background(), did); err == nil && !id.Handle.IsInvalidHandle() { + return id.Handle.String() + } + return did + } + + switch n.Type { + case NotificationTypeFollowed: + return "/" + resolve(n.ActorDid) + } + if n.Repo == nil { + return "" + } + repoHandle := resolve(n.Repo.Did) + slug := n.Repo.Slug() + switch { + case n.Issue != nil: + return fmt.Sprintf("/%s/%s/issues/%d", repoHandle, slug, n.Issue.IssueId) + case n.Pull != nil: + return fmt.Sprintf("/%s/%s/pulls/%d", repoHandle, slug, n.Pull.PullId) + default: + // repo_starred and other repo-level types + return fmt.Sprintf("/%s/%s", repoHandle, slug) + } +} + type NotificationPreferences struct { ID int64 UserDid syntax.DID diff --git a/appview/pages/templates/notifications/fragments/item.html b/appview/pages/templates/notifications/fragments/item.html index f8bf0793..5d5f3ba8 100644 --- a/appview/pages/templates/notifications/fragments/item.html +++ b/appview/pages/templates/notifications/fragments/item.html @@ -1,5 +1,5 @@ {{define "notifications/fragments/item"}} -
{{ .Pull.Title }}
{{ end }} {{ end }} - -{{ define "notificationUrl" }} - {{ $url := "" }} - {{ if eq .Type "repo_starred" }} - {{$url = printf "/%s/%s" (resolve .Repo.Did) .Repo.Slug}} - {{ else if .Issue }} - {{$url = printf "/%s/%s/issues/%d" (resolve .Repo.Did) .Repo.Slug .Issue.IssueId}} - {{ else if .Pull }} - {{$url = printf "/%s/%s/pulls/%d" (resolve .Repo.Did) .Repo.Slug .Pull.PullId}} - {{ else if eq .Type "followed" }} - {{$url = printf "/%s" (resolve .ActorDid)}} - {{ else }} - {{ end }} - - {{ $url }} -{{ end }}