gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

accountaction.go (3479B)


      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 	"errors"
     22 	"fmt"
     23 	"net/http"
     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/oauth"
     30 )
     31 
     32 // AccountActionPOSTHandler swagger:operation POST /api/v1/admin/accounts/{id}/action adminAccountAction
     33 //
     34 // Perform an admin action on an account.
     35 //
     36 //	---
     37 //	tags:
     38 //	- admin
     39 //
     40 //	consumes:
     41 //	- multipart/form-data
     42 //
     43 //	produces:
     44 //	- application/json
     45 //
     46 //	parameters:
     47 //	-
     48 //		name: id
     49 //		required: true
     50 //		in: path
     51 //		description: ID of the account.
     52 //		type: string
     53 //	-
     54 //		name: type
     55 //		in: formData
     56 //		description: Type of action to be taken, currently only supports `suspend`.
     57 //		type: string
     58 //		required: true
     59 //	-
     60 //		name: text
     61 //		in: formData
     62 //		description: Optional text describing why this action was taken.
     63 //		type: string
     64 //
     65 //	security:
     66 //	- OAuth2 Bearer:
     67 //		- admin
     68 //
     69 //	responses:
     70 //		'200':
     71 //			description: OK
     72 //		'400':
     73 //			description: bad request
     74 //		'401':
     75 //			description: unauthorized
     76 //		'403':
     77 //			description: forbidden
     78 //		'404':
     79 //			description: not found
     80 //		'406':
     81 //			description: not acceptable
     82 //		'500':
     83 //			description: internal server error
     84 func (m *Module) AccountActionPOSTHandler(c *gin.Context) {
     85 	authed, err := oauth.Authed(c, true, true, true, true)
     86 	if err != nil {
     87 		apiutil.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1)
     88 		return
     89 	}
     90 
     91 	if !*authed.User.Admin {
     92 		err := fmt.Errorf("user %s not an admin", authed.User.ID)
     93 		apiutil.ErrorHandler(c, gtserror.NewErrorForbidden(err, err.Error()), m.processor.InstanceGetV1)
     94 		return
     95 	}
     96 
     97 	form := &apimodel.AdminAccountActionRequest{}
     98 	if err := c.ShouldBind(form); err != nil {
     99 		apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
    100 		return
    101 	}
    102 
    103 	if form.Type == "" {
    104 		err := errors.New("no type specified")
    105 		apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
    106 		return
    107 	}
    108 
    109 	targetAcctID := c.Param(IDKey)
    110 	if targetAcctID == "" {
    111 		err := errors.New("no account id specified")
    112 		apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
    113 		return
    114 	}
    115 	form.TargetAccountID = targetAcctID
    116 
    117 	if errWithCode := m.processor.Admin().AccountAction(c.Request.Context(), authed.Account, form); errWithCode != nil {
    118 		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
    119 		return
    120 	}
    121 
    122 	c.JSON(http.StatusOK, gin.H{"message": "OK"})
    123 }