diff --git a/src/github.com/matrix-org/dendrite/clientapi/httputil/parse.go b/src/github.com/matrix-org/dendrite/clientapi/httputil/parse.go index 0c8c8e6c6..110d28e70 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/httputil/parse.go +++ b/src/github.com/matrix-org/dendrite/clientapi/httputil/parse.go @@ -22,39 +22,25 @@ import ( "github.com/matrix-org/util" ) -// ParseIntParam takes a request and parses the query param to an int -// returns 0 when not present and -1 when parsing failed as int -func ParseIntParam(req *http.Request, param string) (int64, *util.JSONResponse) { - paramStr := req.URL.Query().Get(param) - if paramStr == "" { - return 0, &util.JSONResponse{ - Code: http.StatusBadRequest, - JSON: jsonerror.MissingArgument(fmt.Sprintf("Param %s not found", param)), - } - } - paramInt, err := strconv.ParseInt(paramStr, 0, 64) - if err != nil { - return -1, &util.JSONResponse{ - Code: http.StatusBadRequest, - JSON: jsonerror.InvalidArgumentValue( - fmt.Sprintf("Param %s is no valid int (%s)", param, err.Error()), - ), - } - } - return paramInt, nil -} - // ParseTSParam takes a req (typically from an application service) and parses a Time object // from the req if it exists in the query parameters. If it doesn't exist, the // current time is returned. func ParseTSParam(req *http.Request) (time.Time, *util.JSONResponse) { - ts, err := ParseIntParam(req, "ts") + // Use the ts parameter's value for event time if present + tsStr := req.URL.Query().Get("ts") + if tsStr == "" { + return time.Now(), nil + } + + // The parameter exists, parse into a Time object + ts, err := strconv.ParseInt(tsStr, 10, 64) if err != nil { - if ts == 0 { - // param was not available - return time.Now(), nil + return time.Time{}, &util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.InvalidArgumentValue( + fmt.Sprintf("Param 'ts' is no valid int (%s)", err.Error()), + ), } - return time.Time{}, err } return time.Unix(ts/1000, 0), nil