From 6ea1271215553a9560f37ee8004dd7ede18c2a8f Mon Sep 17 00:00:00 2001 From: Till Faelligen Date: Fri, 18 Feb 2022 15:12:03 +0100 Subject: [PATCH] Don't create new rooms, use the existing one (follow Synapse behavior) --- clientapi/routing/routing.go | 45 +++++++++++++++++++++++++++++ clientapi/routing/server_notices.go | 24 +++++++++++---- roomserver/api/perform.go | 4 +-- roomserver/internal/api.go | 1 + 4 files changed, 67 insertions(+), 7 deletions(-) diff --git a/clientapi/routing/routing.go b/clientapi/routing/routing.go index 63dcaa413..d75f58b81 100644 --- a/clientapi/routing/routing.go +++ b/clientapi/routing/routing.go @@ -15,6 +15,7 @@ package routing import ( + "context" "encoding/json" "net/http" "strings" @@ -117,6 +118,50 @@ func Setup( ).Methods(http.MethodGet, http.MethodPost, http.MethodOptions) } + // server notifications + if cfg.Matrix.ServerNotices.Enabled { + logrus.Info("Enabling server notices at /_synapse/admin/v1/send_server_notice") + serverNotificationSender, err := getSenderDevice(context.Background(), userAPI, accountDB, cfg) + if err != nil { + logrus.WithError(err).Fatal("unable to get account for sending sending server notices") + } + + synapseAdminRouter.Handle("/admin/v1/send_server_notice/{txnID}", + httputil.MakeAuthAPI("send_server_notice", userAPI, func(req *http.Request, device *userapi.Device) util.JSONResponse { + // not specced, but ensure we're rate limiting requests to this endpoint + if r := rateLimits.Limit(req); r != nil { + return *r + } + vars, err := httputil.URLDecodeMapValues(mux.Vars(req)) + if err != nil { + return util.ErrorResponse(err) + } + txnID := vars["txnID"] + return SendServerNotice( + req, &cfg.Matrix.ServerNotices, + cfg, userAPI, rsAPI, accountDB, asAPI, + device, serverNotificationSender, + &txnID, transactionsCache, + ) + }), + ).Methods(http.MethodPut, http.MethodOptions) + + synapseAdminRouter.Handle("/admin/v1/send_server_notice", + httputil.MakeAuthAPI("send_server_notice", userAPI, func(req *http.Request, device *userapi.Device) util.JSONResponse { + // not specced, but ensure we're rate limiting requests to this endpoint + if r := rateLimits.Limit(req); r != nil { + return *r + } + return SendServerNotice( + req, &cfg.Matrix.ServerNotices, + cfg, userAPI, rsAPI, accountDB, asAPI, + device, serverNotificationSender, + nil, transactionsCache, + ) + }), + ).Methods(http.MethodPost, http.MethodOptions) + } + // You can't just do PathPrefix("/(r0|v3)") because regexps only apply when inside named path variables. // So make a named path variable called 'apiversion' (which we will never read in handlers) and then do // (r0|v3) - BUT this is a captured group, which makes no sense because you cannot extract this group diff --git a/clientapi/routing/server_notices.go b/clientapi/routing/server_notices.go index 66c898d79..42a303a6b 100644 --- a/clientapi/routing/server_notices.go +++ b/clientapi/routing/server_notices.go @@ -21,6 +21,7 @@ import ( "net/http" "time" + userdb "github.com/matrix-org/dendrite/userapi/storage" "github.com/matrix-org/gomatrix" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib/tokens" @@ -36,7 +37,6 @@ import ( "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/setup/config" userapi "github.com/matrix-org/dendrite/userapi/api" - "github.com/matrix-org/dendrite/userapi/storage/accounts" ) // Unspecced server notice request @@ -58,7 +58,7 @@ func SendServerNotice( cfgClient *config.ClientAPI, userAPI userapi.UserInternalAPI, rsAPI api.RoomserverInternalAPI, - accountsDB accounts.Database, + accountsDB userdb.Database, asAPI appserviceAPI.AppServiceQueryAPI, device *userapi.Device, senderDevice *userapi.Device, @@ -112,6 +112,14 @@ func SendServerNotice( return util.ErrorResponse(err) } allUserRooms = append(allUserRooms, userRooms.RoomIDs...) + // get left rooms for specified user + if err := rsAPI.QueryRoomsForUser(ctx, &api.QueryRoomsForUserRequest{ + UserID: r.UserID, + WantMembership: "leave", + }, &userRooms); err != nil { + return util.ErrorResponse(err) + } + allUserRooms = append(allUserRooms, userRooms.RoomIDs...) // get rooms of the sender senderUserID := fmt.Sprintf("@%s:%s", cfgNotices.LocalPart, cfgClient.Matrix.ServerName) @@ -180,7 +188,7 @@ func SendServerNotice( }, }} if err = saveTagData(req, r.UserID, roomID, userAPI, serverAlertTag); err != nil { - util.GetLogger(req.Context()).WithError(err).Error("saveTagData failed") + util.GetLogger(ctx).WithError(err).Error("saveTagData failed") return jsonerror.InternalServerError() } @@ -190,7 +198,13 @@ func SendServerNotice( } } else { + // we've found a room in common, check the membership roomID = commonRooms[0] + // re-invite the user + res, err := sendInvite(ctx, accountsDB, senderDevice, roomID, r.UserID, "Server notice room", cfgClient, rsAPI, asAPI, time.Now()) + if err != nil { + return res + } } startedGeneratingEvent := time.Now() @@ -231,7 +245,7 @@ func SendServerNotice( util.GetLogger(ctx).WithError(err).Error("SendEvents failed") return jsonerror.InternalServerError() } - util.GetLogger(req.Context()).WithFields(logrus.Fields{ + util.GetLogger(ctx).WithFields(logrus.Fields{ "event_id": e.EventID(), "room_id": roomID, "room_version": roomVersion, @@ -270,7 +284,7 @@ func (r sendServerNoticeRequest) valid() (ok bool) { func getSenderDevice( ctx context.Context, userAPI userapi.UserInternalAPI, - accountDB accounts.Database, + accountDB userdb.Database, cfg *config.ClientAPI, ) (*userapi.Device, error) { var accRes userapi.PerformAccountCreationResponse diff --git a/roomserver/api/perform.go b/roomserver/api/perform.go index b6e3623a4..d640858a6 100644 --- a/roomserver/api/perform.go +++ b/roomserver/api/perform.go @@ -95,8 +95,8 @@ type PerformLeaveRequest struct { } type PerformLeaveResponse struct { - Code int `json:"code,omitempty"` - Message string `json:"message,omitempty"` + Code int `json:"code,omitempty"` + Message interface{} `json:"message,omitempty"` } type PerformInviteRequest struct { diff --git a/roomserver/internal/api.go b/roomserver/internal/api.go index 31a45f854..10c8c844e 100644 --- a/roomserver/internal/api.go +++ b/roomserver/internal/api.go @@ -15,6 +15,7 @@ import ( "github.com/matrix-org/dendrite/roomserver/storage" "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/process" + userapi "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/gomatrixserverlib" "github.com/nats-io/nats.go" "github.com/sirupsen/logrus"