(
ctx context.Context,
r map[string]interface{},
device *userapi.Device,
roomID, eventType string, stateKey *string,
cfg *config.ClientAPI,
rsAPI api.ClientRoomserverAPI,
evTime time.Time,
)
| 269 | } |
| 270 | |
| 271 | func generateSendEvent( |
| 272 | ctx context.Context, |
| 273 | r map[string]interface{}, |
| 274 | device *userapi.Device, |
| 275 | roomID, eventType string, stateKey *string, |
| 276 | cfg *config.ClientAPI, |
| 277 | rsAPI api.ClientRoomserverAPI, |
| 278 | evTime time.Time, |
| 279 | ) (*gomatrixserverlib.Event, *util.JSONResponse) { |
| 280 | // parse the incoming http request |
| 281 | userID := device.UserID |
| 282 | |
| 283 | // create the new event and set all the fields we can |
| 284 | builder := gomatrixserverlib.EventBuilder{ |
| 285 | Sender: userID, |
| 286 | RoomID: roomID, |
| 287 | Type: eventType, |
| 288 | StateKey: stateKey, |
| 289 | } |
| 290 | err := builder.SetContent(r) |
| 291 | if err != nil { |
| 292 | util.GetLogger(ctx).WithError(err).Error("builder.SetContent failed") |
| 293 | resErr := jsonerror.InternalServerError() |
| 294 | return nil, &resErr |
| 295 | } |
| 296 | |
| 297 | var queryRes api.QueryLatestEventsAndStateResponse |
| 298 | e, err := eventutil.QueryAndBuildEvent(ctx, &builder, cfg.Matrix, evTime, rsAPI, &queryRes) |
| 299 | if err == eventutil.ErrRoomNoExists { |
| 300 | return nil, &util.JSONResponse{ |
| 301 | Code: http.StatusNotFound, |
| 302 | JSON: jsonerror.NotFound("Room does not exist"), |
| 303 | } |
| 304 | } else if e, ok := err.(gomatrixserverlib.BadJSONError); ok { |
| 305 | return nil, &util.JSONResponse{ |
| 306 | Code: http.StatusBadRequest, |
| 307 | JSON: jsonerror.BadJSON(e.Error()), |
| 308 | } |
| 309 | } else if e, ok := err.(gomatrixserverlib.EventValidationError); ok { |
| 310 | if e.Code == gomatrixserverlib.EventValidationTooLarge { |
| 311 | return nil, &util.JSONResponse{ |
| 312 | Code: http.StatusRequestEntityTooLarge, |
| 313 | JSON: jsonerror.BadJSON(e.Error()), |
| 314 | } |
| 315 | } |
| 316 | return nil, &util.JSONResponse{ |
| 317 | Code: http.StatusBadRequest, |
| 318 | JSON: jsonerror.BadJSON(e.Error()), |
| 319 | } |
| 320 | } else if err != nil { |
| 321 | util.GetLogger(ctx).WithError(err).Error("eventutil.BuildEvent failed") |
| 322 | resErr := jsonerror.InternalServerError() |
| 323 | return nil, &resErr |
| 324 | } |
| 325 | |
| 326 | // check to see if this user can perform this operation |
| 327 | stateEvents := make([]*gomatrixserverlib.Event, len(queryRes.StateEvents)) |
| 328 | for i := range queryRes.StateEvents { |
no test coverage detected