GetProfile implements GET /_matrix/federation/v1/query/profile
( httpReq *http.Request, userAPI userapi.FederationUserAPI, cfg *config.FederationAPI, )
| 28 | |
| 29 | // GetProfile implements GET /_matrix/federation/v1/query/profile |
| 30 | func GetProfile( |
| 31 | httpReq *http.Request, |
| 32 | userAPI userapi.FederationUserAPI, |
| 33 | cfg *config.FederationAPI, |
| 34 | ) util.JSONResponse { |
| 35 | userID, field := httpReq.FormValue("user_id"), httpReq.FormValue("field") |
| 36 | |
| 37 | // httpReq.FormValue will return an empty string if value is not found |
| 38 | if userID == "" { |
| 39 | return util.JSONResponse{ |
| 40 | Code: http.StatusBadRequest, |
| 41 | JSON: jsonerror.MissingArgument("The request body did not contain required argument 'user_id'."), |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | _, domain, err := gomatrixserverlib.SplitID('@', userID) |
| 46 | if err != nil { |
| 47 | util.GetLogger(httpReq.Context()).WithError(err).Error("gomatrixserverlib.SplitID failed") |
| 48 | return util.JSONResponse{ |
| 49 | Code: http.StatusBadRequest, |
| 50 | JSON: jsonerror.MissingArgument(fmt.Sprintf("Format of user ID %q is invalid", userID)), |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | if domain != cfg.Matrix.ServerName { |
| 55 | return util.JSONResponse{ |
| 56 | Code: http.StatusBadRequest, |
| 57 | JSON: jsonerror.InvalidArgumentValue(fmt.Sprintf("Domain %q does not match this server", domain)), |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | var profileRes userapi.QueryProfileResponse |
| 62 | err = userAPI.QueryProfile(httpReq.Context(), &userapi.QueryProfileRequest{ |
| 63 | UserID: userID, |
| 64 | }, &profileRes) |
| 65 | if err != nil { |
| 66 | util.GetLogger(httpReq.Context()).WithError(err).Error("userAPI.QueryProfile failed") |
| 67 | return jsonerror.InternalServerError() |
| 68 | } |
| 69 | |
| 70 | var res interface{} |
| 71 | code := http.StatusOK |
| 72 | |
| 73 | if field != "" { |
| 74 | switch field { |
| 75 | case "displayname": |
| 76 | res = eventutil.DisplayName{ |
| 77 | DisplayName: profileRes.DisplayName, |
| 78 | } |
| 79 | case "avatar_url": |
| 80 | res = eventutil.AvatarURL{ |
| 81 | AvatarURL: profileRes.AvatarURL, |
| 82 | } |
| 83 | default: |
| 84 | code = http.StatusBadRequest |
| 85 | res = jsonerror.InvalidArgumentValue("The request body did not contain an allowed value of argument 'field'. Allowed values are either: 'avatar_url', 'displayname'.") |
| 86 | } |
| 87 | } else { |
no test coverage detected