get.go (3535B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package report 19 20 import ( 21 "context" 22 "fmt" 23 "strconv" 24 25 apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" 26 "github.com/superseriousbusiness/gotosocial/internal/db" 27 "github.com/superseriousbusiness/gotosocial/internal/gtserror" 28 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 29 "github.com/superseriousbusiness/gotosocial/internal/util" 30 ) 31 32 // Get returns the user view of a moderation report, with the given id. 33 func (p *Processor) Get(ctx context.Context, account *gtsmodel.Account, id string) (*apimodel.Report, gtserror.WithCode) { 34 report, err := p.state.DB.GetReportByID(ctx, id) 35 if err != nil { 36 if err == db.ErrNoEntries { 37 return nil, gtserror.NewErrorNotFound(err) 38 } 39 return nil, gtserror.NewErrorInternalError(err) 40 } 41 42 if report.AccountID != account.ID { 43 err = fmt.Errorf("report with id %s does not belong to account %s", report.ID, account.ID) 44 return nil, gtserror.NewErrorNotFound(err) 45 } 46 47 apiReport, err := p.tc.ReportToAPIReport(ctx, report) 48 if err != nil { 49 return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting report to api: %s", err)) 50 } 51 52 return apiReport, nil 53 } 54 55 // GetMultiple returns multiple reports created by the given account, filtered according to the provided parameters. 56 func (p *Processor) GetMultiple( 57 ctx context.Context, 58 account *gtsmodel.Account, 59 resolved *bool, 60 targetAccountID string, 61 maxID string, 62 sinceID string, 63 minID string, 64 limit int, 65 ) (*apimodel.PageableResponse, gtserror.WithCode) { 66 reports, err := p.state.DB.GetReports(ctx, resolved, account.ID, targetAccountID, maxID, sinceID, minID, limit) 67 if err != nil { 68 if err == db.ErrNoEntries { 69 return util.EmptyPageableResponse(), nil 70 } 71 return nil, gtserror.NewErrorInternalError(err) 72 } 73 74 count := len(reports) 75 items := make([]interface{}, 0, count) 76 nextMaxIDValue := "" 77 prevMinIDValue := "" 78 for i, r := range reports { 79 item, err := p.tc.ReportToAPIReport(ctx, r) 80 if err != nil { 81 return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting report to api: %s", err)) 82 } 83 84 if i == count-1 { 85 nextMaxIDValue = item.ID 86 } 87 88 if i == 0 { 89 prevMinIDValue = item.ID 90 } 91 92 items = append(items, item) 93 } 94 95 extraQueryParams := []string{} 96 if resolved != nil { 97 extraQueryParams = append(extraQueryParams, "resolved="+strconv.FormatBool(*resolved)) 98 } 99 if targetAccountID != "" { 100 extraQueryParams = append(extraQueryParams, "target_account_id="+targetAccountID) 101 } 102 103 return util.PackagePageableResponse(util.PageableResponseParams{ 104 Items: items, 105 Path: "/api/v1/reports", 106 NextMaxIDValue: nextMaxIDValue, 107 PrevMinIDValue: prevMinIDValue, 108 Limit: limit, 109 ExtraQueryParams: extraQueryParams, 110 }) 111 }