gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

exportminimal.go (5266B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package trans
     19 
     20 import (
     21 	"context"
     22 	"errors"
     23 	"fmt"
     24 	"os"
     25 
     26 	"github.com/superseriousbusiness/gotosocial/internal/db"
     27 )
     28 
     29 func (e *exporter) ExportMinimal(ctx context.Context, path string) error {
     30 	if path == "" {
     31 		return errors.New("ExportMinimal: path empty")
     32 	}
     33 
     34 	file, err := os.Create(path)
     35 	if err != nil {
     36 		return fmt.Errorf("ExportMinimal: couldn't export to %s: %s", path, err)
     37 	}
     38 
     39 	// export all local accounts we have in the database
     40 	localAccounts, err := e.exportAccounts(ctx, []db.Where{{Key: "domain", Value: nil}}, file)
     41 	if err != nil {
     42 		return fmt.Errorf("ExportMinimal: error exporting accounts: %s", err)
     43 	}
     44 
     45 	// export all blocks that relate to local accounts
     46 	blocks, err := e.exportBlocks(ctx, localAccounts, file)
     47 	if err != nil {
     48 		return fmt.Errorf("ExportMinimal: error exporting blocks: %s", err)
     49 	}
     50 
     51 	// for each block, make sure we've written out the account owning it, or targeted by it --
     52 	// this might include non-local accounts, but we need these so we don't lose anything
     53 	for _, b := range blocks {
     54 		_, alreadyWritten := e.writtenIDs[b.AccountID]
     55 		if !alreadyWritten {
     56 			_, err := e.exportAccounts(ctx, []db.Where{{Key: "id", Value: b.AccountID}}, file)
     57 			if err != nil {
     58 				return fmt.Errorf("ExportMinimal: error exporting block owner account: %s", err)
     59 			}
     60 		}
     61 
     62 		_, alreadyWritten = e.writtenIDs[b.TargetAccountID]
     63 		if !alreadyWritten {
     64 			_, err := e.exportAccounts(ctx, []db.Where{{Key: "id", Value: b.TargetAccountID}}, file)
     65 			if err != nil {
     66 				return fmt.Errorf("ExportMinimal: error exporting block target account: %s", err)
     67 			}
     68 		}
     69 	}
     70 
     71 	// export all follows that relate to local accounts
     72 	follows, err := e.exportFollows(ctx, localAccounts, file)
     73 	if err != nil {
     74 		return fmt.Errorf("ExportMinimal: error exporting follows: %s", err)
     75 	}
     76 
     77 	// for each follow, make sure we've written out the account owning it, or targeted by it --
     78 	// this might include non-local accounts, but we need these so we don't lose anything
     79 	for _, follow := range follows {
     80 		_, alreadyWritten := e.writtenIDs[follow.AccountID]
     81 		if !alreadyWritten {
     82 			_, err := e.exportAccounts(ctx, []db.Where{{Key: "id", Value: follow.AccountID}}, file)
     83 			if err != nil {
     84 				return fmt.Errorf("ExportMinimal: error exporting follow owner account: %s", err)
     85 			}
     86 		}
     87 
     88 		_, alreadyWritten = e.writtenIDs[follow.TargetAccountID]
     89 		if !alreadyWritten {
     90 			_, err := e.exportAccounts(ctx, []db.Where{{Key: "id", Value: follow.TargetAccountID}}, file)
     91 			if err != nil {
     92 				return fmt.Errorf("ExportMinimal: error exporting follow target account: %s", err)
     93 			}
     94 		}
     95 	}
     96 
     97 	// export all follow requests that relate to local accounts
     98 	followRequests, err := e.exportFollowRequests(ctx, localAccounts, file)
     99 	if err != nil {
    100 		return fmt.Errorf("ExportMinimal: error exporting follow requests: %s", err)
    101 	}
    102 
    103 	// for each follow request, make sure we've written out the account owning it, or targeted by it --
    104 	// this might include non-local accounts, but we need these so we don't lose anything
    105 	for _, fr := range followRequests {
    106 		_, alreadyWritten := e.writtenIDs[fr.AccountID]
    107 		if !alreadyWritten {
    108 			_, err := e.exportAccounts(ctx, []db.Where{{Key: "id", Value: fr.AccountID}}, file)
    109 			if err != nil {
    110 				return fmt.Errorf("ExportMinimal: error exporting follow request owner account: %s", err)
    111 			}
    112 		}
    113 
    114 		_, alreadyWritten = e.writtenIDs[fr.TargetAccountID]
    115 		if !alreadyWritten {
    116 			_, err := e.exportAccounts(ctx, []db.Where{{Key: "id", Value: fr.TargetAccountID}}, file)
    117 			if err != nil {
    118 				return fmt.Errorf("ExportMinimal: error exporting follow request target account: %s", err)
    119 			}
    120 		}
    121 	}
    122 
    123 	// export all domain blocks
    124 	if _, err := e.exportDomainBlocks(ctx, file); err != nil {
    125 		return fmt.Errorf("ExportMinimal: error exporting domain blocks: %s", err)
    126 	}
    127 
    128 	// export all users
    129 	if _, err := e.exportUsers(ctx, file); err != nil {
    130 		return fmt.Errorf("ExportMinimal: error exporting users: %s", err)
    131 	}
    132 
    133 	// export all instances
    134 	if _, err := e.exportInstances(ctx, file); err != nil {
    135 		return fmt.Errorf("ExportMinimal: error exporting instances: %s", err)
    136 	}
    137 
    138 	// export all SUSPENDED accounts to make sure the suspension sticks across db migration etc
    139 	whereSuspended := []db.Where{{
    140 		Key:   "suspended_at",
    141 		Not:   true,
    142 		Value: nil,
    143 	}}
    144 	if _, err := e.exportAccounts(ctx, whereSuspended, file); err != nil {
    145 		return fmt.Errorf("ExportMinimal: error exporting suspended accounts: %s", err)
    146 	}
    147 
    148 	return neatClose(file)
    149 }