2017-09-06 13:38:22 +02: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 12:27:44 +02:00
|
|
|
package routing
|
2017-09-06 13:38:22 +02:00
|
|
|
|
|
|
|
import (
|
2017-09-13 14:37:50 +02:00
|
|
|
"context"
|
2020-04-29 19:41:45 +02:00
|
|
|
"encoding/json"
|
2018-03-13 16:55:45 +01:00
|
|
|
"net/http"
|
2020-04-29 19:41:45 +02:00
|
|
|
"time"
|
2017-09-06 13:38:22 +02: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 14:37:50 +02:00
|
|
|
ctx context.Context,
|
2017-09-06 13:38:22 +02:00
|
|
|
request *gomatrixserverlib.FederationRequest,
|
2020-05-01 11:48:17 +02:00
|
|
|
rsAPI api.RoomserverInternalAPI,
|
2017-09-06 13:38:22 +02:00
|
|
|
eventID string,
|
2020-04-29 19:41:45 +02:00
|
|
|
origin gomatrixserverlib.ServerName,
|
2017-09-06 13:38:22 +02:00
|
|
|
) util.JSONResponse {
|
2020-05-01 11:48:17 +02:00
|
|
|
event, err := getEvent(ctx, request, rsAPI, eventID)
|
2018-06-22 12:46:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return *err
|
|
|
|
}
|
|
|
|
|
2020-04-29 19:41:45 +02:00
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: gomatrixserverlib.Transaction{
|
|
|
|
Origin: origin,
|
|
|
|
OriginServerTS: gomatrixserverlib.AsTimestamp(time.Now()),
|
|
|
|
PDUs: []json.RawMessage{
|
|
|
|
event.JSON(),
|
|
|
|
},
|
|
|
|
}}
|
2018-06-22 12:46:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
2020-05-01 11:48:17 +02:00
|
|
|
rsAPI api.RoomserverInternalAPI,
|
2018-06-22 12:46:19 +02:00
|
|
|
eventID string,
|
|
|
|
) (*gomatrixserverlib.Event, *util.JSONResponse) {
|
2017-09-06 13:38:22 +02:00
|
|
|
var authResponse api.QueryServerAllowedToSeeEventResponse
|
2020-05-01 11:48:17 +02:00
|
|
|
err := rsAPI.QueryServerAllowedToSeeEvent(
|
2017-09-13 14:37:50 +02:00
|
|
|
ctx,
|
|
|
|
&api.QueryServerAllowedToSeeEventRequest{
|
|
|
|
EventID: eventID,
|
|
|
|
ServerName: request.Origin(),
|
|
|
|
},
|
|
|
|
&authResponse,
|
|
|
|
)
|
2017-09-06 13:38:22 +02:00
|
|
|
if err != nil {
|
2018-06-22 12:46:19 +02:00
|
|
|
resErr := util.ErrorResponse(err)
|
|
|
|
return nil, &resErr
|
2017-09-06 13:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !authResponse.AllowedToSeeEvent {
|
2018-06-22 12:46:19 +02:00
|
|
|
resErr := util.MessageResponse(http.StatusForbidden, "server not allowed to see event")
|
|
|
|
return nil, &resErr
|
2017-09-06 13:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var eventsResponse api.QueryEventsByIDResponse
|
2020-05-01 11:48:17 +02:00
|
|
|
err = rsAPI.QueryEventsByID(
|
2017-09-13 14:37:50 +02:00
|
|
|
ctx,
|
2017-09-06 13:38:22 +02:00
|
|
|
&api.QueryEventsByIDRequest{EventIDs: []string{eventID}},
|
|
|
|
&eventsResponse,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2018-06-22 12:46:19 +02:00
|
|
|
resErr := util.ErrorResponse(err)
|
|
|
|
return nil, &resErr
|
2017-09-06 13:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(eventsResponse.Events) == 0 {
|
2018-06-22 12:46:19 +02:00
|
|
|
return nil, &util.JSONResponse{Code: http.StatusNotFound, JSON: nil}
|
2017-09-06 13:38:22 +02:00
|
|
|
}
|
|
|
|
|
2020-03-16 18:29:52 +01:00
|
|
|
return &eventsResponse.Events[0].Event, nil
|
2017-09-06 13:38:22 +02:00
|
|
|
}
|