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

Function SetAvatarURL

clientapi/routing/profile.go:314–413  ·  view source on GitHub ↗

SetAvatarURL implements PUT /profile/{userID}/avatar_url

(
	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

312
313// SetAvatarURL implements PUT /profile/{userID}/avatar_url
314func SetAvatarURL(
315 req *http.Request, profileAPI userapi.ClientUserAPI,
316 device *userapi.Device, userID string, cfg *config.ClientAPI, rsAPI api.ClientRoomserverAPI,
317) util.JSONResponse {
318 if userID != device.UserID {
319 return util.JSONResponse{
320 Code: http.StatusForbidden,
321 JSON: jsonerror.Forbidden("userID does not match the current user"),
322 }
323 }
324
325 var r eventutil.AvatarURL
326 if resErr := httputil.UnmarshalJSONRequest(req, &r); resErr != nil {
327 return *resErr
328 }
329 if r.AvatarURL == "" {
330 return util.JSONResponse{
331 Code: http.StatusBadRequest,
332 JSON: jsonerror.BadJSON("'avatar_url' must be supplied."),
333 }
334 }
335
336 localpart, _, err := gomatrixserverlib.SplitID('@', userID)
337 if err != nil {
338 util.GetLogger(req.Context()).WithError(err).Error("gomatrixserverlib.SplitID failed")
339 return jsonerror.InternalServerError()
340 }
341
342 evTime, err := httputil.ParseTSParam(req)
343 if err != nil {
344 return util.JSONResponse{
345 Code: http.StatusBadRequest,
346 JSON: jsonerror.InvalidArgumentValue(err.Error()),
347 }
348 }
349
350 res := &userapi.QueryProfileResponse{}
351 err = profileAPI.QueryProfile(req.Context(), &userapi.QueryProfileRequest{
352 UserID: userID,
353 }, res)
354 if err != nil {
355 util.GetLogger(req.Context()).WithError(err).Error("profileAPI.QueryProfile failed")
356 return jsonerror.InternalServerError()
357 }
358 oldProfile := &authtypes.Profile{
359 Localpart: localpart,
360 DisplayName: res.DisplayName,
361 AvatarURL: res.AvatarURL,
362 }
363
364 setRes := &userapi.PerformSetAvatarURLResponse{}
365 if err = profileAPI.SetAvatarURL(req.Context(), &userapi.PerformSetAvatarURLRequest{
366 Localpart: localpart,
367 AvatarURL: r.AvatarURL,
368 }, setRes); err != nil {
369 util.GetLogger(req.Context()).WithError(err).Error("profileAPI.SetAvatarURL failed")
370 return jsonerror.InternalServerError()
371 }

Callers 1

SetupFunction · 0.85

Calls 6

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

Tested by

no test coverage detected