From 4d80dff4583d278620d9b3ed437e9fcd8d4674ee Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 29 Jan 2020 17:21:31 +0000 Subject: [PATCH] Implement GET endpoints for account_data in clientapi --- .../accounts/sqlite3/account_data_table.go | 2 +- clientapi/routing/account_data.go | 34 +++++++++++++++++++ clientapi/routing/routing.go | 20 +++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/clientapi/auth/storage/accounts/sqlite3/account_data_table.go b/clientapi/auth/storage/accounts/sqlite3/account_data_table.go index 916007023..c2143881b 100644 --- a/clientapi/auth/storage/accounts/sqlite3/account_data_table.go +++ b/clientapi/auth/storage/accounts/sqlite3/account_data_table.go @@ -39,7 +39,7 @@ CREATE TABLE IF NOT EXISTS account_data ( const insertAccountDataSQL = ` INSERT INTO account_data(localpart, room_id, type, content) VALUES($1, $2, $3, $4) - ON CONFLICT (localpart, room_id, type) DO UPDATE SET content = EXCLUDED.content + ON CONFLICT (localpart, room_id, type) DO UPDATE SET content = $4 ` const selectAccountDataSQL = "" + diff --git a/clientapi/routing/account_data.go b/clientapi/routing/account_data.go index 3f099e46c..ed57c6e72 100644 --- a/clientapi/routing/account_data.go +++ b/clientapi/routing/account_data.go @@ -15,6 +15,7 @@ package routing import ( + "fmt" "io/ioutil" "net/http" @@ -28,6 +29,39 @@ import ( "github.com/matrix-org/util" ) +// GetAccountData implements GET /user/{userId}/[rooms/{roomid}/]account_data/{type} +func GetAccountData( + req *http.Request, accountDB accounts.Database, device *authtypes.Device, + userID string, roomID string, dataType string, +) util.JSONResponse { + if userID != device.UserID { + return util.JSONResponse{ + Code: http.StatusForbidden, + JSON: jsonerror.Forbidden("userID does not match the current user"), + } + } + + localpart, _, err := gomatrixserverlib.SplitID('@', userID) + if err != nil { + fmt.Println("Failed to split ID:", err) + return httputil.LogThenError(req, err) + } + + if data, err := accountDB.GetAccountDataByType( + req.Context(), localpart, roomID, dataType, + ); err == nil { + return util.JSONResponse{ + Code: http.StatusOK, + JSON: data, + } + } + + return util.JSONResponse{ + Code: http.StatusNotFound, + JSON: jsonerror.Forbidden("data not found"), + } +} + // SaveAccountData implements PUT /user/{userId}/[rooms/{roomId}/]account_data/{type} func SaveAccountData( req *http.Request, accountDB accounts.Database, device *authtypes.Device, diff --git a/clientapi/routing/routing.go b/clientapi/routing/routing.go index 6a652d35a..933fdf60c 100644 --- a/clientapi/routing/routing.go +++ b/clientapi/routing/routing.go @@ -430,6 +430,26 @@ func Setup( }), ).Methods(http.MethodPut, http.MethodOptions) + r0mux.Handle("/user/{userID}/account_data/{type}", + common.MakeAuthAPI("user_account_data", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { + vars, err := common.URLDecodeMapValues(mux.Vars(req)) + if err != nil { + return util.ErrorResponse(err) + } + return GetAccountData(req, accountDB, device, vars["userID"], "", vars["type"]) + }), + ).Methods(http.MethodGet) + + r0mux.Handle("/user/{userID}/rooms/{roomID}/account_data/{type}", + common.MakeAuthAPI("user_account_data", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { + vars, err := common.URLDecodeMapValues(mux.Vars(req)) + if err != nil { + return util.ErrorResponse(err) + } + return GetAccountData(req, accountDB, device, vars["userID"], vars["roomID"], vars["type"]) + }), + ).Methods(http.MethodGet) + r0mux.Handle("/rooms/{roomID}/members", common.MakeAuthAPI("rooms_members", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { vars, err := common.URLDecodeMapValues(mux.Vars(req))