Build errors resolved

This commit is contained in:
SUMUKHA-PK 2019-03-13 18:19:02 +05:30
parent 98de4e7c05
commit 90cc575962
2 changed files with 37 additions and 17 deletions

View file

@ -18,23 +18,25 @@ import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
"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 // Creates and returns a new MTag type variable
func NewMTag() MTag { func NewMTag() common.MTag {
return MTag{ return common.MTag{
Tags: make(map[string]TagProperties), Tags: make(map[string]common.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, accountDB *accounts.Database, userId string, roomId string) util.JSONResponse {
if req.Method != http.MethodPGet { if req.Method != http.MethodGet {
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusMethodNotAllowed, Code: http.StatusMethodNotAllowed,
JSON: jsonerror.NotFound("Bad method"), JSON: jsonerror.NotFound("Bad method"),
@ -43,6 +45,11 @@ func GetTag(req *http.Request, userId string, roomId string) util.JSONResponse {
mtag := NewMTag() mtag := NewMTag()
localpart, _, err := gomatrixserverlib.SplitID('@', "USER_ID")
if err != nil {
return httputil.LogThenError(req, err)
}
data, err := accountDB.GetAccountDataByType( data, err := accountDB.GetAccountDataByType(
req.Context(), localpart, "ROOM_ID", "m.tag", req.Context(), localpart, "ROOM_ID", "m.tag",
) )
@ -51,7 +58,8 @@ func GetTag(req *http.Request, userId string, roomId string) util.JSONResponse {
return httputil.LogThenError(req, err) return httputil.LogThenError(req, err)
} }
err = json.Unmarshal([]byte(data), &mtag) dataByte, _ := json.Marshal(data)
err = json.Unmarshal(dataByte, &mtag)
if err != nil { if err != nil {
return httputil.LogThenError(req, err) return httputil.LogThenError(req, err)
@ -64,7 +72,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 string) util.JSONResponse { func PutTag(req *http.Request, accountDB *accounts.Database, userId string, roomId string, tag string) util.JSONResponse {
if req.Method != http.MethodPut { if req.Method != http.MethodPut {
return util.JSONResponse{ return util.JSONResponse{
@ -89,14 +97,15 @@ func PutTag(req *http.Request, userId string, roomId string, tag string) util.JS
mtag := NewMTag() mtag := NewMTag()
var properties TagProperties var properties common.TagProperties
if reqErr := httputil.UnmarshalJSONRequest(req, &properties); reqErr != nil { if reqErr := httputil.UnmarshalJSONRequest(req, &properties); reqErr != nil {
return *reqErr return *reqErr
} }
if len(data) > 0 { if len(data) > 0 {
json.Unmarshal([]byte(data), &mtag) dataByte, _ := json.Marshal(data)
err = json.Unmarshal(dataByte, &mtag)
} }
mtag.Tags[tag] = properties mtag.Tags[tag] = properties
@ -104,19 +113,19 @@ func PutTag(req *http.Request, userId string, roomId string, tag string) util.JS
newTagData, _ := json.Marshal(mtag) 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", string(newTagData),
); err != nil { ); err != nil {
return httputil.LogThenError(req, err) return httputil.LogThenError(req, err)
} }
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusOK, Code: http.StatusOK,
JSON: {}, JSON: struct{}{},
} }
} }
// DeleteTag implements DELETE /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags/{tag} // DeleteTag implements DELETE /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags/{tag}
func DeleteTag(req *http.Request, userId string, roomId string, tag string) util.JSONResponse { func DeleteTag(req *http.Request, accountDB *accounts.Database, userId string, roomId string, tag string) util.JSONResponse {
if req.Method != http.MethodDelete { if req.Method != http.MethodDelete {
return util.JSONResponse{ return util.JSONResponse{
@ -141,7 +150,8 @@ func DeleteTag(req *http.Request, userId string, roomId string, tag string) util
mtag := NewMTag() mtag := NewMTag()
if len(data) > 0 { if len(data) > 0 {
json.Unmarshal([]byte(data), &mtag) dataByte, _ := json.Marshal(data)
err = json.Unmarshal(dataByte, &mtag)
} else { } else {
//Error indicating there is no Tag data //Error indicating there is no Tag data
return util.JSONResponse{} return util.JSONResponse{}
@ -155,8 +165,16 @@ func DeleteTag(req *http.Request, userId string, roomId string, tag string) util
return util.JSONResponse{} return util.JSONResponse{}
} }
newTagData, _ := json.Marshal(mtag)
if err := accountDB.SaveAccountData(
req.Context(), localpart, "ROOM_ID", "m.tag", string(newTagData),
); err != nil {
return httputil.LogThenError(req, err)
}
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusOK, Code: http.StatusOK,
JSON: {}, JSON: struct{}{},
} }
} }

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. // 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. // MakeAuthAPI will convert it to a HTTP response and reflect it on the browser.
vars := mux.Vars(req) vars := mux.Vars(req)
return GetTag(req,accountDB vars["userId"], vars["roomId"]) return GetTag(req, accountDB, vars["userId"], vars["roomId"])
}), }),
).Methods(http.MethodGet, http.MethodOptions) ).Methods(http.MethodGet, http.MethodOptions)
@ -420,7 +420,8 @@ func Setup(
common.MakeAuthAPI("account_3pid", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { 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. // 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. // MakeAuthAPI will convert it to a HTTP response and reflect it on the browser.
return PutTag(req,accountDB vars["userId"], vars["roomId"], vars["tag"]) vars := mux.Vars(req)
return PutTag(req, accountDB, vars["userId"], vars["roomId"], vars["tag"])
}), }),
).Methods(http.MethodPut, http.MethodOptions) ).Methods(http.MethodPut, http.MethodOptions)
@ -428,7 +429,8 @@ func Setup(
common.MakeAuthAPI("account_3pid", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse { 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. // 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. // MakeAuthAPI will convert it to a HTTP response and reflect it on the browser.
return DeleteTag(req,accountDB (vars["userId"], vars["roomId"], vars["tag"]) vars := mux.Vars(req)
return DeleteTag(req, accountDB, vars["userId"], vars["roomId"], vars["tag"])
}), }),
).Methods(http.MethodDelete, http.MethodOptions) ).Methods(http.MethodDelete, http.MethodOptions)
} }