diff options
Diffstat (limited to 'internal/api/client/reports')
-rw-r--r-- | internal/api/client/reports/reportget.go | 8 | ||||
-rw-r--r-- | internal/api/client/reports/reportget_test.go | 2 | ||||
-rw-r--r-- | internal/api/client/reports/reports.go | 12 | ||||
-rw-r--r-- | internal/api/client/reports/reportsget.go | 61 | ||||
-rw-r--r-- | internal/api/client/reports/reportsget_test.go | 13 |
5 files changed, 39 insertions, 57 deletions
diff --git a/internal/api/client/reports/reportget.go b/internal/api/client/reports/reportget.go index 4a9b06664..c9ca0054f 100644 --- a/internal/api/client/reports/reportget.go +++ b/internal/api/client/reports/reportget.go @@ -18,7 +18,6 @@ package reports import ( - "errors" "net/http" "github.com/gin-gonic/gin" @@ -77,10 +76,9 @@ func (m *Module) ReportGETHandler(c *gin.Context) { return } - targetReportID := c.Param(IDKey) - if targetReportID == "" { - err := errors.New("no report id specified") - apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) + targetReportID, errWithCode := apiutil.ParseID(c.Param(apiutil.IDKey)) + if errWithCode != nil { + apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) return } diff --git a/internal/api/client/reports/reportget_test.go b/internal/api/client/reports/reportget_test.go index 5b6c406f7..4f01101b2 100644 --- a/internal/api/client/reports/reportget_test.go +++ b/internal/api/client/reports/reportget_test.go @@ -145,7 +145,7 @@ func (suite *ReportGetTestSuite) TestGetReport2() { } func (suite *ReportGetTestSuite) TestGetReport3() { - report, err := suite.getReport(http.StatusBadRequest, `{"error":"Bad Request: no report id specified"}`, "") + report, err := suite.getReport(http.StatusBadRequest, `{"error":"Bad Request: required key id was not set or had empty value"}`, "") suite.NoError(err) suite.Nil(report) } diff --git a/internal/api/client/reports/reports.go b/internal/api/client/reports/reports.go index e881474fb..b10697c1f 100644 --- a/internal/api/client/reports/reports.go +++ b/internal/api/client/reports/reports.go @@ -21,19 +21,13 @@ import ( "net/http" "github.com/gin-gonic/gin" + apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util" "github.com/superseriousbusiness/gotosocial/internal/processing" ) const ( - BasePath = "/v1/reports" - IDKey = "id" - ResolvedKey = "resolved" - TargetAccountIDKey = "target_account_id" - MaxIDKey = "max_id" - SinceIDKey = "since_id" - MinIDKey = "min_id" - LimitKey = "limit" - BasePathWithID = BasePath + "/:" + IDKey + BasePath = "/v1/reports" + BasePathWithID = BasePath + "/:" + apiutil.IDKey ) type Module struct { diff --git a/internal/api/client/reports/reportsget.go b/internal/api/client/reports/reportsget.go index 5f194a589..4c3d4e33a 100644 --- a/internal/api/client/reports/reportsget.go +++ b/internal/api/client/reports/reportsget.go @@ -18,14 +18,13 @@ package reports import ( - "fmt" "net/http" - "strconv" "github.com/gin-gonic/gin" apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util" "github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/oauth" + "github.com/superseriousbusiness/gotosocial/internal/paging" ) // ReportsGETHandler swagger:operation GET /api/v1/reports reports @@ -67,7 +66,7 @@ import ( // name: max_id // type: string // description: >- -// Return only reports *OLDER* than the given max ID. +// Return only reports *OLDER* than the given max ID (for paging downwards). // The report with the specified ID will not be included in the response. // in: query // - @@ -76,24 +75,21 @@ import ( // description: >- // Return only reports *NEWER* than the given since ID. // The report with the specified ID will not be included in the response. -// This parameter is functionally equivalent to min_id. // in: query // - // name: min_id // type: string // description: >- -// Return only reports *NEWER* than the given min ID. +// Return only reports immediately *NEWER* than the given min ID (for paging upwards). // The report with the specified ID will not be included in the response. -// This parameter is functionally equivalent to since_id. // in: query // - // name: limit // type: integer -// description: >- -// Number of reports to return. -// If less than 1, will be clamped to 1. -// If more than 100, will be clamped to 100. +// description: Number of reports to return. // default: 20 +// minimum: 1 +// maximum: 100 // in: query // // security: @@ -134,36 +130,29 @@ func (m *Module) ReportsGETHandler(c *gin.Context) { return } - var resolved *bool - if resolvedString := c.Query(ResolvedKey); resolvedString != "" { - i, err := strconv.ParseBool(resolvedString) - if err != nil { - err := fmt.Errorf("error parsing %s: %s", ResolvedKey, err) - apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) - return - } - resolved = &i + resolved, errWithCode := apiutil.ParseResolved(c.Query(apiutil.ResolvedKey), nil) + if errWithCode != nil { + apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) + return } - limit := 20 - if limitString := c.Query(LimitKey); limitString != "" { - i, err := strconv.Atoi(limitString) - if err != nil { - err := fmt.Errorf("error parsing %s: %s", LimitKey, err) - apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) - return - } - - // normalize - if i <= 0 { - i = 1 - } else if i >= 100 { - i = 100 - } - limit = i + page, errWithCode := paging.ParseIDPage(c, + 1, // min limit + 100, // max limit + 20, // default limit + ) + if errWithCode != nil { + apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) + return } - resp, errWithCode := m.processor.Report().GetMultiple(c.Request.Context(), authed.Account, resolved, c.Query(TargetAccountIDKey), c.Query(MaxIDKey), c.Query(SinceIDKey), c.Query(MinIDKey), limit) + resp, errWithCode := m.processor.Report().GetMultiple( + c.Request.Context(), + authed.Account, + resolved, + c.Query(apiutil.TargetAccountIDKey), + page, + ) if errWithCode != nil { apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) return diff --git a/internal/api/client/reports/reportsget_test.go b/internal/api/client/reports/reportsget_test.go index c63d6c894..2413292a0 100644 --- a/internal/api/client/reports/reportsget_test.go +++ b/internal/api/client/reports/reportsget_test.go @@ -29,6 +29,7 @@ import ( "github.com/stretchr/testify/suite" "github.com/superseriousbusiness/gotosocial/internal/api/client/reports" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" + apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util" "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/oauth" @@ -61,21 +62,21 @@ func (suite *ReportsGetTestSuite) getReports( ctx.Set(oauth.SessionAuthorizedUser, user) // create the request URI - requestPath := reports.BasePath + "?" + reports.LimitKey + "=" + strconv.Itoa(limit) + requestPath := reports.BasePath + "?" + apiutil.LimitKey + "=" + strconv.Itoa(limit) if resolved != nil { - requestPath = requestPath + "&" + reports.ResolvedKey + "=" + strconv.FormatBool(*resolved) + requestPath = requestPath + "&" + apiutil.ResolvedKey + "=" + strconv.FormatBool(*resolved) } if targetAccountID != "" { - requestPath = requestPath + "&" + reports.TargetAccountIDKey + "=" + targetAccountID + requestPath = requestPath + "&" + apiutil.TargetAccountIDKey + "=" + targetAccountID } if maxID != "" { - requestPath = requestPath + "&" + reports.MaxIDKey + "=" + maxID + requestPath = requestPath + "&" + apiutil.MaxIDKey + "=" + maxID } if sinceID != "" { - requestPath = requestPath + "&" + reports.SinceIDKey + "=" + sinceID + requestPath = requestPath + "&" + apiutil.SinceIDKey + "=" + sinceID } if minID != "" { - requestPath = requestPath + "&" + reports.MinIDKey + "=" + minID + requestPath = requestPath + "&" + apiutil.MinIDKey + "=" + minID } baseURI := config.GetProtocol() + "://" + config.GetHost() requestURI := baseURI + "/api/" + requestPath |