From bdcec9fd23db36ebe30839f0a9febeb924fe285d Mon Sep 17 00:00:00 2001 From: Anant Prakash Date: Sat, 16 Jun 2018 18:17:44 +0530 Subject: [PATCH] Use userutil to parse localpart Signed-off-by: Anant Prakash --- .../dendrite/clientapi/routing/account_data.go | 4 ++-- .../dendrite/clientapi/routing/createroom.go | 3 ++- .../matrix-org/dendrite/clientapi/routing/device.go | 8 ++++---- .../matrix-org/dendrite/clientapi/routing/filter.go | 6 +++--- .../matrix-org/dendrite/clientapi/routing/joinroom.go | 3 ++- .../matrix-org/dendrite/clientapi/routing/logout.go | 6 +++--- .../matrix-org/dendrite/clientapi/routing/profile.go | 11 ++++++----- .../matrix-org/dendrite/clientapi/routing/threepid.go | 6 +++--- .../dendrite/federationapi/routing/profile.go | 8 ++------ .../matrix-org/dendrite/syncapi/sync/requestpool.go | 3 ++- 10 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/account_data.go b/src/github.com/matrix-org/dendrite/clientapi/routing/account_data.go index 30e00f723..5df6352a6 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/account_data.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/account_data.go @@ -23,7 +23,7 @@ import ( "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/producers" - "github.com/matrix-org/gomatrixserverlib" + "github.com/matrix-org/dendrite/clientapi/userutil" "github.com/matrix-org/util" ) @@ -47,7 +47,7 @@ func SaveAccountData( } } - localpart, _, err := gomatrixserverlib.SplitID('@', userID) + localpart, err := userutil.ParseUsernameParam(userID, nil) if err != nil { return httputil.LogThenError(req, err) } diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go b/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go index edb1858d2..2a45c88ab 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go @@ -27,6 +27,7 @@ import ( "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/producers" + "github.com/matrix-org/dendrite/clientapi/userutil" "github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/common/config" "github.com/matrix-org/gomatrixserverlib" @@ -152,7 +153,7 @@ func createRoom(req *http.Request, device *authtypes.Device, "roomID": roomID, }).Info("Creating new room") - localpart, _, err := gomatrixserverlib.SplitID('@', userID) + localpart, err := userutil.ParseUsernameParam(userID, nil) if err != nil { return httputil.LogThenError(req, err) } diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/device.go b/src/github.com/matrix-org/dendrite/clientapi/routing/device.go index cf6f24a7d..f97f7a202 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/device.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/device.go @@ -23,7 +23,7 @@ import ( "github.com/matrix-org/dendrite/clientapi/auth/storage/devices" "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" - "github.com/matrix-org/gomatrixserverlib" + "github.com/matrix-org/dendrite/clientapi/userutil" "github.com/matrix-org/util" ) @@ -45,7 +45,7 @@ func GetDeviceByID( req *http.Request, deviceDB *devices.Database, device *authtypes.Device, deviceID string, ) util.JSONResponse { - localpart, _, err := gomatrixserverlib.SplitID('@', device.UserID) + localpart, err := userutil.ParseUsernameParam(device.UserID, nil) if err != nil { return httputil.LogThenError(req, err) } @@ -74,7 +74,7 @@ func GetDeviceByID( func GetDevicesByLocalpart( req *http.Request, deviceDB *devices.Database, device *authtypes.Device, ) util.JSONResponse { - localpart, _, err := gomatrixserverlib.SplitID('@', device.UserID) + localpart, err := userutil.ParseUsernameParam(device.UserID, nil) if err != nil { return httputil.LogThenError(req, err) } @@ -113,7 +113,7 @@ func UpdateDeviceByID( } } - localpart, _, err := gomatrixserverlib.SplitID('@', device.UserID) + localpart, err := userutil.ParseUsernameParam(device.UserID, nil) if err != nil { return httputil.LogThenError(req, err) } diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/filter.go b/src/github.com/matrix-org/dendrite/clientapi/routing/filter.go index 109c55da1..54c78ad18 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/filter.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/filter.go @@ -23,8 +23,8 @@ import ( "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" + "github.com/matrix-org/dendrite/clientapi/userutil" "github.com/matrix-org/gomatrix" - "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" ) @@ -44,7 +44,7 @@ func GetFilter( JSON: jsonerror.Forbidden("Cannot get filters for other users"), } } - localpart, _, err := gomatrixserverlib.SplitID('@', userID) + localpart, err := userutil.ParseUsernameParam(userID, nil) if err != nil { return httputil.LogThenError(req, err) } @@ -92,7 +92,7 @@ func PutFilter( } } - localpart, _, err := gomatrixserverlib.SplitID('@', userID) + localpart, err := userutil.ParseUsernameParam(userID, nil) if err != nil { return httputil.LogThenError(req, err) } diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/joinroom.go b/src/github.com/matrix-org/dendrite/clientapi/routing/joinroom.go index 7c0af2eb8..5901e576c 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/joinroom.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/joinroom.go @@ -25,6 +25,7 @@ import ( "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/producers" + "github.com/matrix-org/dendrite/clientapi/userutil" "github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/common/config" "github.com/matrix-org/dendrite/roomserver/api" @@ -52,7 +53,7 @@ func JoinRoomByIDOrAlias( return *resErr } - localpart, _, err := gomatrixserverlib.SplitID('@', device.UserID) + localpart, err := userutil.ParseUsernameParam(device.UserID, nil) if err != nil { return httputil.LogThenError(req, err) } diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/logout.go b/src/github.com/matrix-org/dendrite/clientapi/routing/logout.go index d20138534..b147e01e2 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/logout.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/logout.go @@ -21,7 +21,7 @@ import ( "github.com/matrix-org/dendrite/clientapi/auth/storage/devices" "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" - "github.com/matrix-org/gomatrixserverlib" + "github.com/matrix-org/dendrite/clientapi/userutil" "github.com/matrix-org/util" ) @@ -36,7 +36,7 @@ func Logout( } } - localpart, _, err := gomatrixserverlib.SplitID('@', device.UserID) + localpart, err := userutil.ParseUsernameParam(device.UserID, nil) if err != nil { return httputil.LogThenError(req, err) } @@ -55,7 +55,7 @@ func Logout( func LogoutAll( req *http.Request, deviceDB *devices.Database, device *authtypes.Device, ) util.JSONResponse { - localpart, _, err := gomatrixserverlib.SplitID('@', device.UserID) + localpart, err := userutil.ParseUsernameParam(device.UserID, nil) if err != nil { return httputil.LogThenError(req, err) } diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/profile.go b/src/github.com/matrix-org/dendrite/clientapi/routing/profile.go index 6fb748fca..8b075bc72 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/profile.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/profile.go @@ -23,6 +23,7 @@ import ( "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/producers" + "github.com/matrix-org/dendrite/clientapi/userutil" "github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/common/config" "github.com/matrix-org/dendrite/roomserver/api" @@ -41,7 +42,7 @@ func GetProfile( JSON: jsonerror.NotFound("Bad method"), } } - localpart, _, err := gomatrixserverlib.SplitID('@', userID) + localpart, err := userutil.ParseUsernameParam(userID, nil) if err != nil { return httputil.LogThenError(req, err) } @@ -64,7 +65,7 @@ func GetProfile( func GetAvatarURL( req *http.Request, accountDB *accounts.Database, userID string, ) util.JSONResponse { - localpart, _, err := gomatrixserverlib.SplitID('@', userID) + localpart, err := userutil.ParseUsernameParam(userID, nil) if err != nil { return httputil.LogThenError(req, err) } @@ -108,7 +109,7 @@ func SetAvatarURL( } } - localpart, _, err := gomatrixserverlib.SplitID('@', userID) + localpart, err := userutil.ParseUsernameParam(userID, nil) if err != nil { return httputil.LogThenError(req, err) } @@ -156,7 +157,7 @@ func SetAvatarURL( func GetDisplayName( req *http.Request, accountDB *accounts.Database, userID string, ) util.JSONResponse { - localpart, _, err := gomatrixserverlib.SplitID('@', userID) + localpart, err := userutil.ParseUsernameParam(userID, nil) if err != nil { return httputil.LogThenError(req, err) } @@ -200,7 +201,7 @@ func SetDisplayName( } } - localpart, _, err := gomatrixserverlib.SplitID('@', userID) + localpart, err := userutil.ParseUsernameParam(userID, nil) if err != nil { return httputil.LogThenError(req, err) } diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/threepid.go b/src/github.com/matrix-org/dendrite/clientapi/routing/threepid.go index 897d13b63..96a8d6dd9 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/threepid.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/threepid.go @@ -22,9 +22,9 @@ import ( "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/threepid" + "github.com/matrix-org/dendrite/clientapi/userutil" "github.com/matrix-org/dendrite/common/config" - "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" ) @@ -125,7 +125,7 @@ func CheckAndSave3PIDAssociation( } // Save the association in the database - localpart, _, err := gomatrixserverlib.SplitID('@', device.UserID) + localpart, err := userutil.ParseUsernameParam(device.UserID, nil) if err != nil { return httputil.LogThenError(req, err) } @@ -144,7 +144,7 @@ func CheckAndSave3PIDAssociation( func GetAssociated3PIDs( req *http.Request, accountDB *accounts.Database, device *authtypes.Device, ) util.JSONResponse { - localpart, _, err := gomatrixserverlib.SplitID('@', device.UserID) + localpart, err := userutil.ParseUsernameParam(device.UserID, nil) if err != nil { return httputil.LogThenError(req, err) } diff --git a/src/github.com/matrix-org/dendrite/federationapi/routing/profile.go b/src/github.com/matrix-org/dendrite/federationapi/routing/profile.go index a9cbfca4a..180884924 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/routing/profile.go +++ b/src/github.com/matrix-org/dendrite/federationapi/routing/profile.go @@ -20,9 +20,9 @@ import ( "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" + "github.com/matrix-org/dendrite/clientapi/userutil" "github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/common/config" - "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" ) @@ -42,15 +42,11 @@ func GetProfile( } } - localpart, domain, err := gomatrixserverlib.SplitID('@', userID) + localpart, err := userutil.ParseUsernameParam(userID, &cfg.Matrix.ServerName) if err != nil { return httputil.LogThenError(httpReq, err) } - if domain != cfg.Matrix.ServerName { - return httputil.LogThenError(httpReq, err) - } - profile, err := accountDB.GetProfileByLocalpart(httpReq.Context(), localpart) if err != nil { return httputil.LogThenError(httpReq, err) diff --git a/src/github.com/matrix-org/dendrite/syncapi/sync/requestpool.go b/src/github.com/matrix-org/dendrite/syncapi/sync/requestpool.go index 5c560ff52..30976631c 100644 --- a/src/github.com/matrix-org/dendrite/syncapi/sync/requestpool.go +++ b/src/github.com/matrix-org/dendrite/syncapi/sync/requestpool.go @@ -22,6 +22,7 @@ import ( "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" + "github.com/matrix-org/dendrite/clientapi/userutil" "github.com/matrix-org/dendrite/syncapi/storage" "github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/gomatrixserverlib" @@ -143,7 +144,7 @@ func (rp *RequestPool) appendAccountData( // data keys were set between two message. This isn't a huge issue since the // duplicate data doesn't represent a huge quantity of data, but an optimisation // here would be making sure each data is sent only once to the client. - localpart, _, err := gomatrixserverlib.SplitID('@', userID) + localpart, err := userutil.ParseUsernameParam(userID, nil) if err != nil { return nil, err }