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"
|
2020-04-29 17:41:45 +00:00
|
|
|
"encoding/json"
|
2018-03-13 15:55:45 +00:00
|
|
|
"net/http"
|
2020-04-29 17:41:45 +00:00
|
|
|
"time"
|
2017-09-06 11:38:22 +00:00
|
|
|
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 14:50:33 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2017-09-06 11:38:22 +00:00
|
|
|
"github.com/matrix-org/util"
|
2023-03-01 16:06:47 +00:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2017-09-06 11:38:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetEvent returns the requested event
|
|
|
|
func GetEvent(
|
2017-09-13 12:37:50 +00:00
|
|
|
ctx context.Context,
|
2023-04-19 14:50:33 +00:00
|
|
|
request *fclient.FederationRequest,
|
2022-05-05 18:30:38 +00:00
|
|
|
rsAPI api.FederationRoomserverAPI,
|
2017-09-06 11:38:22 +00:00
|
|
|
eventID string,
|
2023-04-19 14:50:33 +00:00
|
|
|
origin spec.ServerName,
|
2017-09-06 11:38:22 +00:00
|
|
|
) util.JSONResponse {
|
2020-06-23 10:47:48 +00:00
|
|
|
err := allowedToSeeEvent(ctx, request.Origin(), rsAPI, eventID)
|
|
|
|
if err != nil {
|
|
|
|
return *err
|
|
|
|
}
|
2023-03-01 16:06:47 +00:00
|
|
|
// /_matrix/federation/v1/event/{eventId} doesn't have a roomID, we use an empty string,
|
|
|
|
// which results in `QueryEventsByID` to first get the event and use that to determine the roomID.
|
|
|
|
event, err := fetchEvent(ctx, rsAPI, "", eventID)
|
2018-06-22 10:46:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return *err
|
|
|
|
}
|
|
|
|
|
2020-04-29 17:41:45 +00:00
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: gomatrixserverlib.Transaction{
|
|
|
|
Origin: origin,
|
2023-04-19 14:50:33 +00:00
|
|
|
OriginServerTS: spec.AsTimestamp(time.Now()),
|
2020-04-29 17:41:45 +00:00
|
|
|
PDUs: []json.RawMessage{
|
|
|
|
event.JSON(),
|
|
|
|
},
|
|
|
|
}}
|
2018-06-22 10:46:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 10:47:48 +00:00
|
|
|
// allowedToSeeEvent returns no error if the server is allowed to see this event,
|
2018-06-22 10:46:19 +00:00
|
|
|
// otherwise it returns an error response which can be sent to the client.
|
2020-06-23 10:47:48 +00:00
|
|
|
func allowedToSeeEvent(
|
2018-06-22 10:46:19 +00:00
|
|
|
ctx context.Context,
|
2023-04-19 14:50:33 +00:00
|
|
|
origin spec.ServerName,
|
2022-05-05 18:30:38 +00:00
|
|
|
rsAPI api.FederationRoomserverAPI,
|
2018-06-22 10:46:19 +00:00
|
|
|
eventID string,
|
2020-06-23 10:47:48 +00:00
|
|
|
) *util.JSONResponse {
|
2023-03-01 16:06:47 +00:00
|
|
|
allowed, err := rsAPI.QueryServerAllowedToSeeEvent(ctx, origin, eventID)
|
2017-09-06 11:38:22 +00:00
|
|
|
if err != nil {
|
2018-06-22 10:46:19 +00:00
|
|
|
resErr := util.ErrorResponse(err)
|
2020-06-23 10:47:48 +00:00
|
|
|
return &resErr
|
2017-09-06 11:38:22 +00:00
|
|
|
}
|
|
|
|
|
2023-03-01 16:06:47 +00:00
|
|
|
if !allowed {
|
2018-06-22 10:46:19 +00:00
|
|
|
resErr := util.MessageResponse(http.StatusForbidden, "server not allowed to see event")
|
2020-06-23 10:47:48 +00:00
|
|
|
return &resErr
|
2017-09-06 11:38:22 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 10:47:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchEvent fetches the event without auth checks. Returns an error if the event cannot be found.
|
2023-03-01 16:06:47 +00:00
|
|
|
func fetchEvent(ctx context.Context, rsAPI api.FederationRoomserverAPI, roomID, eventID string) (*gomatrixserverlib.Event, *util.JSONResponse) {
|
2017-09-06 11:38:22 +00:00
|
|
|
var eventsResponse api.QueryEventsByIDResponse
|
2020-06-23 10:47:48 +00:00
|
|
|
err := rsAPI.QueryEventsByID(
|
2017-09-13 12:37:50 +00:00
|
|
|
ctx,
|
2023-03-01 16:06:47 +00:00
|
|
|
&api.QueryEventsByIDRequest{EventIDs: []string{eventID}, RoomID: roomID},
|
2017-09-06 11:38:22 +00:00
|
|
|
&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 {
|
2022-10-11 09:48:36 +00:00
|
|
|
return nil, &util.JSONResponse{
|
|
|
|
Code: http.StatusNotFound,
|
|
|
|
JSON: jsonerror.NotFound("Event not found"),
|
|
|
|
}
|
2017-09-06 11:38:22 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 15:44:53 +00:00
|
|
|
return eventsResponse.Events[0].Event, nil
|
2017-09-06 11:38:22 +00:00
|
|
|
}
|