2023-03-12 15:00:57 +00:00
|
|
|
// 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/>.
|
|
|
|
|
2021-07-05 11:23:03 +00:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
2021-08-25 13:34:33 +00:00
|
|
|
"context"
|
2022-09-02 10:17:46 +00:00
|
|
|
"errors"
|
2021-07-05 11:23:03 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2022-07-19 08:47:55 +00:00
|
|
|
"codeberg.org/gruf/go-kv"
|
2021-08-31 13:59:12 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
2021-07-05 11:23:03 +00:00
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/id"
|
2022-07-19 08:47:55 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2021-08-31 13:59:12 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/messages"
|
2021-07-26 18:25:54 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/text"
|
2021-07-05 11:23:03 +00:00
|
|
|
)
|
|
|
|
|
2023-09-21 10:12:04 +00:00
|
|
|
func (p *Processor) createDomainBlock(
|
2023-07-07 09:34:12 +00:00
|
|
|
ctx context.Context,
|
2023-09-04 13:55:17 +00:00
|
|
|
adminAcct *gtsmodel.Account,
|
2023-07-07 09:34:12 +00:00
|
|
|
domain string,
|
|
|
|
obfuscate bool,
|
|
|
|
publicComment string,
|
|
|
|
privateComment string,
|
|
|
|
subscriptionID string,
|
2023-09-21 10:12:04 +00:00
|
|
|
) (*apimodel.DomainPermission, string, gtserror.WithCode) {
|
2023-07-07 09:34:12 +00:00
|
|
|
// Check if a block already exists for this domain.
|
|
|
|
domainBlock, err := p.state.DB.GetDomainBlock(ctx, domain)
|
|
|
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
// Something went wrong in the DB.
|
|
|
|
err = gtserror.Newf("db error getting domain block %s: %w", domain, err)
|
2023-09-04 13:55:17 +00:00
|
|
|
return nil, "", gtserror.NewErrorInternalError(err)
|
2023-07-07 09:34:12 +00:00
|
|
|
}
|
2021-07-05 11:23:03 +00:00
|
|
|
|
2023-07-07 09:34:12 +00:00
|
|
|
if domainBlock == nil {
|
|
|
|
// No block exists yet, create it.
|
|
|
|
domainBlock = >smodel.DomainBlock{
|
2023-02-03 20:03:05 +00:00
|
|
|
ID: id.NewULID(),
|
2021-07-06 11:29:11 +00:00
|
|
|
Domain: domain,
|
2023-09-04 13:55:17 +00:00
|
|
|
CreatedByAccountID: adminAcct.ID,
|
2023-08-11 12:40:11 +00:00
|
|
|
PrivateComment: text.SanitizeToPlaintext(privateComment),
|
|
|
|
PublicComment: text.SanitizeToPlaintext(publicComment),
|
2022-08-15 10:35:05 +00:00
|
|
|
Obfuscate: &obfuscate,
|
2021-07-06 11:29:11 +00:00
|
|
|
SubscriptionID: subscriptionID,
|
2021-07-05 11:23:03 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 09:34:12 +00:00
|
|
|
// Insert the new block into the database.
|
|
|
|
if err := p.state.DB.CreateDomainBlock(ctx, domainBlock); err != nil {
|
2023-09-04 13:55:17 +00:00
|
|
|
err = gtserror.Newf("db error putting domain block %s: %w", domain, err)
|
|
|
|
return nil, "", gtserror.NewErrorInternalError(err)
|
2021-07-05 11:23:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-04 13:55:17 +00:00
|
|
|
actionID := id.NewULID()
|
|
|
|
|
|
|
|
// Process domain block side
|
|
|
|
// effects asynchronously.
|
|
|
|
if errWithCode := p.actions.Run(
|
|
|
|
ctx,
|
|
|
|
>smodel.AdminAction{
|
|
|
|
ID: actionID,
|
|
|
|
TargetCategory: gtsmodel.AdminActionCategoryDomain,
|
|
|
|
TargetID: domain,
|
|
|
|
Type: gtsmodel.AdminActionSuspend,
|
|
|
|
AccountID: adminAcct.ID,
|
|
|
|
Text: domainBlock.PrivateComment,
|
|
|
|
},
|
|
|
|
func(ctx context.Context) gtserror.MultiError {
|
2023-09-21 10:12:04 +00:00
|
|
|
// Log start + finish.
|
|
|
|
l := log.WithFields(kv.Fields{
|
|
|
|
{"domain", domain},
|
|
|
|
{"actionID", actionID},
|
|
|
|
}...).WithContext(ctx)
|
|
|
|
|
|
|
|
l.Info("processing domain block side effects")
|
|
|
|
defer func() { l.Info("finished processing domain block side effects") }()
|
|
|
|
|
2023-09-04 13:55:17 +00:00
|
|
|
return p.domainBlockSideEffects(ctx, domainBlock)
|
|
|
|
},
|
|
|
|
); errWithCode != nil {
|
|
|
|
return nil, actionID, errWithCode
|
|
|
|
}
|
2021-07-05 11:23:03 +00:00
|
|
|
|
2023-09-21 10:12:04 +00:00
|
|
|
apiDomainBlock, errWithCode := p.apiDomainPerm(ctx, domainBlock, false)
|
2023-09-04 13:55:17 +00:00
|
|
|
if errWithCode != nil {
|
|
|
|
return nil, actionID, errWithCode
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiDomainBlock, actionID, nil
|
|
|
|
}
|
|
|
|
|
2023-07-07 09:34:12 +00:00
|
|
|
// domainBlockSideEffects processes the side effects of a domain block:
|
|
|
|
//
|
|
|
|
// 1. Strip most info away from the instance entry for the domain.
|
|
|
|
// 2. Pass each account from the domain to the processor for deletion.
|
|
|
|
//
|
|
|
|
// It should be called asynchronously, since it can take a while when
|
|
|
|
// there are many accounts present on the given domain.
|
2023-09-04 13:55:17 +00:00
|
|
|
func (p *Processor) domainBlockSideEffects(
|
|
|
|
ctx context.Context,
|
|
|
|
block *gtsmodel.DomainBlock,
|
|
|
|
) gtserror.MultiError {
|
|
|
|
var errs gtserror.MultiError
|
|
|
|
|
2023-07-07 09:34:12 +00:00
|
|
|
// If we have an instance entry for this domain,
|
|
|
|
// update it with the new block ID and clear all fields
|
|
|
|
instance, err := p.state.DB.GetInstance(ctx, block.Domain)
|
|
|
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
2023-09-04 13:55:17 +00:00
|
|
|
errs.Appendf("db error getting instance %s: %w", block.Domain, err)
|
|
|
|
return errs
|
2023-02-22 15:05:26 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 09:34:12 +00:00
|
|
|
if instance != nil {
|
|
|
|
// We had an entry for this domain.
|
|
|
|
columns := stubbifyInstance(instance, block.ID)
|
|
|
|
if err := p.state.DB.UpdateInstance(ctx, instance, columns...); err != nil {
|
2023-09-04 13:55:17 +00:00
|
|
|
errs.Appendf("db error updating instance: %w", err)
|
|
|
|
return errs
|
2023-02-22 15:05:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-04 13:55:17 +00:00
|
|
|
// For each account that belongs to this domain,
|
|
|
|
// process an account delete message to remove
|
|
|
|
// that account's posts, media, etc.
|
|
|
|
if err := p.rangeDomainAccounts(ctx, block.Domain, func(account *gtsmodel.Account) {
|
|
|
|
cMsg := messages.FromClientAPI{
|
2023-07-07 09:34:12 +00:00
|
|
|
APObjectType: ap.ActorPerson,
|
|
|
|
APActivityType: ap.ActivityDelete,
|
|
|
|
GTSModel: block,
|
|
|
|
OriginAccount: account,
|
|
|
|
TargetAccount: account,
|
2023-09-04 13:55:17 +00:00
|
|
|
}
|
2023-07-07 09:34:12 +00:00
|
|
|
|
2023-09-04 13:55:17 +00:00
|
|
|
if err := p.state.Workers.ProcessFromClientAPI(ctx, cMsg); err != nil {
|
|
|
|
errs.Append(err)
|
|
|
|
}
|
2023-07-07 09:34:12 +00:00
|
|
|
}); err != nil {
|
2023-09-04 13:55:17 +00:00
|
|
|
errs.Appendf("db error ranging through accounts: %w", err)
|
2023-02-22 15:05:26 +00:00
|
|
|
}
|
|
|
|
|
2023-09-04 13:55:17 +00:00
|
|
|
return errs
|
2023-07-07 09:34:12 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 10:12:04 +00:00
|
|
|
func (p *Processor) deleteDomainBlock(
|
|
|
|
ctx context.Context,
|
|
|
|
adminAcct *gtsmodel.Account,
|
|
|
|
domainBlockID string,
|
|
|
|
) (*apimodel.DomainPermission, string, gtserror.WithCode) {
|
|
|
|
domainBlock, err := p.state.DB.GetDomainBlockByID(ctx, domainBlockID)
|
|
|
|
if err != nil {
|
|
|
|
if !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
// Real error.
|
|
|
|
err = gtserror.Newf("db error getting domain block: %w", err)
|
|
|
|
return nil, "", gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// There are just no entries for this ID.
|
|
|
|
err = fmt.Errorf("no domain block entry exists with ID %s", domainBlockID)
|
|
|
|
return nil, "", gtserror.NewErrorNotFound(err, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare the domain block to return, *before* the deletion goes through.
|
|
|
|
apiDomainBlock, errWithCode := p.apiDomainPerm(ctx, domainBlock, false)
|
|
|
|
if errWithCode != nil {
|
|
|
|
return nil, "", errWithCode
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the original domain block.
|
|
|
|
if err := p.state.DB.DeleteDomainBlock(ctx, domainBlock.Domain); err != nil {
|
|
|
|
err = gtserror.Newf("db error deleting domain block: %w", err)
|
|
|
|
return nil, "", gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actionID := id.NewULID()
|
|
|
|
|
|
|
|
// Process domain unblock side
|
|
|
|
// effects asynchronously.
|
|
|
|
if errWithCode := p.actions.Run(
|
|
|
|
ctx,
|
|
|
|
>smodel.AdminAction{
|
|
|
|
ID: actionID,
|
|
|
|
TargetCategory: gtsmodel.AdminActionCategoryDomain,
|
|
|
|
TargetID: domainBlock.Domain,
|
|
|
|
Type: gtsmodel.AdminActionUnsuspend,
|
|
|
|
AccountID: adminAcct.ID,
|
|
|
|
},
|
|
|
|
func(ctx context.Context) gtserror.MultiError {
|
|
|
|
// Log start + finish.
|
|
|
|
l := log.WithFields(kv.Fields{
|
|
|
|
{"domain", domainBlock.Domain},
|
|
|
|
{"actionID", actionID},
|
|
|
|
}...).WithContext(ctx)
|
|
|
|
|
|
|
|
l.Info("processing domain unblock side effects")
|
|
|
|
defer func() { l.Info("finished processing domain unblock side effects") }()
|
|
|
|
|
|
|
|
return p.domainUnblockSideEffects(ctx, domainBlock)
|
|
|
|
},
|
|
|
|
); errWithCode != nil {
|
|
|
|
return nil, actionID, errWithCode
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiDomainBlock, actionID, nil
|
|
|
|
}
|
|
|
|
|
2023-07-07 09:34:12 +00:00
|
|
|
// domainUnblockSideEffects processes the side effects of undoing a
|
|
|
|
// domain block:
|
|
|
|
//
|
|
|
|
// 1. Mark instance entry as no longer suspended.
|
|
|
|
// 2. Mark each account from the domain as no longer suspended, if the
|
|
|
|
// suspension origin corresponds to the ID of the provided domain block.
|
|
|
|
//
|
|
|
|
// It should be called asynchronously, since it can take a while when
|
|
|
|
// there are many accounts present on the given domain.
|
2023-09-04 13:55:17 +00:00
|
|
|
func (p *Processor) domainUnblockSideEffects(
|
|
|
|
ctx context.Context,
|
|
|
|
block *gtsmodel.DomainBlock,
|
|
|
|
) gtserror.MultiError {
|
|
|
|
var errs gtserror.MultiError
|
|
|
|
|
2023-07-07 09:34:12 +00:00
|
|
|
// Update instance entry for this domain, if we have it.
|
|
|
|
instance, err := p.state.DB.GetInstance(ctx, block.Domain)
|
|
|
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
2023-09-04 13:55:17 +00:00
|
|
|
errs.Appendf("db error getting instance %s: %w", block.Domain, err)
|
2023-02-22 15:05:26 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 09:34:12 +00:00
|
|
|
if instance != nil {
|
|
|
|
// We had an entry, update it to signal
|
|
|
|
// that it's no longer suspended.
|
|
|
|
instance.SuspendedAt = time.Time{}
|
|
|
|
instance.DomainBlockID = ""
|
|
|
|
if err := p.state.DB.UpdateInstance(
|
|
|
|
ctx,
|
|
|
|
instance,
|
|
|
|
"suspended_at",
|
|
|
|
"domain_block_id",
|
|
|
|
); err != nil {
|
2023-09-04 13:55:17 +00:00
|
|
|
errs.Appendf("db error updating instance: %w", err)
|
|
|
|
return errs
|
2023-07-07 09:34:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unsuspend all accounts whose suspension origin was this domain block.
|
2023-09-04 13:55:17 +00:00
|
|
|
if err := p.rangeDomainAccounts(ctx, block.Domain, func(account *gtsmodel.Account) {
|
2023-07-07 09:34:12 +00:00
|
|
|
if account.SuspensionOrigin == "" || account.SuspendedAt.IsZero() {
|
|
|
|
// Account wasn't suspended, nothing to do.
|
2023-09-04 13:55:17 +00:00
|
|
|
return
|
2023-07-07 09:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if account.SuspensionOrigin != block.ID {
|
|
|
|
// Account was suspended, but not by
|
|
|
|
// this domain block, leave it alone.
|
2023-09-04 13:55:17 +00:00
|
|
|
return
|
2023-07-07 09:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Account was suspended by this domain
|
|
|
|
// block, mark it as unsuspended.
|
|
|
|
account.SuspendedAt = time.Time{}
|
|
|
|
account.SuspensionOrigin = ""
|
|
|
|
|
|
|
|
if err := p.state.DB.UpdateAccount(
|
|
|
|
ctx,
|
|
|
|
account,
|
|
|
|
"suspended_at",
|
|
|
|
"suspension_origin",
|
|
|
|
); err != nil {
|
2023-09-04 13:55:17 +00:00
|
|
|
errs.Appendf("db error updating account %s: %w", account.Username, err)
|
2023-07-07 09:34:12 +00:00
|
|
|
}
|
|
|
|
}); err != nil {
|
2023-09-04 13:55:17 +00:00
|
|
|
errs.Appendf("db error ranging through accounts: %w", err)
|
2023-07-07 09:34:12 +00:00
|
|
|
}
|
2023-09-04 13:55:17 +00:00
|
|
|
|
|
|
|
return errs
|
2023-02-22 15:05:26 +00:00
|
|
|
}
|