2017-04-20 23:40:52 +01:00
|
|
|
// Copyright 2017 Vector Creations 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-11 18:16:53 +01:00
|
|
|
package routing
|
2017-02-03 16:05:46 +00:00
|
|
|
|
|
|
|
import (
|
2022-02-18 15:05:03 +00:00
|
|
|
"context"
|
2022-03-07 09:37:04 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2017-02-03 16:05:46 +00:00
|
|
|
"net/http"
|
2022-06-03 05:43:51 +01:00
|
|
|
"reflect"
|
2020-08-20 08:27:43 +01:00
|
|
|
"sync"
|
2020-12-17 10:52:31 +00:00
|
|
|
"time"
|
2017-02-03 16:05:46 +00:00
|
|
|
|
2022-02-18 15:05:03 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-02-18 15:05:03 +00:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2017-03-15 11:22:40 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
2020-06-12 14:55:57 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/eventutil"
|
2020-05-21 14:40:13 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/transactions"
|
2017-03-15 11:22:40 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2023-04-27 12:54:20 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2020-12-02 17:41:00 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2020-06-16 14:10:55 +01:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2017-02-03 16:05:46 +00:00
|
|
|
)
|
|
|
|
|
2017-03-15 11:22:40 +00:00
|
|
|
// http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid
|
2017-03-17 11:21:52 +00:00
|
|
|
// http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-state-eventtype-statekey
|
|
|
|
type sendEventResponse struct {
|
2017-03-15 11:22:40 +00:00
|
|
|
EventID string `json:"event_id"`
|
|
|
|
}
|
|
|
|
|
2020-08-20 08:27:43 +01:00
|
|
|
var (
|
|
|
|
userRoomSendMutexes sync.Map // (roomID+userID) -> mutex. mutexes to ensure correct ordering of sendEvents
|
|
|
|
)
|
|
|
|
|
2020-12-17 10:52:31 +00:00
|
|
|
var sendEventDuration = prometheus.NewHistogramVec(
|
|
|
|
prometheus.HistogramOpts{
|
|
|
|
Namespace: "dendrite",
|
|
|
|
Subsystem: "clientapi",
|
|
|
|
Name: "sendevent_duration_millis",
|
|
|
|
Help: "How long it takes to build and submit a new event from the client API to the roomserver",
|
|
|
|
Buckets: []float64{ // milliseconds
|
|
|
|
5, 10, 25, 50, 75, 100, 250, 500,
|
|
|
|
1000, 2000, 3000, 4000, 5000, 6000,
|
|
|
|
7000, 8000, 9000, 10000, 15000, 20000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[]string{"action"},
|
|
|
|
)
|
|
|
|
|
2017-03-17 11:21:52 +00:00
|
|
|
// SendEvent implements:
|
2022-08-05 10:12:41 +01:00
|
|
|
//
|
|
|
|
// /rooms/{roomID}/send/{eventType}
|
|
|
|
// /rooms/{roomID}/send/{eventType}/{txnID}
|
|
|
|
// /rooms/{roomID}/state/{eventType}/{stateKey}
|
2017-06-19 15:21:04 +01:00
|
|
|
func SendEvent(
|
|
|
|
req *http.Request,
|
2020-06-16 14:10:55 +01:00
|
|
|
device *userapi.Device,
|
2017-12-04 18:07:52 +00:00
|
|
|
roomID, eventType string, txnID, stateKey *string,
|
2020-08-10 14:18:04 +01:00
|
|
|
cfg *config.ClientAPI,
|
2022-05-05 13:17:38 +01:00
|
|
|
rsAPI api.ClientRoomserverAPI,
|
2018-05-18 10:49:40 +01:00
|
|
|
txnCache *transactions.Cache,
|
2017-06-19 15:21:04 +01:00
|
|
|
) util.JSONResponse {
|
2023-04-27 07:07:13 +01:00
|
|
|
roomVersion, err := rsAPI.QueryRoomVersionForRoom(req.Context(), roomID)
|
|
|
|
if err != nil {
|
2020-03-27 16:28:22 +00:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 23:46:49 +01:00
|
|
|
JSON: spec.UnsupportedRoomVersion(err.Error()),
|
2020-03-27 16:28:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 10:49:40 +01:00
|
|
|
if txnID != nil {
|
|
|
|
// Try to fetch response from transactionsCache
|
2022-10-17 13:48:35 +01:00
|
|
|
if res, ok := txnCache.FetchTransaction(device.AccessToken, *txnID, req.URL); ok {
|
2018-05-18 10:49:40 +01:00
|
|
|
return *res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 08:27:43 +01:00
|
|
|
// create a mutex for the specific user in the specific room
|
|
|
|
// this avoids a situation where events that are received in quick succession are sent to the roomserver in a jumbled order
|
|
|
|
userID := device.UserID
|
2022-10-26 12:59:19 +01:00
|
|
|
domain := device.UserDomain()
|
2020-08-20 08:27:43 +01:00
|
|
|
mutex, _ := userRoomSendMutexes.LoadOrStore(roomID+userID, &sync.Mutex{})
|
|
|
|
mutex.(*sync.Mutex).Lock()
|
|
|
|
defer mutex.(*sync.Mutex).Unlock()
|
|
|
|
|
2022-02-18 15:05:03 +00:00
|
|
|
var r map[string]interface{} // must be a JSON object
|
|
|
|
resErr := httputil.UnmarshalJSONRequest(req, &r)
|
|
|
|
if resErr != nil {
|
|
|
|
return *resErr
|
|
|
|
}
|
|
|
|
|
2022-06-03 05:43:51 +01:00
|
|
|
if stateKey != nil {
|
|
|
|
// If the existing/new state content are equal, return the existing event_id, making the request idempotent.
|
|
|
|
if resp := stateEqual(req.Context(), rsAPI, eventType, *stateKey, roomID, r); resp != nil {
|
|
|
|
return *resp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
startedGeneratingEvent := time.Now()
|
|
|
|
|
2022-05-24 09:11:23 +01:00
|
|
|
// If we're sending a membership update, make sure to strip the authorised
|
|
|
|
// via key if it is present, otherwise other servers won't be able to auth
|
|
|
|
// the event if the room is set to the "restricted" join rule.
|
2023-04-19 15:50:33 +01:00
|
|
|
if eventType == spec.MRoomMember {
|
2022-05-24 10:22:26 +01:00
|
|
|
delete(r, "join_authorised_via_users_server")
|
2022-05-24 09:11:23 +01:00
|
|
|
}
|
|
|
|
|
2022-02-18 15:05:03 +00:00
|
|
|
evTime, err := httputil.ParseTSParam(req)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 23:46:49 +01:00
|
|
|
JSON: spec.InvalidParam(err.Error()),
|
2022-02-18 15:05:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
e, resErr := generateSendEvent(req.Context(), r, device, roomID, eventType, stateKey, cfg, rsAPI, evTime)
|
2018-08-22 13:40:25 +01:00
|
|
|
if resErr != nil {
|
|
|
|
return *resErr
|
|
|
|
}
|
2020-12-17 10:52:31 +00:00
|
|
|
timeToGenerateEvent := time.Since(startedGeneratingEvent)
|
2018-08-22 13:40:25 +01:00
|
|
|
|
2022-03-07 09:37:04 +00:00
|
|
|
// validate that the aliases exists
|
2023-04-19 15:50:33 +01:00
|
|
|
if eventType == spec.MRoomCanonicalAlias && stateKey != nil && *stateKey == "" {
|
2022-03-07 09:37:04 +00:00
|
|
|
aliasReq := api.AliasEvent{}
|
|
|
|
if err = json.Unmarshal(e.Content(), &aliasReq); err != nil {
|
|
|
|
return util.ErrorResponse(fmt.Errorf("unable to parse alias event: %w", err))
|
|
|
|
}
|
|
|
|
if !aliasReq.Valid() {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 23:46:49 +01:00
|
|
|
JSON: spec.InvalidParam("Request contains invalid aliases."),
|
2022-03-07 09:37:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
aliasRes := &api.GetAliasesForRoomIDResponse{}
|
|
|
|
if err = rsAPI.GetAliasesForRoomID(req.Context(), &api.GetAliasesForRoomIDRequest{RoomID: roomID}, aliasRes); err != nil {
|
2023-05-17 01:33:27 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2022-03-07 09:37:04 +00:00
|
|
|
}
|
|
|
|
var found int
|
|
|
|
requestAliases := append(aliasReq.AltAliases, aliasReq.Alias)
|
|
|
|
for _, alias := range aliasRes.Aliases {
|
|
|
|
for _, altAlias := range requestAliases {
|
|
|
|
if altAlias == alias {
|
|
|
|
found++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// check that we found at least the same amount of existing aliases as are in the request
|
|
|
|
if aliasReq.Alias != "" && found < len(requestAliases) {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 23:46:49 +01:00
|
|
|
JSON: spec.BadAlias("No matching alias found."),
|
2022-03-07 09:37:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 17:55:40 +01:00
|
|
|
var txnAndSessionID *api.TransactionID
|
2018-08-22 13:40:25 +01:00
|
|
|
if txnID != nil {
|
2019-08-23 17:55:40 +01:00
|
|
|
txnAndSessionID = &api.TransactionID{
|
2018-08-22 13:40:25 +01:00
|
|
|
TransactionID: *txnID,
|
2019-08-23 17:55:40 +01:00
|
|
|
SessionID: device.SessionID,
|
2018-08-22 13:40:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pass the new event to the roomserver and receive the correct event ID
|
|
|
|
// event ID in case of duplicate transaction is discarded
|
2020-12-17 10:52:31 +00:00
|
|
|
startedSubmittingEvent := time.Now()
|
2020-09-03 15:22:16 +01:00
|
|
|
if err := api.SendEvents(
|
2020-06-10 12:17:54 +01:00
|
|
|
req.Context(), rsAPI,
|
2020-10-19 14:59:13 +01:00
|
|
|
api.KindNew,
|
2023-04-27 12:54:20 +01:00
|
|
|
[]*types.HeaderedEvent{
|
2023-05-02 15:03:16 +01:00
|
|
|
&types.HeaderedEvent{PDU: e},
|
2020-03-27 16:28:22 +00:00
|
|
|
},
|
2022-11-15 15:05:23 +00:00
|
|
|
device.UserDomain(),
|
2022-10-26 12:59:19 +01:00
|
|
|
domain,
|
|
|
|
domain,
|
2020-03-27 16:28:22 +00:00
|
|
|
txnAndSessionID,
|
2022-01-05 17:44:49 +00:00
|
|
|
false,
|
2020-09-03 15:22:16 +01:00
|
|
|
); err != nil {
|
2020-06-10 12:17:54 +01:00
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("SendEvents failed")
|
2023-05-17 01:33:27 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2018-08-22 13:40:25 +01:00
|
|
|
}
|
2020-12-17 10:52:31 +00:00
|
|
|
timeToSubmitEvent := time.Since(startedSubmittingEvent)
|
2020-03-27 16:28:22 +00:00
|
|
|
util.GetLogger(req.Context()).WithFields(logrus.Fields{
|
2020-09-03 15:22:16 +01:00
|
|
|
"event_id": e.EventID(),
|
2020-03-27 16:28:22 +00:00
|
|
|
"room_id": roomID,
|
2023-04-27 07:07:13 +01:00
|
|
|
"room_version": roomVersion,
|
2020-03-27 16:28:22 +00:00
|
|
|
}).Info("Sent event to roomserver")
|
2018-08-22 13:40:25 +01:00
|
|
|
|
|
|
|
res := util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
2020-09-03 15:22:16 +01:00
|
|
|
JSON: sendEventResponse{e.EventID()},
|
2018-08-22 13:40:25 +01:00
|
|
|
}
|
|
|
|
// Add response to transactionsCache
|
|
|
|
if txnID != nil {
|
2022-10-17 13:48:35 +01:00
|
|
|
txnCache.AddTransaction(device.AccessToken, *txnID, req.URL, &res)
|
2018-08-22 13:40:25 +01:00
|
|
|
}
|
|
|
|
|
2020-12-17 10:52:31 +00:00
|
|
|
// Take a note of how long it took to generate the event vs submit
|
|
|
|
// it to the roomserver.
|
|
|
|
sendEventDuration.With(prometheus.Labels{"action": "build"}).Observe(float64(timeToGenerateEvent.Milliseconds()))
|
|
|
|
sendEventDuration.With(prometheus.Labels{"action": "submit"}).Observe(float64(timeToSubmitEvent.Milliseconds()))
|
|
|
|
|
2018-08-22 13:40:25 +01:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-06-03 05:43:51 +01:00
|
|
|
// stateEqual compares the new and the existing state event content. If they are equal, returns a *util.JSONResponse
|
|
|
|
// with the existing event_id, making this an idempotent request.
|
|
|
|
func stateEqual(ctx context.Context, rsAPI api.ClientRoomserverAPI, eventType, stateKey, roomID string, newContent map[string]interface{}) *util.JSONResponse {
|
|
|
|
stateRes := api.QueryCurrentStateResponse{}
|
|
|
|
tuple := gomatrixserverlib.StateKeyTuple{
|
|
|
|
EventType: eventType,
|
|
|
|
StateKey: stateKey,
|
|
|
|
}
|
|
|
|
err := rsAPI.QueryCurrentState(ctx, &api.QueryCurrentStateRequest{
|
|
|
|
RoomID: roomID,
|
|
|
|
StateTuples: []gomatrixserverlib.StateKeyTuple{tuple},
|
|
|
|
}, &stateRes)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if existingEvent, ok := stateRes.StateEvents[tuple]; ok {
|
|
|
|
var existingContent map[string]interface{}
|
|
|
|
if err = json.Unmarshal(existingEvent.Content(), &existingContent); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if reflect.DeepEqual(existingContent, newContent) {
|
|
|
|
return &util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: sendEventResponse{existingEvent.EventID()},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-22 13:40:25 +01:00
|
|
|
func generateSendEvent(
|
2022-02-18 15:05:03 +00:00
|
|
|
ctx context.Context,
|
|
|
|
r map[string]interface{},
|
2020-06-16 14:10:55 +01:00
|
|
|
device *userapi.Device,
|
2018-08-22 13:40:25 +01:00
|
|
|
roomID, eventType string, stateKey *string,
|
2020-08-10 14:18:04 +01:00
|
|
|
cfg *config.ClientAPI,
|
2022-05-05 13:17:38 +01:00
|
|
|
rsAPI api.ClientRoomserverAPI,
|
2022-02-18 15:05:03 +00:00
|
|
|
evTime time.Time,
|
2023-05-02 15:03:16 +01:00
|
|
|
) (gomatrixserverlib.PDU, *util.JSONResponse) {
|
2017-03-15 11:22:40 +00:00
|
|
|
// parse the incoming http request
|
2023-06-07 18:14:35 +01:00
|
|
|
fullUserID, err := spec.NewUserID(device.UserID, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: spec.BadJSON("Bad userID"),
|
|
|
|
}
|
|
|
|
}
|
2023-06-14 15:23:46 +01:00
|
|
|
validRoomID, err := spec.NewRoomID(roomID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: spec.BadJSON("RoomID is invalid"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
senderID, err := rsAPI.QuerySenderIDForUser(ctx, *validRoomID, *fullUserID)
|
2023-06-07 18:14:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, &util.JSONResponse{
|
|
|
|
Code: http.StatusNotFound,
|
|
|
|
JSON: spec.NotFound("Unable to find senderID for user"),
|
|
|
|
}
|
|
|
|
}
|
2018-08-06 14:09:25 +01:00
|
|
|
|
2017-03-15 11:22:40 +00:00
|
|
|
// create the new event and set all the fields we can
|
2023-05-04 11:17:42 +01:00
|
|
|
proto := gomatrixserverlib.ProtoEvent{
|
2023-06-07 18:14:35 +01:00
|
|
|
SenderID: string(senderID),
|
2017-03-15 11:22:40 +00:00
|
|
|
RoomID: roomID,
|
|
|
|
Type: eventType,
|
2017-03-17 11:21:52 +00:00
|
|
|
StateKey: stateKey,
|
2017-03-15 11:22:40 +00:00
|
|
|
}
|
2023-06-07 18:14:35 +01:00
|
|
|
err = proto.SetContent(r)
|
2017-09-20 10:59:19 +01:00
|
|
|
if err != nil {
|
2023-05-04 11:17:42 +01:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("proto.SetContent failed")
|
2023-05-17 01:33:27 +01:00
|
|
|
return nil, &util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2017-09-20 10:59:19 +01:00
|
|
|
}
|
2017-03-15 11:22:40 +00:00
|
|
|
|
2022-11-15 15:05:23 +00:00
|
|
|
identity, err := cfg.Matrix.SigningIdentityFor(device.UserDomain())
|
|
|
|
if err != nil {
|
2023-05-17 01:33:27 +01:00
|
|
|
return nil, &util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2022-11-15 15:05:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-15 11:22:40 +00:00
|
|
|
var queryRes api.QueryLatestEventsAndStateResponse
|
2023-05-31 16:27:08 +01:00
|
|
|
e, err := eventutil.QueryAndBuildEvent(ctx, &proto, identity, evTime, rsAPI, &queryRes)
|
2023-05-17 01:33:27 +01:00
|
|
|
switch specificErr := err.(type) {
|
|
|
|
case nil:
|
|
|
|
case eventutil.ErrRoomNoExists:
|
2018-08-22 13:40:25 +01:00
|
|
|
return nil, &util.JSONResponse{
|
2018-03-13 15:55:45 +00:00
|
|
|
Code: http.StatusNotFound,
|
2023-05-09 23:46:49 +01:00
|
|
|
JSON: spec.NotFound("Room does not exist"),
|
2017-03-15 11:22:40 +00:00
|
|
|
}
|
2023-05-17 01:33:27 +01:00
|
|
|
case gomatrixserverlib.BadJSONError:
|
2020-06-04 10:53:39 +01:00
|
|
|
return nil, &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-17 01:33:27 +01:00
|
|
|
JSON: spec.BadJSON(specificErr.Error()),
|
2020-06-04 10:53:39 +01:00
|
|
|
}
|
2023-05-17 01:33:27 +01:00
|
|
|
case gomatrixserverlib.EventValidationError:
|
|
|
|
if specificErr.Code == gomatrixserverlib.EventValidationTooLarge {
|
2020-06-26 12:51:54 +01:00
|
|
|
return nil, &util.JSONResponse{
|
|
|
|
Code: http.StatusRequestEntityTooLarge,
|
2023-05-17 01:33:27 +01:00
|
|
|
JSON: spec.BadJSON(specificErr.Error()),
|
2020-06-26 12:51:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-17 01:33:27 +01:00
|
|
|
JSON: spec.BadJSON(specificErr.Error()),
|
2020-06-26 12:51:54 +01:00
|
|
|
}
|
2023-05-17 01:33:27 +01:00
|
|
|
default:
|
2022-02-18 15:05:03 +00:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("eventutil.BuildEvent failed")
|
2023-05-17 01:33:27 +01:00
|
|
|
return nil, &util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2017-03-15 11:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check to see if this user can perform this operation
|
2023-05-02 15:03:16 +01:00
|
|
|
stateEvents := make([]gomatrixserverlib.PDU, len(queryRes.StateEvents))
|
2017-03-15 11:22:40 +00:00
|
|
|
for i := range queryRes.StateEvents {
|
2023-05-02 15:03:16 +01:00
|
|
|
stateEvents[i] = queryRes.StateEvents[i].PDU
|
2017-03-15 11:22:40 +00:00
|
|
|
}
|
2023-04-27 16:35:19 +01:00
|
|
|
provider := gomatrixserverlib.NewAuthEvents(gomatrixserverlib.ToPDUs(stateEvents))
|
2023-06-14 15:23:46 +01:00
|
|
|
if err = gomatrixserverlib.Allowed(e.PDU, &provider, func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
|
|
|
return rsAPI.QueryUserIDForSender(ctx, *validRoomID, senderID)
|
2023-06-06 21:55:18 +01:00
|
|
|
}); err != nil {
|
2018-08-22 13:40:25 +01:00
|
|
|
return nil, &util.JSONResponse{
|
2018-03-13 15:55:45 +00:00
|
|
|
Code: http.StatusForbidden,
|
2023-05-09 23:46:49 +01:00
|
|
|
JSON: spec.Forbidden(err.Error()), // TODO: Is this error string comprehensible to the client?
|
2017-03-15 11:22:40 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-05 10:04:08 +01:00
|
|
|
|
|
|
|
// User should not be able to send a tombstone event to the same room.
|
|
|
|
if e.Type() == "m.room.tombstone" {
|
|
|
|
content := make(map[string]interface{})
|
|
|
|
if err = json.Unmarshal(e.Content(), &content); err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("Cannot unmarshal the event content.")
|
|
|
|
return nil, &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 23:46:49 +01:00
|
|
|
JSON: spec.BadJSON("Cannot unmarshal the event content."),
|
2022-04-05 10:04:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if content["replacement_room"] == e.RoomID() {
|
|
|
|
return nil, &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 23:46:49 +01:00
|
|
|
JSON: spec.InvalidParam("Cannot send tombstone event that points to the same room."),
|
2022-04-05 10:04:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-02 15:03:16 +01:00
|
|
|
return e.PDU, nil
|
2017-03-15 11:22:40 +00:00
|
|
|
}
|