diff options
author | Julio Capote <jcapote@gmail.com> | 2022-12-19 00:29:19 +0000 |
---|---|---|
committer | Julio Capote <jcapote@gmail.com> | 2022-12-19 00:29:19 +0000 |
commit | 2ee94a66d89de9eb0461f250251af7776aef9976 (patch) | |
tree | 9cb4a3800c18f4f6b5df84d6cb0d861ff6ae8afc | |
parent | 4f8325f5df55628544f13937975f38c6ef5d17ab (diff) | |
download | communique-2ee94a66d89de9eb0461f250251af7776aef9976.tar.gz |
cleanup
-rw-r--r-- | http/server.go | 4 | ||||
-rw-r--r-- | registry/registry.go | 46 | ||||
-rw-r--r-- | resources/profile.go | 3 | ||||
-rw-r--r-- | resources/webfinger.go | 19 |
4 files changed, 47 insertions, 25 deletions
diff --git a/http/server.go b/http/server.go index d8560bb..5e6f652 100644 --- a/http/server.go +++ b/http/server.go @@ -25,7 +25,7 @@ func (s *Server) Start(zapWriter io.Writer) { // Webfinger router.GET("/.well-known/webfinger", func(c *gin.Context) { resourceParam := c.Query("resource") - resource := s.registry.LookupResource(resourceParam) + resource, _ := s.registry.WebfingerResource(resourceParam) if resource != nil { c.JSON(http.StatusOK, resource) } else { @@ -37,7 +37,7 @@ func (s *Server) Start(zapWriter io.Writer) { // "User" endpoint router.GET("/actors/:actor", func(c *gin.Context) { actorParam := c.Param("actor") - resource, _ := s.registry.LookupByName(actorParam) + resource, _ := s.registry.Profile(actorParam) if resource != nil { c.JSON(http.StatusOK, resource) } else { diff --git a/registry/registry.go b/registry/registry.go index 936ff54..3f88966 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -2,7 +2,6 @@ package registry import ( "fmt" - "path" "git.capotej.com/capotej/communique/config" "git.capotej.com/capotej/communique/resources" @@ -22,37 +21,40 @@ func NewRegistry(cfg config.Config) *Registry { reg.handlerMap = make(map[string]Handler) for _, v := range cfg.Handlers { // TODO clean up - fqdn := fmt.Sprintf("acct:%s@%s", v.Name, cfg.Domain) - reg.handlerMap[fqdn] = Handler{handlerCfg: v} + fqn := fmt.Sprintf("acct:%s@%s", v.Name, cfg.Domain) + reg.handlerMap[fqn] = Handler{handlerCfg: v} } return ® } -// TODO should probably be getprofilebyname -func (r *Registry) LookupByName(name string) (map[string]interface{}, error) { - fqdn := fmt.Sprintf("acct:%s@%s", name, r.cfg.Domain) - if r.LookupResource(fqdn) == nil { +func (r *Registry) Profile(name string) (map[string]interface{}, error) { + handler := r.findByName(name) + if handler == nil { return nil, nil } - return resources.RenderProfile(name, r.cfg.Domain) + return resources.RenderProfile(handler.handlerCfg.Name, r.cfg.Domain) } -// TODO extract an existsbyname existsbyfqdn from this -func (r *Registry) LookupResource(fqdn string) *resources.WebfingerResource { - handler, ok := r.handlerMap[fqdn] +func (r *Registry) WebfingerResource(fqn string) (*resources.WebfingerResource, error) { + handler := r.findByFQN(fqn) + if handler == nil { + handler = r.findByFQN("acct:" + fqn) + } + if handler == nil { + return nil, nil + } + return resources.RenderWebfingerResource(handler.handlerCfg.Name, r.cfg.Domain) +} + +func (r *Registry) findByFQN(fqn string) *Handler { + handler, ok := r.handlerMap[fqn] if !ok { return nil } - rs := resources.WebfingerResource{ - // TODO clean up - Subject: fmt.Sprintf("acct:%s@%s", handler.handlerCfg.Name, r.cfg.Domain), - Aliases: []string{}, - Links: []resources.Link{{ - Rel: "self", - Href: path.Join("https://", r.cfg.Domain, "actors", handler.handlerCfg.Name), - Type: "application/activity+json", - }}, - } - return &rs + return &handler +} +func (r *Registry) findByName(name string) *Handler { + fqn := fmt.Sprintf("acct:%s@%s", name, r.cfg.Domain) + return r.findByFQN(fqn) } diff --git a/resources/profile.go b/resources/profile.go index 23b2844..090075c 100644 --- a/resources/profile.go +++ b/resources/profile.go @@ -1,6 +1,7 @@ package resources import ( + "fmt" "net/url" "path" @@ -10,7 +11,7 @@ import ( func RenderProfile(name, domain string) (map[string]interface{}, error) { u, err := url.Parse(path.Join("https://", domain, "actors", name, "inbox")) if err != nil { - panic(err) + return nil, fmt.Errorf("could not partse url: %w", err) } inb := streams.NewActivityStreamsInboxProperty() diff --git a/resources/webfinger.go b/resources/webfinger.go index d936519..e7c47ea 100644 --- a/resources/webfinger.go +++ b/resources/webfinger.go @@ -1,5 +1,10 @@ package resources +import ( + "fmt" + "path" +) + type Link struct { Rel string `json:"rel"` Type string `json:"type"` @@ -11,3 +16,17 @@ type WebfingerResource struct { Aliases []string `json:"aliases"` Links []Link `json:"links"` } + +func RenderWebfingerResource(name, domain string) (*WebfingerResource, error) { + rs := WebfingerResource{ + // TODO clean up + Subject: fmt.Sprintf("acct:%s@%s", name, domain), + Aliases: []string{}, + Links: []Link{{ + Rel: "self", + Href: path.Join("https://", domain, "actors", name), + Type: "application/activity+json", + }}, + } + return &rs, nil +} |