Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

[federation] Implement state APIs #486

Merged
merged 3 commits into from
Jun 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions src/github.com/matrix-org/dendrite/federationapi/routing/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ func GetEvent(
_ gomatrixserverlib.KeyRing,
eventID string,
) util.JSONResponse {
event, err := getEvent(ctx, request, query, eventID)
if err != nil {
return *err
}

return util.JSONResponse{Code: http.StatusOK, JSON: event}
}

// getEvent returns the requested event,
// otherwise it returns an error response which can be sent to the client.
func getEvent(
ctx context.Context,
request *gomatrixserverlib.FederationRequest,
query api.RoomserverQueryAPI,
eventID string,
) (*gomatrixserverlib.Event, *util.JSONResponse) {
var authResponse api.QueryServerAllowedToSeeEventResponse
err := query.QueryServerAllowedToSeeEvent(
ctx,
Expand All @@ -45,11 +61,13 @@ func GetEvent(
&authResponse,
)
if err != nil {
return util.ErrorResponse(err)
resErr := util.ErrorResponse(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These could also just be

return nil, &util.ErrorResponse(err)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't believe it either. That is not allowed in go.

return nil, &resErr
}

if !authResponse.AllowedToSeeEvent {
return util.MessageResponse(http.StatusForbidden, "server not allowed to see event")
resErr := util.MessageResponse(http.StatusForbidden, "server not allowed to see event")
return nil, &resErr
}

var eventsResponse api.QueryEventsByIDResponse
Expand All @@ -59,12 +77,13 @@ func GetEvent(
&eventsResponse,
)
if err != nil {
return util.ErrorResponse(err)
resErr := util.ErrorResponse(err)
return nil, &resErr
}

if len(eventsResponse.Events) == 0 {
return util.JSONResponse{Code: http.StatusNotFound, JSON: nil}
return nil, &util.JSONResponse{Code: http.StatusNotFound, JSON: nil}
}

return util.JSONResponse{Code: http.StatusOK, JSON: &eventsResponse.Events[0]}
return &eventsResponse.Events[0], nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ func Setup(
},
)).Methods(http.MethodGet)

v1fedmux.Handle("/state/{roomID}/{eventID}", common.MakeFedAPI(
"federation_get_event_auth", cfg.Matrix.ServerName, keys,
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
vars := mux.Vars(httpReq)
return GetState(
httpReq.Context(), request, cfg, query, time.Now(),
keys, vars["roomID"], vars["eventID"],
)
},
)).Methods(http.MethodGet)

v1fedmux.Handle("/state_ids/{roomID}/{eventID}", common.MakeFedAPI(
"federation_get_event_auth", cfg.Matrix.ServerName, keys,
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
vars := mux.Vars(httpReq)
return GetStateIDs(
httpReq.Context(), request, cfg, query, time.Now(),
keys, vars["roomID"], vars["eventID"],
)
},
)).Methods(http.MethodGet)

v1fedmux.Handle("/query/directory/", common.MakeFedAPI(
"federation_query_room_alias", cfg.Matrix.ServerName, keys,
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest) util.JSONResponse {
Expand Down
127 changes: 127 additions & 0 deletions src/github.com/matrix-org/dendrite/federationapi/routing/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// 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 routing

import (
"context"
"net/http"
"time"

"github.com/matrix-org/dendrite/common/config"
"github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util"
)

// GetState returns state events & auth events for the roomID, eventID
func GetState(
ctx context.Context,
request *gomatrixserverlib.FederationRequest,
_ config.Dendrite,
query api.RoomserverQueryAPI,
_ time.Time,
_ gomatrixserverlib.KeyRing,
roomID string,
eventID string,
) util.JSONResponse {
state, err := getState(ctx, request, query, roomID, eventID)
if err != nil {
return *err
}

return util.JSONResponse{Code: http.StatusOK, JSON: state}
}

// GetStateIDs returns state event IDs & auth event IDs for the roomID, eventID
func GetStateIDs(
ctx context.Context,
request *gomatrixserverlib.FederationRequest,
_ config.Dendrite,
query api.RoomserverQueryAPI,
_ time.Time,
_ gomatrixserverlib.KeyRing,
roomID string,
eventID string,
) util.JSONResponse {
state, err := getState(ctx, request, query, roomID, eventID)
if err != nil {
return *err
}

stateEventIDs := getIDsFromEvent(state.StateEvents)
authEventIDs := getIDsFromEvent(state.AuthEvents)

return util.JSONResponse{Code: http.StatusOK, JSON: gomatrixserverlib.RespStateIDs{
StateEventIDs: stateEventIDs,
AuthEventIDs: authEventIDs,
},
}
}

func getState(
ctx context.Context,
request *gomatrixserverlib.FederationRequest,
query api.RoomserverQueryAPI,
roomID string,
eventID string,
) (*gomatrixserverlib.RespState, *util.JSONResponse) {
event, resErr := getEvent(ctx, request, query, eventID)
if resErr != nil {
return nil, resErr
}

prevEventIDs := getIDsFromEventRef(event.PrevEvents())
authEventIDs := getIDsFromEventRef(event.AuthEvents())

var response api.QueryStateAndAuthChainResponse
err := query.QueryStateAndAuthChain(
ctx,
&api.QueryStateAndAuthChainRequest{
RoomID: roomID,
PrevEventIDs: prevEventIDs,
AuthEventIDs: authEventIDs,
},
&response,
)
if err != nil {
resErr := util.ErrorResponse(err)
return nil, &resErr
}

if !response.RoomExists {
return nil, &util.JSONResponse{Code: http.StatusNotFound, JSON: nil}
}

return &gomatrixserverlib.RespState{
StateEvents: response.StateEvents,
AuthEvents: response.AuthChainEvents,
}, nil
}

func getIDsFromEventRef(events []gomatrixserverlib.EventReference) []string {
IDs := make([]string, len(events))
for i := range events {
IDs[i] = events[i].EventID
}

return IDs
}

func getIDsFromEvent(events []gomatrixserverlib.Event) []string {
IDs := make([]string, len(events))
for i := range events {
IDs[i] = events[i].EventID()
}

return IDs
}