bookmarksget.go (3456B)
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 bookmarks 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 const ( 32 // LimitKey is for setting the return amount limit for eg., requesting an account's statuses 33 LimitKey = "limit" 34 35 // MaxIDKey is for specifying the maximum ID of the bookmark to retrieve. 36 MaxIDKey = "max_id" 37 // MinIDKey is for specifying the minimum ID of the bookmark to retrieve. 38 MinIDKey = "min_id" 39 ) 40 41 // BookmarksGETHandler swagger:operation GET /api/v1/bookmarks bookmarksGet 42 // 43 // Get an array of statuses bookmarked in the instance 44 // 45 // --- 46 // tags: 47 // - bookmarks 48 // 49 // produces: 50 // - application/json 51 // 52 // security: 53 // - OAuth2 Bearer: 54 // - read:bookmarks 55 // 56 // responses: 57 // '200': 58 // description: Array of bookmarked statuses 59 // schema: 60 // type: array 61 // items: 62 // "$ref": "#/definitions/status" 63 // headers: 64 // Link: 65 // type: string 66 // description: Links to the next and previous queries. 67 // '401': 68 // description: unauthorized 69 // '406': 70 // description: not acceptable 71 // '500': 72 // description: internal server error 73 func (m *Module) BookmarksGETHandler(c *gin.Context) { 74 authed, err := oauth.Authed(c, true, true, true, true) 75 if err != nil { 76 apiutil.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1) 77 return 78 } 79 80 if _, err := apiutil.NegotiateAccept(c, apiutil.JSONAcceptHeaders...); err != nil { 81 apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1) 82 return 83 } 84 85 limit := 30 86 limitString := c.Query(LimitKey) 87 if limitString != "" { 88 i, err := strconv.ParseInt(limitString, 10, 64) 89 if err != nil { 90 err := fmt.Errorf("error parsing %s: %s", LimitKey, err) 91 apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) 92 return 93 } 94 limit = int(i) 95 } 96 97 maxID := "" 98 maxIDString := c.Query(MaxIDKey) 99 if maxIDString != "" { 100 maxID = maxIDString 101 } 102 103 minID := "" 104 minIDString := c.Query(MinIDKey) 105 if minIDString != "" { 106 minID = minIDString 107 } 108 109 resp, errWithCode := m.processor.Account().BookmarksGet(c.Request.Context(), authed.Account, limit, maxID, minID) 110 if errWithCode != nil { 111 apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) 112 return 113 } 114 115 if errWithCode != nil { 116 apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) 117 return 118 } 119 120 if resp.LinkHeader != "" { 121 c.Header("Link", resp.LinkHeader) 122 } 123 c.JSON(http.StatusOK, resp.Items) 124 }