follow.go (3805B)
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 accounts 19 20 import ( 21 "errors" 22 "net/http" 23 24 "github.com/gin-gonic/gin" 25 apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" 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 // AccountFollowPOSTHandler swagger:operation POST /api/v1/accounts/{id}/follow accountFollow 32 // 33 // Follow account with id. 34 // 35 // The parameters can also be given in the body of the request, as JSON, if the content-type is set to 'application/json'. 36 // The parameters can also be given in the body of the request, as XML, if the content-type is set to 'application/xml'. 37 // 38 // If you already follow (request) the given account, then the follow (request) will be updated instead using the 39 // `reblogs` and `notify` parameters. 40 // 41 // --- 42 // tags: 43 // - accounts 44 // 45 // consumes: 46 // - application/json 47 // - application/xml 48 // - application/x-www-form-urlencoded 49 // 50 // parameters: 51 // - 52 // name: id 53 // required: true 54 // in: path 55 // description: ID of the account to follow. 56 // type: string 57 // - 58 // name: reblogs 59 // type: boolean 60 // default: true 61 // description: Show reblogs from this account. 62 // in: formData 63 // - 64 // name: notify 65 // type: boolean 66 // default: false 67 // description: Notify when this account posts. 68 // in: formData 69 // 70 // produces: 71 // - application/json 72 // 73 // security: 74 // - OAuth2 Bearer: 75 // - write:follows 76 // 77 // responses: 78 // '200': 79 // name: account relationship 80 // description: Your relationship to this account. 81 // schema: 82 // "$ref": "#/definitions/accountRelationship" 83 // '400': 84 // description: bad request 85 // '401': 86 // description: unauthorized 87 // '404': 88 // description: not found 89 // '406': 90 // description: not acceptable 91 // '500': 92 // description: internal server error 93 func (m *Module) AccountFollowPOSTHandler(c *gin.Context) { 94 authed, err := oauth.Authed(c, true, true, true, true) 95 if err != nil { 96 apiutil.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1) 97 return 98 } 99 100 if _, err := apiutil.NegotiateAccept(c, apiutil.JSONAcceptHeaders...); err != nil { 101 apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1) 102 return 103 } 104 105 targetAcctID := c.Param(IDKey) 106 if targetAcctID == "" { 107 err := errors.New("no account id specified") 108 apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) 109 return 110 } 111 112 form := &apimodel.AccountFollowRequest{} 113 if err := c.ShouldBind(form); err != nil { 114 apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) 115 return 116 } 117 form.ID = targetAcctID 118 119 relationship, errWithCode := m.processor.Account().FollowCreate(c.Request.Context(), authed.Account, form) 120 if errWithCode != nil { 121 apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) 122 return 123 } 124 125 c.JSON(http.StatusOK, relationship) 126 }