2017-09-06 06:38:22 -05: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 05:27:44 -05:00
|
|
|
package routing
|
2017-09-06 06:38:22 -05:00
|
|
|
|
|
|
|
import (
|
2017-09-13 07:37:50 -05:00
|
|
|
"context"
|
2020-04-29 12:41:45 -05:00
|
|
|
"encoding/json"
|
2018-03-13 10:55:45 -05:00
|
|
|
"net/http"
|
2020-04-29 12:41:45 -05:00
|
|
|
"time"
|
2017-09-06 06:38:22 -05:00
|
|
|
|
2022-10-11 04:48:36 -05:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
2017-09-06 06:38:22 -05: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 07:37:50 -05:00
|
|
|
ctx context.Context,
|
2017-09-06 06:38:22 -05:00
|
|
|
request *gomatrixserverlib.FederationRequest,
|
2022-05-05 13:30:38 -05:00
|
|
|
rsAPI api.FederationRoomserverAPI,
|
2017-09-06 06:38:22 -05:00
|
|
|
eventID string,
|
2020-04-29 12:41:45 -05:00
|
|
|
origin gomatrixserverlib.ServerName,
|
2017-09-06 06:38:22 -05:00
|
|
|
) util.JSONResponse {
|
2020-06-23 05:47:48 -05:00
|
|
|
err := allowedToSeeEvent(ctx, request.Origin(), rsAPI, eventID)
|
|
|
|
if err != nil {
|
|
|
|
return *err
|
|
|
|
}
|
|
|
|
event, err := fetchEvent(ctx, rsAPI, eventID)
|
2018-06-22 05:46:19 -05:00
|
|
|
if err != nil {
|
|
|
|
return *err
|
|
|
|
}
|
|
|
|
|
2020-04-29 12:41:45 -05: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 05:46:19 -05:00
|
|
|
}
|
|
|
|
|
2020-06-23 05:47:48 -05:00
|
|
|
// allowedToSeeEvent returns no error if the server is allowed to see this event,
|
2018-06-22 05:46:19 -05:00
|
|
|
// otherwise it returns an error response which can be sent to the client.
|
2020-06-23 05:47:48 -05:00
|
|
|
func allowedToSeeEvent(
|
2018-06-22 05:46:19 -05:00
|
|
|
ctx context.Context,
|
2020-06-23 05:47:48 -05:00
|
|
|
origin gomatrixserverlib.ServerName,
|
2022-05-05 13:30:38 -05:00
|
|
|
rsAPI api.FederationRoomserverAPI,
|
2018-06-22 05:46:19 -05:00
|
|
|
eventID string,
|
2020-06-23 05:47:48 -05:00
|
|
|
) *util.JSONResponse {
|
2017-09-06 06:38:22 -05:00
|
|
|
var authResponse api.QueryServerAllowedToSeeEventResponse
|
2020-05-01 04:48:17 -05:00
|
|
|
err := rsAPI.QueryServerAllowedToSeeEvent(
|
2017-09-13 07:37:50 -05:00
|
|
|
ctx,
|
|
|
|
&api.QueryServerAllowedToSeeEventRequest{
|
|
|
|
EventID: eventID,
|
2020-06-23 05:47:48 -05:00
|
|
|
ServerName: origin,
|
2017-09-13 07:37:50 -05:00
|
|
|
},
|
|
|
|
&authResponse,
|
|
|
|
)
|
2017-09-06 06:38:22 -05:00
|
|
|
if err != nil {
|
2018-06-22 05:46:19 -05:00
|
|
|
resErr := util.ErrorResponse(err)
|
2020-06-23 05:47:48 -05:00
|
|
|
return &resErr
|
2017-09-06 06:38:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !authResponse.AllowedToSeeEvent {
|
2018-06-22 05:46:19 -05:00
|
|
|
resErr := util.MessageResponse(http.StatusForbidden, "server not allowed to see event")
|
2020-06-23 05:47:48 -05:00
|
|
|
return &resErr
|
2017-09-06 06:38:22 -05:00
|
|
|
}
|
|
|
|
|
2020-06-23 05:47:48 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchEvent fetches the event without auth checks. Returns an error if the event cannot be found.
|
2022-05-05 13:30:38 -05:00
|
|
|
func fetchEvent(ctx context.Context, rsAPI api.FederationRoomserverAPI, eventID string) (*gomatrixserverlib.Event, *util.JSONResponse) {
|
2017-09-06 06:38:22 -05:00
|
|
|
var eventsResponse api.QueryEventsByIDResponse
|
2020-06-23 05:47:48 -05:00
|
|
|
err := rsAPI.QueryEventsByID(
|
2017-09-13 07:37:50 -05:00
|
|
|
ctx,
|
2017-09-06 06:38:22 -05:00
|
|
|
&api.QueryEventsByIDRequest{EventIDs: []string{eventID}},
|
|
|
|
&eventsResponse,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2018-06-22 05:46:19 -05:00
|
|
|
resErr := util.ErrorResponse(err)
|
|
|
|
return nil, &resErr
|
2017-09-06 06:38:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(eventsResponse.Events) == 0 {
|
2022-10-11 04:48:36 -05:00
|
|
|
return nil, &util.JSONResponse{
|
|
|
|
Code: http.StatusNotFound,
|
|
|
|
JSON: jsonerror.NotFound("Event not found"),
|
|
|
|
}
|
2017-09-06 06:38:22 -05:00
|
|
|
}
|
|
|
|
|
2020-11-16 09:44:53 -06:00
|
|
|
return eventsResponse.Events[0].Event, nil
|
2017-09-06 06:38:22 -05:00
|
|
|
}
|