[feature] Allow editing domain blocks/allows, fix comment import (#3967)

* start implementing editing of existing domain permissions

* [feature] Allow editing domain blocks/allows, fix comment import

* [bugfix] Use "comment" via /api/v1/instance

* fix the stuff
This commit is contained in:
tobi
2025-04-04 18:29:22 +02:00
committed by GitHub
parent db4b857159
commit b184432331
32 changed files with 1021 additions and 313 deletions

View File

@ -102,12 +102,14 @@ func (m *Module) Route(attachHandler func(method string, path string, f ...gin.H
attachHandler(http.MethodPost, DomainBlocksPath, m.DomainBlocksPOSTHandler)
attachHandler(http.MethodGet, DomainBlocksPath, m.DomainBlocksGETHandler)
attachHandler(http.MethodGet, DomainBlocksPathWithID, m.DomainBlockGETHandler)
attachHandler(http.MethodPut, DomainBlocksPathWithID, m.DomainBlockUpdatePUTHandler)
attachHandler(http.MethodDelete, DomainBlocksPathWithID, m.DomainBlockDELETEHandler)
// domain allow stuff
attachHandler(http.MethodPost, DomainAllowsPath, m.DomainAllowsPOSTHandler)
attachHandler(http.MethodGet, DomainAllowsPath, m.DomainAllowsGETHandler)
attachHandler(http.MethodGet, DomainAllowsPathWithID, m.DomainAllowGETHandler)
attachHandler(http.MethodPut, DomainAllowsPathWithID, m.DomainAllowUpdatePUTHandler)
attachHandler(http.MethodDelete, DomainAllowsPathWithID, m.DomainAllowDELETEHandler)
// domain permission draft stuff

View File

@ -0,0 +1,91 @@
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package admin
import (
"github.com/gin-gonic/gin"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
// DomainAllowUpdatePUTHandler swagger:operation PUT /api/v1/admin/domain_allows/{id} domainAllowUpdate
//
// Update a single domain allow.
//
// ---
// tags:
// - admin
//
// consumes:
// - multipart/form-data
//
// produces:
// - application/json
//
// parameters:
// -
// name: id
// type: string
// description: The id of the domain allow.
// in: path
// required: true
// -
// name: obfuscate
// in: formData
// description: >-
// Obfuscate the name of the domain when serving it publicly.
// Eg., `example.org` becomes something like `ex***e.org`.
// type: boolean
// -
// name: public_comment
// in: formData
// description: >-
// Public comment about this domain allow.
// This will be displayed alongside the domain allow if you choose to share allows.
// type: string
// -
// name: private_comment
// in: formData
// description: >-
// Private comment about this domain allow. Will only be shown to other admins, so this
// is a useful way of internally keeping track of why a certain domain ended up allowed.
// type: string
//
// security:
// - OAuth2 Bearer:
// - admin:write:domain_allows
//
// responses:
// '200':
// description: The updated domain allow.
// schema:
// "$ref": "#/definitions/domainPermission"
// '400':
// description: bad request
// '401':
// description: unauthorized
// '403':
// description: forbidden
// '404':
// description: not found
// '406':
// description: not acceptable
// '500':
// description: internal server error
func (m *Module) DomainAllowUpdatePUTHandler(c *gin.Context) {
m.updateDomainPermission(c, gtsmodel.DomainPermissionAllow)
}

View File

@ -0,0 +1,91 @@
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package admin
import (
"github.com/gin-gonic/gin"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
// DomainBlockUpdatePUTHandler swagger:operation PUT /api/v1/admin/domain_blocks/{id} domainBlockUpdate
//
// Update a single domain block.
//
// ---
// tags:
// - admin
//
// consumes:
// - multipart/form-data
//
// produces:
// - application/json
//
// parameters:
// -
// name: id
// type: string
// description: The id of the domain block.
// in: path
// required: true
// -
// name: obfuscate
// in: formData
// description: >-
// Obfuscate the name of the domain when serving it publicly.
// Eg., `example.org` becomes something like `ex***e.org`.
// type: boolean
// -
// name: public_comment
// in: formData
// description: >-
// Public comment about this domain block.
// This will be displayed alongside the domain block if you choose to share blocks.
// type: string
// -
// name: private_comment
// in: formData
// description: >-
// Private comment about this domain block. Will only be shown to other admins, so this
// is a useful way of internally keeping track of why a certain domain ended up blocked.
// type: string
//
// security:
// - OAuth2 Bearer:
// - admin:write:domain_blocks
//
// responses:
// '200':
// description: The updated domain block.
// schema:
// "$ref": "#/definitions/domainPermission"
// '400':
// description: bad request
// '401':
// description: unauthorized
// '403':
// description: forbidden
// '404':
// description: not found
// '406':
// description: not acceptable
// '500':
// description: internal server error
func (m *Module) DomainBlockUpdatePUTHandler(c *gin.Context) {
m.updateDomainPermission(c, gtsmodel.DomainPermissionBlock)
}

View File

@ -29,6 +29,7 @@ import (
apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/util"
)
type singleDomainPermCreate func(
@ -112,7 +113,7 @@ func (m *Module) createDomainPermissions(
if importing && form.Domains.Size == 0 {
err = errors.New("import was specified but list of domains is empty")
} else if !importing && form.Domain == "" {
err = errors.New("empty domain provided")
err = errors.New("no domain provided")
}
if err != nil {
@ -122,14 +123,14 @@ func (m *Module) createDomainPermissions(
if !importing {
// Single domain permission creation.
domainBlock, _, errWithCode := single(
perm, _, errWithCode := single(
c.Request.Context(),
permType,
authed.Account,
form.Domain,
form.Obfuscate,
form.PublicComment,
form.PrivateComment,
util.PtrOrZero(form.Obfuscate),
util.PtrOrZero(form.PublicComment),
util.PtrOrZero(form.PrivateComment),
"", // No sub ID for single perm creation.
)
@ -138,7 +139,7 @@ func (m *Module) createDomainPermissions(
return
}
apiutil.JSON(c, http.StatusOK, domainBlock)
apiutil.JSON(c, http.StatusOK, perm)
return
}
@ -177,6 +178,82 @@ func (m *Module) createDomainPermissions(
apiutil.JSON(c, http.StatusOK, domainPerms)
}
func (m *Module) updateDomainPermission(
c *gin.Context,
permType gtsmodel.DomainPermissionType,
) {
// Scope differs based on permType.
var requireScope apiutil.Scope
if permType == gtsmodel.DomainPermissionBlock {
requireScope = apiutil.ScopeAdminWriteDomainBlocks
} else {
requireScope = apiutil.ScopeAdminWriteDomainAllows
}
authed, errWithCode := apiutil.TokenAuth(c,
true, true, true, true,
requireScope,
)
if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return
}
if !*authed.User.Admin {
err := fmt.Errorf("user %s not an admin", authed.User.ID)
apiutil.ErrorHandler(c, gtserror.NewErrorForbidden(err, err.Error()), m.processor.InstanceGetV1)
return
}
if authed.Account.IsMoving() {
apiutil.ForbiddenAfterMove(c)
return
}
if _, err := apiutil.NegotiateAccept(c, apiutil.JSONAcceptHeaders...); err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1)
return
}
permID, errWithCode := apiutil.ParseID(c.Param(apiutil.IDKey))
if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return
}
// Parse + validate form.
form := new(apimodel.DomainPermissionRequest)
if err := c.ShouldBind(form); err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
return
}
if form.Obfuscate == nil &&
form.PrivateComment == nil &&
form.PublicComment == nil {
const errText = "empty form submitted"
errWithCode := gtserror.NewErrorBadRequest(errors.New(errText), errText)
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return
}
perm, errWithCode := m.processor.Admin().DomainPermissionUpdate(
c.Request.Context(),
permType,
permID,
form.Obfuscate,
form.PublicComment,
form.PrivateComment,
nil, // Can't update perm sub ID this way yet.
)
if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return
}
apiutil.JSON(c, http.StatusOK, perm)
}
// deleteDomainPermission deletes a single domain permission (block or allow).
func (m *Module) deleteDomainPermission(
c *gin.Context,

View File

@ -26,6 +26,7 @@ import (
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/util"
)
// DomainPermissionDraftsPOSTHandler swagger:operation POST /api/v1/admin/domain_permission_drafts domainPermissionDraftCreate
@ -148,9 +149,9 @@ func (m *Module) DomainPermissionDraftsPOSTHandler(c *gin.Context) {
authed.Account,
form.Domain,
permType,
form.Obfuscate,
form.PublicComment,
form.PrivateComment,
util.PtrOrZero(form.Obfuscate),
util.PtrOrZero(form.PublicComment),
util.PtrOrZero(form.PrivateComment),
)
if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)

View File

@ -97,14 +97,21 @@ func (suite *DomainPermissionSubscriptionTestTestSuite) TestDomainPermissionSubs
suite.Equal(`[
{
"domain": "bumfaces.net",
"public_comment": "big jerks"
"public_comment": "big jerks",
"obfuscate": false,
"private_comment": ""
},
{
"domain": "peepee.poopoo",
"public_comment": "harassment"
"public_comment": "harassment",
"obfuscate": false,
"private_comment": ""
},
{
"domain": "nothanks.com"
"domain": "nothanks.com",
"public_comment": "",
"obfuscate": false,
"private_comment": ""
}
]`, dst.String())
@ -177,13 +184,22 @@ func (suite *DomainPermissionSubscriptionTestTestSuite) TestDomainPermissionSubs
// Ensure expected.
suite.Equal(`[
{
"domain": "bumfaces.net"
"domain": "bumfaces.net",
"public_comment": "",
"obfuscate": false,
"private_comment": ""
},
{
"domain": "peepee.poopoo"
"domain": "peepee.poopoo",
"public_comment": "",
"obfuscate": false,
"private_comment": ""
},
{
"domain": "nothanks.com"
"domain": "nothanks.com",
"public_comment": "",
"obfuscate": false,
"private_comment": ""
}
]`, dst.String())

View File

@ -136,7 +136,7 @@ func (suite *InstancePeersGetTestSuite) TestInstancePeersGetOnlySuspended() {
{
"domain": "replyguys.com",
"suspended_at": "2020-05-13T13:29:12.000Z",
"public_comment": "reply-guying to tech posts"
"comment": "reply-guying to tech posts"
}
]`, dst.String())
}
@ -186,7 +186,7 @@ func (suite *InstancePeersGetTestSuite) TestInstancePeersGetOnlySuspendedAuthori
{
"domain": "replyguys.com",
"suspended_at": "2020-05-13T13:29:12.000Z",
"public_comment": "reply-guying to tech posts"
"comment": "reply-guying to tech posts"
}
]`, dst.String())
}
@ -219,7 +219,7 @@ func (suite *InstancePeersGetTestSuite) TestInstancePeersGetAll() {
{
"domain": "replyguys.com",
"suspended_at": "2020-05-13T13:29:12.000Z",
"public_comment": "reply-guying to tech posts"
"comment": "reply-guying to tech posts"
}
]`, dst.String())
}
@ -263,12 +263,12 @@ func (suite *InstancePeersGetTestSuite) TestInstancePeersGetAllWithObfuscated()
{
"domain": "o*g.*u**.t**.*or*t.*r**ev**",
"suspended_at": "2021-06-09T10:34:55.000Z",
"public_comment": "just absolutely the worst, wowza"
"comment": "just absolutely the worst, wowza"
},
{
"domain": "replyguys.com",
"suspended_at": "2020-05-13T13:29:12.000Z",
"public_comment": "reply-guying to tech posts"
"comment": "reply-guying to tech posts"
}
]`, dst.String())
}

