func (e roomsEndpoint) handlePutState(rw http.ResponseWriter, req *http.Request, params httprouter.Params) { room, user, err := e.getRoomAndUser(req, params) if err != nil { WriteJsonResponseWithStatus(rw, err) return } eventType := params[1].Value stateKey := "" if len(params) > 2 { stateKey = params[2].Value } var content ct.TypedContent switch eventType { case types.EventTypeMembership: content = &types.MembershipEventContent{} case types.EventTypeName: content = &types.NameEventContent{} case types.EventTypeTopic: content = &types.TopicEventContent{} case types.EventTypePowerLevels: content = &types.PowerLevelsEventContent{} case types.EventTypeJoinRules: content = &types.JoinRulesEventContent{} } var jsonErr error if content != nil { jsonErr = json.NewDecoder(req.Body).Decode(content) } else { genericContent := types.NewGenericContent(map[string]interface{}{}, eventType) content = genericContent jsonErr = json.NewDecoder(req.Body).Decode(&genericContent.Content) } if jsonErr != nil { switch err := jsonErr.(type) { case *json.SyntaxError: msg := fmt.Sprintf("error at [%d]: %s", err.Offset, err.Error()) WriteJsonResponseWithStatus(rw, types.NotJsonError(msg)) case *json.UnmarshalTypeError: msg := fmt.Sprintf("error at [%d]: expected type %s but got %s", err.Offset, err.Type, err.Value) WriteJsonResponseWithStatus(rw, types.BadJsonError(msg)) default: WriteJsonResponseWithStatus(rw, types.BadJsonError(err.Error())) } return } state, err := e.roomService.SetState(room, user, content, stateKey) if err != nil { WriteJsonResponseWithStatus(rw, err) return } res := eventIdResponse{state.EventId} WriteJsonResponse(rw, 200, res) }
func (e roomsEndpoint) sendMessage(req *http.Request, params httprouter.Params, content *map[string]interface{}) interface{} { user, err := readAccessToken(e.userService, e.tokenService, req) if err != nil { return err } room, parseErr := ct.ParseRoomId(params[0].Value) if parseErr != nil { return types.BadParamError(parseErr.Error()) } eventType := params[1].Value typedContent := types.NewGenericContent(*content, eventType) message, err := e.roomService.AddMessage(room, user, typedContent) if err != nil { return err } return eventIdResponse{message.EventId} }