Beispiel #1
0
func (e roomsEndpoint) getMessages(req *http.Request, params httprouter.Params) interface{} {
	room, user, err := e.getRoomAndUser(req, params)
	if err != nil {
		return err
	}

	var from *types.StreamToken
	var to *types.StreamToken

	query := req.URL.Query()
	fromStr := query.Get("from")
	toStr := query.Get("to")
	dir := query.Get("dir")
	limitStr := query.Get("limit")

	if fromStr != "" {
		token, err := types.ParseStreamToken(fromStr)
		if err != nil {
			return types.BadQueryError(err.Error())
		}
		from = &token
	}

	if toStr != "" {
		token, err := types.ParseStreamToken(toStr)
		if err != nil {
			return types.BadQueryError(err.Error())
		}
		to = &token
	}

	if dir == "b" {
		token := types.NewStreamToken(0, 0, 0)
		to = &token
	}

	var limit uint64

	var parseErr error
	if limitStr == "" {
		limit = 10 //TODO: make configurable
	} else {
		limit, parseErr = strconv.ParseUint(limitStr, 10, 32)
		if parseErr != nil {
			return types.BadQueryError(parseErr.Error())
		}
		if limit > 100 {
			limit = 100 //TODO: make configurable
		}
	}
	eventRange, err := e.eventService.Messages(user, room, from, to, uint(limit))
	log.Println("TO", to, eventRange)
	if err != nil {
		return err
	}

	return eventRange
}
Beispiel #2
0
func (q urlQuery) parseStreamToken(name string) (*types.StreamToken, types.Error) {
	str := q.Get(name)
	if str == "" {
		return nil, nil
	}

	token, err := types.ParseStreamToken(str)
	if err != nil {
		return nil, types.BadQueryError(err.Error())
	}
	return &token, nil
}