diff --git a/appservice/api/query.go b/appservice/api/query.go index d36e138cb..6fcb20890 100644 --- a/appservice/api/query.go +++ b/appservice/api/query.go @@ -20,16 +20,12 @@ package api import ( "context" "database/sql" - "errors" - "net/http" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/dendrite/internal" - internalHTTP "github.com/matrix-org/dendrite/internal/http" - opentracing "github.com/opentracing/opentracing-go" ) // RoomAliasExistsRequest is a request to an application service @@ -83,60 +79,9 @@ type AppServiceQueryAPI interface { ) error } -// AppServiceRoomAliasExistsPath is the HTTP path for the RoomAliasExists API -const AppServiceRoomAliasExistsPath = "/appservice/RoomAliasExists" - -// AppServiceUserIDExistsPath is the HTTP path for the UserIDExists API -const AppServiceUserIDExistsPath = "/appservice/UserIDExists" - -// httpAppServiceQueryAPI contains the URL to an appservice query API and a -// reference to a httpClient used to reach it -type httpAppServiceQueryAPI struct { - appserviceURL string - httpClient *http.Client -} - -// NewAppServiceQueryAPIHTTP creates a AppServiceQueryAPI implemented by talking -// to a HTTP POST API. -// If httpClient is nil an error is returned -func NewAppServiceQueryAPIHTTP( - appserviceURL string, - httpClient *http.Client, -) (AppServiceQueryAPI, error) { - if httpClient == nil { - return nil, errors.New("NewRoomserverAliasAPIHTTP: httpClient is ") - } - return &httpAppServiceQueryAPI{appserviceURL, httpClient}, nil -} - -// RoomAliasExists implements AppServiceQueryAPI -func (h *httpAppServiceQueryAPI) RoomAliasExists( - ctx context.Context, - request *RoomAliasExistsRequest, - response *RoomAliasExistsResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "appserviceRoomAliasExists") - defer span.Finish() - - apiURL := h.appserviceURL + AppServiceRoomAliasExistsPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// UserIDExists implements AppServiceQueryAPI -func (h *httpAppServiceQueryAPI) UserIDExists( - ctx context.Context, - request *UserIDExistsRequest, - response *UserIDExistsResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "appserviceUserIDExists") - defer span.Finish() - - apiURL := h.appserviceURL + AppServiceUserIDExistsPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - // RetrieveUserProfile is a wrapper that queries both the local database and // application services for a given user's profile +// TODO: Remove this, it's called from federationapi and clientapi but is a pure function func RetrieveUserProfile( ctx context.Context, userID string, diff --git a/appservice/appservice.go b/appservice/appservice.go index 68cf52e79..b5ffba5eb 100644 --- a/appservice/appservice.go +++ b/appservice/appservice.go @@ -23,6 +23,7 @@ import ( appserviceAPI "github.com/matrix-org/dendrite/appservice/api" "github.com/matrix-org/dendrite/appservice/consumers" + "github.com/matrix-org/dendrite/appservice/inthttp" "github.com/matrix-org/dendrite/appservice/query" "github.com/matrix-org/dendrite/appservice/routing" "github.com/matrix-org/dendrite/appservice/storage" @@ -77,14 +78,14 @@ func SetupAppServiceAPIComponent( // Create appserivce query API with an HTTP client that will be used for all // outbound and inbound requests (inbound only for the internal API) - appserviceQueryAPI := query.AppServiceQueryAPI{ + appserviceQueryAPI := &query.AppServiceQueryAPI{ HTTPClient: &http.Client{ Timeout: time.Second * 30, }, Cfg: base.Cfg, } - appserviceQueryAPI.SetupHTTP(base.InternalAPIMux) + inthttp.AddRoutes(appserviceQueryAPI, base.InternalAPIMux) consumer := consumers.NewOutputRoomEventConsumer( base.Cfg, base.KafkaConsumer, accountsDB, appserviceDB, @@ -105,7 +106,7 @@ func SetupAppServiceAPIComponent( accountsDB, federation, transactionsCache, ) - return &appserviceQueryAPI + return appserviceQueryAPI } // generateAppServiceAccounts creates a dummy account based off the diff --git a/appservice/inthttp/client.go b/appservice/inthttp/client.go new file mode 100644 index 000000000..acbd1211b --- /dev/null +++ b/appservice/inthttp/client.go @@ -0,0 +1,63 @@ +package inthttp + +import ( + "context" + "errors" + "net/http" + + "github.com/matrix-org/dendrite/appservice/api" + internalHTTP "github.com/matrix-org/dendrite/internal/http" + "github.com/opentracing/opentracing-go" +) + +// HTTP paths for the internal HTTP APIs +const ( + AppServiceRoomAliasExistsPath = "/appservice/RoomAliasExists" + AppServiceUserIDExistsPath = "/appservice/UserIDExists" +) + +// httpAppServiceQueryAPI contains the URL to an appservice query API and a +// reference to a httpClient used to reach it +type httpAppServiceQueryAPI struct { + appserviceURL string + httpClient *http.Client +} + +// NewAppserviceClient creates a AppServiceQueryAPI implemented by talking +// to a HTTP POST API. +// If httpClient is nil an error is returned +func NewAppserviceClient( + appserviceURL string, + httpClient *http.Client, +) (api.AppServiceQueryAPI, error) { + if httpClient == nil { + return nil, errors.New("NewRoomserverAliasAPIHTTP: httpClient is ") + } + return &httpAppServiceQueryAPI{appserviceURL, httpClient}, nil +} + +// RoomAliasExists implements AppServiceQueryAPI +func (h *httpAppServiceQueryAPI) RoomAliasExists( + ctx context.Context, + request *api.RoomAliasExistsRequest, + response *api.RoomAliasExistsResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "appserviceRoomAliasExists") + defer span.Finish() + + apiURL := h.appserviceURL + AppServiceRoomAliasExistsPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// UserIDExists implements AppServiceQueryAPI +func (h *httpAppServiceQueryAPI) UserIDExists( + ctx context.Context, + request *api.UserIDExistsRequest, + response *api.UserIDExistsResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "appserviceUserIDExists") + defer span.Finish() + + apiURL := h.appserviceURL + AppServiceUserIDExistsPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} diff --git a/appservice/inthttp/server.go b/appservice/inthttp/server.go new file mode 100644 index 000000000..1900635a8 --- /dev/null +++ b/appservice/inthttp/server.go @@ -0,0 +1,43 @@ +package inthttp + +import ( + "encoding/json" + "net/http" + + "github.com/gorilla/mux" + "github.com/matrix-org/dendrite/appservice/api" + "github.com/matrix-org/dendrite/internal" + "github.com/matrix-org/util" +) + +// AddRoutes adds the AppServiceQueryAPI handlers to the http.ServeMux. +func AddRoutes(a api.AppServiceQueryAPI, internalAPIMux *mux.Router) { + internalAPIMux.Handle( + AppServiceRoomAliasExistsPath, + internal.MakeInternalAPI("appserviceRoomAliasExists", func(req *http.Request) util.JSONResponse { + var request api.RoomAliasExistsRequest + var response api.RoomAliasExistsResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := a.RoomAliasExists(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle( + AppServiceUserIDExistsPath, + internal.MakeInternalAPI("appserviceUserIDExists", func(req *http.Request) util.JSONResponse { + var request api.UserIDExistsRequest + var response api.UserIDExistsResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := a.UserIDExists(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) +} diff --git a/appservice/query/query.go b/appservice/query/query.go index 812ca9f49..fa3844f68 100644 --- a/appservice/query/query.go +++ b/appservice/query/query.go @@ -18,16 +18,12 @@ package query import ( "context" - "encoding/json" "net/http" "net/url" "time" - "github.com/gorilla/mux" "github.com/matrix-org/dendrite/appservice/api" - "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/internal/config" - "github.com/matrix-org/util" opentracing "github.com/opentracing/opentracing-go" log "github.com/sirupsen/logrus" ) @@ -180,36 +176,3 @@ func makeHTTPClient() *http.Client { Timeout: time.Second * 30, } } - -// SetupHTTP adds the AppServiceQueryPAI handlers to the http.ServeMux. This -// handles and muxes incoming api requests the to internal AppServiceQueryAPI. -func (a *AppServiceQueryAPI) SetupHTTP(internalAPIMux *mux.Router) { - internalAPIMux.Handle( - api.AppServiceRoomAliasExistsPath, - internal.MakeInternalAPI("appserviceRoomAliasExists", func(req *http.Request) util.JSONResponse { - var request api.RoomAliasExistsRequest - var response api.RoomAliasExistsResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := a.RoomAliasExists(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle( - api.AppServiceUserIDExistsPath, - internal.MakeInternalAPI("appserviceUserIDExists", func(req *http.Request) util.JSONResponse { - var request api.UserIDExistsRequest - var response api.UserIDExistsResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := a.UserIDExists(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) -} diff --git a/cmd/dendrite-appservice-server/main.go b/cmd/dendrite-appservice-server/main.go index a508be3b4..c16505fd8 100644 --- a/cmd/dendrite-appservice-server/main.go +++ b/cmd/dendrite-appservice-server/main.go @@ -28,7 +28,7 @@ func main() { accountDB := base.CreateAccountsDB() deviceDB := base.CreateDeviceDB() federation := base.CreateFederationClient() - rsAPI := base.CreateHTTPRoomserverAPIs() + rsAPI := base.RoomserverHTTPClient() cache := transactions.New() appservice.SetupAppServiceAPIComponent( diff --git a/cmd/dendrite-client-api-server/main.go b/cmd/dendrite-client-api-server/main.go index 280e9cdef..ba8e6af7c 100644 --- a/cmd/dendrite-client-api-server/main.go +++ b/cmd/dendrite-client-api-server/main.go @@ -35,8 +35,8 @@ func main() { serverKeyAPI := base.CreateHTTPServerKeyAPIs() keyRing := serverKeyAPI.KeyRing() - asQuery := base.CreateHTTPAppServiceAPIs() - rsAPI := base.CreateHTTPRoomserverAPIs() + asQuery := base.AppserviceHTTPClient() + rsAPI := base.RoomserverHTTPClient() fsAPI := base.FederationSenderHTTPClient() rsAPI.SetFederationSenderAPI(fsAPI) eduInputAPI := eduserver.SetupEDUServerComponent(base, cache.New(), deviceDB) diff --git a/cmd/dendrite-federation-api-server/main.go b/cmd/dendrite-federation-api-server/main.go index 3c42532c0..aca6ba579 100644 --- a/cmd/dendrite-federation-api-server/main.go +++ b/cmd/dendrite-federation-api-server/main.go @@ -36,8 +36,8 @@ func main() { fsAPI := base.FederationSenderHTTPClient() - rsAPI := base.CreateHTTPRoomserverAPIs() - asAPI := base.CreateHTTPAppServiceAPIs() + rsAPI := base.RoomserverHTTPClient() + asAPI := base.AppserviceHTTPClient() rsAPI.SetFederationSenderAPI(fsAPI) eduInputAPI := eduserver.SetupEDUServerComponent(base, cache.New(), deviceDB) eduProducer := producers.NewEDUServerProducer(eduInputAPI) diff --git a/cmd/dendrite-federation-sender-server/main.go b/cmd/dendrite-federation-sender-server/main.go index 2b1e0ae8f..14524dcad 100644 --- a/cmd/dendrite-federation-sender-server/main.go +++ b/cmd/dendrite-federation-sender-server/main.go @@ -29,7 +29,7 @@ func main() { serverKeyAPI := base.CreateHTTPServerKeyAPIs() keyRing := serverKeyAPI.KeyRing() - rsAPI := base.CreateHTTPRoomserverAPIs() + rsAPI := base.RoomserverHTTPClient() fsAPI := federationsender.SetupFederationSenderComponent( base, federation, rsAPI, keyRing, ) diff --git a/cmd/dendrite-monolith-server/main.go b/cmd/dendrite-monolith-server/main.go index eedd4412e..ba9189256 100644 --- a/cmd/dendrite-monolith-server/main.go +++ b/cmd/dendrite-monolith-server/main.go @@ -83,21 +83,21 @@ func main() { ) rsAPI := rsComponent if base.UseHTTPAPIs { - rsAPI = base.CreateHTTPRoomserverAPIs() + rsAPI = base.RoomserverHTTPClient() } eduInputAPI := eduserver.SetupEDUServerComponent( base, cache.New(), deviceDB, ) if base.UseHTTPAPIs { - eduInputAPI = base.CreateHTTPEDUServerAPIs() + eduInputAPI = base.EDUServerClient() } asAPI := appservice.SetupAppServiceAPIComponent( base, accountDB, deviceDB, federation, rsAPI, transactions.New(), ) if base.UseHTTPAPIs { - asAPI = base.CreateHTTPAppServiceAPIs() + asAPI = base.AppserviceHTTPClient() } fsAPI := federationsender.SetupFederationSenderComponent( diff --git a/cmd/dendrite-public-rooms-api-server/main.go b/cmd/dendrite-public-rooms-api-server/main.go index c2df1b3f8..18a813baf 100644 --- a/cmd/dendrite-public-rooms-api-server/main.go +++ b/cmd/dendrite-public-rooms-api-server/main.go @@ -29,7 +29,7 @@ func main() { deviceDB := base.CreateDeviceDB() fsAPI := base.FederationSenderHTTPClient() - rsAPI := base.CreateHTTPRoomserverAPIs() + rsAPI := base.RoomserverHTTPClient() rsAPI.SetFederationSenderAPI(fsAPI) publicRoomsDB, err := storage.NewPublicRoomsServerDatabase(string(base.Cfg.Database.PublicRoomsAPI), base.Cfg.DbProperties(), cfg.Matrix.ServerName) diff --git a/cmd/dendrite-sync-api-server/main.go b/cmd/dendrite-sync-api-server/main.go index 5a1a80dfd..eeab115a9 100644 --- a/cmd/dendrite-sync-api-server/main.go +++ b/cmd/dendrite-sync-api-server/main.go @@ -28,7 +28,7 @@ func main() { accountDB := base.CreateAccountsDB() federation := base.CreateFederationClient() - rsAPI := base.CreateHTTPRoomserverAPIs() + rsAPI := base.RoomserverHTTPClient() syncapi.SetupSyncAPIComponent(base, deviceDB, accountDB, rsAPI, federation, cfg) diff --git a/cmd/roomserver-integration-tests/main.go b/cmd/roomserver-integration-tests/main.go index 126053bac..2433f9ff0 100644 --- a/cmd/roomserver-integration-tests/main.go +++ b/cmd/roomserver-integration-tests/main.go @@ -31,6 +31,7 @@ import ( "github.com/matrix-org/dendrite/internal/caching" "github.com/matrix-org/dendrite/internal/test" "github.com/matrix-org/dendrite/roomserver/api" + "github.com/matrix-org/dendrite/roomserver/inthttp" "github.com/matrix-org/gomatrixserverlib" ) @@ -209,7 +210,7 @@ func writeToRoomServer(input []string, roomserverURL string) error { return err } } - x, err := api.NewRoomserverInternalAPIHTTP(roomserverURL, &http.Client{Timeout: timeoutHTTP}, nil) + x, err := inthttp.NewRoomserverClient(roomserverURL, &http.Client{Timeout: timeoutHTTP}, nil) if err != nil { return err } @@ -276,7 +277,7 @@ func testRoomserver(input []string, wantOutput []string, checkQueries func(api.R cmd.Args = []string{"dendrite-room-server", "--config", filepath.Join(dir, test.ConfigFile)} gotOutput, err := runAndReadFromTopic(cmd, cfg.RoomServerURL()+"/metrics", doInput, outputTopic, len(wantOutput), func() { - queryAPI, _ := api.NewRoomserverInternalAPIHTTP("http://"+string(cfg.Listen.RoomServer), &http.Client{Timeout: timeoutHTTP}, cache) + queryAPI, _ := inthttp.NewRoomserverClient("http://"+string(cfg.Listen.RoomServer), &http.Client{Timeout: timeoutHTTP}, cache) checkQueries(queryAPI) }) if err != nil { diff --git a/eduserver/api/input.go b/eduserver/api/input.go index fa7f30cb6..0d0d21f33 100644 --- a/eduserver/api/input.go +++ b/eduserver/api/input.go @@ -19,12 +19,8 @@ package api import ( "context" - "errors" - "net/http" - internalHTTP "github.com/matrix-org/dendrite/internal/http" "github.com/matrix-org/gomatrixserverlib" - opentracing "github.com/opentracing/opentracing-go" ) // InputTypingEvent is an event for notifying the typing server about typing updates. @@ -77,48 +73,3 @@ type EDUServerInputAPI interface { response *InputSendToDeviceEventResponse, ) error } - -// EDUServerInputTypingEventPath is the HTTP path for the InputTypingEvent API. -const EDUServerInputTypingEventPath = "/eduserver/input" - -// EDUServerInputSendToDeviceEventPath is the HTTP path for the InputSendToDeviceEvent API. -const EDUServerInputSendToDeviceEventPath = "/eduserver/sendToDevice" - -// NewEDUServerInputAPIHTTP creates a EDUServerInputAPI implemented by talking to a HTTP POST API. -func NewEDUServerInputAPIHTTP(eduServerURL string, httpClient *http.Client) (EDUServerInputAPI, error) { - if httpClient == nil { - return nil, errors.New("NewTypingServerInputAPIHTTP: httpClient is ") - } - return &httpEDUServerInputAPI{eduServerURL, httpClient}, nil -} - -type httpEDUServerInputAPI struct { - eduServerURL string - httpClient *http.Client -} - -// InputTypingEvent implements EDUServerInputAPI -func (h *httpEDUServerInputAPI) InputTypingEvent( - ctx context.Context, - request *InputTypingEventRequest, - response *InputTypingEventResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "InputTypingEvent") - defer span.Finish() - - apiURL := h.eduServerURL + EDUServerInputTypingEventPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// InputSendToDeviceEvent implements EDUServerInputAPI -func (h *httpEDUServerInputAPI) InputSendToDeviceEvent( - ctx context.Context, - request *InputSendToDeviceEventRequest, - response *InputSendToDeviceEventResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "InputSendToDeviceEvent") - defer span.Finish() - - apiURL := h.eduServerURL + EDUServerInputSendToDeviceEventPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} diff --git a/eduserver/eduserver.go b/eduserver/eduserver.go index 6f664eb67..59decc8fb 100644 --- a/eduserver/eduserver.go +++ b/eduserver/eduserver.go @@ -21,6 +21,7 @@ import ( "github.com/matrix-org/dendrite/eduserver/api" "github.com/matrix-org/dendrite/eduserver/cache" "github.com/matrix-org/dendrite/eduserver/input" + "github.com/matrix-org/dendrite/eduserver/inthttp" "github.com/matrix-org/dendrite/internal/basecomponent" ) @@ -42,7 +43,7 @@ func SetupEDUServerComponent( ServerName: base.Cfg.Matrix.ServerName, } - inputAPI.SetupHTTP(base.InternalAPIMux) + inthttp.AddRoutes(inputAPI, base.InternalAPIMux) return inputAPI } diff --git a/eduserver/input/input.go b/eduserver/input/input.go index 4e3051950..0bbf5b844 100644 --- a/eduserver/input/input.go +++ b/eduserver/input/input.go @@ -19,17 +19,13 @@ package input import ( "context" "encoding/json" - "net/http" "time" "github.com/Shopify/sarama" - "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/auth/storage/devices" "github.com/matrix-org/dendrite/eduserver/api" "github.com/matrix-org/dendrite/eduserver/cache" - "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/gomatrixserverlib" - "github.com/matrix-org/util" "github.com/sirupsen/logrus" ) @@ -170,33 +166,3 @@ func (t *EDUServerInputAPI) sendToDeviceEvent(ise *api.InputSendToDeviceEvent) e return nil } - -// SetupHTTP adds the EDUServerInputAPI handlers to the http.ServeMux. -func (t *EDUServerInputAPI) SetupHTTP(internalAPIMux *mux.Router) { - internalAPIMux.Handle(api.EDUServerInputTypingEventPath, - internal.MakeInternalAPI("inputTypingEvents", func(req *http.Request) util.JSONResponse { - var request api.InputTypingEventRequest - var response api.InputTypingEventResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.MessageResponse(http.StatusBadRequest, err.Error()) - } - if err := t.InputTypingEvent(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle(api.EDUServerInputSendToDeviceEventPath, - internal.MakeInternalAPI("inputSendToDeviceEvents", func(req *http.Request) util.JSONResponse { - var request api.InputSendToDeviceEventRequest - var response api.InputSendToDeviceEventResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.MessageResponse(http.StatusBadRequest, err.Error()) - } - if err := t.InputSendToDeviceEvent(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) -} diff --git a/eduserver/inthttp/client.go b/eduserver/inthttp/client.go new file mode 100644 index 000000000..149e4fb04 --- /dev/null +++ b/eduserver/inthttp/client.go @@ -0,0 +1,56 @@ +package inthttp + +import ( + "context" + "errors" + "net/http" + + "github.com/matrix-org/dendrite/eduserver/api" + internalHTTP "github.com/matrix-org/dendrite/internal/http" + "github.com/opentracing/opentracing-go" +) + +// HTTP paths for the internal HTTP APIs +const ( + EDUServerInputTypingEventPath = "/eduserver/input" + EDUServerInputSendToDeviceEventPath = "/eduserver/sendToDevice" +) + +// NewEDUServerClient creates a EDUServerInputAPI implemented by talking to a HTTP POST API. +func NewEDUServerClient(eduServerURL string, httpClient *http.Client) (api.EDUServerInputAPI, error) { + if httpClient == nil { + return nil, errors.New("NewEDUServerClient: httpClient is ") + } + return &httpEDUServerInputAPI{eduServerURL, httpClient}, nil +} + +type httpEDUServerInputAPI struct { + eduServerURL string + httpClient *http.Client +} + +// InputTypingEvent implements EDUServerInputAPI +func (h *httpEDUServerInputAPI) InputTypingEvent( + ctx context.Context, + request *api.InputTypingEventRequest, + response *api.InputTypingEventResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "InputTypingEvent") + defer span.Finish() + + apiURL := h.eduServerURL + EDUServerInputTypingEventPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// InputSendToDeviceEvent implements EDUServerInputAPI +func (h *httpEDUServerInputAPI) InputSendToDeviceEvent( + ctx context.Context, + request *api.InputSendToDeviceEventRequest, + response *api.InputSendToDeviceEventResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "InputSendToDeviceEvent") + defer span.Finish() + + apiURL := h.eduServerURL + EDUServerInputSendToDeviceEventPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} diff --git a/eduserver/inthttp/server.go b/eduserver/inthttp/server.go new file mode 100644 index 000000000..6c7d21a4e --- /dev/null +++ b/eduserver/inthttp/server.go @@ -0,0 +1,41 @@ +package inthttp + +import ( + "encoding/json" + "net/http" + + "github.com/gorilla/mux" + "github.com/matrix-org/dendrite/eduserver/api" + "github.com/matrix-org/dendrite/internal" + "github.com/matrix-org/util" +) + +// AddRoutes adds the EDUServerInputAPI handlers to the http.ServeMux. +func AddRoutes(t api.EDUServerInputAPI, internalAPIMux *mux.Router) { + internalAPIMux.Handle(EDUServerInputTypingEventPath, + internal.MakeInternalAPI("inputTypingEvents", func(req *http.Request) util.JSONResponse { + var request api.InputTypingEventRequest + var response api.InputTypingEventResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.MessageResponse(http.StatusBadRequest, err.Error()) + } + if err := t.InputTypingEvent(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle(EDUServerInputSendToDeviceEventPath, + internal.MakeInternalAPI("inputSendToDeviceEvents", func(req *http.Request) util.JSONResponse { + var request api.InputSendToDeviceEventRequest + var response api.InputSendToDeviceEventResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.MessageResponse(http.StatusBadRequest, err.Error()) + } + if err := t.InputSendToDeviceEvent(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) +} diff --git a/internal/basecomponent/base.go b/internal/basecomponent/base.go index a1a908760..cd297b82e 100644 --- a/internal/basecomponent/base.go +++ b/internal/basecomponent/base.go @@ -36,11 +36,14 @@ import ( "github.com/gorilla/mux" appserviceAPI "github.com/matrix-org/dendrite/appservice/api" + asinthttp "github.com/matrix-org/dendrite/appservice/inthttp" eduServerAPI "github.com/matrix-org/dendrite/eduserver/api" + eduinthttp "github.com/matrix-org/dendrite/eduserver/inthttp" federationSenderAPI "github.com/matrix-org/dendrite/federationsender/api" - "github.com/matrix-org/dendrite/federationsender/inthttp" + fsinthttp "github.com/matrix-org/dendrite/federationsender/inthttp" "github.com/matrix-org/dendrite/internal/config" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" + rsinthttp "github.com/matrix-org/dendrite/roomserver/inthttp" serverKeyAPI "github.com/matrix-org/dendrite/serverkeyapi/api" "github.com/sirupsen/logrus" @@ -136,32 +139,29 @@ func (b *BaseDendrite) Close() error { return b.tracerCloser.Close() } -// CreateHTTPAppServiceAPIs returns the QueryAPI for hitting the appservice -// component over HTTP. -func (b *BaseDendrite) CreateHTTPAppServiceAPIs() appserviceAPI.AppServiceQueryAPI { - a, err := appserviceAPI.NewAppServiceQueryAPIHTTP(b.Cfg.AppServiceURL(), b.httpClient) +// AppserviceHTTPClient returns the AppServiceQueryAPI for hitting the appservice component over HTTP. +func (b *BaseDendrite) AppserviceHTTPClient() appserviceAPI.AppServiceQueryAPI { + a, err := asinthttp.NewAppserviceClient(b.Cfg.AppServiceURL(), b.httpClient) if err != nil { logrus.WithError(err).Panic("CreateHTTPAppServiceAPIs failed") } return a } -// CreateHTTPRoomserverAPIs returns the AliasAPI, InputAPI and QueryAPI for hitting -// the roomserver over HTTP. -func (b *BaseDendrite) CreateHTTPRoomserverAPIs() roomserverAPI.RoomserverInternalAPI { - rsAPI, err := roomserverAPI.NewRoomserverInternalAPIHTTP(b.Cfg.RoomServerURL(), b.httpClient, b.ImmutableCache) +// RoomserverHTTPClient returns RoomserverInternalAPI for hitting the roomserver over HTTP. +func (b *BaseDendrite) RoomserverHTTPClient() roomserverAPI.RoomserverInternalAPI { + rsAPI, err := rsinthttp.NewRoomserverClient(b.Cfg.RoomServerURL(), b.httpClient, b.ImmutableCache) if err != nil { - logrus.WithError(err).Panic("NewRoomserverInternalAPIHTTP failed", b.httpClient) + logrus.WithError(err).Panic("RoomserverHTTPClient failed", b.httpClient) } return rsAPI } -// CreateHTTPEDUServerAPIs returns eduInputAPI for hitting the EDU -// server over HTTP -func (b *BaseDendrite) CreateHTTPEDUServerAPIs() eduServerAPI.EDUServerInputAPI { - e, err := eduServerAPI.NewEDUServerInputAPIHTTP(b.Cfg.EDUServerURL(), b.httpClient) +// EDUServerClient returns EDUServerInputAPI for hitting the EDU server over HTTP +func (b *BaseDendrite) EDUServerClient() eduServerAPI.EDUServerInputAPI { + e, err := eduinthttp.NewEDUServerClient(b.Cfg.EDUServerURL(), b.httpClient) if err != nil { - logrus.WithError(err).Panic("NewEDUServerInputAPIHTTP failed", b.httpClient) + logrus.WithError(err).Panic("EDUServerClient failed", b.httpClient) } return e } @@ -169,9 +169,9 @@ func (b *BaseDendrite) CreateHTTPEDUServerAPIs() eduServerAPI.EDUServerInputAPI // FederationSenderHTTPClient returns FederationSenderInternalAPI for hitting // the federation sender over HTTP func (b *BaseDendrite) FederationSenderHTTPClient() federationSenderAPI.FederationSenderInternalAPI { - f, err := inthttp.NewFederationSenderClient(b.Cfg.FederationSenderURL(), b.httpClient) + f, err := fsinthttp.NewFederationSenderClient(b.Cfg.FederationSenderURL(), b.httpClient) if err != nil { - logrus.WithError(err).Panic("NewFederationSenderInternalAPIHTTP failed", b.httpClient) + logrus.WithError(err).Panic("FederationSenderHTTPClient failed", b.httpClient) } return f } diff --git a/roomserver/api/alias.go b/roomserver/api/alias.go index 54d2c633b..61fdc6116 100644 --- a/roomserver/api/alias.go +++ b/roomserver/api/alias.go @@ -14,13 +14,6 @@ package api -import ( - "context" - - internalHTTP "github.com/matrix-org/dendrite/internal/http" - opentracing "github.com/opentracing/opentracing-go" -) - // SetRoomAliasRequest is a request to SetRoomAlias type SetRoomAliasRequest struct { // ID of the user setting the alias @@ -83,83 +76,3 @@ type RemoveRoomAliasRequest struct { // RemoveRoomAliasResponse is a response to RemoveRoomAlias type RemoveRoomAliasResponse struct{} - -// RoomserverSetRoomAliasPath is the HTTP path for the SetRoomAlias API. -const RoomserverSetRoomAliasPath = "/roomserver/setRoomAlias" - -// RoomserverGetRoomIDForAliasPath is the HTTP path for the GetRoomIDForAlias API. -const RoomserverGetRoomIDForAliasPath = "/roomserver/GetRoomIDForAlias" - -// RoomserverGetAliasesForRoomIDPath is the HTTP path for the GetAliasesForRoomID API. -const RoomserverGetAliasesForRoomIDPath = "/roomserver/GetAliasesForRoomID" - -// RoomserverGetCreatorIDForAliasPath is the HTTP path for the GetCreatorIDForAlias API. -const RoomserverGetCreatorIDForAliasPath = "/roomserver/GetCreatorIDForAlias" - -// RoomserverRemoveRoomAliasPath is the HTTP path for the RemoveRoomAlias API. -const RoomserverRemoveRoomAliasPath = "/roomserver/removeRoomAlias" - -// SetRoomAlias implements RoomserverAliasAPI -func (h *httpRoomserverInternalAPI) SetRoomAlias( - ctx context.Context, - request *SetRoomAliasRequest, - response *SetRoomAliasResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "SetRoomAlias") - defer span.Finish() - - apiURL := h.roomserverURL + RoomserverSetRoomAliasPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// GetRoomIDForAlias implements RoomserverAliasAPI -func (h *httpRoomserverInternalAPI) GetRoomIDForAlias( - ctx context.Context, - request *GetRoomIDForAliasRequest, - response *GetRoomIDForAliasResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "GetRoomIDForAlias") - defer span.Finish() - - apiURL := h.roomserverURL + RoomserverGetRoomIDForAliasPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// GetAliasesForRoomID implements RoomserverAliasAPI -func (h *httpRoomserverInternalAPI) GetAliasesForRoomID( - ctx context.Context, - request *GetAliasesForRoomIDRequest, - response *GetAliasesForRoomIDResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "GetAliasesForRoomID") - defer span.Finish() - - apiURL := h.roomserverURL + RoomserverGetAliasesForRoomIDPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// GetCreatorIDForAlias implements RoomserverAliasAPI -func (h *httpRoomserverInternalAPI) GetCreatorIDForAlias( - ctx context.Context, - request *GetCreatorIDForAliasRequest, - response *GetCreatorIDForAliasResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "GetCreatorIDForAlias") - defer span.Finish() - - apiURL := h.roomserverURL + RoomserverGetCreatorIDForAliasPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// RemoveRoomAlias implements RoomserverAliasAPI -func (h *httpRoomserverInternalAPI) RemoveRoomAlias( - ctx context.Context, - request *RemoveRoomAliasRequest, - response *RemoveRoomAliasResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "RemoveRoomAlias") - defer span.Finish() - - apiURL := h.roomserverURL + RoomserverRemoveRoomAliasPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} diff --git a/roomserver/api/http.go b/roomserver/api/http.go deleted file mode 100644 index 8a4d72e4e..000000000 --- a/roomserver/api/http.go +++ /dev/null @@ -1,41 +0,0 @@ -package api - -import ( - "errors" - "net/http" - - fsInputAPI "github.com/matrix-org/dendrite/federationsender/api" - "github.com/matrix-org/dendrite/internal/caching" -) - -type httpRoomserverInternalAPI struct { - roomserverURL string - httpClient *http.Client - fsAPI fsInputAPI.FederationSenderInternalAPI - immutableCache caching.ImmutableCache -} - -// NewRoomserverInputAPIHTTP creates a RoomserverInputAPI implemented by talking to a HTTP POST API. -// If httpClient is nil an error is returned -func NewRoomserverInternalAPIHTTP( - roomserverURL string, - httpClient *http.Client, - //fsInputAPI fsAPI.FederationSenderInternalAPI, - immutableCache caching.ImmutableCache, -) (RoomserverInternalAPI, error) { - if httpClient == nil { - return nil, errors.New("NewRoomserverInternalAPIHTTP: httpClient is ") - } - return &httpRoomserverInternalAPI{ - roomserverURL: roomserverURL, - httpClient: httpClient, - immutableCache: immutableCache, - }, nil -} - -// SetFederationSenderInputAPI passes in a federation sender input API reference -// so that we can avoid the chicken-and-egg problem of both the roomserver input API -// and the federation sender input API being interdependent. -func (h *httpRoomserverInternalAPI) SetFederationSenderAPI(fsAPI fsInputAPI.FederationSenderInternalAPI) { - h.fsAPI = fsAPI -} diff --git a/roomserver/api/input.go b/roomserver/api/input.go index d35ead764..6c3c89413 100644 --- a/roomserver/api/input.go +++ b/roomserver/api/input.go @@ -16,11 +16,7 @@ package api import ( - "context" - - internalHTTP "github.com/matrix-org/dendrite/internal/http" "github.com/matrix-org/gomatrixserverlib" - opentracing "github.com/opentracing/opentracing-go" ) const ( @@ -101,19 +97,3 @@ type InputRoomEventsRequest struct { type InputRoomEventsResponse struct { EventID string `json:"event_id"` } - -// RoomserverInputRoomEventsPath is the HTTP path for the InputRoomEvents API. -const RoomserverInputRoomEventsPath = "/roomserver/inputRoomEvents" - -// InputRoomEvents implements RoomserverInputAPI -func (h *httpRoomserverInternalAPI) InputRoomEvents( - ctx context.Context, - request *InputRoomEventsRequest, - response *InputRoomEventsResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "InputRoomEvents") - defer span.Finish() - - apiURL := h.roomserverURL + RoomserverInputRoomEventsPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} diff --git a/roomserver/api/perform.go b/roomserver/api/perform.go index f5afd67bf..1cf54144e 100644 --- a/roomserver/api/perform.go +++ b/roomserver/api/perform.go @@ -1,19 +1,7 @@ package api import ( - "context" - - internalHTTP "github.com/matrix-org/dendrite/internal/http" "github.com/matrix-org/gomatrixserverlib" - "github.com/opentracing/opentracing-go" -) - -const ( - // RoomserverPerformJoinPath is the HTTP path for the PerformJoin API. - RoomserverPerformJoinPath = "/roomserver/performJoin" - - // RoomserverPerformLeavePath is the HTTP path for the PerformLeave API. - RoomserverPerformLeavePath = "/roomserver/performLeave" ) type PerformJoinRequest struct { @@ -27,18 +15,6 @@ type PerformJoinResponse struct { RoomID string `json:"room_id"` } -func (h *httpRoomserverInternalAPI) PerformJoin( - ctx context.Context, - request *PerformJoinRequest, - response *PerformJoinResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "PerformJoin") - defer span.Finish() - - apiURL := h.roomserverURL + RoomserverPerformJoinPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - type PerformLeaveRequest struct { RoomID string `json:"room_id"` UserID string `json:"user_id"` @@ -46,15 +22,3 @@ type PerformLeaveRequest struct { type PerformLeaveResponse struct { } - -func (h *httpRoomserverInternalAPI) PerformLeave( - ctx context.Context, - request *PerformLeaveRequest, - response *PerformLeaveResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "PerformLeave") - defer span.Finish() - - apiURL := h.roomserverURL + RoomserverPerformLeavePath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} diff --git a/roomserver/api/query.go b/roomserver/api/query.go index 916ecb362..dc005c77c 100644 --- a/roomserver/api/query.go +++ b/roomserver/api/query.go @@ -17,12 +17,8 @@ package api import ( - "context" - - internalHTTP "github.com/matrix-org/dendrite/internal/http" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" - opentracing "github.com/opentracing/opentracing-go" ) // QueryLatestEventsAndStateRequest is a request to QueryLatestEventsAndState @@ -271,204 +267,3 @@ type QueryRoomVersionForRoomRequest struct { type QueryRoomVersionForRoomResponse struct { RoomVersion gomatrixserverlib.RoomVersion `json:"room_version"` } - -// RoomserverQueryLatestEventsAndStatePath is the HTTP path for the QueryLatestEventsAndState API. -const RoomserverQueryLatestEventsAndStatePath = "/roomserver/queryLatestEventsAndState" - -// RoomserverQueryStateAfterEventsPath is the HTTP path for the QueryStateAfterEvents API. -const RoomserverQueryStateAfterEventsPath = "/roomserver/queryStateAfterEvents" - -// RoomserverQueryEventsByIDPath is the HTTP path for the QueryEventsByID API. -const RoomserverQueryEventsByIDPath = "/roomserver/queryEventsByID" - -// RoomserverQueryMembershipForUserPath is the HTTP path for the QueryMembershipForUser API. -const RoomserverQueryMembershipForUserPath = "/roomserver/queryMembershipForUser" - -// RoomserverQueryMembershipsForRoomPath is the HTTP path for the QueryMembershipsForRoom API -const RoomserverQueryMembershipsForRoomPath = "/roomserver/queryMembershipsForRoom" - -// RoomserverQueryInvitesForUserPath is the HTTP path for the QueryInvitesForUser API -const RoomserverQueryInvitesForUserPath = "/roomserver/queryInvitesForUser" - -// RoomserverQueryServerAllowedToSeeEventPath is the HTTP path for the QueryServerAllowedToSeeEvent API -const RoomserverQueryServerAllowedToSeeEventPath = "/roomserver/queryServerAllowedToSeeEvent" - -// RoomserverQueryMissingEventsPath is the HTTP path for the QueryMissingEvents API -const RoomserverQueryMissingEventsPath = "/roomserver/queryMissingEvents" - -// RoomserverQueryStateAndAuthChainPath is the HTTP path for the QueryStateAndAuthChain API -const RoomserverQueryStateAndAuthChainPath = "/roomserver/queryStateAndAuthChain" - -// RoomserverQueryBackfillPath is the HTTP path for the QueryBackfillPath API -const RoomserverQueryBackfillPath = "/roomserver/queryBackfill" - -// RoomserverQueryRoomVersionCapabilitiesPath is the HTTP path for the QueryRoomVersionCapabilities API -const RoomserverQueryRoomVersionCapabilitiesPath = "/roomserver/queryRoomVersionCapabilities" - -// RoomserverQueryRoomVersionForRoomPath is the HTTP path for the QueryRoomVersionForRoom API -const RoomserverQueryRoomVersionForRoomPath = "/roomserver/queryRoomVersionForRoom" - -// QueryLatestEventsAndState implements RoomserverQueryAPI -func (h *httpRoomserverInternalAPI) QueryLatestEventsAndState( - ctx context.Context, - request *QueryLatestEventsAndStateRequest, - response *QueryLatestEventsAndStateResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "QueryLatestEventsAndState") - defer span.Finish() - - apiURL := h.roomserverURL + RoomserverQueryLatestEventsAndStatePath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// QueryStateAfterEvents implements RoomserverQueryAPI -func (h *httpRoomserverInternalAPI) QueryStateAfterEvents( - ctx context.Context, - request *QueryStateAfterEventsRequest, - response *QueryStateAfterEventsResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "QueryStateAfterEvents") - defer span.Finish() - - apiURL := h.roomserverURL + RoomserverQueryStateAfterEventsPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// QueryEventsByID implements RoomserverQueryAPI -func (h *httpRoomserverInternalAPI) QueryEventsByID( - ctx context.Context, - request *QueryEventsByIDRequest, - response *QueryEventsByIDResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "QueryEventsByID") - defer span.Finish() - - apiURL := h.roomserverURL + RoomserverQueryEventsByIDPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// QueryMembershipForUser implements RoomserverQueryAPI -func (h *httpRoomserverInternalAPI) QueryMembershipForUser( - ctx context.Context, - request *QueryMembershipForUserRequest, - response *QueryMembershipForUserResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "QueryMembershipForUser") - defer span.Finish() - - apiURL := h.roomserverURL + RoomserverQueryMembershipForUserPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// QueryMembershipsForRoom implements RoomserverQueryAPI -func (h *httpRoomserverInternalAPI) QueryMembershipsForRoom( - ctx context.Context, - request *QueryMembershipsForRoomRequest, - response *QueryMembershipsForRoomResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "QueryMembershipsForRoom") - defer span.Finish() - - apiURL := h.roomserverURL + RoomserverQueryMembershipsForRoomPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// QueryInvitesForUser implements RoomserverQueryAPI -func (h *httpRoomserverInternalAPI) QueryInvitesForUser( - ctx context.Context, - request *QueryInvitesForUserRequest, - response *QueryInvitesForUserResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "QueryInvitesForUser") - defer span.Finish() - - apiURL := h.roomserverURL + RoomserverQueryInvitesForUserPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// QueryServerAllowedToSeeEvent implements RoomserverQueryAPI -func (h *httpRoomserverInternalAPI) QueryServerAllowedToSeeEvent( - ctx context.Context, - request *QueryServerAllowedToSeeEventRequest, - response *QueryServerAllowedToSeeEventResponse, -) (err error) { - span, ctx := opentracing.StartSpanFromContext(ctx, "QueryServerAllowedToSeeEvent") - defer span.Finish() - - apiURL := h.roomserverURL + RoomserverQueryServerAllowedToSeeEventPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// QueryMissingEvents implements RoomServerQueryAPI -func (h *httpRoomserverInternalAPI) QueryMissingEvents( - ctx context.Context, - request *QueryMissingEventsRequest, - response *QueryMissingEventsResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "QueryMissingEvents") - defer span.Finish() - - apiURL := h.roomserverURL + RoomserverQueryMissingEventsPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// QueryStateAndAuthChain implements RoomserverQueryAPI -func (h *httpRoomserverInternalAPI) QueryStateAndAuthChain( - ctx context.Context, - request *QueryStateAndAuthChainRequest, - response *QueryStateAndAuthChainResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "QueryStateAndAuthChain") - defer span.Finish() - - apiURL := h.roomserverURL + RoomserverQueryStateAndAuthChainPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// QueryBackfill implements RoomServerQueryAPI -func (h *httpRoomserverInternalAPI) QueryBackfill( - ctx context.Context, - request *QueryBackfillRequest, - response *QueryBackfillResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "QueryBackfill") - defer span.Finish() - - apiURL := h.roomserverURL + RoomserverQueryBackfillPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// QueryRoomVersionCapabilities implements RoomServerQueryAPI -func (h *httpRoomserverInternalAPI) QueryRoomVersionCapabilities( - ctx context.Context, - request *QueryRoomVersionCapabilitiesRequest, - response *QueryRoomVersionCapabilitiesResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "QueryRoomVersionCapabilities") - defer span.Finish() - - apiURL := h.roomserverURL + RoomserverQueryRoomVersionCapabilitiesPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// QueryRoomVersionForRoom implements RoomServerQueryAPI -func (h *httpRoomserverInternalAPI) QueryRoomVersionForRoom( - ctx context.Context, - request *QueryRoomVersionForRoomRequest, - response *QueryRoomVersionForRoomResponse, -) error { - if roomVersion, ok := h.immutableCache.GetRoomVersion(request.RoomID); ok { - response.RoomVersion = roomVersion - return nil - } - - span, ctx := opentracing.StartSpanFromContext(ctx, "QueryRoomVersionForRoom") - defer span.Finish() - - apiURL := h.roomserverURL + RoomserverQueryRoomVersionForRoomPath - err := internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) - if err == nil { - h.immutableCache.StoreRoomVersion(request.RoomID, response.RoomVersion) - } - return err -} diff --git a/roomserver/internal/api.go b/roomserver/internal/api.go index 248e457d1..3a7b0d76a 100644 --- a/roomserver/internal/api.go +++ b/roomserver/internal/api.go @@ -1,20 +1,14 @@ package internal import ( - "encoding/json" - "net/http" "sync" "github.com/Shopify/sarama" - "github.com/gorilla/mux" fsAPI "github.com/matrix-org/dendrite/federationsender/api" - "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/internal/caching" "github.com/matrix-org/dendrite/internal/config" - "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/roomserver/storage" "github.com/matrix-org/gomatrixserverlib" - "github.com/matrix-org/util" ) // RoomserverInternalAPI is an implementation of api.RoomserverInternalAPI @@ -30,285 +24,3 @@ type RoomserverInternalAPI struct { mutex sync.Mutex // Protects calls to processRoomEvent fsAPI fsAPI.FederationSenderInternalAPI } - -// SetupHTTP adds the RoomserverInternalAPI handlers to the http.ServeMux. -// nolint: gocyclo -func (r *RoomserverInternalAPI) SetupHTTP(internalAPIMux *mux.Router) { - internalAPIMux.Handle(api.RoomserverInputRoomEventsPath, - internal.MakeInternalAPI("inputRoomEvents", func(req *http.Request) util.JSONResponse { - var request api.InputRoomEventsRequest - var response api.InputRoomEventsResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.MessageResponse(http.StatusBadRequest, err.Error()) - } - if err := r.InputRoomEvents(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle(api.RoomserverPerformJoinPath, - internal.MakeInternalAPI("performJoin", func(req *http.Request) util.JSONResponse { - var request api.PerformJoinRequest - var response api.PerformJoinResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.MessageResponse(http.StatusBadRequest, err.Error()) - } - if err := r.PerformJoin(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle(api.RoomserverPerformLeavePath, - internal.MakeInternalAPI("performLeave", func(req *http.Request) util.JSONResponse { - var request api.PerformLeaveRequest - var response api.PerformLeaveResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.MessageResponse(http.StatusBadRequest, err.Error()) - } - if err := r.PerformLeave(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle( - api.RoomserverQueryLatestEventsAndStatePath, - internal.MakeInternalAPI("queryLatestEventsAndState", func(req *http.Request) util.JSONResponse { - var request api.QueryLatestEventsAndStateRequest - var response api.QueryLatestEventsAndStateResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := r.QueryLatestEventsAndState(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle( - api.RoomserverQueryStateAfterEventsPath, - internal.MakeInternalAPI("queryStateAfterEvents", func(req *http.Request) util.JSONResponse { - var request api.QueryStateAfterEventsRequest - var response api.QueryStateAfterEventsResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := r.QueryStateAfterEvents(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle( - api.RoomserverQueryEventsByIDPath, - internal.MakeInternalAPI("queryEventsByID", func(req *http.Request) util.JSONResponse { - var request api.QueryEventsByIDRequest - var response api.QueryEventsByIDResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := r.QueryEventsByID(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle( - api.RoomserverQueryMembershipForUserPath, - internal.MakeInternalAPI("QueryMembershipForUser", func(req *http.Request) util.JSONResponse { - var request api.QueryMembershipForUserRequest - var response api.QueryMembershipForUserResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := r.QueryMembershipForUser(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle( - api.RoomserverQueryMembershipsForRoomPath, - internal.MakeInternalAPI("queryMembershipsForRoom", func(req *http.Request) util.JSONResponse { - var request api.QueryMembershipsForRoomRequest - var response api.QueryMembershipsForRoomResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := r.QueryMembershipsForRoom(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle( - api.RoomserverQueryInvitesForUserPath, - internal.MakeInternalAPI("queryInvitesForUser", func(req *http.Request) util.JSONResponse { - var request api.QueryInvitesForUserRequest - var response api.QueryInvitesForUserResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := r.QueryInvitesForUser(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle( - api.RoomserverQueryServerAllowedToSeeEventPath, - internal.MakeInternalAPI("queryServerAllowedToSeeEvent", func(req *http.Request) util.JSONResponse { - var request api.QueryServerAllowedToSeeEventRequest - var response api.QueryServerAllowedToSeeEventResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := r.QueryServerAllowedToSeeEvent(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle( - api.RoomserverQueryMissingEventsPath, - internal.MakeInternalAPI("queryMissingEvents", func(req *http.Request) util.JSONResponse { - var request api.QueryMissingEventsRequest - var response api.QueryMissingEventsResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := r.QueryMissingEvents(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle( - api.RoomserverQueryStateAndAuthChainPath, - internal.MakeInternalAPI("queryStateAndAuthChain", func(req *http.Request) util.JSONResponse { - var request api.QueryStateAndAuthChainRequest - var response api.QueryStateAndAuthChainResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := r.QueryStateAndAuthChain(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle( - api.RoomserverQueryBackfillPath, - internal.MakeInternalAPI("QueryBackfill", func(req *http.Request) util.JSONResponse { - var request api.QueryBackfillRequest - var response api.QueryBackfillResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := r.QueryBackfill(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle( - api.RoomserverQueryRoomVersionCapabilitiesPath, - internal.MakeInternalAPI("QueryRoomVersionCapabilities", func(req *http.Request) util.JSONResponse { - var request api.QueryRoomVersionCapabilitiesRequest - var response api.QueryRoomVersionCapabilitiesResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := r.QueryRoomVersionCapabilities(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle( - api.RoomserverQueryRoomVersionForRoomPath, - internal.MakeInternalAPI("QueryRoomVersionForRoom", func(req *http.Request) util.JSONResponse { - var request api.QueryRoomVersionForRoomRequest - var response api.QueryRoomVersionForRoomResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := r.QueryRoomVersionForRoom(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle( - api.RoomserverSetRoomAliasPath, - internal.MakeInternalAPI("setRoomAlias", func(req *http.Request) util.JSONResponse { - var request api.SetRoomAliasRequest - var response api.SetRoomAliasResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := r.SetRoomAlias(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle( - api.RoomserverGetRoomIDForAliasPath, - internal.MakeInternalAPI("GetRoomIDForAlias", func(req *http.Request) util.JSONResponse { - var request api.GetRoomIDForAliasRequest - var response api.GetRoomIDForAliasResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := r.GetRoomIDForAlias(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle( - api.RoomserverGetCreatorIDForAliasPath, - internal.MakeInternalAPI("GetCreatorIDForAlias", func(req *http.Request) util.JSONResponse { - var request api.GetCreatorIDForAliasRequest - var response api.GetCreatorIDForAliasResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := r.GetCreatorIDForAlias(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle( - api.RoomserverGetAliasesForRoomIDPath, - internal.MakeInternalAPI("getAliasesForRoomID", func(req *http.Request) util.JSONResponse { - var request api.GetAliasesForRoomIDRequest - var response api.GetAliasesForRoomIDResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := r.GetAliasesForRoomID(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle( - api.RoomserverRemoveRoomAliasPath, - internal.MakeInternalAPI("removeRoomAlias", func(req *http.Request) util.JSONResponse { - var request api.RemoveRoomAliasRequest - var response api.RemoveRoomAliasResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := r.RemoveRoomAlias(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) -} diff --git a/roomserver/inthttp/client.go b/roomserver/inthttp/client.go new file mode 100644 index 000000000..f8f922fbd --- /dev/null +++ b/roomserver/inthttp/client.go @@ -0,0 +1,341 @@ +package inthttp + +import ( + "context" + "errors" + "net/http" + + fsInputAPI "github.com/matrix-org/dendrite/federationsender/api" + "github.com/matrix-org/dendrite/internal/caching" + internalHTTP "github.com/matrix-org/dendrite/internal/http" + "github.com/matrix-org/dendrite/roomserver/api" + "github.com/opentracing/opentracing-go" +) + +const ( + // Alias operations + RoomserverSetRoomAliasPath = "/roomserver/setRoomAlias" + RoomserverGetRoomIDForAliasPath = "/roomserver/GetRoomIDForAlias" + RoomserverGetAliasesForRoomIDPath = "/roomserver/GetAliasesForRoomID" + RoomserverGetCreatorIDForAliasPath = "/roomserver/GetCreatorIDForAlias" + RoomserverRemoveRoomAliasPath = "/roomserver/removeRoomAlias" + + // Input operations + RoomserverInputRoomEventsPath = "/roomserver/inputRoomEvents" + + // Perform operations + RoomserverPerformJoinPath = "/roomserver/performJoin" + RoomserverPerformLeavePath = "/roomserver/performLeave" + + // Query operations + RoomserverQueryLatestEventsAndStatePath = "/roomserver/queryLatestEventsAndState" + RoomserverQueryStateAfterEventsPath = "/roomserver/queryStateAfterEvents" + RoomserverQueryEventsByIDPath = "/roomserver/queryEventsByID" + RoomserverQueryMembershipForUserPath = "/roomserver/queryMembershipForUser" + RoomserverQueryMembershipsForRoomPath = "/roomserver/queryMembershipsForRoom" + RoomserverQueryInvitesForUserPath = "/roomserver/queryInvitesForUser" + RoomserverQueryServerAllowedToSeeEventPath = "/roomserver/queryServerAllowedToSeeEvent" + RoomserverQueryMissingEventsPath = "/roomserver/queryMissingEvents" + RoomserverQueryStateAndAuthChainPath = "/roomserver/queryStateAndAuthChain" + RoomserverQueryBackfillPath = "/roomserver/queryBackfill" + RoomserverQueryRoomVersionCapabilitiesPath = "/roomserver/queryRoomVersionCapabilities" + RoomserverQueryRoomVersionForRoomPath = "/roomserver/queryRoomVersionForRoom" +) + +type httpRoomserverInternalAPI struct { + roomserverURL string + httpClient *http.Client + fsAPI fsInputAPI.FederationSenderInternalAPI + immutableCache caching.ImmutableCache +} + +// NewRoomserverClient creates a RoomserverInputAPI implemented by talking to a HTTP POST API. +// If httpClient is nil an error is returned +func NewRoomserverClient( + roomserverURL string, + httpClient *http.Client, + immutableCache caching.ImmutableCache, +) (api.RoomserverInternalAPI, error) { + if httpClient == nil { + return nil, errors.New("NewRoomserverInternalAPIHTTP: httpClient is ") + } + return &httpRoomserverInternalAPI{ + roomserverURL: roomserverURL, + httpClient: httpClient, + immutableCache: immutableCache, + }, nil +} + +// SetFederationSenderInputAPI passes in a federation sender input API reference +// so that we can avoid the chicken-and-egg problem of both the roomserver input API +// and the federation sender input API being interdependent. +func (h *httpRoomserverInternalAPI) SetFederationSenderAPI(fsAPI fsInputAPI.FederationSenderInternalAPI) { + h.fsAPI = fsAPI +} + +// SetRoomAlias implements RoomserverAliasAPI +func (h *httpRoomserverInternalAPI) SetRoomAlias( + ctx context.Context, + request *api.SetRoomAliasRequest, + response *api.SetRoomAliasResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "SetRoomAlias") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverSetRoomAliasPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// GetRoomIDForAlias implements RoomserverAliasAPI +func (h *httpRoomserverInternalAPI) GetRoomIDForAlias( + ctx context.Context, + request *api.GetRoomIDForAliasRequest, + response *api.GetRoomIDForAliasResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "GetRoomIDForAlias") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverGetRoomIDForAliasPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// GetAliasesForRoomID implements RoomserverAliasAPI +func (h *httpRoomserverInternalAPI) GetAliasesForRoomID( + ctx context.Context, + request *api.GetAliasesForRoomIDRequest, + response *api.GetAliasesForRoomIDResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "GetAliasesForRoomID") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverGetAliasesForRoomIDPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// GetCreatorIDForAlias implements RoomserverAliasAPI +func (h *httpRoomserverInternalAPI) GetCreatorIDForAlias( + ctx context.Context, + request *api.GetCreatorIDForAliasRequest, + response *api.GetCreatorIDForAliasResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "GetCreatorIDForAlias") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverGetCreatorIDForAliasPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// RemoveRoomAlias implements RoomserverAliasAPI +func (h *httpRoomserverInternalAPI) RemoveRoomAlias( + ctx context.Context, + request *api.RemoveRoomAliasRequest, + response *api.RemoveRoomAliasResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "RemoveRoomAlias") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverRemoveRoomAliasPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// InputRoomEvents implements RoomserverInputAPI +func (h *httpRoomserverInternalAPI) InputRoomEvents( + ctx context.Context, + request *api.InputRoomEventsRequest, + response *api.InputRoomEventsResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "InputRoomEvents") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverInputRoomEventsPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +func (h *httpRoomserverInternalAPI) PerformJoin( + ctx context.Context, + request *api.PerformJoinRequest, + response *api.PerformJoinResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "PerformJoin") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverPerformJoinPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +func (h *httpRoomserverInternalAPI) PerformLeave( + ctx context.Context, + request *api.PerformLeaveRequest, + response *api.PerformLeaveResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "PerformLeave") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverPerformLeavePath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// QueryLatestEventsAndState implements RoomserverQueryAPI +func (h *httpRoomserverInternalAPI) QueryLatestEventsAndState( + ctx context.Context, + request *api.QueryLatestEventsAndStateRequest, + response *api.QueryLatestEventsAndStateResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "QueryLatestEventsAndState") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverQueryLatestEventsAndStatePath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// QueryStateAfterEvents implements RoomserverQueryAPI +func (h *httpRoomserverInternalAPI) QueryStateAfterEvents( + ctx context.Context, + request *api.QueryStateAfterEventsRequest, + response *api.QueryStateAfterEventsResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "QueryStateAfterEvents") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverQueryStateAfterEventsPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// QueryEventsByID implements RoomserverQueryAPI +func (h *httpRoomserverInternalAPI) QueryEventsByID( + ctx context.Context, + request *api.QueryEventsByIDRequest, + response *api.QueryEventsByIDResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "QueryEventsByID") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverQueryEventsByIDPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// QueryMembershipForUser implements RoomserverQueryAPI +func (h *httpRoomserverInternalAPI) QueryMembershipForUser( + ctx context.Context, + request *api.QueryMembershipForUserRequest, + response *api.QueryMembershipForUserResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "QueryMembershipForUser") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverQueryMembershipForUserPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// QueryMembershipsForRoom implements RoomserverQueryAPI +func (h *httpRoomserverInternalAPI) QueryMembershipsForRoom( + ctx context.Context, + request *api.QueryMembershipsForRoomRequest, + response *api.QueryMembershipsForRoomResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "QueryMembershipsForRoom") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverQueryMembershipsForRoomPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// QueryInvitesForUser implements RoomserverQueryAPI +func (h *httpRoomserverInternalAPI) QueryInvitesForUser( + ctx context.Context, + request *api.QueryInvitesForUserRequest, + response *api.QueryInvitesForUserResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "QueryInvitesForUser") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverQueryInvitesForUserPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// QueryServerAllowedToSeeEvent implements RoomserverQueryAPI +func (h *httpRoomserverInternalAPI) QueryServerAllowedToSeeEvent( + ctx context.Context, + request *api.QueryServerAllowedToSeeEventRequest, + response *api.QueryServerAllowedToSeeEventResponse, +) (err error) { + span, ctx := opentracing.StartSpanFromContext(ctx, "QueryServerAllowedToSeeEvent") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverQueryServerAllowedToSeeEventPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// QueryMissingEvents implements RoomServerQueryAPI +func (h *httpRoomserverInternalAPI) QueryMissingEvents( + ctx context.Context, + request *api.QueryMissingEventsRequest, + response *api.QueryMissingEventsResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "QueryMissingEvents") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverQueryMissingEventsPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// QueryStateAndAuthChain implements RoomserverQueryAPI +func (h *httpRoomserverInternalAPI) QueryStateAndAuthChain( + ctx context.Context, + request *api.QueryStateAndAuthChainRequest, + response *api.QueryStateAndAuthChainResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "QueryStateAndAuthChain") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverQueryStateAndAuthChainPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// QueryBackfill implements RoomServerQueryAPI +func (h *httpRoomserverInternalAPI) QueryBackfill( + ctx context.Context, + request *api.QueryBackfillRequest, + response *api.QueryBackfillResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "QueryBackfill") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverQueryBackfillPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// QueryRoomVersionCapabilities implements RoomServerQueryAPI +func (h *httpRoomserverInternalAPI) QueryRoomVersionCapabilities( + ctx context.Context, + request *api.QueryRoomVersionCapabilitiesRequest, + response *api.QueryRoomVersionCapabilitiesResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "QueryRoomVersionCapabilities") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverQueryRoomVersionCapabilitiesPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// QueryRoomVersionForRoom implements RoomServerQueryAPI +func (h *httpRoomserverInternalAPI) QueryRoomVersionForRoom( + ctx context.Context, + request *api.QueryRoomVersionForRoomRequest, + response *api.QueryRoomVersionForRoomResponse, +) error { + if roomVersion, ok := h.immutableCache.GetRoomVersion(request.RoomID); ok { + response.RoomVersion = roomVersion + return nil + } + + span, ctx := opentracing.StartSpanFromContext(ctx, "QueryRoomVersionForRoom") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverQueryRoomVersionForRoomPath + err := internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) + if err == nil { + h.immutableCache.StoreRoomVersion(request.RoomID, response.RoomVersion) + } + return err +} diff --git a/roomserver/inthttp/server.go b/roomserver/inthttp/server.go new file mode 100644 index 000000000..9a58a30b0 --- /dev/null +++ b/roomserver/inthttp/server.go @@ -0,0 +1,293 @@ +package inthttp + +import ( + "encoding/json" + "net/http" + + "github.com/gorilla/mux" + "github.com/matrix-org/dendrite/internal" + "github.com/matrix-org/dendrite/roomserver/api" + "github.com/matrix-org/util" +) + +// AddRoutes adds the RoomserverInternalAPI handlers to the http.ServeMux. +// nolint: gocyclo +func AddRoutes(r api.RoomserverInternalAPI, internalAPIMux *mux.Router) { + internalAPIMux.Handle(RoomserverInputRoomEventsPath, + internal.MakeInternalAPI("inputRoomEvents", func(req *http.Request) util.JSONResponse { + var request api.InputRoomEventsRequest + var response api.InputRoomEventsResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.MessageResponse(http.StatusBadRequest, err.Error()) + } + if err := r.InputRoomEvents(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle(RoomserverPerformJoinPath, + internal.MakeInternalAPI("performJoin", func(req *http.Request) util.JSONResponse { + var request api.PerformJoinRequest + var response api.PerformJoinResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.MessageResponse(http.StatusBadRequest, err.Error()) + } + if err := r.PerformJoin(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle(RoomserverPerformLeavePath, + internal.MakeInternalAPI("performLeave", func(req *http.Request) util.JSONResponse { + var request api.PerformLeaveRequest + var response api.PerformLeaveResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.MessageResponse(http.StatusBadRequest, err.Error()) + } + if err := r.PerformLeave(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle( + RoomserverQueryLatestEventsAndStatePath, + internal.MakeInternalAPI("queryLatestEventsAndState", func(req *http.Request) util.JSONResponse { + var request api.QueryLatestEventsAndStateRequest + var response api.QueryLatestEventsAndStateResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := r.QueryLatestEventsAndState(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle( + RoomserverQueryStateAfterEventsPath, + internal.MakeInternalAPI("queryStateAfterEvents", func(req *http.Request) util.JSONResponse { + var request api.QueryStateAfterEventsRequest + var response api.QueryStateAfterEventsResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := r.QueryStateAfterEvents(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle( + RoomserverQueryEventsByIDPath, + internal.MakeInternalAPI("queryEventsByID", func(req *http.Request) util.JSONResponse { + var request api.QueryEventsByIDRequest + var response api.QueryEventsByIDResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := r.QueryEventsByID(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle( + RoomserverQueryMembershipForUserPath, + internal.MakeInternalAPI("QueryMembershipForUser", func(req *http.Request) util.JSONResponse { + var request api.QueryMembershipForUserRequest + var response api.QueryMembershipForUserResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := r.QueryMembershipForUser(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle( + RoomserverQueryMembershipsForRoomPath, + internal.MakeInternalAPI("queryMembershipsForRoom", func(req *http.Request) util.JSONResponse { + var request api.QueryMembershipsForRoomRequest + var response api.QueryMembershipsForRoomResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := r.QueryMembershipsForRoom(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle( + RoomserverQueryInvitesForUserPath, + internal.MakeInternalAPI("queryInvitesForUser", func(req *http.Request) util.JSONResponse { + var request api.QueryInvitesForUserRequest + var response api.QueryInvitesForUserResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := r.QueryInvitesForUser(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle( + RoomserverQueryServerAllowedToSeeEventPath, + internal.MakeInternalAPI("queryServerAllowedToSeeEvent", func(req *http.Request) util.JSONResponse { + var request api.QueryServerAllowedToSeeEventRequest + var response api.QueryServerAllowedToSeeEventResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := r.QueryServerAllowedToSeeEvent(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle( + RoomserverQueryMissingEventsPath, + internal.MakeInternalAPI("queryMissingEvents", func(req *http.Request) util.JSONResponse { + var request api.QueryMissingEventsRequest + var response api.QueryMissingEventsResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := r.QueryMissingEvents(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle( + RoomserverQueryStateAndAuthChainPath, + internal.MakeInternalAPI("queryStateAndAuthChain", func(req *http.Request) util.JSONResponse { + var request api.QueryStateAndAuthChainRequest + var response api.QueryStateAndAuthChainResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := r.QueryStateAndAuthChain(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle( + RoomserverQueryBackfillPath, + internal.MakeInternalAPI("QueryBackfill", func(req *http.Request) util.JSONResponse { + var request api.QueryBackfillRequest + var response api.QueryBackfillResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := r.QueryBackfill(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle( + RoomserverQueryRoomVersionCapabilitiesPath, + internal.MakeInternalAPI("QueryRoomVersionCapabilities", func(req *http.Request) util.JSONResponse { + var request api.QueryRoomVersionCapabilitiesRequest + var response api.QueryRoomVersionCapabilitiesResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := r.QueryRoomVersionCapabilities(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle( + RoomserverQueryRoomVersionForRoomPath, + internal.MakeInternalAPI("QueryRoomVersionForRoom", func(req *http.Request) util.JSONResponse { + var request api.QueryRoomVersionForRoomRequest + var response api.QueryRoomVersionForRoomResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := r.QueryRoomVersionForRoom(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle( + RoomserverSetRoomAliasPath, + internal.MakeInternalAPI("setRoomAlias", func(req *http.Request) util.JSONResponse { + var request api.SetRoomAliasRequest + var response api.SetRoomAliasResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := r.SetRoomAlias(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle( + RoomserverGetRoomIDForAliasPath, + internal.MakeInternalAPI("GetRoomIDForAlias", func(req *http.Request) util.JSONResponse { + var request api.GetRoomIDForAliasRequest + var response api.GetRoomIDForAliasResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := r.GetRoomIDForAlias(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle( + RoomserverGetCreatorIDForAliasPath, + internal.MakeInternalAPI("GetCreatorIDForAlias", func(req *http.Request) util.JSONResponse { + var request api.GetCreatorIDForAliasRequest + var response api.GetCreatorIDForAliasResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := r.GetCreatorIDForAlias(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle( + RoomserverGetAliasesForRoomIDPath, + internal.MakeInternalAPI("getAliasesForRoomID", func(req *http.Request) util.JSONResponse { + var request api.GetAliasesForRoomIDRequest + var response api.GetAliasesForRoomIDResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := r.GetAliasesForRoomID(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle( + RoomserverRemoveRoomAliasPath, + internal.MakeInternalAPI("removeRoomAlias", func(req *http.Request) util.JSONResponse { + var request api.RemoveRoomAliasRequest + var response api.RemoveRoomAliasResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := r.RemoveRoomAlias(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) +} diff --git a/roomserver/roomserver.go b/roomserver/roomserver.go index 82934d50d..ae0b0794a 100644 --- a/roomserver/roomserver.go +++ b/roomserver/roomserver.go @@ -16,6 +16,7 @@ package roomserver import ( "github.com/matrix-org/dendrite/roomserver/api" + "github.com/matrix-org/dendrite/roomserver/inthttp" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/dendrite/internal/basecomponent" @@ -38,7 +39,7 @@ func SetupRoomServerComponent( logrus.WithError(err).Panicf("failed to connect to room server db") } - internalAPI := internal.RoomserverInternalAPI{ + internalAPI := &internal.RoomserverInternalAPI{ DB: roomserverDB, Cfg: base.Cfg, Producer: base.KafkaProducer, @@ -49,7 +50,7 @@ func SetupRoomServerComponent( KeyRing: keyRing, } - internalAPI.SetupHTTP(base.InternalAPIMux) + inthttp.AddRoutes(internalAPI, base.InternalAPIMux) - return &internalAPI + return internalAPI } diff --git a/syncapi/api/query.go b/syncapi/api/query.go deleted file mode 100644 index 1da05fcc8..000000000 --- a/syncapi/api/query.go +++ /dev/null @@ -1,123 +0,0 @@ -// Copyright 2020 The Matrix.org Foundation C.I.C. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package api - -import ( - "context" - "net/http" - - internalHTTP "github.com/matrix-org/dendrite/internal/http" - "github.com/matrix-org/util" - opentracing "github.com/opentracing/opentracing-go" -) - -const ( - SyncAPIQuerySyncPath = "/syncapi/querySync" - SyncAPIQueryStatePath = "/syncapi/queryState" - SyncAPIQueryStateTypePath = "/syncapi/queryStateType" - SyncAPIQueryMessagesPath = "/syncapi/queryMessages" -) - -func NewSyncQueryAPIHTTP(syncapiURL string, httpClient *http.Client) SyncQueryAPI { - if httpClient == nil { - httpClient = http.DefaultClient - } - return &httpSyncQueryAPI{syncapiURL, httpClient} -} - -type httpSyncQueryAPI struct { - syncapiURL string - httpClient *http.Client -} - -type SyncQueryAPI interface { - QuerySync(ctx context.Context, request *QuerySyncRequest, response *QuerySyncResponse) error - QueryState(ctx context.Context, request *QueryStateRequest, response *QueryStateResponse) error - QueryStateType(ctx context.Context, request *QueryStateTypeRequest, response *QueryStateTypeResponse) error - QueryMessages(ctx context.Context, request *QueryMessagesRequest, response *QueryMessagesResponse) error -} - -type QuerySyncRequest struct{} - -type QueryStateRequest struct { - RoomID string -} - -type QueryStateTypeRequest struct { - RoomID string - EventType string - StateKey string -} - -type QueryMessagesRequest struct { - RoomID string -} - -type QuerySyncResponse util.JSONResponse -type QueryStateResponse util.JSONResponse -type QueryStateTypeResponse util.JSONResponse -type QueryMessagesResponse util.JSONResponse - -// QueryLatestEventsAndState implements SyncQueryAPI -func (h *httpSyncQueryAPI) QuerySync( - ctx context.Context, - request *QuerySyncRequest, - response *QuerySyncResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "QuerySync") - defer span.Finish() - - apiURL := h.syncapiURL + SyncAPIQuerySyncPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// QueryStateAfterEvents implements SyncQueryAPI -func (h *httpSyncQueryAPI) QueryState( - ctx context.Context, - request *QueryStateRequest, - response *QueryStateResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "QueryState") - defer span.Finish() - - apiURL := h.syncapiURL + SyncAPIQueryStatePath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// QueryEventsByID implements SyncQueryAPI -func (h *httpSyncQueryAPI) QueryStateType( - ctx context.Context, - request *QueryStateTypeRequest, - response *QueryStateTypeResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "QueryStateType") - defer span.Finish() - - apiURL := h.syncapiURL + SyncAPIQueryStateTypePath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// QueryMembershipForUser implements SyncQueryAPI -func (h *httpSyncQueryAPI) QueryMessages( - ctx context.Context, - request *QueryMessagesRequest, - response *QueryMessagesResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "QueryMessages") - defer span.Finish() - - apiURL := h.syncapiURL + SyncAPIQueryMessagesPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -}