2021-07-05 14:23:03 +03:00
|
|
|
/*
|
|
|
|
GoToSocial
|
|
|
|
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
|
|
|
|
|
|
|
|
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 16:34:33 +03:00
|
|
|
"context"
|
2021-07-05 14:23:03 +03:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
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"
|
2021-07-26 21:25:54 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/text"
|
2021-07-05 14:23:03 +03:00
|
|
|
)
|
|
|
|
|
2021-08-25 16:34:33 +03: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) {
|
2021-07-05 14:23:03 +03: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
|
|
|
|
domainBlock := >smodel.DomainBlock{}
|
2021-08-25 16:34:33 +03:00
|
|
|
err := p.db.GetWhere(ctx, []db.Where{{Key: "domain", Value: domain, CaseInsensitive: true}}, domainBlock)
|
2021-07-05 14:23:03 +03:00
|
|
|
if err != nil {
|
2021-08-20 13:26:56 +03:00
|
|
|
if err != db.ErrNoEntries {
|
2021-07-05 14:23:03 +03:00
|
|
|
// something went wrong in the DB
|
2021-07-06 14:29:11 +03:00
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("DomainBlockCreate: db error checking for existence of domain block %s: %s", domain, err))
|
2021-07-05 14:23:03 +03: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 {
|
2021-07-06 14:29:11 +03:00
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("DomainBlockCreate: error creating id for new domain block %s: %s", domain, err))
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
domainBlock = >smodel.DomainBlock{
|
|
|
|
ID: blockID,
|
2021-07-06 14:29:11 +03:00
|
|
|
Domain: domain,
|
2021-07-05 14:23:03 +03:00
|
|
|
CreatedByAccountID: account.ID,
|
2021-07-26 21:25:54 +03:00
|
|
|
PrivateComment: text.RemoveHTML(privateComment),
|
|
|
|
PublicComment: text.RemoveHTML(publicComment),
|
2021-07-06 14:29:11 +03:00
|
|
|
Obfuscate: obfuscate,
|
|
|
|
SubscriptionID: subscriptionID,
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// put the new block in the database
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := p.db.Put(ctx, domainBlock); err != nil {
|
2021-08-20 13:26:56 +03:00
|
|
|
if err != db.ErrNoEntries {
|
2021-07-05 14:23:03 +03:00
|
|
|
// there's a real error creating the block
|
2021-07-06 14:29:11 +03:00
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("DomainBlockCreate: db error putting new domain block %s: %s", domain, err))
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// process the side effects of the domain block asynchronously since it might take a while
|
2021-08-25 16:34:33 +03:00
|
|
|
go p.initiateDomainBlockSideEffects(ctx, account, domainBlock) // TODO: add this to a queuing system so it can retry/resume
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
|
|
|
|
2021-08-25 16:34:33 +03:00
|
|
|
mastoDomainBlock, err := p.tc.DomainBlockToMasto(ctx, domainBlock, false)
|
2021-07-05 14:23:03 +03:00
|
|
|
if err != nil {
|
2021-07-06 14:29:11 +03:00
|
|
|
return nil, gtserror.NewErrorInternalError(fmt.Errorf("DomainBlockCreate: error converting domain block to frontend/masto representation %s: %s", domain, err))
|
2021-07-05 14:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return mastoDomainBlock, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 16:34:33 +03:00
|
|
|
func (p *processor) initiateDomainBlockSideEffects(ctx context.Context, account *gtsmodel.Account, block *gtsmodel.DomainBlock) {
|
2021-07-05 14:23:03 +03:00
|
|
|
l := p.log.WithFields(logrus.Fields{
|
|
|
|
"func": "domainBlockProcessSideEffects",
|
|
|
|
"domain": block.Domain,
|
|
|
|
})
|
|
|
|
|
|
|
|
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{}
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := p.db.GetWhere(ctx, []db.Where{{Key: "domain", Value: block.Domain, CaseInsensitive: true}}, instance); err == nil {
|
2021-07-05 14:23:03 +03: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 = ""
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := p.db.UpdateByID(ctx, instance.ID, instance); err != nil {
|
2021-07-05 14:23:03 +03: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
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := p.db.DeleteWhere(ctx, []db.Where{{Key: "username", Value: block.Domain, CaseInsensitive: true}}, >smodel.Account{}); err != nil {
|
2021-07-05 14:23:03 +03:00
|
|
|
l.Errorf("domainBlockProcessSideEffects: db error removing instance account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 16:34:33 +03:00
|
|
|
accounts, err := p.db.GetInstanceAccounts(ctx, block.Domain, maxID, limit)
|
2021-07-05 14:23:03 +03:00
|
|
|
if err != nil {
|
2021-08-20 13:26:56 +03:00
|
|
|
if err == db.ErrNoEntries {
|
2021-07-05 14:23:03 +03: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
|
|
|
|
p.fromClientAPI <- gtsmodel.FromClientAPI{
|
|
|
|
APObjectType: gtsmodel.ActivityStreamsPerson,
|
|
|
|
APActivityType: gtsmodel.ActivityStreamsDelete,
|
2021-07-06 14:29:11 +03:00
|
|
|
GTSModel: block,
|
2021-07-05 14:23:03 +03:00
|
|
|
OriginAccount: account,
|
|
|
|
TargetAccount: a,
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|