2017-09-06 11:38:22 +00:00
|
|
|
// Copyright 2017 New Vector Ltd
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-10-25 10:27:44 +00:00
|
|
|
package routing
|
2017-09-06 11:38:22 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-13 12:37:50 +00:00
|
|
|
"context"
|
2018-03-13 15:55:45 +00:00
|
|
|
"net/http"
|
2017-09-06 11:38:22 +00:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetEvent returns the requested event
|
|
|
|
func GetEvent(
|
2017-09-13 12:37:50 +00:00
|
|
|
ctx context.Context,
|
2017-09-06 11:38:22 +00:00
|
|
|
request *gomatrixserverlib.FederationRequest,
|
|
|
|
query api.RoomserverQueryAPI,
|
|
|
|
eventID string,
|
|
|
|
) util.JSONResponse {
|
2018-06-22 10:46:19 +00:00
|
|
|
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) {
|
2017-09-06 11:38:22 +00:00
|
|
|
var authResponse api.QueryServerAllowedToSeeEventResponse
|
2017-09-13 12:37:50 +00:00
|
|
|
err := query.QueryServerAllowedToSeeEvent(
|
|
|
|
ctx,
|
|
|
|
&api.QueryServerAllowedToSeeEventRequest{
|
|
|
|
EventID: eventID,
|
|
|
|
ServerName: request.Origin(),
|
|
|
|
},
|
|
|
|
&authResponse,
|
|
|
|
)
|
2017-09-06 11:38:22 +00:00
|
|
|
if err != nil {
|
2018-06-22 10:46:19 +00:00
|
|
|
resErr := util.ErrorResponse(err)
|
|
|
|
return nil, &resErr
|
2017-09-06 11:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !authResponse.AllowedToSeeEvent {
|
2018-06-22 10:46:19 +00:00
|
|
|
resErr := util.MessageResponse(http.StatusForbidden, "server not allowed to see event")
|
|
|
|
return nil, &resErr
|
2017-09-06 11:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var eventsResponse api.QueryEventsByIDResponse
|
|
|
|
err = query.QueryEventsByID(
|
2017-09-13 12:37:50 +00:00
|
|
|
ctx,
|
2017-09-06 11:38:22 +00:00
|
|
|
&api.QueryEventsByIDRequest{EventIDs: []string{eventID}},
|
|
|
|
&eventsResponse,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2018-06-22 10:46:19 +00:00
|
|
|
resErr := util.ErrorResponse(err)
|
|
|
|
return nil, &resErr
|
2017-09-06 11:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(eventsResponse.Events) == 0 {
|
2018-06-22 10:46:19 +00:00
|
|
|
return nil, &util.JSONResponse{Code: http.StatusNotFound, JSON: nil}
|
2017-09-06 11:38:22 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 10:46:19 +00:00
|
|
|
return &eventsResponse.Events[0], nil
|
2017-09-06 11:38:22 +00:00
|
|
|
}
|