diff options
Diffstat (limited to '')
-rw-r--r-- | http/router.go | 14 | ||||
-rw-r--r-- | registry/registry.go | 41 | ||||
-rw-r--r-- | views/webfinger.go | 14 |
3 files changed, 41 insertions, 28 deletions
diff --git a/http/router.go b/http/router.go index f763c79..d5d0de5 100644 --- a/http/router.go +++ b/http/router.go @@ -37,28 +37,30 @@ func (s *Router) Start(zapWriter io.Writer) { // Webfinger router.GET("/.well-known/webfinger", func(c *gin.Context) { resourceParam := c.Query("resource") - resource, _ := s.registry.WebfingerResource(resourceParam) + resource, _ := s.registry.Webfinger(resourceParam) if resource != nil { c.JSON(http.StatusOK, resource) } else { - c.JSON(http.StatusNotFound, nil) + c.JSON(http.StatusNotFound, map[string]interface{}{}) } }) - // "User" endpoint + // Actor router.GET("/actors/:actor", func(c *gin.Context) { actorParam := c.Param("actor") resource, err := s.registry.Actor(actorParam) render(c, resource, err) }) + // Actor Followers router.GET("/actors/:actor/followers", func(c *gin.Context) { actorParam := c.Param("actor") resource, err := s.registry.Followers(actorParam) render(c, resource, err) }) + // Actor Following router.GET("/actors/:actor/following", func(c *gin.Context) { actorParam := c.Param("actor") resource, err := s.registry.Following(actorParam) @@ -77,7 +79,7 @@ func (s *Router) Start(zapWriter io.Writer) { // }) - // Outbox + // Actor Outbox router.GET("/actors/:actor/outbox", func(c *gin.Context) { actorParam := c.Param("actor") var resource map[string]interface{} @@ -90,7 +92,7 @@ func (s *Router) Start(zapWriter io.Writer) { render(c, resource, err) }) - // Single activity + // Actor Activity router.GET("/actors/:actor/activity/:id", func(c *gin.Context) { actorParam := c.Param("actor") idParam := c.Param("id") @@ -98,7 +100,7 @@ func (s *Router) Start(zapWriter io.Writer) { render(c, resource, err) }) - // Single note + // Activity Note router.GET("/actors/:actor/activity/:id/note", func(c *gin.Context) { actorParam := c.Param("actor") idParam := c.Param("id") diff --git a/registry/registry.go b/registry/registry.go index a24d2d7..451704e 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -3,7 +3,8 @@ package registry import ( "bytes" "encoding/gob" - "fmt" + "net/url" + "strings" "git.capotej.com/capotej/communique/config" "git.capotej.com/capotej/communique/models" @@ -26,7 +27,7 @@ func NewRegistry(cfg config.Config, persister *models.Persister) *Registry { reg := Registry{cfg: cfg, persister: persister} reg.handlerMap = make(map[string]Handler) for _, v := range cfg.Handlers { - reg.handlerMap[fqn(v.Name, cfg.Domain)] = Handler{handlerCfg: v} + reg.handlerMap[v.Name] = Handler{handlerCfg: v} } return ® } @@ -148,29 +149,35 @@ func (r *Registry) ActivityOrNote(activityOrNote, name, id string) (map[string]i return views.RenderNote(handler.handlerCfg.Name, r.cfg.Domain, outboxItem) } -func (r *Registry) WebfingerResource(fqn string) (*views.WebfingerResource, error) { - handler := r.findByFQN(fqn) - if handler == nil { - handler = r.findByFQN("acct:" + fqn) +// This has to handle various lookup formats: +// ?resource=acct:actor@domain +// ?resource=actor@domain +// ?resource=actor +// ?resource=acct:actor +func (r *Registry) Webfinger(fqn string) (*views.WebfingerResource, error) { + // Strip away acct: prefix, if found + fqn = strings.TrimPrefix(fqn, "acct:") + + // Strip away @domain suffix, if found + domainUrl, err := url.Parse(r.cfg.Domain) + if err != nil { + return nil, err } + hostname := "@" + domainUrl.Hostname() + fqn = strings.TrimSuffix(fqn, hostname) + + // We should just have $actor left + handler := r.findByName(fqn) if handler == nil { return nil, nil } - return views.RenderWebfingerResource(handler.handlerCfg.Name, r.cfg.Domain) + return views.RenderWebfinger(handler.handlerCfg.Name, r.cfg.Domain, hostname) } -func fqn(name, domain string) string { - return fmt.Sprintf("acct:%s@%s", name, domain) -} - -func (r *Registry) findByFQN(fqn string) *Handler { - handler, ok := r.handlerMap[fqn] +func (r *Registry) findByName(name string) *Handler { + handler, ok := r.handlerMap[name] if !ok { return nil } return &handler } - -func (r *Registry) findByName(name string) *Handler { - return r.findByFQN(fqn(name, r.cfg.Domain)) -} diff --git a/views/webfinger.go b/views/webfinger.go index 4252bae..5af6308 100644 --- a/views/webfinger.go +++ b/views/webfinger.go @@ -2,7 +2,8 @@ package views import ( "fmt" - "path" + + "git.capotej.com/capotej/communique/urls" ) type Link struct { @@ -17,14 +18,17 @@ type WebfingerResource struct { Links []Link `json:"links"` } -func RenderWebfingerResource(name, domain string) (*WebfingerResource, error) { +func RenderWebfinger(name, domain, hostname string) (*WebfingerResource, error) { + actorUrl, err := urls.UrlProfile(name, domain) + if err != nil { + return nil, err + } rs := WebfingerResource{ - // TODO clean up - Subject: fmt.Sprintf("acct:%s@%s", name, domain), + Subject: fmt.Sprintf("acct:%s%s", name, hostname), //hostname contains @ Aliases: []string{}, Links: []Link{{ Rel: "self", - Href: path.Join("https://", domain, "actors", name), + Href: actorUrl.String(), Type: "application/activity+json", }}, } |