From 19b867fbbcbf5263c31f0e9886d0a3a61373d7d4 Mon Sep 17 00:00:00 2001 From: SUMUKHA-PK Date: Fri, 2 Aug 2019 10:09:43 +0530 Subject: [PATCH] Incremental sync op made aysnc --- clientapi/routing/room_tagging.go | 111 +++++++++++------------------- testfile | 6 +- 2 files changed, 44 insertions(+), 73 deletions(-) diff --git a/clientapi/routing/room_tagging.go b/clientapi/routing/room_tagging.go index 541aa190f..95844e28f 100644 --- a/clientapi/routing/room_tagging.go +++ b/clientapi/routing/room_tagging.go @@ -18,6 +18,8 @@ import ( "encoding/json" "net/http" + "github.com/sirupsen/logrus" + "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/dendrite/clientapi/httputil" @@ -28,7 +30,7 @@ import ( "github.com/matrix-org/util" ) -// newTag creates and returns a new Tag type +// newTag creates and returns a new gomatrix.TagContent func newTag() gomatrix.TagContent { return gomatrix.TagContent{ Tags: make(map[string]gomatrix.TagProperties), @@ -53,43 +55,32 @@ func GetTags( } _, data, err := obtainSavedTags(req, userID, roomID, accountDB) - if err != nil { return httputil.LogThenError(req, err) } - dataByte, err := json.Marshal(data) - if err != nil { - return httputil.LogThenError(req, err) + if len(data) == 0 { + return util.JSONResponse{ + Code: http.StatusOK, + JSON: struct{}{}, + } } - var tagData []gomatrixserverlib.ClientEvent - tagContent := newTag() - err = json.Unmarshal(dataByte, &tagData) - if err != nil { - return httputil.LogThenError(req, err) - } - - err = json.Unmarshal(tagData[0].Content, &tagContent) - - if err != nil { - return httputil.LogThenError(req, err) - } - - // send data to syncapi - if err := syncProducer.SendData(userID, roomID, "m.tag"); err != nil { - return httputil.LogThenError(req, err) - } + go func() { + if err := syncProducer.SendData(userID, roomID, "m.tag"); err != nil { + logrus.WithError(err).Error("Incremental sync operation failed") + } + }() return util.JSONResponse{ Code: http.StatusOK, - JSON: tagContent, + JSON: struct{}{}, } } // PutTag implements PUT /_matrix/client/r0/user/{userID}/rooms/{roomID}/tags/{tag} // Put functionality works by getting existing data from the DB (if any), adding -// the tag onto the "map" and saving the new "map" onto the DB +// the tag on to the "map" and saving the new "map" onto the DB func PutTag( req *http.Request, accountDB *accounts.Database, @@ -107,40 +98,33 @@ func PutTag( } } - localpart, data, err := obtainSavedTags(req, userID, roomID, accountDB) - - if err != nil { - return httputil.LogThenError(req, err) - } - var properties gomatrix.TagProperties - if reqErr := httputil.UnmarshalJSONRequest(req, &properties); reqErr != nil { return *reqErr } - tagContent := newTag() - var dataByte []byte - if len(data) > 0 { - dataByte, err = json.Marshal(data) - if err != nil { - return httputil.LogThenError(req, err) - } - if err = json.Unmarshal(dataByte, &tagContent); err != nil { - return httputil.LogThenError(req, err) - } - } - tagContent.Tags[tag] = properties - err = saveTagData(req, localpart, roomID, accountDB, tagContent) + localpart, data, err := obtainSavedTags(req, userID, roomID, accountDB) if err != nil { return httputil.LogThenError(req, err) } - // send data to syncapi - if err := syncProducer.SendData(userID, roomID, "m.tag"); err != nil { + tagContent := newTag() + if len(data) > 0 { + if err = json.Unmarshal(data[0].Content, &tagContent); err != nil { + return httputil.LogThenError(req, err) + } + } + tagContent.Tags[tag] = properties + if err = saveTagData(req, localpart, roomID, accountDB, tagContent); err != nil { return httputil.LogThenError(req, err) } + go func() { + if err := syncProducer.SendData(userID, roomID, "m.tag"); err != nil { + logrus.WithError(err).Error("Incremental sync operation failed") + } + }() + return util.JSONResponse{ Code: http.StatusOK, JSON: struct{}{}, @@ -163,38 +147,26 @@ func DeleteTag( if device.UserID != userID { return util.JSONResponse{ Code: http.StatusForbidden, - JSON: jsonerror.Forbidden("Cannot delete another user's tags"), + JSON: jsonerror.Forbidden("Cannot modify another user's tags"), } } localpart, data, err := obtainSavedTags(req, userID, roomID, accountDB) - if err != nil { return httputil.LogThenError(req, err) } - // If there are no tags in the database, exit. + // If there are no tags in the database, exit if len(data) == 0 { - //Synapse returns a 200 OK response on finding no Tags, same policy is followed here. + //Specifications mentions a 200 OK response is returned on finding no Tags, same policy is followed here. return util.JSONResponse{ Code: http.StatusOK, JSON: struct{}{}, } } - dataByte, err := json.Marshal(data) - if err != nil { - return httputil.LogThenError(req, err) - } - - var tagData []gomatrixserverlib.ClientEvent tagContent := newTag() - err = json.Unmarshal(dataByte, &tagData) - if err != nil { - return httputil.LogThenError(req, err) - } - - err = json.Unmarshal(tagData[0].Content, &tagContent) + err = json.Unmarshal(data[0].Content, &tagContent) if err != nil { return httputil.LogThenError(req, err) } @@ -203,22 +175,21 @@ func DeleteTag( if _, ok := tagContent.Tags[tag]; ok { delete(tagContent.Tags, tag) } else { - //Synapse returns a 200 OK response on finding no Tags, same policy is followed here. + //Specifications mentions a 200 OK response is returned on finding no Tags, same policy is followed here. return util.JSONResponse{ Code: http.StatusOK, JSON: struct{}{}, } } - err = saveTagData(req, localpart, roomID, accountDB, tagContent) - - if err != nil { + if err = saveTagData(req, localpart, roomID, accountDB, tagContent); err != nil { return httputil.LogThenError(req, err) } - // send data to syncapi - if err := syncProducer.SendData(userID, roomID, "m.tag"); err != nil { - return httputil.LogThenError(req, err) - } + go func() { + if err := syncProducer.SendData(userID, roomID, "m.tag"); err != nil { + logrus.WithError(err).Error("Incremental sync operation failed") + } + }() return util.JSONResponse{ Code: http.StatusOK, diff --git a/testfile b/testfile index 2cd289fa5..1d97eb37e 100644 --- a/testfile +++ b/testfile @@ -147,8 +147,6 @@ Inbound federation can receive room-join requests Typing events appear in initial sync Typing events appear in incremental sync Typing events appear in gapped sync -Can add tag -Can remove tag Inbound federation of state requires event_id as a mandatory paramater Inbound federation of state_ids requires event_id as a mandatory paramater POST /register returns the same device_id as that in the request @@ -161,7 +159,9 @@ Inbound federation rejects remote attempts to kick local users to rooms An event which redacts itself should be ignored A pair of events which redact each other should be ignored Full state sync includes joined rooms +Can add tag +Can remove tag Can list tags for a room Tags appear in an initial v2 /sync Newly updated tags appear in an incremental v2 /sync -Deleted tags appear in an incremental v2 /sync \ No newline at end of file +Deleted tags appear in an incremental v2 /sync