aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNeil Alexander <neilalexander@users.noreply.github.com>2020-01-29 17:53:05 +0000
committerGitHub <noreply@github.com>2020-01-29 17:53:05 +0000
commit6885c10083cc05494d4bfad43968355666c233a5 (patch)
treea00889cee7c0cd1766b527f9f10b40e8153509a1
parent054f5383c40e3b26fd3ab853d319ff818a3c7aa1 (diff)
Implement GET endpoints for account_data in clientapi (#861)
* Implement GET endpoints for account_data in clientapi * Fix accountDB parameter * Remove fmt.Println
-rw-r--r--clientapi/routing/account_data.go32
-rw-r--r--clientapi/routing/routing.go20
2 files changed, 52 insertions, 0 deletions
diff --git a/clientapi/routing/account_data.go b/clientapi/routing/account_data.go
index d57a6d37..bbc8c258 100644
--- a/clientapi/routing/account_data.go
+++ b/clientapi/routing/account_data.go
@@ -28,6 +28,38 @@ 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 {
+ 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 4a36661d..5c98cd0d 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))