gtsocial-umbx

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

domainblockget.go (3327B)


      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 	"strconv"
     25 
     26 	"github.com/gin-gonic/gin"
     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 // DomainBlockGETHandler swagger:operation GET /api/v1/admin/domain_blocks/{id} domainBlockGet
     33 //
     34 // View domain block with the given ID.
     35 //
     36 //	---
     37 //	tags:
     38 //	- admin
     39 //
     40 //	produces:
     41 //	- application/json
     42 //
     43 //	parameters:
     44 //	-
     45 //		name: id
     46 //		type: string
     47 //		description: The id of the domain block.
     48 //		in: path
     49 //		required: true
     50 //
     51 //	security:
     52 //	- OAuth2 Bearer:
     53 //		- admin
     54 //
     55 //	responses:
     56 //		'200':
     57 //			description: The requested domain block.
     58 //			schema:
     59 //				"$ref": "#/definitions/domainBlock"
     60 //		'400':
     61 //			description: bad request
     62 //		'401':
     63 //			description: unauthorized
     64 //		'403':
     65 //			description: forbidden
     66 //		'404':
     67 //			description: not found
     68 //		'406':
     69 //			description: not acceptable
     70 //		'500':
     71 //			description: internal server error
     72 func (m *Module) DomainBlockGETHandler(c *gin.Context) {
     73 	authed, err := oauth.Authed(c, true, true, true, true)
     74 	if err != nil {
     75 		apiutil.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1)
     76 		return
     77 	}
     78 
     79 	if !*authed.User.Admin {
     80 		err := fmt.Errorf("user %s not an admin", authed.User.ID)
     81 		apiutil.ErrorHandler(c, gtserror.NewErrorForbidden(err, err.Error()), m.processor.InstanceGetV1)
     82 		return
     83 	}
     84 
     85 	if _, err := apiutil.NegotiateAccept(c, apiutil.JSONAcceptHeaders...); err != nil {
     86 		apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1)
     87 		return
     88 	}
     89 
     90 	domainBlockID := c.Param(IDKey)
     91 	if domainBlockID == "" {
     92 		err := errors.New("no domain block id specified")
     93 		apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
     94 		return
     95 	}
     96 
     97 	export := false
     98 	exportString := c.Query(ExportQueryKey)
     99 	if exportString != "" {
    100 		i, err := strconv.ParseBool(exportString)
    101 		if err != nil {
    102 			err := fmt.Errorf("error parsing %s: %s", ExportQueryKey, err)
    103 			apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
    104 			return
    105 		}
    106 		export = i
    107 	}
    108 
    109 	domainBlock, errWithCode := m.processor.Admin().DomainBlockGet(c.Request.Context(), authed.Account, domainBlockID, export)
    110 	if errWithCode != nil {
    111 		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
    112 		return
    113 	}
    114 
    115 	c.JSON(http.StatusOK, domainBlock)
    116 }