diff options
author | 2021-09-09 16:15:25 +0200 | |
---|---|---|
committer | 2021-09-09 16:15:25 +0200 | |
commit | 555ea8edfb2c30d149b3ca6cb0fbe53f2798c7bc (patch) | |
tree | 24567c4c365a007fcd2d6603e696b363129abb77 /internal/trans | |
parent | Merge pull request #198 from NyaaaWhatsUpDoc/update/sqlite-library (diff) | |
download | gotosocial-555ea8edfb2c30d149b3ca6cb0fbe53f2798c7bc.tar.xz |
Import export (#194)
* start with export/import code
* messing about with decoding/encoding
* some more fiddling
* stuff is WORKING
* working pretty alright!
* go fmt
* fix up tests, add docs
* start backup/restore doc
* tweaks
* credits
* update advancedVisibility settings
* update bun library -> v1.0.4
Signed-off-by: kim (grufwub) <grufwub@gmail.com>
* update oauth library -> v4.3.1-SSB
Signed-off-by: kim (grufwub) <grufwub@gmail.com>
* handle oauth token scope, fix user.SigninCount + token.UserID
Signed-off-by: kim (grufwub) <grufwub@gmail.com>
* update oauth library --> v4.3.2-SSB
Signed-off-by: kim (grufwub) <grufwub@gmail.com>
* update sqlite library -> v1.13.0
Signed-off-by: kim (grufwub) <grufwub@gmail.com>
* review changes
* start with export/import code
* messing about with decoding/encoding
* some more fiddling
* stuff is WORKING
* working pretty alright!
* go fmt
* fix up tests, add docs
* start backup/restore doc
* tweaks
* credits
* update advancedVisibility settings
* review changes
Co-authored-by: kim (grufwub) <grufwub@gmail.com>
Co-authored-by: kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>
Diffstat (limited to 'internal/trans')
-rw-r--r-- | internal/trans/decoders.go | 138 | ||||
-rw-r--r-- | internal/trans/encoders.go | 83 | ||||
-rw-r--r-- | internal/trans/export.go | 223 | ||||
-rw-r--r-- | internal/trans/exporter.go | 46 | ||||
-rw-r--r-- | internal/trans/exportminimal.go | 150 | ||||
-rw-r--r-- | internal/trans/exportminimal_test.go | 54 | ||||
-rw-r--r-- | internal/trans/import.go | 146 | ||||
-rw-r--r-- | internal/trans/import_test.go | 91 | ||||
-rw-r--r-- | internal/trans/importer.go | 44 | ||||
-rw-r--r-- | internal/trans/model/account.go | 54 | ||||
-rw-r--r-- | internal/trans/model/block.go | 31 | ||||
-rw-r--r-- | internal/trans/model/domainblock.go | 34 | ||||
-rw-r--r-- | internal/trans/model/follow.go | 31 | ||||
-rw-r--r-- | internal/trans/model/followrequest.go | 31 | ||||
-rw-r--r-- | internal/trans/model/instance.go | 43 | ||||
-rw-r--r-- | internal/trans/model/type.go | 41 | ||||
-rw-r--r-- | internal/trans/model/user.go | 50 | ||||
-rw-r--r-- | internal/trans/trans_test.go | 42 | ||||
-rw-r--r-- | internal/trans/util.go | 32 |
19 files changed, 1364 insertions, 0 deletions
diff --git a/internal/trans/decoders.go b/internal/trans/decoders.go new file mode 100644 index 000000000..b4f146023 --- /dev/null +++ b/internal/trans/decoders.go @@ -0,0 +1,138 @@ +/* + 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 trans + +import ( + "crypto/x509" + "encoding/pem" + "errors" + "fmt" + "time" + + "github.com/mitchellh/mapstructure" + transmodel "github.com/superseriousbusiness/gotosocial/internal/trans/model" +) + +func newDecoder(target interface{}) (*mapstructure.Decoder, error) { + decoderConfig := &mapstructure.DecoderConfig{ + DecodeHook: mapstructure.StringToTimeHookFunc(time.RFC3339), // this is needed to decode time.Time entries serialized as string + Result: target, + } + return mapstructure.NewDecoder(decoderConfig) +} + +func (i *importer) accountDecode(e transmodel.Entry) (*transmodel.Account, error) { + a := &transmodel.Account{} + if err := i.simpleDecode(e, a); err != nil { + return nil, err + } + + // extract public key + publicKeyBlock, _ := pem.Decode([]byte(a.PublicKeyString)) + if publicKeyBlock == nil { + return nil, errors.New("accountDecode: error decoding account public key") + } + publicKey, err := x509.ParsePKCS1PublicKey(publicKeyBlock.Bytes) + if err != nil { + return nil, fmt.Errorf("accountDecode: error parsing account public key: %s", err) + } + a.PublicKey = publicKey + + if a.Domain == "" { + // extract private key (local account) + privateKeyBlock, _ := pem.Decode([]byte(a.PrivateKeyString)) + if privateKeyBlock == nil { + return nil, errors.New("accountDecode: error decoding account private key") + } + privateKey, err := x509.ParsePKCS1PrivateKey(privateKeyBlock.Bytes) + if err != nil { + return nil, fmt.Errorf("accountDecode: error parsing account private key: %s", err) + } + a.PrivateKey = privateKey + } + + return a, nil +} + +func (i *importer) blockDecode(e transmodel.Entry) (*transmodel.Block, error) { + b := &transmodel.Block{} + if err := i.simpleDecode(e, b); err != nil { + return nil, err + } + + return b, nil +} + +func (i *importer) domainBlockDecode(e transmodel.Entry) (*transmodel.DomainBlock, error) { + b := &transmodel.DomainBlock{} + if err := i.simpleDecode(e, b); err != nil { + return nil, err + } + + return b, nil +} + +func (i *importer) followDecode(e transmodel.Entry) (*transmodel.Follow, error) { + f := &transmodel.Follow{} + if err := i.simpleDecode(e, f); err != nil { + return nil, err + } + + return f, nil +} + +func (i *importer) followRequestDecode(e transmodel.Entry) (*transmodel.FollowRequest, error) { + f := &transmodel.FollowRequest{} + if err := i.simpleDecode(e, f); err != nil { + return nil, err + } + + return f, nil +} + +func (i *importer) instanceDecode(e transmodel.Entry) (*transmodel.Instance, error) { + inst := &transmodel.Instance{} + if err := i.simpleDecode(e, inst); err != nil { + return nil, err + } + + return inst, nil +} + +func (i *importer) userDecode(e transmodel.Entry) (*transmodel.User, error) { + u := &transmodel.User{} + if err := i.simpleDecode(e, u); err != nil { + return nil, err + } + + return u, nil +} + +func (i *importer) simpleDecode(entry transmodel.Entry, target interface{}) error { + decoder, err := newDecoder(target) + if err != nil { + return fmt.Errorf("simpleDecode: error creating decoder: %s", err) + } + + if err := decoder.Decode(&entry); err != nil { + return fmt.Errorf("simpleDecode: error decoding: %s", err) + } + + return nil +} diff --git a/internal/trans/encoders.go b/internal/trans/encoders.go new file mode 100644 index 000000000..76c2acadc --- /dev/null +++ b/internal/trans/encoders.go @@ -0,0 +1,83 @@ +/* + 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 trans + +import ( + "context" + "crypto/x509" + "encoding/json" + "encoding/pem" + "errors" + "fmt" + "os" + + transmodel "github.com/superseriousbusiness/gotosocial/internal/trans/model" +) + +// accountEncode handles special fields like private + public keys on accounts +func (e *exporter) accountEncode(ctx context.Context, f *os.File, a *transmodel.Account) error { + a.Type = transmodel.TransAccount + + // marshal public key + encodedPublicKey := x509.MarshalPKCS1PublicKey(a.PublicKey) + if encodedPublicKey == nil { + return errors.New("could not MarshalPKCS1PublicKey") + } + publicKeyBytes := pem.EncodeToMemory(&pem.Block{ + Type: "RSA PUBLIC KEY", + Bytes: encodedPublicKey, + }) + a.PublicKeyString = string(publicKeyBytes) + + if a.Domain == "" { + // marshal private key for local account + encodedPrivateKey := x509.MarshalPKCS1PrivateKey(a.PrivateKey) + if encodedPrivateKey == nil { + return errors.New("could not MarshalPKCS1PrivateKey") + } + privateKeyBytes := pem.EncodeToMemory(&pem.Block{ + Type: "RSA PRIVATE KEY", + Bytes: encodedPrivateKey, + }) + a.PrivateKeyString = string(privateKeyBytes) + } + + return e.simpleEncode(ctx, f, a, a.ID) +} + +// simpleEncode can be used for any type that doesn't have special keys which need handling differently, +// or for types where special keys have already been handled. +// +// Beware, the 'type' key on the passed interface should already have been set, since simpleEncode won't know +// what type it is! If you try to decode stuff you've encoded with a missing type key, you're going to have a bad time. +func (e *exporter) simpleEncode(ctx context.Context, file *os.File, i interface{}, id string) error { + _, alreadyWritten := e.writtenIDs[id] + if alreadyWritten { + // this exporter has already exported an entry with this ID, no need to do it twice + return nil + } + + err := json.NewEncoder(file).Encode(i) + if err != nil { + return fmt.Errorf("simpleEncode: error encoding entry with id %s: %s", id, err) + } + + e.writtenIDs[id] = true + return nil +} diff --git a/internal/trans/export.go b/internal/trans/export.go new file mode 100644 index 000000000..a432975be --- /dev/null +++ b/internal/trans/export.go @@ -0,0 +1,223 @@ +/* + 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 trans + +import ( + "context" + "fmt" + "os" + + "github.com/superseriousbusiness/gotosocial/internal/db" + transmodel "github.com/superseriousbusiness/gotosocial/internal/trans/model" +) + +func (e *exporter) exportAccounts(ctx context.Context, where []db.Where, file *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, file, 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, file *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, file, 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, file, 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 +} + +func (e *exporter) exportDomainBlocks(ctx context.Context, file *os.File) ([]*transmodel.DomainBlock, error) { + domainBlocks := []*transmodel.DomainBlock{} + + if err := e.db.GetAll(ctx, &domainBlocks); err != nil { + return nil, fmt.Errorf("exportBlocks: error selecting domain blocks: %s", err) + } + + for _, b := range domainBlocks { + b.Type = transmodel.TransDomainBlock + if err := e.simpleEncode(ctx, file, b, b.ID); err != nil { + return nil, fmt.Errorf("exportBlocks: error encoding domain block: %s", err) + } + } + + return domainBlocks, nil +} + +func (e *exporter) exportFollows(ctx context.Context, accounts []*transmodel.Account, file *os.File) ([]*transmodel.Follow, error) { + followsUnique := make(map[string]*transmodel.Follow) + + // for each account we want to export both where it's following and where it's followed + for _, a := range accounts { + // 1. export follows owned by given account + whereFollowing := []db.Where{{Key: "account_id", Value: a.ID}} + following := []*transmodel.Follow{} + if err := e.db.GetWhere(ctx, whereFollowing, &following); err != nil { + return nil, fmt.Errorf("exportFollows: error selecting follows owned by account %s: %s", a.ID, err) + } + for _, follow := range following { + follow.Type = transmodel.TransFollow + if err := e.simpleEncode(ctx, file, follow, follow.ID); err != nil { + return nil, fmt.Errorf("exportFollows: error encoding follow owned by account %s: %s", a.ID, err) + } + followsUnique[follow.ID] = follow + } + + // 2. export follows that target given account + whereFollowed := []db.Where{{Key: "target_account_id", Value: a.ID}} + followed := []*transmodel.Follow{} + if err := e.db.GetWhere(ctx, whereFollowed, &followed); err != nil { + return nil, fmt.Errorf("exportFollows: error selecting follows targeting account %s: %s", a.ID, err) + } + for _, follow := range followed { + follow.Type = transmodel.TransFollow + if err := e.simpleEncode(ctx, file, follow, follow.ID); err != nil { + return nil, fmt.Errorf("exportFollows: error encoding follow targeting account %s: %s", a.ID, err) + } + followsUnique[follow.ID] = follow + } + } + + // now return all the follows we found + follows := []*transmodel.Follow{} + for _, follow := range followsUnique { + follows = append(follows, follow) + } + + return follows, nil +} + +func (e *exporter) exportFollowRequests(ctx context.Context, accounts []*transmodel.Account, file *os.File) ([]*transmodel.FollowRequest, error) { + frsUnique := make(map[string]*transmodel.FollowRequest) + + // for each account we want to export both where it's following and where it's followed + for _, a := range accounts { + // 1. export follow requests owned by given account + whereRequesting := []db.Where{{Key: "account_id", Value: a.ID}} + requesting := []*transmodel.FollowRequest{} + if err := e.db.GetWhere(ctx, whereRequesting, &requesting); err != nil { + return nil, fmt.Errorf("exportFollowRequests: error selecting follow requests owned by account %s: %s", a.ID, err) + } + for _, fr := range requesting { + fr.Type = transmodel.TransFollowRequest + if err := e.simpleEncode(ctx, file, fr, fr.ID); err != nil { + return nil, fmt.Errorf("exportFollowRequests: error encoding follow request owned by account %s: %s", a.ID, err) + } + frsUnique[fr.ID] = fr + } + + // 2. export follow requests that target given account + whereRequested := []db.Where{{Key: "target_account_id", Value: a.ID}} + requested := []*transmodel.FollowRequest{} + if err := e.db.GetWhere(ctx, whereRequested, &requested); err != nil { + return nil, fmt.Errorf("exportFollowRequests: error selecting follow requests targeting account %s: %s", a.ID, err) + } + for _, fr := range requested { + fr.Type = transmodel.TransFollowRequest + if err := e.simpleEncode(ctx, file, fr, fr.ID); err != nil { + return nil, fmt.Errorf("exportFollowRequests: error encoding follow request targeting account %s: %s", a.ID, err) + } + frsUnique[fr.ID] = fr + } + } + + // now return all the followRequests we found + followRequests := []*transmodel.FollowRequest{} + for _, fr := range frsUnique { + followRequests = append(followRequests, fr) + } + + return followRequests, nil +} + +func (e *exporter) exportInstances(ctx context.Context, file *os.File) ([]*transmodel.Instance, error) { + instances := []*transmodel.Instance{} + + if err := e.db.GetAll(ctx, &instances); err != nil { + return nil, fmt.Errorf("exportInstances: error selecting instance: %s", err) + } + + for _, u := range instances { + u.Type = transmodel.TransInstance + if err := e.simpleEncode(ctx, file, u, u.ID); err != nil { + return nil, fmt.Errorf("exportInstances: error encoding instance: %s", err) + } + } + + return instances, nil +} + +func (e *exporter) exportUsers(ctx context.Context, file *os.File) ([]*transmodel.User, error) { + users := []*transmodel.User{} + + if err := e.db.GetAll(ctx, &users); err != nil { + return nil, fmt.Errorf("exportUsers: error selecting users: %s", err) + } + + for _, u := range users { + u.Type = transmodel.TransUser + if err := e.simpleEncode(ctx, file, u, u.ID); err != nil { + return nil, fmt.Errorf("exportUsers: error encoding user: %s", err) + } + } + + return users, nil +} diff --git a/internal/trans/exporter.go b/internal/trans/exporter.go new file mode 100644 index 000000000..3dc0558f5 --- /dev/null +++ b/internal/trans/exporter.go @@ -0,0 +1,46 @@ +/* + 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 trans + +import ( + "context" + + "github.com/sirupsen/logrus" + "github.com/superseriousbusiness/gotosocial/internal/db" +) + +// Exporter wraps functionality for exporting entries from the database to a file. +type Exporter interface { + ExportMinimal(ctx context.Context, path string) error +} + +type exporter struct { + db db.DB + log *logrus.Logger + writtenIDs map[string]bool +} + +// NewExporter returns a new Exporter that will use the given db and logger. +func NewExporter(db db.DB, log *logrus.Logger) Exporter { + return &exporter{ + db: db, + log: log, + writtenIDs: make(map[string]bool), + } +} diff --git a/internal/trans/exportminimal.go b/internal/trans/exportminimal.go new file mode 100644 index 000000000..a9ace9b91 --- /dev/null +++ b/internal/trans/exportminimal.go @@ -0,0 +1,150 @@ +/* + 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 trans + +import ( + "context" + "errors" + "fmt" + "os" + + "github.com/superseriousbusiness/gotosocial/internal/db" +) + +func (e *exporter) ExportMinimal(ctx context.Context, path string) error { + if path == "" { + return errors.New("ExportMinimal: path empty") + } + + file, err := os.Create(path) + if err != nil { + return fmt.Errorf("ExportMinimal: couldn't export to %s: %s", path, err) + } + + // export all local accounts we have in the database + localAccounts, err := e.exportAccounts(ctx, []db.Where{{Key: "domain", Value: nil}}, file) + if err != nil { + return fmt.Errorf("ExportMinimal: error exporting accounts: %s", err) + } + + // export all blocks that relate to local accounts + blocks, err := e.exportBlocks(ctx, localAccounts, file) + 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 -- + // 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 { + _, err := e.exportAccounts(ctx, []db.Where{{Key: "id", Value: b.AccountID}}, file) + if err != nil { + return fmt.Errorf("ExportMinimal: error exporting block owner account: %s", err) + } + } + + _, alreadyWritten = e.writtenIDs[b.TargetAccountID] + if !alreadyWritten { + _, err := e.exportAccounts(ctx, []db.Where{{Key: "id", Value: b.TargetAccountID}}, file) + if err != nil { + return fmt.Errorf("ExportMinimal: error exporting block target account: %s", err) + } + } + } + + // export all follows that relate to local accounts + follows, err := e.exportFollows(ctx, localAccounts, file) + 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}}, file) + 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}}, file) + if err != nil { + return fmt.Errorf("ExportMinimal: error exporting follow target account: %s", err) + } + } + } + + // export all follow requests that relate to local accounts + followRequests, err := e.exportFollowRequests(ctx, localAccounts, file) + 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 followRequests { + _, alreadyWritten := e.writtenIDs[fr.AccountID] + if !alreadyWritten { + _, err := e.exportAccounts(ctx, []db.Where{{Key: "id", Value: fr.AccountID}}, file) + 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}}, file) + if err != nil { + return fmt.Errorf("ExportMinimal: error exporting follow request target account: %s", err) + } + } + } + + // export all domain blocks + if _, err := e.exportDomainBlocks(ctx, file); err != nil { + return fmt.Errorf("ExportMinimal: error exporting domain blocks: %s", err) + } + + // export all users + if _, err := e.exportUsers(ctx, file); err != nil { + return fmt.Errorf("ExportMinimal: error exporting users: %s", err) + } + + // export all instances + if _, err := e.exportInstances(ctx, file); err != nil { + return fmt.Errorf("ExportMinimal: error exporting instances: %s", err) + } + + // export all SUSPENDED accounts to make sure the suspension sticks across db migration etc + whereSuspended := []db.Where{{ + Key: "suspended_at", + Not: true, + Value: nil, + }} + if _, err := e.exportAccounts(ctx, whereSuspended, file); err != nil { + return fmt.Errorf("ExportMinimal: error exporting suspended accounts: %s", err) + } + + return neatClose(file) +} diff --git a/internal/trans/exportminimal_test.go b/internal/trans/exportminimal_test.go new file mode 100644 index 000000000..2bffffcfe --- /dev/null +++ b/internal/trans/exportminimal_test.go @@ -0,0 +1,54 @@ +/* + 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 trans_test + +import ( + "context" + "fmt" + "os" + "testing" + + "github.com/google/uuid" + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/internal/trans" +) + +type ExportMinimalTestSuite struct { + TransTestSuite +} + +func (suite *ExportMinimalTestSuite) TestExportMinimalOK() { + // use a temporary file path that will be cleaned when the test is closed + tempFilePath := fmt.Sprintf("%s/%s", suite.T().TempDir(), uuid.NewString()) + + // export to the tempFilePath + exporter := trans.NewExporter(suite.db, suite.log) + err := exporter.ExportMinimal(context.Background(), tempFilePath) + suite.NoError(err) + + // we should have some bytes in that file now + b, err := os.ReadFile(tempFilePath) + suite.NoError(err) + suite.NotEmpty(b) + fmt.Println(string(b)) +} + +func TestExportMinimalTestSuite(t *testing.T) { + suite.Run(t, &ExportMinimalTestSuite{}) +} diff --git a/internal/trans/import.go b/internal/trans/import.go new file mode 100644 index 000000000..74624e540 --- /dev/null +++ b/internal/trans/import.go @@ -0,0 +1,146 @@ +/* + 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 trans + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io" + "os" + + transmodel "github.com/superseriousbusiness/gotosocial/internal/trans/model" +) + +func (i *importer) Import(ctx context.Context, path string) error { + if path == "" { + return errors.New("Export: path empty") + } + + file, err := os.Open(path) + if err != nil { + return fmt.Errorf("Import: couldn't export to %s: %s", path, err) + } + + decoder := json.NewDecoder(file) + decoder.UseNumber() + + for { + entry := transmodel.Entry{} + err := decoder.Decode(&entry) + if err != nil { + if err == io.EOF { + i.log.Infof("Import: reached end of file") + return neatClose(file) + } + return fmt.Errorf("Import: error decoding in readLoop: %s", err) + } + if err := i.inputEntry(ctx, entry); err != nil { + return fmt.Errorf("Import: error inputting entry: %s", err) + } + } +} + +func (i *importer) inputEntry(ctx context.Context, entry transmodel.Entry) error { + t, ok := entry[transmodel.TypeKey].(string) + if !ok { + return errors.New("inputEntry: could not derive entry type: missing or malformed 'type' key in json") + } + + switch transmodel.Type(t) { + case transmodel.TransAccount: + account, err := i.accountDecode(entry) + if err != nil { + return fmt.Errorf("inputEntry: error decoding entry into account: %s", err) + } + if err := i.putInDB(ctx, account); err != nil { + return fmt.Errorf("inputEntry: error adding account to database: %s", err) + } + i.log.Infof("inputEntry: added account with id %s", account.ID) + return nil + case transmodel.TransBlock: + block, err := i.blockDecode(entry) + if err != nil { + return fmt.Errorf("inputEntry: error decoding entry into block: %s", err) + } + if err := i.putInDB(ctx, block); err != nil { + return fmt.Errorf("inputEntry: error adding block to database: %s", err) + } + i.log.Infof("inputEntry: added block with id %s", block.ID) + return nil + case transmodel.TransDomainBlock: + block, err := i.domainBlockDecode(entry) + if err != nil { + return fmt.Errorf("inputEntry: error decoding entry into domain block: %s", err) + } + if err := i.putInDB(ctx, block); err != nil { + return fmt.Errorf("inputEntry: error adding domain block to database: %s", err) + } + i.log.Infof("inputEntry: added domain block with id %s", block.ID) + return nil + case transmodel.TransFollow: + follow, err := i.followDecode(entry) + if err != nil { + return fmt.Errorf("inputEntry: error decoding entry into follow: %s", err) + } + if err := i.putInDB(ctx, follow); err != nil { + return fmt.Errorf("inputEntry: error adding follow to database: %s", err) + } + i.log.Infof("inputEntry: added follow with id %s", follow.ID) + return nil + case transmodel.TransFollowRequest: + fr, err := i.followRequestDecode(entry) + if err != nil { + return fmt.Errorf("inputEntry: error decoding entry into follow request: %s", err) + } + if err := i.putInDB(ctx, fr); err != nil { + return fmt.Errorf("inputEntry: error adding follow request to database: %s", err) + } + i.log.Infof("inputEntry: added follow request with id %s", fr.ID) + return nil + case transmodel.TransInstance: + inst, err := i.instanceDecode(entry) + if err != nil { + return fmt.Errorf("inputEntry: error decoding entry into instance: %s", err) + } + if err := i.putInDB(ctx, inst); err != nil { + return fmt.Errorf("inputEntry: error adding instance to database: %s", err) + } + i.log.Infof("inputEntry: added instance with id %s", inst.ID) + return nil + case transmodel.TransUser: + user, err := i.userDecode(entry) + if err != nil { + return fmt.Errorf("inputEntry: error decoding entry into user: %s", err) + } + if err := i.putInDB(ctx, user); err != nil { + return fmt.Errorf("inputEntry: error adding user to database: %s", err) + } + i.log.Infof("inputEntry: added user with id %s", user.ID) + return nil + } + + i.log.Errorf("inputEntry: didn't recognize transtype '%s', skipping it", t) + return nil +} + +func (i *importer) putInDB(ctx context.Context, entry interface{}) error { + return i.db.Put(ctx, entry) +} diff --git a/internal/trans/import_test.go b/internal/trans/import_test.go new file mode 100644 index 000000000..137a5fae1 --- /dev/null +++ b/internal/trans/import_test.go @@ -0,0 +1,91 @@ +/* + 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 trans_test + +import ( + "context" + "fmt" + "os" + "testing" + + "github.com/google/uuid" + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/trans" + "github.com/superseriousbusiness/gotosocial/testrig" +) + +type ImportMinimalTestSuite struct { + TransTestSuite +} + +func (suite *ImportMinimalTestSuite) TestImportMinimalOK() { + ctx := context.Background() + + // use a temporary file path + tempFilePath := fmt.Sprintf("%s/%s", suite.T().TempDir(), uuid.NewString()) + + // export to the tempFilePath + exporter := trans.NewExporter(suite.db, suite.log) + err := exporter.ExportMinimal(ctx, tempFilePath) + suite.NoError(err) + + // we should have some bytes in that file now + b, err := os.ReadFile(tempFilePath) + suite.NoError(err) + suite.NotEmpty(b) + fmt.Println(string(b)) + + // create a new database with just the tables created, no entries + testrig.StandardDBTeardown(suite.db) + newDB := testrig.NewTestDB() + testrig.CreateTestTables(newDB) + + importer := trans.NewImporter(newDB, suite.log) + err = importer.Import(ctx, tempFilePath) + suite.NoError(err) + + // we should have some accounts in the database + accounts := []*gtsmodel.Account{} + err = newDB.GetAll(ctx, &accounts) + suite.NoError(err) + suite.NotEmpty(accounts) + + // we should have some blocks in the database + blocks := []*gtsmodel.Block{} + err = newDB.GetAll(ctx, &blocks) + suite.NoError(err) + suite.NotEmpty(blocks) + + // we should have some follows in the database + follows := []*gtsmodel.Follow{} + err = newDB.GetAll(ctx, &follows) + suite.NoError(err) + suite.NotEmpty(follows) + + // we should have some domain blocks in the database + domainBlocks := []*gtsmodel.DomainBlock{} + err = newDB.GetAll(ctx, &domainBlocks) + suite.NoError(err) + suite.NotEmpty(domainBlocks) +} + +func TestImportMinimalTestSuite(t *testing.T) { + suite.Run(t, &ImportMinimalTestSuite{}) +} diff --git a/internal/trans/importer.go b/internal/trans/importer.go new file mode 100644 index 000000000..ea8866f53 --- /dev/null +++ b/internal/trans/importer.go @@ -0,0 +1,44 @@ +/* + 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 trans + +import ( + "context" + + "github.com/sirupsen/logrus" + "github.com/superseriousbusiness/gotosocial/internal/db" +) + +// Importer wraps functionality for importing entries from a file into the database. +type Importer interface { + Import(ctx context.Context, path string) error +} + +type importer struct { + db db.DB + log *logrus.Logger +} + +// NewImporter returns a new Importer interface that uses the given db and logger. +func NewImporter(db db.DB, log *logrus.Logger) Importer { + return &importer{ + db: db, + log: log, + } +} diff --git a/internal/trans/model/account.go b/internal/trans/model/account.go new file mode 100644 index 000000000..2350048d7 --- /dev/null +++ b/internal/trans/model/account.go @@ -0,0 +1,54 @@ +/* + 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 trans + +import ( + "crypto/rsa" + "time" +) + +// Account represents the minimum viable representation of an account for export/import. +type Account struct { + Type Type `json:"type" bun:"-"` + ID string `json:"id" bun:",nullzero"` + CreatedAt *time.Time `json:"createdAt" bun:",nullzero"` + Username string `json:"username" bun:",nullzero"` + DisplayName string `json:"displayName,omitempty" bun:",nullzero"` + Note string `json:"note,omitempty" bun:",nullzero"` + Domain string `json:"domain,omitempty" bun:",nullzero"` + HeaderRemoteURL string `json:"headerRemoteURL,omitempty" bun:",nullzero"` + AvatarRemoteURL string `json:"avatarRemoteURL,omitempty" bun:",nullzero"` + Locked bool `json:"locked"` + Language string `json:"language,omitempty" bun:",nullzero"` + URI string `json:"uri" bun:",nullzero"` + URL string `json:"url" bun:",nullzero"` + InboxURI string `json:"inboxURI" bun:",nullzero"` + OutboxURI string `json:"outboxURI" bun:",nullzero"` + FollowingURI string `json:"followingUri" bun:",nullzero"` + FollowersURI string `json:"followersUri" bun:",nullzero"` + FeaturedCollectionURI string `json:"featuredCollectionUri" bun:",nullzero"` + ActorType string `json:"actorType" bun:",nullzero"` + PrivateKey *rsa.PrivateKey `json:"-" mapstructure:"-"` + PrivateKeyString string `json:"privateKey,omitempty" mapstructure:"privateKey" bun:"-"` + PublicKey *rsa.PublicKey `json:"-" mapstructure:"-"` + PublicKeyString string `json:"publicKey,omitempty" mapstructure:"publicKey" bun:"-"` + PublicKeyURI string `json:"publicKeyUri" bun:",nullzero"` + SuspendedAt *time.Time `json:"suspendedAt,omitempty" bun:",nullzero"` + SuspensionOrigin string `json:"suspensionOrigin,omitempty" bun:",nullzero"` +} diff --git a/internal/trans/model/block.go b/internal/trans/model/block.go new file mode 100644 index 000000000..313e6a7cd --- /dev/null +++ b/internal/trans/model/block.go @@ -0,0 +1,31 @@ +/* + 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 trans + +import "time" + +// Block represents an account block as serialized in an exported file. +type Block struct { + Type Type `json:"type" bun:"-"` + ID string `json:"id" bun:",nullzero"` + CreatedAt *time.Time `json:"createdAt" bun:",nullzero"` + URI string `json:"uri" bun:",nullzero"` + AccountID string `json:"accountId" bun:",nullzero"` + TargetAccountID string `json:"targetAccountId" bun:",nullzero"` +} diff --git a/internal/trans/model/domainblock.go b/internal/trans/model/domainblock.go new file mode 100644 index 000000000..de2bcd00a --- /dev/null +++ b/internal/trans/model/domainblock.go @@ -0,0 +1,34 @@ +/* + 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 trans + +import "time" + +// DomainBlock represents a domain block as serialized in an exported file. +type DomainBlock struct { + Type Type `json:"type" bun:"-"` + ID string `json:"id" bun:",nullzero"` + CreatedAt *time.Time `json:"createdAt" bun:",nullzero"` + Domain string `json:"domain" bun:",nullzero"` + CreatedByAccountID string `json:"createdByAccountID" bun:",nullzero"` + PrivateComment string `json:"privateComment,omitempty" bun:",nullzero"` + PublicComment string `json:"publicComment,omitempty" bun:",nullzero"` + Obfuscate bool `json:"obfuscate" bun:",nullzero"` + SubscriptionID string `json:"subscriptionID,omitempty" bun:",nullzero"` +} diff --git a/internal/trans/model/follow.go b/internal/trans/model/follow.go new file mode 100644 index 000000000..b94f2600d --- /dev/null +++ b/internal/trans/model/follow.go @@ -0,0 +1,31 @@ +/* + 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 trans + +import "time" + +// Follow represents an account follow as serialized in an export file. +type Follow struct { + Type Type `json:"type" bun:"-"` + ID string `json:"id" bun:",nullzero"` + CreatedAt *time.Time `json:"createdAt" bun:",nullzero"` + URI string `json:"uri" bun:",nullzero"` + AccountID string `json:"accountId" bun:",nullzero"` + TargetAccountID string `json:"targetAccountId" bun:",nullzero"` +} diff --git a/internal/trans/model/followrequest.go b/internal/trans/model/followrequest.go new file mode 100644 index 000000000..844bcb7af --- /dev/null +++ b/internal/trans/model/followrequest.go @@ -0,0 +1,31 @@ +/* + 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 trans + +import "time" + +// FollowRequest represents an account follow request as serialized in an export file. +type FollowRequest struct { + Type Type `json:"type" bun:"-"` + ID string `json:"id" bun:",nullzero"` + CreatedAt *time.Time `json:"createdAt" bun:",nullzero"` + URI string `json:"uri" bun:",nullzero"` + AccountID string `json:"accountId" bun:",nullzero"` + TargetAccountID string `json:"targetAccountId" bun:",nullzero"` +} diff --git a/internal/trans/model/instance.go b/internal/trans/model/instance.go new file mode 100644 index 000000000..a75aa65bf --- /dev/null +++ b/internal/trans/model/instance.go @@ -0,0 +1,43 @@ +/* + 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 trans + +import ( + "time" +) + +// Instance represents an instance entry as serialized in an export file. +type Instance struct { + Type Type `json:"type" bun:"-"` + ID string `json:"id" bun:",nullzero"` + CreatedAt *time.Time `json:"createdAt" bun:",nullzero"` + Domain string `json:"domain" bun:",nullzero"` + Title string `json:"title,omitempty" bun:",nullzero"` + URI string `json:"uri" bun:",nullzero"` + SuspendedAt *time.Time `json:"suspendedAt,omitempty" bun:",nullzero"` + DomainBlockID string `json:"domainBlockID,omitempty" bun:",nullzero"` + ShortDescription string `json:"shortDescription,omitempty" bun:",nullzero"` + Description string `json:"description,omitempty" bun:",nullzero"` + Terms string `json:"terms,omitempty" bun:",nullzero"` + ContactEmail string `json:"contactEmail,omitempty" bun:",nullzero"` + ContactAccountUsername string `json:"contactAccountUsername,omitempty" bun:",nullzero"` + ContactAccountID string `json:"contactAccountID,omitempty" bun:",nullzero"` + Reputation int64 `json:"reputation"` + Version string `json:"version,omitempty" bun:",nullzero"` +} diff --git a/internal/trans/model/type.go b/internal/trans/model/type.go new file mode 100644 index 000000000..76f57c843 --- /dev/null +++ b/internal/trans/model/type.go @@ -0,0 +1,41 @@ +/* + 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 trans + +// TypeKey should be set on a TransEntry to indicate the type of entry it is. +const TypeKey = "type" + +// Type describes the type of a trans entry, and how it should be read/serialized. +type Type string + +// Type of the trans entry. Describes how it should be read from file. +const ( + TransAccount Type = "account" + TransBlock Type = "block" + TransDomainBlock Type = "domainBlock" + TransEmailDomainBlock Type = "emailDomainBlock" + TransFollow Type = "follow" + TransFollowRequest Type = "followRequest" + TransInstance Type = "instance" + TransUser Type = "user" +) + +// Entry is used for deserializing trans entries into a rough interface so that +// the TypeKey can be fetched, before continuing with full parsing. +type Entry map[string]interface{} diff --git a/internal/trans/model/user.go b/internal/trans/model/user.go new file mode 100644 index 000000000..293b124a2 --- /dev/null +++ b/internal/trans/model/user.go @@ -0,0 +1,50 @@ +/* + 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 trans + +import ( + "time" +) + +// User represents a local instance user as serialized to an export file. +type User struct { + Type Type `json:"type" bun:"-"` + ID string `json:"id" bun:",nullzero"` + CreatedAt *time.Time `json:"createdAt" bun:",nullzero"` + Email string `json:"email,omitempty" bun:",nullzero"` + AccountID string `json:"accountID" bun:",nullzero"` + EncryptedPassword string `json:"encryptedPassword" bun:",nullzero"` + CurrentSignInAt *time.Time `json:"currentSignInAt,omitempty" bun:",nullzero"` + LastSignInAt *time.Time `json:"lastSignInAt,omitempty" bun:",nullzero"` + InviteID string `json:"inviteID,omitempty" bun:",nullzero"` + ChosenLanguages []string `json:"chosenLanguages,omitempty" bun:",nullzero"` + FilteredLanguages []string `json:"filteredLanguage,omitempty" bun:",nullzero"` + Locale string `json:"locale" bun:",nullzero"` + LastEmailedAt time.Time `json:"lastEmailedAt,omitempty" bun:",nullzero"` + ConfirmationToken string `json:"confirmationToken,omitempty" bun:",nullzero"` + ConfirmationSentAt *time.Time `json:"confirmationTokenSentAt,omitempty" bun:",nullzero"` + ConfirmedAt *time.Time `json:"confirmedAt,omitempty" bun:",nullzero"` + UnconfirmedEmail string `json:"unconfirmedEmail,omitempty" bun:",nullzero"` + Moderator bool `json:"moderator"` + Admin bool `json:"admin"` + Disabled bool `json:"disabled"` + Approved bool `json:"approved"` + ResetPasswordToken string `json:"resetPasswordToken,omitempty" bun:",nullzero"` + ResetPasswordSentAt *time.Time `json:"resetPasswordSentAt,omitempty" bun:",nullzero"` +} diff --git a/internal/trans/trans_test.go b/internal/trans/trans_test.go new file mode 100644 index 000000000..4a231486b --- /dev/null +++ b/internal/trans/trans_test.go @@ -0,0 +1,42 @@ +/* + 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 trans_test + +import ( + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/internal/db" + "github.com/superseriousbusiness/gotosocial/testrig" +) + +type TransTestSuite struct { + suite.Suite + db db.DB + log *logrus.Logger +} + +func (suite *TransTestSuite) SetupTest() { + suite.db = testrig.NewTestDB() + suite.log = testrig.NewTestLog() + testrig.StandardDBSetup(suite.db, nil) +} + +func (suite *TransTestSuite) TearDownTest() { + testrig.StandardDBTeardown(suite.db) +} diff --git a/internal/trans/util.go b/internal/trans/util.go new file mode 100644 index 000000000..4ccc1a4b6 --- /dev/null +++ b/internal/trans/util.go @@ -0,0 +1,32 @@ +/* + 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 trans + +import ( + "fmt" + "os" +) + +func neatClose(f *os.File) error { + if err := f.Close(); err != nil { + return fmt.Errorf("error closing file: %s", err) + } + + return nil +} |