Cleaned up code + responded to comments

This commit is contained in:
Derek Meer 2018-08-03 10:29:46 -07:00
parent 681d74635f
commit 5b5ec0cd67

View file

@ -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/" baseReqURL := "http://" + string(cfg.Matrix.ServerName) + "/_matrix/app/unstable/thirdparty/user/"
//appServices := cfg.Derived.ApplicationServices
for _, appservice := range cfg.Derived.ApplicationServices { for _, appservice := range cfg.Derived.ApplicationServices {
// Check all the fields associated with each application service // Check all the fields associated with each application service
if appservice.IsInterestedInUserID(uri) { if !appservice.IsInterestedInUserID(uri) {
// call the application service continue
reqURL := baseReqURL + appservice.ID + "?access_token=" + appservice.HSToken + }
"&fields=" + uri // call the application service
resp, err := http.Get(reqURL) reqURL := baseReqURL + appservice.ID + "?access_token=" + appservice.HSToken +
// take the first successful match and send that back to the user "&fields=" + uri
if err == nil { resp, err := http.Get(reqURL)
body, _ := ioutil.ReadAll(resp.Body) // take the first successful match and send that back to the user
respMap := map[string]interface{}{} if err != nil {
err := json.Unmarshal(body, &respMap) continue
if err != nil { }
panic(err) // decode the JSON to get the field we want
} body, _ := ioutil.ReadAll(resp.Body)
if userID, ok := respMap["userid"].(string); ok { respMap := map[string]interface{}{}
return util.JSONResponse{ if err := json.Unmarshal(body, &respMap); err != nil {
Code: http.StatusOK, return util.JSONResponse{
JSON: URIToUIDResponse{UserID: userID}, 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},
} }
} }
} }