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-06-23 12:47:48 +02:00
|
|
|
err := allowedToSeeEvent(ctx, request.Origin(), rsAPI, eventID)
|
|
|
|
if err != nil {
|
|
|
|
return *err
|
|
|
|
}
|
|
|
|
event, err := fetchEvent(ctx, 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
|
|
|
}
|
|
|
|
|
2020-06-23 12:47:48 +02:00
|
|
|
// allowedToSeeEvent returns no error if the server is allowed to see this event,
|
2018-06-22 12:46:19 +02:00
|
|
|
// otherwise it returns an error response which can be sent to the client.
|
2020-06-23 12:47:48 +02:00
|
|
|
func allowedToSeeEvent(
|
2018-06-22 12:46:19 +02:00
|
|
|
ctx context.Context,
|
2020-06-23 12:47:48 +02:00
|
|
|
origin gomatrixserverlib.ServerName,
|
2020-05-01 11:48:17 +02:00
|
|
|
rsAPI api.RoomserverInternalAPI,
|
2018-06-22 12:46:19 +02:00
|
|
|
eventID string,
|
2020-06-23 12:47:48 +02:00
|
|
|
) *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,
|
2020-06-23 12:47:48 +02:00
|
|
|
ServerName: origin,
|
2017-09-13 14:37:50 +02:00
|
|
|
},
|
|
|
|
&authResponse,
|
|
|
|
)
|
2017-09-06 13:38:22 +02:00
|
|
|
if err != nil {
|
2018-06-22 12:46:19 +02:00
|
|
|
resErr := util.ErrorResponse(err)
|
2020-06-23 12:47:48 +02:00
|
|
|
return &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")
|
2020-06-23 12:47:48 +02:00
|
|
|
return &resErr
|
2017-09-06 13:38:22 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 12:47:48 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchEvent fetches the event without auth checks. Returns an error if the event cannot be found.
|
|
|
|
func fetchEvent(ctx context.Context, rsAPI api.RoomserverInternalAPI, eventID string) (*gomatrixserverlib.Event, *util.JSONResponse) {
|
2017-09-06 13:38:22 +02:00
|
|
|
var eventsResponse api.QueryEventsByIDResponse
|
2020-06-23 12:47:48 +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
|
|
|
}
|