blocksget.go (3896B)
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 blocks 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 // BlocksGETHandler swagger:operation GET /api/v1/blocks blocksGet 32 // 33 // Get an array of accounts that requesting account has blocked. 34 // 35 // The next and previous queries can be parsed from the returned Link header. 36 // Example: 37 // 38 // ``` 39 // <https://example.org/api/v1/blocks?limit=80&max_id=01FC0SKA48HNSVR6YKZCQGS2V8>; rel="next", <https://example.org/api/v1/blocks?limit=80&min_id=01FC0SKW5JK2Q4EVAV2B462YY0>; rel="prev" 40 // ```` 41 // 42 // --- 43 // tags: 44 // - blocks 45 // 46 // produces: 47 // - application/json 48 // 49 // parameters: 50 // - 51 // name: limit 52 // type: integer 53 // description: Number of blocks to return. 54 // default: 20 55 // in: query 56 // - 57 // name: max_id 58 // type: string 59 // description: >- 60 // Return only blocks *OLDER* than the given block ID. 61 // The block with the specified ID will not be included in the response. 62 // in: query 63 // - 64 // name: since_id 65 // type: string 66 // description: >- 67 // Return only blocks *NEWER* than the given block ID. 68 // The block with the specified ID will not be included in the response. 69 // in: query 70 // 71 // security: 72 // - OAuth2 Bearer: 73 // - read:blocks 74 // 75 // responses: 76 // '200': 77 // headers: 78 // Link: 79 // type: string 80 // description: Links to the next and previous queries. 81 // schema: 82 // type: array 83 // items: 84 // "$ref": "#/definitions/account" 85 // '400': 86 // description: bad request 87 // '401': 88 // description: unauthorized 89 // '404': 90 // description: not found 91 // '406': 92 // description: not acceptable 93 // '500': 94 // description: internal server error 95 func (m *Module) BlocksGETHandler(c *gin.Context) { 96 authed, err := oauth.Authed(c, true, true, true, true) 97 if err != nil { 98 apiutil.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1) 99 return 100 } 101 102 if _, err := apiutil.NegotiateAccept(c, apiutil.JSONAcceptHeaders...); err != nil { 103 apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1) 104 return 105 } 106 107 maxID := "" 108 maxIDString := c.Query(MaxIDKey) 109 if maxIDString != "" { 110 maxID = maxIDString 111 } 112 113 sinceID := "" 114 sinceIDString := c.Query(SinceIDKey) 115 if sinceIDString != "" { 116 sinceID = sinceIDString 117 } 118 119 limit := 20 120 limitString := c.Query(LimitKey) 121 if limitString != "" { 122 i, err := strconv.ParseInt(limitString, 10, 32) 123 if err != nil { 124 err := fmt.Errorf("error parsing %s: %s", LimitKey, err) 125 apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) 126 return 127 } 128 limit = int(i) 129 } 130 131 resp, errWithCode := m.processor.BlocksGet(c.Request.Context(), authed, maxID, sinceID, limit) 132 if errWithCode != nil { 133 apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) 134 return 135 } 136 137 if resp.LinkHeader != "" { 138 c.Header("Link", resp.LinkHeader) 139 } 140 c.JSON(http.StatusOK, resp.Accounts) 141 }