make ParseTSParam() return error instead of JSONResponse

This commit is contained in:
Krombel 2018-08-22 14:29:22 +02:00
parent 335ecb1d26
commit 88c6c32b20
6 changed files with 39 additions and 29 deletions

View file

@ -17,15 +17,12 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"time" "time"
"github.com/matrix-org/dendrite/clientapi/jsonerror"
"github.com/matrix-org/util"
) )
// ParseTSParam takes a req (typically from an application service) and parses a Time object // 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 // from the req if it exists in the query parameters. If it doesn't exist, the
// current time is returned. // current time is returned.
func ParseTSParam(req *http.Request) (time.Time, *util.JSONResponse) { func ParseTSParam(req *http.Request) (time.Time, error) {
// Use the ts parameter's value for event time if present // Use the ts parameter's value for event time if present
tsStr := req.URL.Query().Get("ts") tsStr := req.URL.Query().Get("ts")
if tsStr == "" { if tsStr == "" {
@ -35,12 +32,7 @@ func ParseTSParam(req *http.Request) (time.Time, *util.JSONResponse) {
// The parameter exists, parse into a Time object // The parameter exists, parse into a Time object
ts, err := strconv.ParseInt(tsStr, 10, 64) ts, err := strconv.ParseInt(tsStr, 10, 64)
if err != nil { if err != nil {
return time.Time{}, &util.JSONResponse{ return time.Time{}, fmt.Errorf("Param 'ts' is no valid int (%s)", err.Error())
Code: http.StatusBadRequest,
JSON: jsonerror.InvalidArgumentValue(
fmt.Sprintf("Param 'ts' is no valid int (%s)", err.Error()),
),
}
} }
return time.Unix(ts/1000, 0), nil return time.Unix(ts/1000, 0), nil

View file

@ -147,9 +147,12 @@ func createRoom(
return *resErr return *resErr
} }
evTime, resErr := httputil.ParseTSParam(req) evTime, err := httputil.ParseTSParam(req)
if resErr != nil { if err != nil {
return *resErr return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.InvalidArgumentValue(err.Error()),
}
} }
// TODO: visibility/presets/raw initial state/creation content // TODO: visibility/presets/raw initial state/creation content
// TODO: Create room alias association // TODO: Create room alias association

View file

@ -52,9 +52,12 @@ func JoinRoomByIDOrAlias(
return *resErr return *resErr
} }
evTime, resErr := httputil.ParseTSParam(req) evTime, err := httputil.ParseTSParam(req)
if resErr != nil { if err != nil {
return *resErr return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.InvalidArgumentValue(err.Error()),
}
} }
localpart, _, err := gomatrixserverlib.SplitID('@', device.UserID) localpart, _, err := gomatrixserverlib.SplitID('@', device.UserID)

View file

@ -50,9 +50,12 @@ func SendMembership(
return *reqErr return *reqErr
} }
evTime, resErr := httputil.ParseTSParam(req) evTime, err := httputil.ParseTSParam(req)
if resErr != nil { if err != nil {
return *resErr return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.InvalidArgumentValue(err.Error()),
}
} }
inviteStored, err := threepid.CheckAndProcessInvite( inviteStored, err := threepid.CheckAndProcessInvite(

View file

@ -107,9 +107,12 @@ func SetAvatarURL(
return httputil.LogThenError(req, err) return httputil.LogThenError(req, err)
} }
evTime, resErr := httputil.ParseTSParam(req) evTime, err := httputil.ParseTSParam(req)
if resErr != nil { if err != nil {
return *resErr return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.InvalidArgumentValue(err.Error()),
}
} }
oldProfile, err := accountDB.GetProfileByLocalpart(req.Context(), localpart) oldProfile, err := accountDB.GetProfileByLocalpart(req.Context(), localpart)
@ -201,9 +204,12 @@ func SetDisplayName(
return httputil.LogThenError(req, err) return httputil.LogThenError(req, err)
} }
evTime, resErr := httputil.ParseTSParam(req) evTime, err := httputil.ParseTSParam(req)
if resErr != nil { if err != nil {
return *resErr return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.InvalidArgumentValue(err.Error()),
}
} }
oldProfile, err := accountDB.GetProfileByLocalpart(req.Context(), localpart) oldProfile, err := accountDB.GetProfileByLocalpart(req.Context(), localpart)

View file

@ -104,9 +104,12 @@ func generateSendEvent(
return nil, resErr return nil, resErr
} }
evTime, resErr := httputil.ParseTSParam(req) evTime, err := httputil.ParseTSParam(req)
if resErr != nil { if err != nil {
return nil, resErr return nil, &util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.InvalidArgumentValue(err.Error()),
}
} }
// create the new event and set all the fields we can // create the new event and set all the fields we can
@ -116,7 +119,7 @@ func generateSendEvent(
Type: eventType, Type: eventType,
StateKey: stateKey, StateKey: stateKey,
} }
err := builder.SetContent(r) err = builder.SetContent(r)
if err != nil { if err != nil {
resErr := httputil.LogThenError(req, err) resErr := httputil.LogThenError(req, err)
return nil, &resErr return nil, &resErr