2017-04-21 00:40:52 +02: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 19:16:53 +02:00
|
|
|
package routing
|
2017-02-03 17:05:46 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2020-08-20 09:27:43 +02:00
|
|
|
"sync"
|
2017-02-03 17:05:46 +01:00
|
|
|
|
2017-03-15 12:22:40 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
2020-05-21 15:40:13 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/config"
|
2020-06-12 15:55:57 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/eventutil"
|
2020-05-21 15:40:13 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/transactions"
|
2017-03-15 12:22:40 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-06-16 15:10:55 +02:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2017-03-15 12:22:40 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-02-03 17:05:46 +01:00
|
|
|
"github.com/matrix-org/util"
|
2020-03-27 17:28:22 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-02-03 17:05:46 +01:00
|
|
|
)
|
|
|
|
|
2017-03-15 12:22:40 +01:00
|
|
|
// http://matrix.org/docs/spec/client_server/r0.2.0.html#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid
|
2017-03-17 12:21:52 +01: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 12:22:40 +01:00
|
|
|
EventID string `json:"event_id"`
|
|
|
|
}
|
|
|
|
|
2020-08-20 09:27:43 +02:00
|
|
|
var (
|
|
|
|
userRoomSendMutexes sync.Map // (roomID+userID) -> mutex. mutexes to ensure correct ordering of sendEvents
|
|
|
|
)
|
|
|
|
|
2017-03-17 12:21:52 +01:00
|
|
|
// SendEvent implements:
|
2017-09-26 13:55:48 +02:00
|
|
|
// /rooms/{roomID}/send/{eventType}
|
2017-03-17 12:21:52 +01:00
|
|
|
// /rooms/{roomID}/send/{eventType}/{txnID}
|
|
|
|
// /rooms/{roomID}/state/{eventType}/{stateKey}
|
2017-06-19 16:21:04 +02:00
|
|
|
func SendEvent(
|
|
|
|
req *http.Request,
|
2020-06-16 15:10:55 +02:00
|
|
|
device *userapi.Device,
|
2017-12-04 19:07:52 +01:00
|
|
|
roomID, eventType string, txnID, stateKey *string,
|
2020-08-10 15:18:04 +02:00
|
|
|
cfg *config.ClientAPI,
|
2020-05-01 11:48:17 +02:00
|
|
|
rsAPI api.RoomserverInternalAPI,
|
2018-05-18 11:49:40 +02:00
|
|
|
txnCache *transactions.Cache,
|
2017-06-19 16:21:04 +02:00
|
|
|
) util.JSONResponse {
|
2020-03-27 17:28:22 +01:00
|
|
|
verReq := api.QueryRoomVersionForRoomRequest{RoomID: roomID}
|
|
|
|
verRes := api.QueryRoomVersionForRoomResponse{}
|
2020-05-01 11:48:17 +02:00
|
|
|
if err := rsAPI.QueryRoomVersionForRoom(req.Context(), &verReq, &verRes); err != nil {
|
2020-03-27 17:28:22 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.UnsupportedRoomVersion(err.Error()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 11:49:40 +02:00
|
|
|
if txnID != nil {
|
|
|
|
// Try to fetch response from transactionsCache
|
2019-08-06 17:33:53 +02:00
|
|
|
if res, ok := txnCache.FetchTransaction(device.AccessToken, *txnID); ok {
|
2018-05-18 11:49:40 +02:00
|
|
|
return *res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 09:27:43 +02: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
|
|
|
|
mutex, _ := userRoomSendMutexes.LoadOrStore(roomID+userID, &sync.Mutex{})
|
|
|
|
mutex.(*sync.Mutex).Lock()
|
|
|
|
defer mutex.(*sync.Mutex).Unlock()
|
|
|
|
|
2020-05-01 11:48:17 +02:00
|
|
|
e, resErr := generateSendEvent(req, device, roomID, eventType, stateKey, cfg, rsAPI)
|
2018-08-22 14:40:25 +02:00
|
|
|
if resErr != nil {
|
|
|
|
return *resErr
|
|
|
|
}
|
|
|
|
|
2019-08-23 18:55:40 +02:00
|
|
|
var txnAndSessionID *api.TransactionID
|
2018-08-22 14:40:25 +02:00
|
|
|
if txnID != nil {
|
2019-08-23 18:55:40 +02:00
|
|
|
txnAndSessionID = &api.TransactionID{
|
2018-08-22 14:40:25 +02:00
|
|
|
TransactionID: *txnID,
|
2019-08-23 18:55:40 +02:00
|
|
|
SessionID: device.SessionID,
|
2018-08-22 14:40:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pass the new event to the roomserver and receive the correct event ID
|
|
|
|
// event ID in case of duplicate transaction is discarded
|
2020-06-10 13:17:54 +02:00
|
|
|
eventID, err := api.SendEvents(
|
|
|
|
req.Context(), rsAPI,
|
2020-03-27 17:28:22 +01:00
|
|
|
[]gomatrixserverlib.HeaderedEvent{
|
|
|
|
e.Headered(verRes.RoomVersion),
|
|
|
|
},
|
|
|
|
cfg.Matrix.ServerName,
|
|
|
|
txnAndSessionID,
|
2018-08-22 14:40:25 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
2020-06-10 13:17:54 +02:00
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("SendEvents failed")
|
2020-03-02 17:20:44 +01:00
|
|
|
return jsonerror.InternalServerError()
|
2018-08-22 14:40:25 +02:00
|
|
|
}
|
2020-03-27 17:28:22 +01:00
|
|
|
util.GetLogger(req.Context()).WithFields(logrus.Fields{
|
|
|
|
"event_id": eventID,
|
|
|
|
"room_id": roomID,
|
|
|
|
"room_version": verRes.RoomVersion,
|
|
|
|
}).Info("Sent event to roomserver")
|
2018-08-22 14:40:25 +02:00
|
|
|
|
|
|
|
res := util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: sendEventResponse{eventID},
|
|
|
|
}
|
|
|
|
// Add response to transactionsCache
|
|
|
|
if txnID != nil {
|
2019-08-06 17:33:53 +02:00
|
|
|
txnCache.AddTransaction(device.AccessToken, *txnID, &res)
|
2018-08-22 14:40:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateSendEvent(
|
|
|
|
req *http.Request,
|
2020-06-16 15:10:55 +02:00
|
|
|
device *userapi.Device,
|
2018-08-22 14:40:25 +02:00
|
|
|
roomID, eventType string, stateKey *string,
|
2020-08-10 15:18:04 +02:00
|
|
|
cfg *config.ClientAPI,
|
2020-05-01 11:48:17 +02:00
|
|
|
rsAPI api.RoomserverInternalAPI,
|
2018-08-22 14:40:25 +02:00
|
|
|
) (*gomatrixserverlib.Event, *util.JSONResponse) {
|
2017-03-15 12:22:40 +01:00
|
|
|
// parse the incoming http request
|
2017-05-23 18:43:05 +02:00
|
|
|
userID := device.UserID
|
2017-03-15 12:22:40 +01:00
|
|
|
var r map[string]interface{} // must be a JSON object
|
2017-05-23 18:43:05 +02:00
|
|
|
resErr := httputil.UnmarshalJSONRequest(req, &r)
|
2017-03-15 12:22:40 +01:00
|
|
|
if resErr != nil {
|
2018-08-22 14:40:25 +02:00
|
|
|
return nil, resErr
|
2017-03-15 12:22:40 +01:00
|
|
|
}
|
|
|
|
|
2018-08-22 14:40:25 +02:00
|
|
|
evTime, err := httputil.ParseTSParam(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.InvalidArgumentValue(err.Error()),
|
|
|
|
}
|
|
|
|
}
|
2018-08-06 15:09:25 +02:00
|
|
|
|
2017-03-15 12:22:40 +01:00
|
|
|
// create the new event and set all the fields we can
|
|
|
|
builder := gomatrixserverlib.EventBuilder{
|
|
|
|
Sender: userID,
|
|
|
|
RoomID: roomID,
|
|
|
|
Type: eventType,
|
2017-03-17 12:21:52 +01:00
|
|
|
StateKey: stateKey,
|
2017-03-15 12:22:40 +01:00
|
|
|
}
|
2018-08-22 14:40:25 +02:00
|
|
|
err = builder.SetContent(r)
|
2017-09-20 11:59:19 +02:00
|
|
|
if err != nil {
|
2020-03-02 17:20:44 +01:00
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("builder.SetContent failed")
|
|
|
|
resErr := jsonerror.InternalServerError()
|
2018-08-22 14:40:25 +02:00
|
|
|
return nil, &resErr
|
2017-09-20 11:59:19 +02:00
|
|
|
}
|
2017-03-15 12:22:40 +01:00
|
|
|
|
|
|
|
var queryRes api.QueryLatestEventsAndStateResponse
|
2020-08-10 15:18:04 +02:00
|
|
|
e, err := eventutil.BuildEvent(req.Context(), &builder, cfg.Matrix, evTime, rsAPI, &queryRes)
|
2020-06-12 15:55:57 +02:00
|
|
|
if err == eventutil.ErrRoomNoExists {
|
2018-08-22 14:40:25 +02:00
|
|
|
return nil, &util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusNotFound,
|
2017-03-15 12:22:40 +01:00
|
|
|
JSON: jsonerror.NotFound("Room does not exist"),
|
|
|
|
}
|
2020-06-04 11:53:39 +02:00
|
|
|
} else if e, ok := err.(gomatrixserverlib.BadJSONError); ok {
|
|
|
|
return nil, &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.BadJSON(e.Error()),
|
|
|
|
}
|
2020-06-26 13:51:54 +02:00
|
|
|
} else if e, ok := err.(gomatrixserverlib.EventValidationError); ok {
|
|
|
|
if e.Code == gomatrixserverlib.EventValidationTooLarge {
|
|
|
|
return nil, &util.JSONResponse{
|
|
|
|
Code: http.StatusRequestEntityTooLarge,
|
|
|
|
JSON: jsonerror.BadJSON(e.Error()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.BadJSON(e.Error()),
|
|
|
|
}
|
2017-08-04 17:32:10 +02:00
|
|
|
} else if err != nil {
|
2020-06-12 15:55:57 +02:00
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("eventutil.BuildEvent failed")
|
2020-03-02 17:20:44 +01:00
|
|
|
resErr := jsonerror.InternalServerError()
|
2018-08-22 14:40:25 +02:00
|
|
|
return nil, &resErr
|
2017-03-15 12:22:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// check to see if this user can perform this operation
|
|
|
|
stateEvents := make([]*gomatrixserverlib.Event, len(queryRes.StateEvents))
|
|
|
|
for i := range queryRes.StateEvents {
|
2020-03-16 18:29:52 +01:00
|
|
|
stateEvents[i] = &queryRes.StateEvents[i].Event
|
2017-03-15 12:22:40 +01:00
|
|
|
}
|
|
|
|
provider := gomatrixserverlib.NewAuthEvents(stateEvents)
|
2020-07-03 18:24:51 +02:00
|
|
|
if err = gomatrixserverlib.Allowed(e.Event, &provider); err != nil {
|
2018-08-22 14:40:25 +02:00
|
|
|
return nil, &util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusForbidden,
|
2017-03-15 12:22:40 +01:00
|
|
|
JSON: jsonerror.Forbidden(err.Error()), // TODO: Is this error string comprehensible to the client?
|
|
|
|
}
|
|
|
|
}
|
2020-07-03 18:24:51 +02:00
|
|
|
return &e.Event, nil
|
2017-03-15 12:22:40 +01:00
|
|
|
}
|