From 19273f5fdf768841d272b8b538bd044029b03085 Mon Sep 17 00:00:00 2001 From: Sam Wedgwood Date: Wed, 16 Aug 2023 16:25:57 +0100 Subject: [PATCH] translate sent state event keys to room keys in pseudo ID rooms --- clientapi/routing/sendevent.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/clientapi/routing/sendevent.go b/clientapi/routing/sendevent.go index a167a5a77..6aff1c725 100644 --- a/clientapi/routing/sendevent.go +++ b/clientapi/routing/sendevent.go @@ -29,6 +29,7 @@ import ( "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/roomserver/types" "github.com/matrix-org/dendrite/setup/config" + "github.com/matrix-org/dendrite/syncapi/synctypes" userapi "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib/spec" @@ -92,6 +93,31 @@ func SendEvent( } } + // Only enable translation logic on pseudo ID rooms + // TODO: remove the if check, synctypes.FromClientStateKey handles non-pseudo ID rooms, + // but has some logic to be worked out (see comment in synctypes.FromClientStateKey for details) + if roomVersion == gomatrixserverlib.RoomVersionPseudoIDs && stateKey != nil { + parsedRoomID, innerErr := spec.NewRoomID(roomID) + if innerErr != nil { + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: spec.InvalidParam("invalid room ID"), + } + } + + newStateKey, _, innerErr := synctypes.FromClientStateKey(*parsedRoomID, *stateKey, func(roomID spec.RoomID, userID spec.UserID) (*spec.SenderID, error) { + return rsAPI.QuerySenderIDForUser(req.Context(), roomID, userID) + }) + if innerErr != nil { + util.GetLogger(req.Context()).WithError(innerErr).Error("synctypes.FromClientStateKey failed") + return util.JSONResponse{ + Code: http.StatusInternalServerError, + JSON: spec.Unknown("internal server error"), + } + } + stateKey = newStateKey + } + // 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