working pretty alright!

This commit is contained in:
tsmethurst 2021-09-07 18:17:12 +02:00
commit 7e80eafaea
20 changed files with 744 additions and 187 deletions

View file

@ -20,17 +20,21 @@ package trans
import (
"context"
"errors"
"fmt"
"os"
"github.com/superseriousbusiness/gotosocial/internal/db"
transmodel "github.com/superseriousbusiness/gotosocial/internal/trans/model"
)
func (e *exporter) ExportMinimal(ctx context.Context, path string) error {
if path == "" {
return errors.New("ExportMinimal: path empty")
}
f, err := os.Create(path)
if err != nil {
return err
return fmt.Errorf("ExportMinimal: couldn't export to %s: %s", path, err)
}
// export all local accounts we have in the database
@ -39,13 +43,14 @@ func (e *exporter) ExportMinimal(ctx context.Context, path string) error {
return fmt.Errorf("ExportMinimal: error exporting accounts: %s", err)
}
// export all blocks that relate to those accounts
// export all blocks that relate to local accounts
blocks, err := e.exportBlocks(ctx, localAccounts, f)
if err != nil {
return fmt.Errorf("ExportMinimal: error exporting blocks: %s", err)
}
// for each block, make sure we've written out the account owning it, or targeted by it
// for each block, make sure we've written out the account owning it, or targeted by it --
// this might include non-local accounts, but we need these so we don't lose anything
for _, b := range blocks {
_, alreadyWritten := e.writtenIDs[b.AccountID]
if !alreadyWritten {
@ -64,65 +69,72 @@ func (e *exporter) ExportMinimal(ctx context.Context, path string) error {
}
}
// export all follows that relate to local accounts
follows, err := e.exportFollows(ctx, localAccounts, f)
if err != nil {
return fmt.Errorf("ExportMinimal: error exporting follows: %s", err)
}
// for each follow, make sure we've written out the account owning it, or targeted by it --
// this might include non-local accounts, but we need these so we don't lose anything
for _, follow := range follows {
_, alreadyWritten := e.writtenIDs[follow.AccountID]
if !alreadyWritten {
_, err := e.exportAccounts(ctx, []db.Where{{Key: "id", Value: follow.AccountID}}, f)
if err != nil {
return fmt.Errorf("ExportMinimal: error exporting follow owner account: %s", err)
}
}
_, alreadyWritten = e.writtenIDs[follow.TargetAccountID]
if !alreadyWritten {
_, err := e.exportAccounts(ctx, []db.Where{{Key: "id", Value: follow.TargetAccountID}}, f)
if err != nil {
return fmt.Errorf("ExportMinimal: error exporting follow target account: %s", err)
}
}
}
// export all follow requests that relate to local accounts
frs, err := e.exportFollowRequests(ctx, localAccounts, f)
if err != nil {
return fmt.Errorf("ExportMinimal: error exporting follow requests: %s", err)
}
// for each follow request, make sure we've written out the account owning it, or targeted by it --
// this might include non-local accounts, but we need these so we don't lose anything
for _, fr := range frs {
_, alreadyWritten := e.writtenIDs[fr.AccountID]
if !alreadyWritten {
_, err := e.exportAccounts(ctx, []db.Where{{Key: "id", Value: fr.AccountID}}, f)
if err != nil {
return fmt.Errorf("ExportMinimal: error exporting follow request owner account: %s", err)
}
}
_, alreadyWritten = e.writtenIDs[fr.TargetAccountID]
if !alreadyWritten {
_, err := e.exportAccounts(ctx, []db.Where{{Key: "id", Value: fr.TargetAccountID}}, f)
if err != nil {
return fmt.Errorf("ExportMinimal: error exporting follow request target account: %s", err)
}
}
}
// export all domain blocks
if _, err := e.exportDomainBlocks(ctx, f); err != nil {
return fmt.Errorf("ExportMinimal: error exporting domain blocks: %s", err)
}
// export all users
if _, err := e.exportUsers(ctx, f); err != nil {
return fmt.Errorf("ExportMinimal: error exporting users: %s", err)
}
// export all instances
if _, err := e.exportInstances(ctx, f); err != nil {
return fmt.Errorf("ExportMinimal: error exporting instances: %s", err)
}
return neatClose(f)
}
func (e *exporter) exportAccounts(ctx context.Context, where []db.Where, f *os.File) ([]*transmodel.Account, error) {
// select using the 'where' we've been provided
accounts := []*transmodel.Account{}
if err := e.db.GetWhere(ctx, where, &accounts); err != nil {
return nil, fmt.Errorf("exportAccounts: error selecting accounts: %s", err)
}
// write any accounts found to file
for _, a := range accounts {
if err := e.accountEncode(ctx, f, a); err != nil {
return nil, fmt.Errorf("exportAccounts: error encoding account: %s", err)
}
}
return accounts, nil
}
func (e *exporter) exportBlocks(ctx context.Context, accounts []*transmodel.Account, f *os.File) ([]*transmodel.Block, error) {
blocksUnique := make(map[string]*transmodel.Block)
// for each account we want to export both where it's blocking and where it's blocked
for _, a := range accounts {
// 1. export blocks owned by given account
whereBlocking := []db.Where{{Key: "account_id", Value: a.ID}}
blocking := []*transmodel.Block{}
if err := e.db.GetWhere(ctx, whereBlocking, &blocking); err != nil {
return nil, fmt.Errorf("exportBlocks: error selecting blocks owned by account %s: %s", a.ID, err)
}
for _, b := range blocking {
b.Type = transmodel.TransBlock
if err := e.simpleEncode(ctx, f, b, b.ID); err != nil {
return nil, fmt.Errorf("exportBlocks: error encoding block owned by account %s: %s", a.ID, err)
}
blocksUnique[b.ID] = b
}
// 2. export blocks that target given account
whereBlocked := []db.Where{{Key: "target_account_id", Value: a.ID}}
blocked := []*transmodel.Block{}
if err := e.db.GetWhere(ctx, whereBlocked, &blocked); err != nil {
return nil, fmt.Errorf("exportBlocks: error selecting blocks targeting account %s: %s", a.ID, err)
}
for _, b := range blocked {
b.Type = transmodel.TransBlock
if err := e.simpleEncode(ctx, f, b, b.ID); err != nil {
return nil, fmt.Errorf("exportBlocks: error encoding block targeting account %s: %s", a.ID, err)
}
blocksUnique[b.ID] = b
}
}
// now return all the blocks we found
blocks := []*transmodel.Block{}
for _, b := range blocksUnique {
blocks = append(blocks, b)
}
return blocks, nil
}