+11
appview/pages/pages.go
+11
appview/pages/pages.go
···
85
func NewRepo(w io.Writer, p NewRepoParams) error {
86
return parse("new-repo.html").Execute(w, p)
87
}
88
+
89
+
type ProfilePageParams struct {
90
+
LoggedInUser *auth.User
91
+
UserDid string
92
+
UserHandle string
93
+
Repos []db.Repo
94
+
}
95
+
96
+
func ProfilePage(w io.Writer, p ProfilePageParams) error {
97
+
return parse("profile.html").Execute(w, p)
98
+
}
+18
appview/pages/profile.html
+18
appview/pages/profile.html
···
···
1
+
{{define "title"}}{{ or .UserHandle .UserDid }}{{end}}
2
+
3
+
{{define "content"}}
4
+
<a href="/">back to timeline</a>
5
+
<h1>{{ or .UserHandle .UserDid }} profile</h1>
6
+
7
+
<h3>repos</h3>
8
+
<ul id="my-knots">
9
+
{{range .Repos}}
10
+
<li>
11
+
<code>name: {{.Name}}</code><br>
12
+
<code>knot: {{.Knot}}</code><br>
13
+
</li>
14
+
{{else}}
15
+
<p>you don't have any knots yet</p>
16
+
{{end}}
17
+
</ul>
18
+
{{end}}
+11
appview/state/middleware.go
+11
appview/state/middleware.go
···
3
import (
4
"log"
5
"net/http"
6
+
"strings"
7
"time"
8
9
comatproto "github.com/bluesky-social/indigo/api/atproto"
···
100
})
101
}
102
}
103
+
104
+
func StripLeadingAt(next http.Handler) http.Handler {
105
+
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
106
+
path := req.URL.Path
107
+
if strings.HasPrefix(path, "/@") {
108
+
req.URL.Path = "/" + strings.TrimPrefix(path, "/@")
109
+
}
110
+
next.ServeHTTP(w, req)
111
+
})
112
+
}
+55
appview/state/state.go
+55
appview/state/state.go
···
538
}
539
}
540
541
+
func (s *State) ProfilePage(w http.ResponseWriter, r *http.Request) {
542
+
didOrHandle := chi.URLParam(r, "user")
543
+
if didOrHandle == "" {
544
+
http.Error(w, "Bad request", http.StatusBadRequest)
545
+
return
546
+
}
547
+
548
+
ident, err := auth.ResolveIdent(r.Context(), didOrHandle)
549
+
if err != nil {
550
+
log.Printf("resolving identity: %s", err)
551
+
w.WriteHeader(http.StatusNotFound)
552
+
return
553
+
}
554
+
555
+
repos, err := s.db.GetAllReposByDid(ident.DID.String())
556
+
if err != nil {
557
+
log.Printf("getting repos for %s: %s", ident.DID.String(), err)
558
+
}
559
+
560
+
pages.ProfilePage(w, pages.ProfilePageParams{
561
+
LoggedInUser: s.auth.GetUser(r),
562
+
UserDid: ident.DID.String(),
563
+
UserHandle: ident.Handle.String(),
564
+
Repos: repos,
565
+
})
566
+
}
567
+
568
func (s *State) Router() http.Handler {
569
+
router := chi.NewRouter()
570
+
571
+
router.HandleFunc("/*", func(w http.ResponseWriter, r *http.Request) {
572
+
pat := chi.URLParam(r, "*")
573
+
if strings.HasPrefix(pat, "did:") || strings.HasPrefix(pat, "@") {
574
+
s.UserRouter().ServeHTTP(w, r)
575
+
} else {
576
+
s.StandardRouter().ServeHTTP(w, r)
577
+
}
578
+
})
579
+
580
+
return router
581
+
}
582
+
583
+
func (s *State) UserRouter() http.Handler {
584
+
r := chi.NewRouter()
585
+
586
+
// strip @ from user
587
+
r.Use(StripLeadingAt)
588
+
589
+
r.Route("/{user}", func(r chi.Router) {
590
+
r.Get("/", s.ProfilePage)
591
+
})
592
+
593
+
return r
594
+
}
595
+
596
+
func (s *State) StandardRouter() http.Handler {
597
r := chi.NewRouter()
598
599
r.Get("/", s.Timeline)