gtsocial-umbx

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

domainblocksget.go (3511B)


      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 	"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 // DomainBlocksGETHandler swagger:operation GET /api/v1/admin/domain_blocks domainBlocksGet
     32 //
     33 // View all domain blocks currently in place.
     34 //
     35 //	---
     36 //	tags:
     37 //	- admin
     38 //
     39 //	produces:
     40 //	- application/json
     41 //
     42 //	parameters:
     43 //	-
     44 //		name: export
     45 //		type: boolean
     46 //		description: >-
     47 //			If set to `true`, then each entry in the returned list of domain blocks will only consist of
     48 //			the fields `domain` and `public_comment`. This is perfect for when you want to save and share
     49 //			a list of all the domains you have blocked on your instance, so that someone else can easily import them,
     50 //			but you don't want them to see the database IDs of your blocks, or private comments etc.
     51 //		in: query
     52 //		required: false
     53 //
     54 //	security:
     55 //	- OAuth2 Bearer:
     56 //		- admin
     57 //
     58 //	responses:
     59 //		'200':
     60 //			description: All domain blocks currently in place.
     61 //			schema:
     62 //				type: array
     63 //				items:
     64 //					"$ref": "#/definitions/domainBlock"
     65 //		'400':
     66 //			description: bad request
     67 //		'401':
     68 //			description: unauthorized
     69 //		'403':
     70 //			description: forbidden
     71 //		'404':
     72 //			description: not found
     73 //		'406':
     74 //			description: not acceptable
     75 //		'500':
     76 //			description: internal server error
     77 func (m *Module) DomainBlocksGETHandler(c *gin.Context) {
     78 	authed, err := oauth.Authed(c, true, true, true, true)
     79 	if err != nil {
     80 		apiutil.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1)
     81 		return
     82 	}
     83 
     84 	if !*authed.User.Admin {
     85 		err := fmt.Errorf("user %s not an admin", authed.User.ID)
     86 		apiutil.ErrorHandler(c, gtserror.NewErrorForbidden(err, err.Error()), m.processor.InstanceGetV1)
     87 		return
     88 	}
     89 
     90 	if _, err := apiutil.NegotiateAccept(c, apiutil.JSONAcceptHeaders...); err != nil {
     91 		apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1)
     92 		return
     93 	}
     94 
     95 	export := false
     96 	exportString := c.Query(ExportQueryKey)
     97 	if exportString != "" {
     98 		i, err := strconv.ParseBool(exportString)
     99 		if err != nil {
    100 			err := fmt.Errorf("error parsing %s: %s", ExportQueryKey, err)
    101 			apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
    102 			return
    103 		}
    104 		export = i
    105 	}
    106 
    107 	domainBlocks, errWithCode := m.processor.Admin().DomainBlocksGet(c.Request.Context(), authed.Account, export)
    108 	if errWithCode != nil {
    109 		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
    110 		return
    111 	}
    112 
    113 	c.JSON(http.StatusOK, domainBlocks)
    114 }