[feature] Implement /api/v1/reports endpoints on client API (#1330)

* start adding report client api

* route + test reports get

* start report create endpoint

* you can create reports now babyy

* stub account report processor

* add single reportGet endpoint

* fix test

* add more filtering params to /api/v1/reports GET

* update swagger

* use marshalIndent in tests

* add + test missing Link info
This commit is contained in:
tobi 2023-01-23 13:14:21 +01:00 committed by GitHub
commit e9747247d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 2184 additions and 20 deletions

View file

@ -25,6 +25,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/state"
"github.com/uptrace/bun"
)
@ -49,6 +50,73 @@ func (r *reportDB) GetReportByID(ctx context.Context, id string) (*gtsmodel.Repo
)
}
func (r *reportDB) GetReports(ctx context.Context, resolved *bool, accountID string, targetAccountID string, maxID string, sinceID string, minID string, limit int) ([]*gtsmodel.Report, db.Error) {
reportIDs := []string{}
q := r.conn.
NewSelect().
TableExpr("? AS ?", bun.Ident("reports"), bun.Ident("report")).
Column("report.id").
Order("report.id DESC")
if resolved != nil {
i := bun.Ident("report.action_taken_by_account_id")
if *resolved {
q = q.Where("? IS NOT NULL", i)
} else {
q = q.Where("? IS NULL", i)
}
}
if accountID != "" {
q = q.Where("? = ?", bun.Ident("report.account_id"), accountID)
}
if targetAccountID != "" {
q = q.Where("? = ?", bun.Ident("report.target_account_id"), targetAccountID)
}
if maxID != "" {
q = q.Where("? < ?", bun.Ident("report.id"), maxID)
}
if sinceID != "" {
q = q.Where("? > ?", bun.Ident("report.id"), minID)
}
if minID != "" {
q = q.Where("? > ?", bun.Ident("report.id"), minID)
}
if limit != 0 {
q = q.Limit(limit)
}
if err := q.Scan(ctx, &reportIDs); err != nil {
return nil, r.conn.ProcessError(err)
}
// Catch case of no reports early
if len(reportIDs) == 0 {
return nil, db.ErrNoEntries
}
// Allocate return slice (will be at most len reportIDs)
reports := make([]*gtsmodel.Report, 0, len(reportIDs))
for _, id := range reportIDs {
report, err := r.GetReportByID(ctx, id)
if err != nil {
log.Errorf("GetReports: error getting report %q: %v", id, err)
continue
}
// Append to return slice
reports = append(reports, report)
}
return reports, nil
}
func (r *reportDB) getReport(ctx context.Context, lookup string, dbQuery func(*gtsmodel.Report) error, keyParts ...any) (*gtsmodel.Report, db.Error) {
// Fetch report from database cache with loader callback
report, err := r.state.Caches.GTS.Report().Load(lookup, func() (*gtsmodel.Report, error) {