From 2c9ec8c1ab68928483d2acf1ea4ec3c26ec056bf Mon Sep 17 00:00:00 2001 From: Dan Peleg Date: Fri, 23 Apr 2021 21:29:05 +0300 Subject: [PATCH] Initial implementation of GET /_matrix/client/r0/pushers --- clientapi/routing/pusher.go | 81 +++++++++++++++++++++++++++++++++++++ userapi/inthttp/client.go | 9 +++++ userapi/inthttp/server.go | 13 ++++++ 3 files changed, 103 insertions(+) create mode 100644 clientapi/routing/pusher.go diff --git a/clientapi/routing/pusher.go b/clientapi/routing/pusher.go new file mode 100644 index 00000000..902ad7c9 --- /dev/null +++ b/clientapi/routing/pusher.go @@ -0,0 +1,81 @@ +// Copyright 2021 Dan Peleg +// +// 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. + +package routing + +import ( + "net/http" + + "github.com/matrix-org/dendrite/clientapi/jsonerror" + "github.com/matrix-org/dendrite/userapi/api" + userapi "github.com/matrix-org/dendrite/userapi/api" + "github.com/matrix-org/util" +) + +// https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-pushers +type pusherJSON struct { + PusherID string `json:"pusher_id"` + PushKey string `json:"pushkey"` + Kind string `json:"kind"` + AppID string `json:"app_id"` + AppDisplayName string `json:"app_display_name"` + DeviceDisplayName string `json:"device_display_name"` + ProfileTag string `json:"profile_tag"` + Language string `json:"lang"` + Data pusherDataJSON `json:"data"` +} + +type pushersJSON struct { + Pushers []pusherJSON `json:"pushers"` +} + +type pusherDataJSON struct { + URL string `json:"url"` + Format string `json:"format"` +} + +// GetPushersByLocalpart handles /pushers +func GetPushersByLocalpart( + req *http.Request, userAPI userapi.UserInternalAPI, pusher *api.Pusher, +) util.JSONResponse { + var queryRes userapi.QueryPushersResponse + err := userAPI.QueryPushers(req.Context(), &userapi.QueryPushersRequest{ + UserID: pusher.UserID, + }, &queryRes) + if err != nil { + util.GetLogger(req.Context()).WithError(err).Error("QueryPushers failed") + return jsonerror.InternalServerError() + } + + res := pushersJSON{} + + for _, pusher := range queryRes.Pushers { + res.Pushers = append(res.Pushers, pusherJSON{ + PusherID: pusher.ID, + PushKey: pusher.PushKey, + Kind: pusher.Kind, + AppID: pusher.AppID, + AppDisplayName: pusher.AppDisplayName, + DeviceDisplayName: pusher.DeviceDisplayName, + ProfileTag: pusher.ProfileTag, + Language: pusher.Language, + Data: pusherDataJSON(pusher.Data), + }) + } + + return util.JSONResponse{ + Code: http.StatusOK, + JSON: res, + } +} diff --git a/userapi/inthttp/client.go b/userapi/inthttp/client.go index 1cb5ef0a..aa3ab609 100644 --- a/userapi/inthttp/client.go +++ b/userapi/inthttp/client.go @@ -40,6 +40,7 @@ const ( QueryProfilePath = "/userapi/queryProfile" QueryAccessTokenPath = "/userapi/queryAccessToken" QueryDevicesPath = "/userapi/queryDevices" + QueryPushersPath = "/userapi/queryPushers" QueryAccountDataPath = "/userapi/queryAccountData" QueryDeviceInfosPath = "/userapi/queryDeviceInfos" QuerySearchProfilesPath = "/userapi/querySearchProfiles" @@ -202,6 +203,14 @@ func (h *httpUserInternalAPI) QueryDevices(ctx context.Context, req *api.QueryDe return httputil.PostJSON(ctx, span, h.httpClient, apiURL, req, res) } +func (h *httpUserInternalAPI) QueryPushers(ctx context.Context, req *api.QueryPushersRequest, res *api.QueryPushersResponse) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "QueryPushers") + defer span.Finish() + + apiURL := h.apiURL + QueryPushersPath + return httputil.PostJSON(ctx, span, h.httpClient, apiURL, req, res) +} + func (h *httpUserInternalAPI) QueryAccountData(ctx context.Context, req *api.QueryAccountDataRequest, res *api.QueryAccountDataResponse) error { span, ctx := opentracing.StartSpanFromContext(ctx, "QueryAccountData") defer span.Finish() diff --git a/userapi/inthttp/server.go b/userapi/inthttp/server.go index 1c1cfdcd..5fddb2a8 100644 --- a/userapi/inthttp/server.go +++ b/userapi/inthttp/server.go @@ -169,6 +169,19 @@ func AddRoutes(internalAPIMux *mux.Router, s api.UserInternalAPI) { return util.JSONResponse{Code: http.StatusOK, JSON: &response} }), ) + internalAPIMux.Handle(QueryPushersPath, + httputil.MakeInternalAPI("queryPushers", func(req *http.Request) util.JSONResponse { + request := api.QueryPushersRequest{} + response := api.QueryPushersResponse{} + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.MessageResponse(http.StatusBadRequest, err.Error()) + } + if err := s.QueryPushers(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) internalAPIMux.Handle(QueryAccountDataPath, httputil.MakeInternalAPI("queryAccountData", func(req *http.Request) util.JSONResponse { request := api.QueryAccountDataRequest{}