Skip to content

Commit

Permalink
Support receiving /versions and enabling MSC3916 support
Browse files Browse the repository at this point in the history
  • Loading branch information
turt2live committed Apr 11, 2024
1 parent 7106477 commit c7e5b4b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
26 changes: 26 additions & 0 deletions api/r0/versions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package r0

import (
"github.com/getsentry/sentry-go"
"github.com/t2bot/matrix-media-repo/api/_apimeta"
"github.com/t2bot/matrix-media-repo/api/_responses"
"github.com/t2bot/matrix-media-repo/matrix"

"net/http"

"github.com/t2bot/matrix-media-repo/common/rcontext"
)

func ClientVersions(r *http.Request, rctx rcontext.RequestContext, user _apimeta.UserInfo) interface{} {
versions, err := matrix.ClientVersions(rctx, r.Host, user.UserId, user.AccessToken, r.RemoteAddr)
if err != nil {
rctx.Log.Error(err)
sentry.CaptureException(err)
return _responses.InternalServerError("unable to get versions")
}
if versions.UnstableFeatures == nil {
versions.UnstableFeatures = make(map[string]bool)
}
versions.UnstableFeatures["org.matrix.msc3916"] = true
return versions
}
5 changes: 5 additions & 0 deletions api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func buildRoutes() http.Handler {
register([]string{"POST"}, PrefixClient, "logout", mxSpecV3TransitionCS, router, makeRoute(_routers.RequireAccessToken(r0.Logout), "logout", counter))
register([]string{"POST"}, PrefixClient, "logout/all", mxSpecV3TransitionCS, router, makeRoute(_routers.RequireAccessToken(r0.LogoutAll), "logout_all", counter))
register([]string{"POST"}, PrefixMedia, "create", mxV1, router, makeRoute(_routers.RequireAccessToken(v1.CreateMedia), "create", counter))
register([]string{"GET"}, PrefixClient, "versions", mxNoVersion, router, makeRoute(_routers.OptionalAccessToken(r0.ClientVersions), "client_versions", counter))

// MSC3916 - Authentication & endpoint API separation
register([]string{"GET"}, PrefixClient, "media/preview_url", msc3916, router, previewUrlRoute)
Expand Down Expand Up @@ -148,12 +149,16 @@ var (
mxR0 matrixVersions = []string{"r0"}
mxV1 matrixVersions = []string{"v1"}
mxV3 matrixVersions = []string{"v3"}
mxNoVersion matrixVersions = []string{""}
)

func register(methods []string, prefix string, postfix string, versions matrixVersions, router *httprouter.Router, handler http.Handler) {
for _, method := range methods {
for _, version := range versions {
path := fmt.Sprintf("%s/%s/%s", prefix, version, postfix)
if version == "" {
path = fmt.Sprintf("%s/%s", prefix, postfix)
}
router.Handler(method, path, http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
defer func() {
// hopefully the body was already closed, but maybe it wasn't
Expand Down
10 changes: 10 additions & 0 deletions dev/homeserver.nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ server {
proxy_pass http://host.docker.internal:8001;
}

location /_matrix/client/versions {
proxy_set_header Host localhost;
proxy_pass http://host.docker.internal:8001;
}

location /_matrix/client/unstable/org.matrix.msc3916 {
proxy_set_header Host localhost;
proxy_pass http://host.docker.internal:8001;
}

location /_matrix {
proxy_pass http://media_repo_synapse:8008;
}
Expand Down
4 changes: 0 additions & 4 deletions matrix/breakers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ func getFederationBreaker(hostname string) *circuit.Breaker {
}

func doBreakerRequest(ctx rcontext.RequestContext, serverName string, accessToken string, appserviceUserId string, ipAddr string, method string, path string, resp interface{}) error {
if accessToken == "" {
return ErrInvalidToken
}

hs, cb := getBreakerAndConfig(serverName)

var replyError error
Expand Down
17 changes: 17 additions & 0 deletions matrix/requests_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package matrix

import "github.com/t2bot/matrix-media-repo/common/rcontext"

type ClientVersionsResponse struct {
Versions []string `json:"versions"`
UnstableFeatures map[string]bool `json:"unstable_features"`
}

func ClientVersions(ctx rcontext.RequestContext, serverName string, accessToken string, appserviceUserId string, ipAddr string) (*ClientVersionsResponse, error) {
response := &ClientVersionsResponse{}
err := doBreakerRequest(ctx, serverName, accessToken, appserviceUserId, ipAddr, "GET", "/_matrix/client/versions", response)
if err != nil {
return nil, err
}
return response, nil
}

0 comments on commit c7e5b4b

Please sign in to comment.