diff --git a/clientapi/routing/routing.go b/clientapi/routing/routing.go index 956c402a3..9b7d319ad 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,40 +118,48 @@ func Setup( ).Methods(http.MethodGet, http.MethodPost, http.MethodOptions) } - 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, - &txnID, transactionsCache, - ) - }), - ).Methods(http.MethodPut, http.MethodOptions) + // server notifications + if cfg.Matrix.ServerNotices.Enabled { + 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", - 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, - nil, transactionsCache, - ) - }), - ).Methods(http.MethodPost, http.MethodOptions) + 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) + } r0mux := publicAPIMux.PathPrefix("/r0").Subrouter() unstableMux := publicAPIMux.PathPrefix("/unstable").Subrouter() diff --git a/clientapi/routing/server_notices.go b/clientapi/routing/server_notices.go index ac75dce84..b485a0048 100644 --- a/clientapi/routing/server_notices.go +++ b/clientapi/routing/server_notices.go @@ -61,6 +61,7 @@ func SendServerNotice( accountsDB accounts.Database, asAPI appserviceAPI.AppServiceQueryAPI, device *userapi.Device, + senderDevice *userapi.Device, txnID *string, txnCache *transactions.Cache, ) util.JSONResponse { @@ -139,15 +140,10 @@ func SendServerNotice( roomVersion = gomatrixserverlib.RoomVersionV6 ) - senderDevice, err := getSenderDevice(ctx, userAPI, accountsDB, cfgClient) - if err != nil { - logrus.WithError(err).Error("unable to get device") - return util.ErrorResponse(err) - } // create a new room for the user if len(commonRooms) == 0 { powerLevelContent := eventutil.InitialPowerLevelsContent(senderUserID) - powerLevelContent.Users[r.UserID] = -10 + powerLevelContent.Users[r.UserID] = -10 // taken from Synapse pl, err := json.Marshal(powerLevelContent) if err != nil { return util.ErrorResponse(err)