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

Function SaveAccountData

clientapi/routing/account_data.go:77–133  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

75
76// SaveAccountData implements PUT /user/{userId}/[rooms/{roomId}/]account_data/{type}
77func 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

Callers 1

SetupFunction · 0.85

Calls 5

ContextMethod · 0.80
ValidMethod · 0.80
CloseMethod · 0.65
InputAccountDataMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected