MCPcopy Create free account
hub / github.com/daodst/chat / SetDisplayName

Function SetDisplayName

clientapi/routing/profile.go:443–542  ·  view source on GitHub ↗

SetDisplayName implements PUT /profile/{userID}/displayname

(
	req *http.Request, profileAPI userapi.ClientUserAPI,
	device *userapi.Device, userID string, cfg *config.ClientAPI, rsAPI api.ClientRoomserverAPI,
)

Source from the content-addressed store, hash-verified

441
442// SetDisplayName implements PUT /profile/{userID}/displayname
443func 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 }

Callers 1

SetupFunction · 0.85

Calls 6

buildMembershipEventsFunction · 0.85
ContextMethod · 0.80
QueryProfileMethod · 0.65
SetDisplayNameMethod · 0.65
QueryRoomsForUserMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected