Put Tag almost done. Get and Delete Tag in progress

This commit is contained in:
SUMUKHA-PK 2019-03-12 18:25:11 +05:30
parent 795bb5b6f8
commit b15a444404
3 changed files with 102 additions and 8 deletions

View file

@ -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"),
}
}
// PutTag implements GET /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags/{tag}
func PutTag(req *http.Request, userId string, roomId string, tag string) {
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 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: {},
}
}

View file

@ -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)
}

View file

@ -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 {