SetDisplayName implements PUT /profile/{userID}/displayname
( req *http.Request, profileAPI userapi.ClientUserAPI, device *userapi.Device, userID string, cfg *config.ClientAPI, rsAPI api.ClientRoomserverAPI, )
| 441 | |
| 442 | // SetDisplayName implements PUT /profile/{userID}/displayname |
| 443 | func SetDisplayName( |
| 444 | req *http.Request, profileAPI userapi.ClientUserAPI, |
| 445 | device *userapi.Device, userID string, cfg *config.ClientAPI, rsAPI api.ClientRoomserverAPI, |
| 446 | ) util.JSONResponse { |
| 447 | if userID != device.UserID { |
| 448 | return util.JSONResponse{ |
| 449 | Code: http.StatusForbidden, |
| 450 | JSON: jsonerror.Forbidden("userID does not match the current user"), |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | var r eventutil.DisplayName |
| 455 | if resErr := httputil.UnmarshalJSONRequest(req, &r); resErr != nil { |
| 456 | return *resErr |
| 457 | } |
| 458 | if r.DisplayName == "" { |
| 459 | return util.JSONResponse{ |
| 460 | Code: http.StatusBadRequest, |
| 461 | JSON: jsonerror.BadJSON("'displayname' must be supplied."), |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | localpart, _, err := gomatrixserverlib.SplitID('@', userID) |
| 466 | if err != nil { |
| 467 | util.GetLogger(req.Context()).WithError(err).Error("gomatrixserverlib.SplitID failed") |
| 468 | return jsonerror.InternalServerError() |
| 469 | } |
| 470 | |
| 471 | evTime, err := httputil.ParseTSParam(req) |
| 472 | if err != nil { |
| 473 | return util.JSONResponse{ |
| 474 | Code: http.StatusBadRequest, |
| 475 | JSON: jsonerror.InvalidArgumentValue(err.Error()), |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | pRes := &userapi.QueryProfileResponse{} |
| 480 | err = profileAPI.QueryProfile(req.Context(), &userapi.QueryProfileRequest{ |
| 481 | UserID: userID, |
| 482 | }, pRes) |
| 483 | if err != nil { |
| 484 | util.GetLogger(req.Context()).WithError(err).Error("profileAPI.QueryProfile failed") |
| 485 | return jsonerror.InternalServerError() |
| 486 | } |
| 487 | oldProfile := &authtypes.Profile{ |
| 488 | Localpart: localpart, |
| 489 | DisplayName: pRes.DisplayName, |
| 490 | AvatarURL: pRes.AvatarURL, |
| 491 | } |
| 492 | |
| 493 | err = profileAPI.SetDisplayName(req.Context(), &userapi.PerformUpdateDisplayNameRequest{ |
| 494 | Localpart: localpart, |
| 495 | DisplayName: r.DisplayName, |
| 496 | }, &struct{}{}) |
| 497 | if err != nil { |
| 498 | util.GetLogger(req.Context()).WithError(err).Error("profileAPI.SetDisplayName failed") |
| 499 | return jsonerror.InternalServerError() |
| 500 | } |
no test coverage detected