forked from tangled.org/core
this repo has no description

begin work on user/ pages

Changed files
+95
appview
+11
appview/pages/pages.go
···
func NewRepo(w io.Writer, p NewRepoParams) error {
return parse("new-repo.html").Execute(w, p)
}
+
+
type ProfilePageParams struct {
+
LoggedInUser *auth.User
+
UserDid string
+
UserHandle string
+
Repos []db.Repo
+
}
+
+
func ProfilePage(w io.Writer, p ProfilePageParams) error {
+
return parse("profile.html").Execute(w, p)
+
}
+18
appview/pages/profile.html
···
+
{{define "title"}}{{ or .UserHandle .UserDid }}{{end}}
+
+
{{define "content"}}
+
<a href="/">back to timeline</a>
+
<h1>{{ or .UserHandle .UserDid }} profile</h1>
+
+
<h3>repos</h3>
+
<ul id="my-knots">
+
{{range .Repos}}
+
<li>
+
<code>name: {{.Name}}</code><br>
+
<code>knot: {{.Knot}}</code><br>
+
</li>
+
{{else}}
+
<p>you don't have any knots yet</p>
+
{{end}}
+
</ul>
+
{{end}}
+11
appview/state/middleware.go
···
import (
"log"
"net/http"
+
"strings"
"time"
comatproto "github.com/bluesky-social/indigo/api/atproto"
···
})
}
}
+
+
func StripLeadingAt(next http.Handler) http.Handler {
+
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
+
path := req.URL.Path
+
if strings.HasPrefix(path, "/@") {
+
req.URL.Path = "/" + strings.TrimPrefix(path, "/@")
+
}
+
next.ServeHTTP(w, req)
+
})
+
}
+55
appview/state/state.go
···
}
}
+
func (s *State) ProfilePage(w http.ResponseWriter, r *http.Request) {
+
didOrHandle := chi.URLParam(r, "user")
+
if didOrHandle == "" {
+
http.Error(w, "Bad request", http.StatusBadRequest)
+
return
+
}
+
+
ident, err := auth.ResolveIdent(r.Context(), didOrHandle)
+
if err != nil {
+
log.Printf("resolving identity: %s", err)
+
w.WriteHeader(http.StatusNotFound)
+
return
+
}
+
+
repos, err := s.db.GetAllReposByDid(ident.DID.String())
+
if err != nil {
+
log.Printf("getting repos for %s: %s", ident.DID.String(), err)
+
}
+
+
pages.ProfilePage(w, pages.ProfilePageParams{
+
LoggedInUser: s.auth.GetUser(r),
+
UserDid: ident.DID.String(),
+
UserHandle: ident.Handle.String(),
+
Repos: repos,
+
})
+
}
+
func (s *State) Router() http.Handler {
+
router := chi.NewRouter()
+
+
router.HandleFunc("/*", func(w http.ResponseWriter, r *http.Request) {
+
pat := chi.URLParam(r, "*")
+
if strings.HasPrefix(pat, "did:") || strings.HasPrefix(pat, "@") {
+
s.UserRouter().ServeHTTP(w, r)
+
} else {
+
s.StandardRouter().ServeHTTP(w, r)
+
}
+
})
+
+
return router
+
}
+
+
func (s *State) UserRouter() http.Handler {
+
r := chi.NewRouter()
+
+
// strip @ from user
+
r.Use(StripLeadingAt)
+
+
r.Route("/{user}", func(r chi.Router) {
+
r.Get("/", s.ProfilePage)
+
})
+
+
return r
+
}
+
+
func (s *State) StandardRouter() http.Handler {
r := chi.NewRouter()
r.Get("/", s.Timeline)