View File

@ -33,8 +33,13 @@ type Domain struct {
// example: 2021-07-30T09:20:25+00:00
SilencedAt string `json:"silenced_at,omitempty"`
// If the domain is blocked, what's the publicly-stated reason for the block.
// Alternative to `public_comment` to be used when serializing/deserializing via /api/v1/instance.
// example: they smell
PublicComment string `form:"public_comment" json:"public_comment,omitempty"`
Comment *string `form:"comment" json:"comment,omitempty"`
// If the domain is blocked, what's the publicly-stated reason for the block.
// Alternative to `comment` to be used when serializing/deserializing NOT via /api/v1/instance.
// example: they smell
PublicComment *string `form:"public_comment" json:"public_comment,omitempty"`
}
// DomainPermission represents a permission applied to one domain (explicit block/allow).
@ -48,10 +53,10 @@ type DomainPermission struct {
ID string `json:"id,omitempty"`
// Obfuscate the domain name when serving this domain permission entry publicly.
// example: false
Obfuscate bool `json:"obfuscate,omitempty"`
Obfuscate *bool `json:"obfuscate,omitempty"`
// Private comment for this permission entry, visible to this instance's admins only.
// example: they are poopoo
PrivateComment string `json:"private_comment,omitempty"`
PrivateComment *string `json:"private_comment,omitempty"`
// If applicable, the ID of the subscription that caused this domain permission entry to be created.
// example: 01FBW25TF5J67JW3HFHZCSD23K
SubscriptionID string `json:"subscription_id,omitempty"`
@ -80,14 +85,14 @@ type DomainPermissionRequest struct {
// Obfuscate the domain name when displaying this permission entry publicly.
// Ie., instead of 'example.org' show something like 'e**mpl*.or*'.
// example: false
Obfuscate bool `form:"obfuscate" json:"obfuscate"`
Obfuscate *bool `form:"obfuscate" json:"obfuscate"`
// Private comment for other admins on why this permission entry was created.
// example: don't like 'em!!!!
PrivateComment string `form:"private_comment" json:"private_comment"`
PrivateComment *string `form:"private_comment" json:"private_comment"`
// Public comment on why this permission entry was created.
// Will be visible to requesters at /api/v1/instance/peers if this endpoint is exposed.
// example: foss dorks 😫
PublicComment string `form:"public_comment" json:"public_comment"`
PublicComment *string `form:"public_comment" json:"public_comment"`
// Permission type to create (only applies to domain permission drafts, not explicit blocks and allows).
PermissionType string `form:"permission_type" json:"permission_type"`
}