Interface approach removed, map approach added

This commit is contained in:
SUMUKHA-PK 2019-03-12 19:52:11 +05:30
parent b15a444404
commit 9f7b1290af
2 changed files with 25 additions and 11 deletions

View file

@ -20,11 +20,17 @@ import (
"github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/httputil"
"github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/jsonerror"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util" "github.com/matrix-org/util"
) )
// Creates and returns a new MTag type variable
func NewMTag() MTag {
return MTag{
Tags: make(map[string]TagProperties),
}
}
// GetTag implements GET /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags // GetTag implements GET /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags
func GetTag(req *http.Request, userId string, roomId string) util.JSONResponse { func GetTag(req *http.Request, userId string, roomId string) util.JSONResponse {
@ -50,7 +56,7 @@ func GetTag(req *http.Request, userId string, roomId string) util.JSONResponse {
} }
// PutTag implements PUT /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags/{tag} // 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 { func PutTag(req *http.Request, userId string, roomId string, tag string) util.JSONResponse {
if req.Method != http.MethodPut { if req.Method != http.MethodPut {
return util.JSONResponse{ return util.JSONResponse{
@ -73,18 +79,22 @@ func PutTag(req *http.Request, userId string, roomId string, tag common.Tag) uti
return httputil.LogThenError(req, err) return httputil.LogThenError(req, err)
} }
outInterface := map[string]interface{}{} mtag := NewMTag()
json.Unmarshal([]byte(data), &outInterface)
// outInterface := map[string]interface{}{}
if len(data) > 0 { if len(data) > 0 {
if outInterface[tag.Name] != nil { json.Unmarshal([]byte(data), &mtag)
if outInterface[tag] != nil {
// Error saying this Tag already exists // Error saying this Tag already exists
} }
} }
outInterface[tag.Name] = tag.Order mtag.Tags[tag] = TagProperties{
Order: 0.5, // Change value based on need
}
newTagData, _ := json.Marshal(outInterface) newTagData, _ := json.Marshal(mtag)
if err := accountDB.SaveAccountData( if err := accountDB.SaveAccountData(
req.Context(), localpart, "ROOM_ID", "m.tag", newTagData, req.Context(), localpart, "ROOM_ID", "m.tag", newTagData,

View file

@ -46,10 +46,14 @@ type DisplayName struct {
// recognized by strconv.ParseBool // recognized by strconv.ParseBool
type WeakBoolean bool type WeakBoolean bool
// RoomTags contain the data of the Tags per User for each Room // MTag contains the data for a Tag which can be referenced by the Tag name
type Tag struct { type MTag struct {
Name string Tags map[string]TagProperties `json:"tags"`
Order string }
// TagProperties contains the properties of an MTag datatype
type TagProperties struct {
Order float32 `json:"order,omitempty"` // Empty values must be neglected
} }
// UnmarshalJSON is overridden here to allow strings vaguely representing a true // UnmarshalJSON is overridden here to allow strings vaguely representing a true