From b15a444404625ec86291a166f7dcda296127192e Mon Sep 17 00:00:00 2001 From: SUMUKHA-PK Date: Tue, 12 Mar 2019 18:25:11 +0530 Subject: [PATCH] Put Tag almost done. Get and Delete Tag in progress --- .../clientapi/routing/room_tagging.go | 98 ++++++++++++++++++- .../dendrite/clientapi/routing/routing.go | 6 +- .../matrix-org/dendrite/common/types.go | 6 ++ 3 files changed, 102 insertions(+), 8 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/room_tagging.go b/src/github.com/matrix-org/dendrite/clientapi/routing/room_tagging.go index f9c3134b7..acc7f45f1 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/room_tagging.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/room_tagging.go @@ -14,19 +14,107 @@ package routing -import "net/http" +import ( + "encoding/json" + "net/http" + + "github.com/matrix-org/dendrite/clientapi/httputil" + "github.com/matrix-org/dendrite/clientapi/jsonerror" + "github.com/matrix-org/dendrite/common" + "github.com/matrix-org/gomatrixserverlib" + "github.com/matrix-org/util" +) // GetTag implements GET /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags -func GetTag(req *http.Request, userId string, roomId string) { +func GetTag(req *http.Request, userId string, roomId string) util.JSONResponse { + if req.Method != http.MethodPGet { + return util.JSONResponse{ + Code: http.StatusMethodNotAllowed, + JSON: jsonerror.NotFound("Bad method"), + } + } + + data, err := accountDB.GetAccountDataByType( + req.Context(), localpart, "ROOM_ID", "m.tag", + ) + + if err != nil { + return httputil.LogThenError(req, err) + } + + return util.JSONResponse{ + Code: http.StatusOK, + JSON: tags, + } } -// PutTag implements GET /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags/{tag} -func PutTag(req *http.Request, userId string, roomId string, tag string) { +// PutTag implements PUT /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags/{tag} +func PutTag(req *http.Request, userId string, roomId string, tag common.Tag) util.JSONResponse { + if req.Method != http.MethodPut { + return util.JSONResponse{ + Code: http.StatusMethodNotAllowed, + JSON: jsonerror.NotFound("Bad method"), + } + } + + localpart, _, err := gomatrixserverlib.SplitID('@', "USER_ID") + if err != nil { + return httputil.LogThenError(req, err) + } + + //Check for existing entries of tags for this ROOM ID and localpart + data, err := accountDB.GetAccountDataByType( + req.Context(), localpart, "ROOM_ID", "m.tag", + ) + + if err != nil { + return httputil.LogThenError(req, err) + } + + outInterface := map[string]interface{}{} + json.Unmarshal([]byte(data), &outInterface) + + if len(data) > 0 { + if outInterface[tag.Name] != nil { + // Error saying this Tag already exists + } + } + + outInterface[tag.Name] = tag.Order + + newTagData, _ := json.Marshal(outInterface) + + if err := accountDB.SaveAccountData( + req.Context(), localpart, "ROOM_ID", "m.tag", newTagData, + ); err != nil { + return httputil.LogThenError(req, err) + } + + return util.JSONResponse{ + Code: http.StatusOK, + JSON: {}, + } } // DeleteTag implements DELETE /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags/{tag} -func DeleteTag(req *http.Request, userId string, roomId string, tag string) { +func DeleteTag(req *http.Request, userId string, roomId string, tag string) util.JSONResponse { + if req.Method != http.MethodDelete { + return util.JSONResponse{ + Code: http.StatusMethodNotAllowed, + JSON: jsonerror.NotFound("Bad method"), + } + } + + localpart, _, err := gomatrixserverlib.SplitID('@', "USER_ID") + if err != nil { + return httputil.LogThenError(req, err) + } + + return util.JSONResponse{ + Code: http.StatusOK, + JSON: {}, + } } diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go index 19473d752..f2ad5a8c3 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go @@ -412,7 +412,7 @@ func Setup( // When a GET tag request is done, obtain all data of user from here and provide a JSON response. // MakeAuthAPI will convert it to a HTTP response and reflect it on the browser. vars := mux.Vars(req) - return GetTag(req, vars["userId"], vars["roomId"]) + return GetTag(req,accountDB vars["userId"], vars["roomId"]) }), ).Methods(http.MethodGet, http.MethodOptions) @@ -420,7 +420,7 @@ func Setup( common.MakeAuthAPI("account_3pid", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { // When a PUT tag request is done, obtain all data of user from here and provide a JSON response. // MakeAuthAPI will convert it to a HTTP response and reflect it on the browser. - return PutTag(req, vars["userId"], vars["roomId"], vars["tag"]) + return PutTag(req,accountDB vars["userId"], vars["roomId"], vars["tag"]) }), ).Methods(http.MethodPut, http.MethodOptions) @@ -428,7 +428,7 @@ func Setup( common.MakeAuthAPI("account_3pid", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { // When a DELETE tag request is done, obtain all data of user from here and provide a JSON response. // MakeAuthAPI will convert it to a HTTP response and reflect it on the browser. - return DeleteTagreq, (vars["userId"], vars["roomId"], vars["tag"]) + return DeleteTag(req,accountDB (vars["userId"], vars["roomId"], vars["tag"]) }), ).Methods(http.MethodDelete, http.MethodOptions) } diff --git a/src/github.com/matrix-org/dendrite/common/types.go b/src/github.com/matrix-org/dendrite/common/types.go index e539774e2..a6171050d 100644 --- a/src/github.com/matrix-org/dendrite/common/types.go +++ b/src/github.com/matrix-org/dendrite/common/types.go @@ -46,6 +46,12 @@ type DisplayName struct { // recognized by strconv.ParseBool type WeakBoolean bool +// RoomTags contain the data of the Tags per User for each Room +type Tag struct { + Name string + Order string +} + // UnmarshalJSON is overridden here to allow strings vaguely representing a true // or false boolean to be set as their closest counterpart func (b *WeakBoolean) UnmarshalJSON(data []byte) error {