2021-07-05 12:23:03 +01:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2021-12-20 17:42:19 +00:00
|
|
|
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
|
2021-07-05 12:23:03 +01:00
|
|
|
|
|
|
|
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 (
|
2021-08-25 14:34:33 +01:00
|
|
|
"context"
|
2022-09-02 11:17:46 +01:00
|
|
|
"errors"
|
2021-07-05 12:23:03 +01:00
|
|
|
"fmt"
|
2022-05-02 11:53:46 +01:00
|
|
|
"strings"
|
2021-07-05 12:23:03 +01:00
|
|
|
"time"
|
|
|
|
|
2022-07-19 09:47:55 +01:00
|
|
|
"codeberg.org/gruf/go-kv"
|
2021-08-31 14:59:12 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
2021-07-05 12:23:03 +01: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 09:47:55 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2021-08-31 14:59:12 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/messages"
|
2021-07-26 19:25:54 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/text"
|
2021-07-05 12:23:03 +01:00
|
|
|
)
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
func (p *processor) DomainBlockCreate(ctx context.Context, account *gtsmodel.Account, domain string, obfuscate bool, publicComment string, privateComment string, subscriptionID string) (*apimodel.DomainBlock, gtserror.WithCode) {
|
2022-05-02 11:53:46 +01:00
|
|
|
// domain blocks will always be lowercase
|
|
|
|
domain = strings.ToLower(domain)
|
|
|
|
|
2021-07-05 12:23:03 +01:00
|
|
|
// first check if we already have a block -- if err == nil we already had a block so we can skip a whole lot of work
|
2022-09-02 11:17:46 +01:00
|
|
|
block, err := p.db.GetDomainBlock(ctx, domain)
|
2021-07-05 12:23:03 +01:00
|
|
|
if err != nil {
|
2022-09-02 11:17:46 +01:00
|
|
|
if !errors.Is(err, db.ErrNoEntries) {
|
2021-07-05 12:23:03 +01:00
|
|
|
// something went wrong in the DB
|
2022-09-02 11:17:46 +01:00
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error checking for existence of domain block %s: %s", domain, err))
|
2021-07-05 12:23:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// there's no block for this domain yet so create one
|
|
|
|
// note: we take a new ulid from timestamp here in case we need to sort blocks
|
|
|
|
blockID, err := id.NewULID()
|
|
|
|
if err != nil {
|
2022-09-02 11:17:46 +01:00
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("error creating id for new domain block %s: %s", domain, err))
|
2021-07-05 12:23:03 +01:00
|
|
|
}
|
|
|
|
|
2022-10-06 11:48:17 +01:00
|
|
|
newBlock := >smodel.DomainBlock{
|
2021-07-05 12:23:03 +01:00
|
|
|
ID: blockID,
|
2021-07-06 12:29:11 +01:00
|
|
|
Domain: domain,
|
2021-07-05 12:23:03 +01:00
|
|
|
CreatedByAccountID: account.ID,
|
2022-05-26 10:37:13 +01:00
|
|
|
PrivateComment: text.SanitizePlaintext(privateComment),
|
|
|
|
PublicComment: text.SanitizePlaintext(publicComment),
|
2022-08-15 11:35:05 +01:00
|
|
|
Obfuscate: &obfuscate,
|
2021-07-06 12:29:11 +01:00
|
|
|
SubscriptionID: subscriptionID,
|
2021-07-05 12:23:03 +01:00
|
|
|
}
|
|
|
|
|
2022-09-02 11:17:46 +01:00
|
|
|
// Insert the new block into the database
|
|
|
|
if err := p.db.CreateDomainBlock(ctx, newBlock); err != nil {
|
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error putting new domain block %s: %s", domain, err))
|
2021-07-05 12:23:03 +01:00
|
|
|
}
|
2022-09-02 11:17:46 +01:00
|
|
|
|
|
|
|
// Set the newly created block
|
2022-10-06 11:48:17 +01:00
|
|
|
block = newBlock
|
2022-09-02 11:17:46 +01:00
|
|
|
|
|
|
|
// Process the side effects of the domain block asynchronously since it might take a while
|
2022-10-04 16:50:29 +01:00
|
|
|
go func() {
|
|
|
|
p.initiateDomainBlockSideEffects(context.Background(), account, block)
|
|
|
|
}()
|
2021-07-05 12:23:03 +01:00
|
|
|
}
|
|
|
|
|
2022-09-02 11:17:46 +01:00
|
|
|
// Convert our gts model domain block into an API model
|
|
|
|
apiDomainBlock, err := p.tc.DomainBlockToAPIDomainBlock(ctx, block, false)
|
2021-07-05 12:23:03 +01:00
|
|
|
if err != nil {
|
2022-09-02 11:17:46 +01:00
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting domain block to frontend/api representation %s: %s", domain, err))
|
2021-07-05 12:23:03 +01:00
|
|
|
}
|
|
|
|
|
2021-10-04 14:24:19 +01:00
|
|
|
return apiDomainBlock, nil
|
2021-07-05 12:23:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// initiateDomainBlockSideEffects should be called asynchronously, to process the side effects of a domain block:
|
|
|
|
//
|
|
|
|
// 1. Strip most info away from the instance entry for the domain.
|
|
|
|
// 2. Delete the instance account for that instance if it exists.
|
|
|
|
// 3. Select all accounts from this instance and pass them through the delete functionality of the processor.
|
2021-08-25 14:34:33 +01:00
|
|
|
func (p *processor) initiateDomainBlockSideEffects(ctx context.Context, account *gtsmodel.Account, block *gtsmodel.DomainBlock) {
|
2022-07-19 09:47:55 +01:00
|
|
|
l := log.WithFields(kv.Fields{
|
|
|
|
{"domain", block.Domain},
|
|
|
|
}...)
|
2021-07-05 12:23:03 +01:00
|
|
|
|
|
|
|
l.Debug("processing domain block side effects")
|
|
|
|
|
|
|
|
// if we have an instance entry for this domain, update it with the new block ID and clear all fields
|
|
|
|
instance := >smodel.Instance{}
|
2022-05-02 11:53:46 +01:00
|
|
|
if err := p.db.GetWhere(ctx, []db.Where{{Key: "domain", Value: block.Domain}}, instance); err == nil {
|
2022-08-15 11:35:05 +01:00
|
|
|
updatingColumns := []string{
|
|
|
|
"title",
|
|
|
|
"updated_at",
|
|
|
|
"suspended_at",
|
|
|
|
"domain_block_id",
|
|
|
|
"short_description",
|
|
|
|
"description",
|
|
|
|
"terms",
|
|
|
|
"contact_email",
|
|
|
|
"contact_account_username",
|
|
|
|
"contact_account_id",
|
|
|
|
"version",
|
|
|
|
}
|
2021-07-05 12:23:03 +01:00
|
|
|
instance.Title = ""
|
|
|
|
instance.UpdatedAt = time.Now()
|
|
|
|
instance.SuspendedAt = time.Now()
|
|
|
|
instance.DomainBlockID = block.ID
|
|
|
|
instance.ShortDescription = ""
|
|
|
|
instance.Description = ""
|
|
|
|
instance.Terms = ""
|
|
|
|
instance.ContactEmail = ""
|
|
|
|
instance.ContactAccountUsername = ""
|
|
|
|
instance.ContactAccountID = ""
|
|
|
|
instance.Version = ""
|
2022-10-08 12:50:48 +01:00
|
|
|
if err := p.db.UpdateByID(ctx, instance, instance.ID, updatingColumns...); err != nil {
|
2021-07-05 12:23:03 +01:00
|
|
|
l.Errorf("domainBlockProcessSideEffects: db error updating instance: %s", err)
|
|
|
|
}
|
|
|
|
l.Debug("domainBlockProcessSideEffects: instance entry updated")
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have an instance account for this instance, delete it
|
2022-10-08 12:50:48 +01:00
|
|
|
if instanceAccount, err := p.db.GetAccountByUsernameDomain(ctx, block.Domain, block.Domain); err == nil {
|
|
|
|
if err := p.db.DeleteAccount(ctx, instanceAccount.ID); err != nil {
|
|
|
|
l.Errorf("domainBlockProcessSideEffects: db error deleting instance account: %s", err)
|
|
|
|
}
|
2021-07-05 12:23:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete accounts through the normal account deletion system (which should also delete media + posts + remove posts from timelines)
|
|
|
|
|
|
|
|
limit := 20 // just select 20 accounts at a time so we don't nuke our DB/mem with one huge query
|
|
|
|
var maxID string // this is initially an empty string so we'll start at the top of accounts list (sorted by ID)
|
|
|
|
|
|
|
|
selectAccountsLoop:
|
|
|
|
for {
|
2021-08-25 14:34:33 +01:00
|
|
|
accounts, err := p.db.GetInstanceAccounts(ctx, block.Domain, maxID, limit)
|
2021-07-05 12:23:03 +01:00
|
|
|
if err != nil {
|
2021-08-20 11:26:56 +01:00
|
|
|
if err == db.ErrNoEntries {
|
2021-07-05 12:23:03 +01:00
|
|
|
// no accounts left for this instance so we're done
|
|
|
|
l.Infof("domainBlockProcessSideEffects: done iterating through accounts for domain %s", block.Domain)
|
|
|
|
break selectAccountsLoop
|
|
|
|
}
|
|
|
|
// an actual error has occurred
|
|
|
|
l.Errorf("domainBlockProcessSideEffects: db error selecting accounts for domain %s: %s", block.Domain, err)
|
|
|
|
break selectAccountsLoop
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, a := range accounts {
|
|
|
|
l.Debugf("putting delete for account %s in the clientAPI channel", a.Username)
|
|
|
|
|
|
|
|
// pass the account delete through the client api channel for processing
|
2022-04-28 13:23:11 +01:00
|
|
|
p.clientWorker.Queue(messages.FromClientAPI{
|
2021-08-31 14:59:12 +01:00
|
|
|
APObjectType: ap.ActorPerson,
|
|
|
|
APActivityType: ap.ActivityDelete,
|
2021-07-06 12:29:11 +01:00
|
|
|
GTSModel: block,
|
2021-07-05 12:23:03 +01:00
|
|
|
OriginAccount: account,
|
|
|
|
TargetAccount: a,
|
2022-04-28 13:23:11 +01:00
|
|
|
})
|
2021-07-05 12:23:03 +01:00
|
|
|
|
|
|
|
// if this is the last account in the slice, set the maxID appropriately for the next query
|
|
|
|
if i == len(accounts)-1 {
|
|
|
|
maxID = a.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|