2019-08-22 06:47:52 -05:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-08-23 12:25:10 -05:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2019-08-22 06:47:52 -05:00
|
|
|
|
2019-08-23 12:25:10 -05:00
|
|
|
"github.com/matrix-org/dendrite/common"
|
2019-08-22 06:47:52 -05:00
|
|
|
"github.com/matrix-org/dendrite/federationsender/api"
|
|
|
|
"github.com/matrix-org/dendrite/federationsender/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2019-08-23 12:25:10 -05:00
|
|
|
"github.com/matrix-org/util"
|
2019-08-22 06:47:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// FederationSenderQueryDatabase has the APIs needed to implement the query API.
|
|
|
|
type FederationSenderQueryDatabase interface {
|
|
|
|
GetJoinedHosts(
|
|
|
|
ctx context.Context, roomID string,
|
|
|
|
) ([]types.JoinedHost, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FederationSenderQueryAPI is an implementation of api.FederationSenderQueryAPI
|
|
|
|
type FederationSenderQueryAPI struct {
|
|
|
|
DB FederationSenderQueryDatabase
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryJoinedHostsInRoom implements api.FederationSenderQueryAPI
|
|
|
|
func (f *FederationSenderQueryAPI) QueryJoinedHostsInRoom(
|
|
|
|
ctx context.Context,
|
|
|
|
request *api.QueryJoinedHostsInRoomRequest,
|
|
|
|
response *api.QueryJoinedHostsInRoomResponse,
|
|
|
|
) (err error) {
|
|
|
|
response.JoinedHosts, err = f.DB.GetJoinedHosts(ctx, request.RoomID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryJoinedHostServerNamesInRoom implements api.FederationSenderQueryAPI
|
|
|
|
func (f *FederationSenderQueryAPI) QueryJoinedHostServerNamesInRoom(
|
|
|
|
ctx context.Context,
|
|
|
|
request *api.QueryJoinedHostServerNamesInRoomRequest,
|
|
|
|
response *api.QueryJoinedHostServerNamesInRoomResponse,
|
|
|
|
) (err error) {
|
|
|
|
joinedHosts, err := f.DB.GetJoinedHosts(ctx, request.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-20 07:24:57 -06:00
|
|
|
response.ServerNames = make([]gomatrixserverlib.ServerName, 0, len(joinedHosts))
|
2019-08-22 06:47:52 -05:00
|
|
|
for _, host := range joinedHosts {
|
2019-12-20 07:24:57 -06:00
|
|
|
response.ServerNames = append(response.ServerNames, host.ServerName)
|
2019-08-22 06:47:52 -05:00
|
|
|
}
|
|
|
|
|
2019-12-20 07:24:57 -06:00
|
|
|
// TODO: remove duplicates?
|
2019-08-22 06:47:52 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2019-08-23 12:25:10 -05:00
|
|
|
|
|
|
|
// 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}
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|