reportsget.go (5379B)
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 admin 19 20 import ( 21 "fmt" 22 "net/http" 23 "strconv" 24 25 "github.com/gin-gonic/gin" 26 apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util" 27 "github.com/superseriousbusiness/gotosocial/internal/gtserror" 28 "github.com/superseriousbusiness/gotosocial/internal/oauth" 29 ) 30 31 // ReportsGETHandler swagger:operation GET /api/v1/admin/reports adminReports 32 // 33 // View user moderation reports. 34 // 35 // The reports will be returned in descending chronological order (newest first), with sequential IDs (bigger = newer). 36 // 37 // The next and previous queries can be parsed from the returned Link header. 38 // 39 // Example: 40 // 41 // ``` 42 // <https://example.org/api/v1/admin/reports?limit=20&max_id=01FC0SKA48HNSVR6YKZCQGS2V8>; rel="next", <https://example.org/api/v1/admin/reports?limit=20&min_id=01FC0SKW5JK2Q4EVAV2B462YY0>; rel="prev" 43 // ```` 44 // 45 // --- 46 // tags: 47 // - admin 48 // 49 // produces: 50 // - application/json 51 // 52 // parameters: 53 // - 54 // name: resolved 55 // type: boolean 56 // description: >- 57 // If set to true, only resolved reports will be returned. 58 // If false, only unresolved reports will be returned. 59 // If unset, reports will not be filtered on their resolved status. 60 // in: query 61 // - 62 // name: account_id 63 // type: string 64 // description: Return only reports created by the given account id. 65 // in: query 66 // - 67 // name: target_account_id 68 // type: string 69 // description: Return only reports that target the given account id. 70 // in: query 71 // - 72 // name: max_id 73 // type: string 74 // description: >- 75 // Return only reports *OLDER* than the given max ID. 76 // The report with the specified ID will not be included in the response. 77 // in: query 78 // - 79 // name: since_id 80 // type: string 81 // description: >- 82 // Return only reports *NEWER* than the given since ID. 83 // The report with the specified ID will not be included in the response. 84 // This parameter is functionally equivalent to min_id. 85 // in: query 86 // - 87 // name: min_id 88 // type: string 89 // description: >- 90 // Return only reports *NEWER* than the given min ID. 91 // The report with the specified ID will not be included in the response. 92 // This parameter is functionally equivalent to since_id. 93 // in: query 94 // - 95 // name: limit 96 // type: integer 97 // description: >- 98 // Number of reports to return. 99 // If more than 100 or less than 1, will be clamped to 100. 100 // default: 20 101 // in: query 102 // 103 // security: 104 // - OAuth2 Bearer: 105 // - admin 106 // 107 // responses: 108 // '200': 109 // name: reports 110 // description: Array of reports. 111 // schema: 112 // type: array 113 // items: 114 // "$ref": "#/definitions/adminReport" 115 // '400': 116 // description: bad request 117 // '401': 118 // description: unauthorized 119 // '404': 120 // description: not found 121 // '406': 122 // description: not acceptable 123 // '500': 124 // description: internal server error 125 func (m *Module) ReportsGETHandler(c *gin.Context) { 126 authed, err := oauth.Authed(c, true, true, true, true) 127 if err != nil { 128 apiutil.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1) 129 return 130 } 131 132 if !*authed.User.Admin { 133 err := fmt.Errorf("user %s not an admin", authed.User.ID) 134 apiutil.ErrorHandler(c, gtserror.NewErrorForbidden(err, err.Error()), m.processor.InstanceGetV1) 135 return 136 } 137 138 if _, err := apiutil.NegotiateAccept(c, apiutil.JSONAcceptHeaders...); err != nil { 139 apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1) 140 return 141 } 142 143 var resolved *bool 144 if resolvedString := c.Query(ResolvedKey); resolvedString != "" { 145 i, err := strconv.ParseBool(resolvedString) 146 if err != nil { 147 err := fmt.Errorf("error parsing %s: %s", ResolvedKey, err) 148 apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) 149 return 150 } 151 resolved = &i 152 } 153 154 limit := 20 155 if limitString := c.Query(LimitKey); limitString != "" { 156 i, err := strconv.Atoi(limitString) 157 if err != nil { 158 err := fmt.Errorf("error parsing %s: %s", LimitKey, err) 159 apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) 160 return 161 } 162 163 // normalize 164 if i < 1 || i > 100 { 165 i = 100 166 } 167 limit = i 168 } 169 170 resp, errWithCode := m.processor.Admin().ReportsGet(c.Request.Context(), authed.Account, resolved, c.Query(AccountIDKey), c.Query(TargetAccountIDKey), c.Query(MaxIDKey), c.Query(SinceIDKey), c.Query(MinIDKey), limit) 171 if errWithCode != nil { 172 apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) 173 return 174 } 175 176 if resp.LinkHeader != "" { 177 c.Header("Link", resp.LinkHeader) 178 } 179 c.JSON(http.StatusOK, resp.Items) 180 }