From 016d451d90afe6d75d8ace327361921367372af3 Mon Sep 17 00:00:00 2001 From: Till Faelligen Date: Tue, 14 Jun 2022 08:20:42 +0200 Subject: [PATCH] Add new DeleteProfile API --- userapi/api/api.go | 7 +++++++ userapi/api/api_trace.go | 6 ++++++ userapi/internal/api.go | 8 ++++++++ userapi/inthttp/client.go | 9 +++++++++ userapi/inthttp/server.go | 12 ++++++++++++ 5 files changed, 42 insertions(+) diff --git a/userapi/api/api.go b/userapi/api/api.go index 2fb98b714..63daae914 100644 --- a/userapi/api/api.go +++ b/userapi/api/api.go @@ -61,6 +61,7 @@ type MediaUserAPI interface { type FederationUserAPI interface { QueryOpenIDToken(ctx context.Context, req *QueryOpenIDTokenRequest, res *QueryOpenIDTokenResponse) error QueryProfile(ctx context.Context, req *QueryProfileRequest, res *QueryProfileResponse) error + DeleteProfile(ctx context.Context, req *PerformDeleteProfileRequest, res *struct{}) error } // api functions required by the sync api @@ -629,3 +630,9 @@ type PerformForgetThreePIDRequest QueryLocalpartForThreePIDRequest type PerformSaveThreePIDAssociationRequest struct { ThreePID, Localpart, Medium string } + +type PerformDeleteProfileRequest struct { + UserID string +} + +type PerformDeleteProfileResponse struct{} diff --git a/userapi/api/api_trace.go b/userapi/api/api_trace.go index 6d8d28007..256365ea5 100644 --- a/userapi/api/api_trace.go +++ b/userapi/api/api_trace.go @@ -203,6 +203,12 @@ func (t *UserInternalAPITrace) PerformSaveThreePIDAssociation(ctx context.Contex return err } +func (t *UserInternalAPITrace) DeleteProfile(ctx context.Context, req *PerformDeleteProfileRequest, res *struct{}) error { + err := t.Impl.DeleteProfile(ctx, req, res) + util.GetLogger(ctx).Infof("DeleteProfile req=%+v res=%+v", js(req), js(res)) + return err +} + func js(thing interface{}) string { b, err := json.Marshal(thing) if err != nil { diff --git a/userapi/internal/api.go b/userapi/internal/api.go index 582cbc740..42ecff102 100644 --- a/userapi/internal/api.go +++ b/userapi/internal/api.go @@ -832,4 +832,12 @@ func (a *UserInternalAPI) PerformSaveThreePIDAssociation(ctx context.Context, re return a.DB.SaveThreePIDAssociation(ctx, req.ThreePID, req.Localpart, req.Medium) } +func (a *UserInternalAPI) DeleteProfile(ctx context.Context, req *api.PerformDeleteProfileRequest, res *struct{}) error { + localpart, serverName, err := gomatrixserverlib.SplitID('@', req.UserID) + if err != nil { + return err + } + return a.DB.DeleteProfile(ctx, localpart, serverName) +} + const pushRulesAccountDataType = "m.push_rules" diff --git a/userapi/inthttp/client.go b/userapi/inthttp/client.go index 23c335cf2..9f11f2353 100644 --- a/userapi/inthttp/client.go +++ b/userapi/inthttp/client.go @@ -44,6 +44,7 @@ const ( PerformSetDisplayNamePath = "/userapi/performSetDisplayName" PerformForgetThreePIDPath = "/userapi/performForgetThreePID" PerformSaveThreePIDAssociationPath = "/userapi/performSaveThreePIDAssociation" + PerformDeleteUserProfilePath = "/userapi/performDeleteUserProfile" QueryKeyBackupPath = "/userapi/queryKeyBackup" QueryProfilePath = "/userapi/queryProfile" @@ -391,3 +392,11 @@ func (h *httpUserInternalAPI) PerformSaveThreePIDAssociation(ctx context.Context apiURL := h.apiURL + PerformSaveThreePIDAssociationPath return httputil.PostJSON(ctx, span, h.httpClient, apiURL, req, res) } + +func (h *httpUserInternalAPI) DeleteProfile(ctx context.Context, req *api.PerformDeleteProfileRequest, res *struct{}) error { + span, ctx := opentracing.StartSpanFromContext(ctx, PerformDeleteUserProfilePath) + defer span.Finish() + + apiURL := h.apiURL + PerformDeleteUserProfilePath + return httputil.PostJSON(ctx, span, h.httpClient, apiURL, req, res) +} diff --git a/userapi/inthttp/server.go b/userapi/inthttp/server.go index ad532b901..ee9429198 100644 --- a/userapi/inthttp/server.go +++ b/userapi/inthttp/server.go @@ -457,4 +457,16 @@ func AddRoutes(internalAPIMux *mux.Router, s api.UserInternalAPI) { return util.JSONResponse{Code: http.StatusOK, JSON: &struct{}{}} }), ) + internalAPIMux.Handle(PerformDeleteUserProfilePath, + httputil.MakeInternalAPI("performDeleteUserProfilePath", func(req *http.Request) util.JSONResponse { + request := api.PerformDeleteProfileRequest{} + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.MessageResponse(http.StatusBadRequest, err.Error()) + } + if err := s.DeleteProfile(req.Context(), &request, &struct{}{}); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &struct{}{}} + }), + ) }