diff options
author | oliverpool <3864879+oliverpool@users.noreply.github.com> | 2020-08-25 22:04:35 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-25 21:04:35 +0100 |
commit | 3205b9212d76f61301efae3c554bf9c3fbfc94c8 (patch) | |
tree | 6ec60e814c018c85e88ccf3acba1206b43adc96c /clientapi/routing | |
parent | 55498c8deb262e564e1b79bf4409eb5593f3034e (diff) |
[readability] use event.StateKeyEquals where relevant and minor for-loop refactoring (#1339)
* use event.StateKeyEquals where relevant
Signed-off-by: Olivier Charvin <git@olivier.pfad.fr>
* use userID
Diffstat (limited to 'clientapi/routing')
-rw-r--r-- | clientapi/routing/getevent.go | 23 | ||||
-rw-r--r-- | clientapi/routing/membership.go | 11 | ||||
-rw-r--r-- | clientapi/routing/sendtyping.go | 38 |
3 files changed, 25 insertions, 47 deletions
diff --git a/clientapi/routing/getevent.go b/clientapi/routing/getevent.go index c74509f0..18b96e1e 100644 --- a/clientapi/routing/getevent.go +++ b/clientapi/routing/getevent.go @@ -104,17 +104,18 @@ func GetEvent( } for _, stateEvent := range stateResp.StateEvents { - if stateEvent.StateKeyEquals(r.device.UserID) { - membership, err := stateEvent.Membership() - if err != nil { - util.GetLogger(req.Context()).WithError(err).Error("stateEvent.Membership failed") - return jsonerror.InternalServerError() - } - if membership == gomatrixserverlib.Join { - return util.JSONResponse{ - Code: http.StatusOK, - JSON: gomatrixserverlib.ToClientEvent(r.requestedEvent, gomatrixserverlib.FormatAll), - } + if !stateEvent.StateKeyEquals(device.UserID) { + continue + } + membership, err := stateEvent.Membership() + if err != nil { + util.GetLogger(req.Context()).WithError(err).Error("stateEvent.Membership failed") + return jsonerror.InternalServerError() + } + if membership == gomatrixserverlib.Join { + return util.JSONResponse{ + Code: http.StatusOK, + JSON: gomatrixserverlib.ToClientEvent(r.requestedEvent, gomatrixserverlib.FormatAll), } } } diff --git a/clientapi/routing/membership.go b/clientapi/routing/membership.go index 37fafa5a..5d635c01 100644 --- a/clientapi/routing/membership.go +++ b/clientapi/routing/membership.go @@ -388,15 +388,20 @@ func checkMemberInRoom(ctx context.Context, stateAPI currentstateAPI.CurrentStat e := jsonerror.InternalServerError() return &e } - ev, ok := membershipRes.StateEvents[tuple] - if !ok { + ev := membershipRes.StateEvents[tuple] + if ev == nil { return &util.JSONResponse{ Code: http.StatusForbidden, JSON: jsonerror.Forbidden("user does not belong to room"), } } membership, err := ev.Membership() - if err != nil || membership != "join" { + if err != nil { + util.GetLogger(ctx).WithError(err).Error("Member event isn't valid") + e := jsonerror.InternalServerError() + return &e + } + if membership != gomatrixserverlib.Join { return &util.JSONResponse{ Code: http.StatusForbidden, JSON: jsonerror.Forbidden("user does not belong to room"), diff --git a/clientapi/routing/sendtyping.go b/clientapi/routing/sendtyping.go index 54a82286..e4b5b7a3 100644 --- a/clientapi/routing/sendtyping.go +++ b/clientapi/routing/sendtyping.go @@ -21,7 +21,6 @@ import ( "github.com/matrix-org/dendrite/eduserver/api" userapi "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/dendrite/userapi/storage/accounts" - "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" ) @@ -46,46 +45,19 @@ func SendTyping( } // Verify that the user is a member of this room - tuple := gomatrixserverlib.StateKeyTuple{ - EventType: gomatrixserverlib.MRoomMember, - StateKey: userID, - } - var res currentstateAPI.QueryCurrentStateResponse - err := stateAPI.QueryCurrentState(req.Context(), ¤tstateAPI.QueryCurrentStateRequest{ - RoomID: roomID, - StateTuples: []gomatrixserverlib.StateKeyTuple{tuple}, - }, &res) - if err != nil { - util.GetLogger(req.Context()).WithError(err).Error("QueryCurrentState failed") - return jsonerror.InternalServerError() - } - ev := res.StateEvents[tuple] - if ev == nil { - return util.JSONResponse{ - Code: http.StatusForbidden, - JSON: jsonerror.Forbidden("User not in this room"), - } - } - membership, err := ev.Membership() - if err != nil { - util.GetLogger(req.Context()).WithError(err).Error("Member event isn't valid") - return jsonerror.InternalServerError() - } - if membership != gomatrixserverlib.Join { - return util.JSONResponse{ - Code: http.StatusForbidden, - JSON: jsonerror.Forbidden("User not in this room"), - } + resErr := checkMemberInRoom(req.Context(), stateAPI, userID, roomID) + if resErr != nil { + return *resErr } // parse the incoming http request var r typingContentJSON - resErr := httputil.UnmarshalJSONRequest(req, &r) + resErr = httputil.UnmarshalJSONRequest(req, &r) if resErr != nil { return *resErr } - if err = api.SendTyping( + if err := api.SendTyping( req.Context(), eduAPI, userID, roomID, r.Typing, r.Timeout, ); err != nil { util.GetLogger(req.Context()).WithError(err).Error("eduProducer.Send failed") |