Skip to content

Commit

Permalink
Add nodeInfo endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
benpate committed Nov 7, 2023
1 parent 1ce1ee5 commit 4b48840
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 4 deletions.
128 changes: 125 additions & 3 deletions handler/nodeInfo.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,137 @@
package handler

import (
"net/http"

"github.com/EmissarySocial/emissary/server"
"github.com/benpate/derp"
"github.com/benpate/domain"
"github.com/labstack/echo/v4"
)

// GetNodeInfo returns public webfinger information for a designated user
func GetNodeInfo(factory *server.Factory) echo.HandlerFunc {
// GetNodeInfo returns the discovery links for nodeInfo endpoints
// http://nodeinfo.diaspora.software/protocol.html
// http://nodeinfo.diaspora.software/schema.html
func GetNodeInfo(serverFactory *server.Factory) echo.HandlerFunc {

return func(ctx echo.Context) error {

return nil
host := ctx.Request().Host
server := domain.AddProtocol(host)

result := map[string]any{
"links": []map[string]any{
{
"rel": "http://nodeinfo.diaspora.software/ns/schema/2.0",
"href": server + "/.well-known/nodeinfo/2.0",
},
{
"rel": "http://nodeinfo.diaspora.software/ns/schema/2.1",
"href": server + "/.well-known/nodeinfo/2.1",
},
},
}

return ctx.JSON(http.StatusOK, result)
}
}

// GetNodeInfo20 returns the nodeInfo 2.0 document for this server
// http://nodeinfo.diaspora.software/ns/schema/2.0
// http://nodeinfo.diaspora.software/docson/index.html#/ns/schema/2.0#$$expand
func GetNodeInfo20(serverFactory *server.Factory) echo.HandlerFunc {

return func(ctx echo.Context) error {

factory, err := serverFactory.ByContext(ctx)

if err != nil {
return derp.Wrap(err, "handler.GetNodeInfo20", "Error loading server factory")
}

// Get the Domain
domainService := factory.Domain()
domain, err := domainService.LoadDomain()

if err != nil {
return derp.Wrap(err, "handler.GetNodeInfo20", "Error loading domain")
}

result := map[string]any{
"version": "2.0",
"software": map[string]any{
"name": "Emissary",
"version": serverFactory.Version(),
},
"protocols": []string{"activitypub"},
"services": map[string]any{
"inbound": []string{"atom1.0", "rss2.0"},
"outbound": []string{"atom1.0", "rss2.0"},
},
"openRegistrations": domain.SignupForm.Active,
"usage": map[string]any{
"users": map[string]any{
"total": 0,
"activeHalfYear": 0,
"activeMonth": 0,
},
"localPosts": 0,
"localComments": 0,
},
"metadata": map[string]any{},
}

return ctx.JSON(http.StatusOK, result)
}
}

// GetNodeInfo21 returns the nodeInfo 2.1 document for this server
// http://nodeinfo.diaspora.software/ns/schema/2.1
// http://nodeinfo.diaspora.software/docson/index.html#/ns/schema/2.1#$$expand
func GetNodeInfo21(serverFactory *server.Factory) echo.HandlerFunc {

return func(ctx echo.Context) error {

factory, err := serverFactory.ByContext(ctx)

if err != nil {
return derp.Wrap(err, "handler.GetNodeInfo20", "Error loading server factory")
}

// Get the Domain
domainService := factory.Domain()
domain, err := domainService.LoadDomain()

if err != nil {
return derp.Wrap(err, "handler.GetNodeInfo20", "Error loading domain")
}

result := map[string]any{
"version": "2.1",
"software": map[string]any{
"name": "Emissary",
"version": serverFactory.Version(),
"repository": "https://github.com/EmissarySocial/emissary",
"homepage": "https://emissary.social",
},
"protocols": []string{"activitypub"},
"services": map[string]any{
"inbound": []string{"atom1.0", "rss2.0"},
"outbound": []string{"atom1.0", "rss2.0"},
},
"openRegistrations": domain.SignupForm.Active,
"usage": map[string]any{
"users": map[string]any{
"total": 0,
"activeHalfYear": 0,
"activeMonth": 0,
},
"localPosts": 0,
"localComments": 0,
},
"metadata": map[string]any{},
}

return ctx.JSON(http.StatusOK, result)
}
}
5 changes: 4 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,12 @@ func makeStandardRoutes(factory *server.Factory, e *echo.Echo) {
fmt.Println("Starting Emissary Server.")

e.Pre(mw.HttpsRedirect)
e.Pre(middleware.RemoveTrailingSlash())

// Middleware for standard pages
e.Use(mw.Domain(factory))
e.Use(steranko.Middleware(factory))
// e.Use(middleware.Logger())
e.Use(middleware.Logger())
/*
e.Use(middleware.BodyDump(func(c echo.Context, reqBody, resBody []byte) {
fmt.Println("---")
Expand All @@ -205,6 +206,8 @@ func makeStandardRoutes(factory *server.Factory, e *echo.Echo) {
e.GET("/.well-known/change-password", handler.TBD)
e.GET("/.well-known/host-meta", handler.TBD)
e.GET("/.well-known/nodeinfo", handler.GetNodeInfo(factory))
e.GET("/.well-known/nodeinfo/2.0", handler.GetNodeInfo20(factory))
e.GET("/.well-known/nodeinfo/2.1", handler.GetNodeInfo21(factory))
e.GET("/.well-known/oembed", handler.GetOEmbed(factory))
e.GET("/.well-known/webfinger", handler.GetWebfinger(factory))

Expand Down
4 changes: 4 additions & 0 deletions server/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ func (factory *Factory) refreshDomain(config config.Config, domainConfig config.
* Server Config Methods
****************************/

func (factory *Factory) Version() string {
return "0.1.0"
}

// Config returns the current configuration for the Factory
func (factory *Factory) Config() config.Config {

Expand Down

0 comments on commit 4b48840

Please sign in to comment.