SaveAccountData implements PUT /user/{userId}/[rooms/{roomId}/]account_data/{type}
( req *http.Request, userAPI api.ClientUserAPI, device *api.Device, userID string, roomID string, dataType string, syncProducer *producers.SyncAPIProducer, )
| 75 | |
| 76 | // SaveAccountData implements PUT /user/{userId}/[rooms/{roomId}/]account_data/{type} |
| 77 | func SaveAccountData( |
| 78 | req *http.Request, userAPI api.ClientUserAPI, device *api.Device, |
| 79 | userID string, roomID string, dataType string, syncProducer *producers.SyncAPIProducer, |
| 80 | ) util.JSONResponse { |
| 81 | if userID != device.UserID { |
| 82 | return util.JSONResponse{ |
| 83 | Code: http.StatusForbidden, |
| 84 | JSON: jsonerror.Forbidden("userID does not match the current user"), |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | defer req.Body.Close() // nolint: errcheck |
| 89 | |
| 90 | if req.Body == http.NoBody { |
| 91 | return util.JSONResponse{ |
| 92 | Code: http.StatusBadRequest, |
| 93 | JSON: jsonerror.NotJSON("Content not JSON"), |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if dataType == "m.fully_read" || dataType == "m.push_rules" { |
| 98 | return util.JSONResponse{ |
| 99 | Code: http.StatusForbidden, |
| 100 | JSON: jsonerror.Forbidden(fmt.Sprintf("Unable to modify %q using this API", dataType)), |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | body, err := io.ReadAll(req.Body) |
| 105 | if err != nil { |
| 106 | util.GetLogger(req.Context()).WithError(err).Error("io.ReadAll failed") |
| 107 | return jsonerror.InternalServerError() |
| 108 | } |
| 109 | |
| 110 | if !json.Valid(body) { |
| 111 | return util.JSONResponse{ |
| 112 | Code: http.StatusBadRequest, |
| 113 | JSON: jsonerror.BadJSON("Bad JSON content"), |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | dataReq := api.InputAccountDataRequest{ |
| 118 | UserID: userID, |
| 119 | DataType: dataType, |
| 120 | RoomID: roomID, |
| 121 | AccountData: json.RawMessage(body), |
| 122 | } |
| 123 | dataRes := api.InputAccountDataResponse{} |
| 124 | if err := userAPI.InputAccountData(req.Context(), &dataReq, &dataRes); err != nil { |
| 125 | util.GetLogger(req.Context()).WithError(err).Error("userAPI.InputAccountData failed") |
| 126 | return util.ErrorResponse(err) |
| 127 | } |
| 128 | |
| 129 | return util.JSONResponse{ |
| 130 | Code: http.StatusOK, |
| 131 | JSON: struct{}{}, |
| 132 | } |
| 133 | } |
| 134 |