From 5b5ec0cd67f46eb03febce48d314ecf4183f9332 Mon Sep 17 00:00:00 2001 From: Derek Meer Date: Fri, 3 Aug 2018 10:29:46 -0700 Subject: [PATCH] Cleaned up code + responded to comments --- .../dendrite/appservice/routing/user.go | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/appservice/routing/user.go b/src/github.com/matrix-org/dendrite/appservice/routing/user.go index 4157cd45b..a4b49eaee 100644 --- a/src/github.com/matrix-org/dendrite/appservice/routing/user.go +++ b/src/github.com/matrix-org/dendrite/appservice/routing/user.go @@ -28,28 +28,32 @@ func URIToUID(req *http.Request, cfg config.Dendrite) util.JSONResponse { } } baseReqURL := "http://" + string(cfg.Matrix.ServerName) + "/_matrix/app/unstable/thirdparty/user/" - //appServices := cfg.Derived.ApplicationServices for _, appservice := range cfg.Derived.ApplicationServices { // Check all the fields associated with each application service - if appservice.IsInterestedInUserID(uri) { - // call the application service - reqURL := baseReqURL + appservice.ID + "?access_token=" + appservice.HSToken + - "&fields=" + uri - resp, err := http.Get(reqURL) - // take the first successful match and send that back to the user - if err == nil { - body, _ := ioutil.ReadAll(resp.Body) - respMap := map[string]interface{}{} - err := json.Unmarshal(body, &respMap) - if err != nil { - panic(err) - } - if userID, ok := respMap["userid"].(string); ok { - return util.JSONResponse{ - Code: http.StatusOK, - JSON: URIToUIDResponse{UserID: userID}, - } - } + if !appservice.IsInterestedInUserID(uri) { + continue + } + // call the application service + reqURL := baseReqURL + appservice.ID + "?access_token=" + appservice.HSToken + + "&fields=" + uri + resp, err := http.Get(reqURL) + // take the first successful match and send that back to the user + if err != nil { + continue + } + // decode the JSON to get the field we want + body, _ := ioutil.ReadAll(resp.Body) + respMap := map[string]interface{}{} + if err := json.Unmarshal(body, &respMap); err != nil { + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.NotJSON("The request body could not be decoded into valid JSON. " + err.Error()), + } + } + if userID, ok := respMap["userid"].(string); ok { + return util.JSONResponse{ + Code: http.StatusOK, + JSON: URIToUIDResponse{UserID: userID}, } } }