2020-04-29 12:34:31 +02:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/common"
|
2020-04-29 16:29:39 +02:00
|
|
|
"github.com/matrix-org/dendrite/common/config"
|
2020-04-29 12:34:31 +02:00
|
|
|
"github.com/matrix-org/dendrite/federationsender/api"
|
2020-04-29 16:29:39 +02:00
|
|
|
"github.com/matrix-org/dendrite/federationsender/producers"
|
2020-04-29 12:34:31 +02:00
|
|
|
"github.com/matrix-org/dendrite/federationsender/storage"
|
2020-04-29 16:29:39 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2020-04-29 12:34:31 +02:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FederationSenderInternalAPI is an implementation of api.FederationSenderInternalAPI
|
|
|
|
type FederationSenderInternalAPI struct {
|
|
|
|
api.FederationSenderInternalAPI
|
2020-04-29 16:29:39 +02:00
|
|
|
db storage.Database
|
|
|
|
cfg *config.Dendrite
|
|
|
|
producer *producers.RoomserverProducer
|
|
|
|
federation *gomatrixserverlib.FederationClient
|
|
|
|
keyRing *gomatrixserverlib.KeyRing
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFederationSenderInternalAPI(
|
|
|
|
db storage.Database, cfg *config.Dendrite,
|
|
|
|
producer *producers.RoomserverProducer,
|
|
|
|
federation *gomatrixserverlib.FederationClient,
|
|
|
|
keyRing *gomatrixserverlib.KeyRing,
|
|
|
|
) *FederationSenderInternalAPI {
|
|
|
|
return &FederationSenderInternalAPI{
|
|
|
|
db: db,
|
|
|
|
cfg: cfg,
|
|
|
|
producer: producer,
|
|
|
|
federation: federation,
|
|
|
|
keyRing: keyRing,
|
|
|
|
}
|
2020-04-29 12:34:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetupHTTP adds the FederationSenderInternalAPI handlers to the http.ServeMux.
|
|
|
|
func (f *FederationSenderInternalAPI) 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}
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
servMux.Handle(api.FederationSenderPerformJoinRequestPath,
|
|
|
|
common.MakeInternalAPI("PerformJoinRequest", 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())
|
|
|
|
}
|
2020-04-29 16:29:39 +02:00
|
|
|
if err := f.PerformJoin(req.Context(), &request, &response); err != nil {
|
2020-04-29 12:34:31 +02:00
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
servMux.Handle(api.FederationSenderPerformLeaveRequestPath,
|
|
|
|
common.MakeInternalAPI("PerformLeaveRequest", 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())
|
|
|
|
}
|
2020-04-29 16:29:39 +02:00
|
|
|
if err := f.PerformLeave(req.Context(), &request, &response); err != nil {
|
2020-04-29 12:34:31 +02:00
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|