diff --git a/federationsender/federationsender.go b/federationsender/federationsender.go index 9b732b386..a318d2099 100644 --- a/federationsender/federationsender.go +++ b/federationsender/federationsender.go @@ -15,11 +15,15 @@ package federationsender import ( + "net/http" + "github.com/matrix-org/dendrite/common/basecomponent" + "github.com/matrix-org/dendrite/federationsender/api" "github.com/matrix-org/dendrite/federationsender/consumers" + "github.com/matrix-org/dendrite/federationsender/query" "github.com/matrix-org/dendrite/federationsender/queue" "github.com/matrix-org/dendrite/federationsender/storage" - "github.com/matrix-org/dendrite/roomserver/api" + roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/gomatrixserverlib" "github.com/sirupsen/logrus" ) @@ -29,8 +33,8 @@ import ( func SetupFederationSenderComponent( base *basecomponent.BaseDendrite, federation *gomatrixserverlib.FederationClient, - queryAPI api.RoomserverQueryAPI, -) { + rsQueryAPI roomserverAPI.RoomserverQueryAPI, +) api.FederationSenderQueryAPI { federationSenderDB, err := storage.NewDatabase(string(base.Cfg.Database.FederationSender)) if err != nil { logrus.WithError(err).Panic("failed to connect to federation sender db") @@ -40,7 +44,7 @@ func SetupFederationSenderComponent( rsConsumer := consumers.NewOutputRoomEventConsumer( base.Cfg, base.KafkaConsumer, queues, - federationSenderDB, queryAPI, + federationSenderDB, rsQueryAPI, ) if err = rsConsumer.Start(); err != nil { logrus.WithError(err).Panic("failed to start room server consumer") @@ -52,4 +56,11 @@ func SetupFederationSenderComponent( if err := tsConsumer.Start(); err != nil { logrus.WithError(err).Panic("failed to start typing server consumer") } + + queryAPI := query.FederationSenderQueryAPI{ + DB: federationSenderDB, + } + queryAPI.SetupHTTP(http.DefaultServeMux) + + return &queryAPI } diff --git a/federationsender/query/query.go b/federationsender/query/query.go index ec9242863..088244826 100644 --- a/federationsender/query/query.go +++ b/federationsender/query/query.go @@ -2,10 +2,14 @@ package query import ( "context" + "encoding/json" + "net/http" + "github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/federationsender/api" "github.com/matrix-org/dendrite/federationsender/types" "github.com/matrix-org/gomatrixserverlib" + "github.com/matrix-org/util" ) // FederationSenderQueryDatabase has the APIs needed to implement the query API. @@ -53,3 +57,35 @@ func (f *FederationSenderQueryAPI) QueryJoinedHostServerNamesInRoom( return } + +// SetupHTTP adds the FederationSenderQueryAPI handlers to the http.ServeMux. +func (f *FederationSenderQueryAPI) SetupHTTP(servMux *http.ServeMux) { + servMux.Handle( + api.FederationSenderQueryJoinedHostsInRoomPath, + common.MakeInternalAPI("QueryJoinedHostsInRoom", func(req *http.Request) util.JSONResponse { + var request api.QueryJoinedHostsInRoomRequest + var response api.QueryJoinedHostsInRoomResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := f.QueryJoinedHostsInRoom(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + servMux.Handle( + api.FederationSenderQueryJoinedHostServerNamesInRoomPath, + common.MakeInternalAPI("QueryJoinedHostServerNamesInRoom", func(req *http.Request) util.JSONResponse { + var request api.QueryJoinedHostServerNamesInRoomRequest + var response api.QueryJoinedHostServerNamesInRoomResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := f.QueryJoinedHostServerNamesInRoom(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) +}