listupdate.go (4396B)
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 lists 19 20 import ( 21 "errors" 22 "net/http" 23 "strings" 24 25 "github.com/gin-gonic/gin" 26 apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" 27 apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util" 28 "github.com/superseriousbusiness/gotosocial/internal/gtserror" 29 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 30 "github.com/superseriousbusiness/gotosocial/internal/oauth" 31 "github.com/superseriousbusiness/gotosocial/internal/validate" 32 ) 33 34 // ListUpdatePUTHandler swagger:operation PUT /api/v1/lists/{id} listUpdate 35 // 36 // Update an existing list. 37 // 38 // --- 39 // tags: 40 // - lists 41 // 42 // consumes: 43 // - application/json 44 // - application/xml 45 // - application/x-www-form-urlencoded 46 // 47 // produces: 48 // - application/json 49 // 50 // parameters: 51 // - 52 // name: id 53 // type: string 54 // description: ID of the list 55 // in: path 56 // required: true 57 // - 58 // name: title 59 // type: string 60 // description: Title of this list. 61 // in: formData 62 // example: Cool People 63 // - 64 // name: replies_policy 65 // type: string 66 // description: |- 67 // RepliesPolicy for this list. 68 // followed = Show replies to any followed user 69 // list = Show replies to members of the list 70 // none = Show replies to no one 71 // in: formData 72 // example: list 73 // 74 // security: 75 // - OAuth2 Bearer: 76 // - write:lists 77 // 78 // responses: 79 // '200': 80 // description: "The newly updated list." 81 // schema: 82 // "$ref": "#/definitions/list" 83 // '400': 84 // description: bad request 85 // '401': 86 // description: unauthorized 87 // '403': 88 // description: forbidden 89 // '404': 90 // description: not found 91 // '406': 92 // description: not acceptable 93 // '500': 94 // description: internal server error 95 func (m *Module) ListUpdatePUTHandler(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 targetListID := c.Param(IDKey) 108 if targetListID == "" { 109 err := errors.New("no list id specified") 110 apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) 111 return 112 } 113 114 form := &apimodel.ListUpdateRequest{} 115 if err := c.ShouldBind(form); err != nil { 116 apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) 117 return 118 } 119 120 if form.Title != nil { 121 if err := validate.ListTitle(*form.Title); err != nil { 122 apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) 123 return 124 } 125 } 126 127 var repliesPolicy *gtsmodel.RepliesPolicy 128 if form.RepliesPolicy != nil { 129 rp := gtsmodel.RepliesPolicy(strings.ToLower(*form.RepliesPolicy)) 130 131 if err := validate.ListRepliesPolicy(rp); err != nil { 132 apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) 133 return 134 } 135 136 repliesPolicy = &rp 137 } 138 139 if form.Title == nil && repliesPolicy == nil { 140 err = errors.New("neither title nor replies_policy was set; nothing to update") 141 apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) 142 return 143 } 144 145 apiList, errWithCode := m.processor.List().Update(c.Request.Context(), authed.Account, targetListID, form.Title, repliesPolicy) 146 if errWithCode != nil { 147 apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) 148 return 149 } 150 151 c.JSON(http.StatusOK, apiList) 152 }