From e8f1c33b48ac8affd51022c9f60514fe30de2c34 Mon Sep 17 00:00:00 2001 From: tsmethurst Date: Tue, 6 Jul 2021 13:16:30 +0200 Subject: [PATCH] unblock domains properly --- internal/db/db.go | 3 + internal/db/pg/delete.go | 57 +++++++++ internal/db/pg/get.go | 75 ++++++++++++ internal/db/pg/instance.go | 18 +++ internal/db/pg/pg.go | 113 ------------------ internal/db/pg/put.go | 33 +++++ internal/db/pg/statuscontext.go | 18 +++ internal/db/pg/update.go | 73 +++++++++++ internal/gtsmodel/account.go | 6 +- internal/processing/account/account.go | 3 +- internal/processing/account/delete.go | 9 +- internal/processing/admin.go | 2 +- .../processing/admin/createdomainblock.go | 3 +- .../processing/admin/deletedomainblock.go | 15 +++ internal/processing/fromclientapi.go | 18 +-- 15 files changed, 316 insertions(+), 130 deletions(-) create mode 100644 internal/db/pg/delete.go create mode 100644 internal/db/pg/get.go create mode 100644 internal/db/pg/put.go create mode 100644 internal/db/pg/update.go diff --git a/internal/db/db.go b/internal/db/db.go index 1ec02d22c..1d7ed8b58 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -86,6 +86,9 @@ type DB interface { // UpdateOneByID updates interface i with database the given database id. It will update one field of key key and value value. UpdateOneByID(id string, key string, value interface{}, i interface{}) error + // UpdateWhere updates column key of interface i with the given value, where the given parameters apply. + UpdateWhere(where []Where, key string, value interface{}, i interface{}) error + // DeleteByID removes i with id id. // If i didn't exist anyway, then no error should be returned. DeleteByID(id string, i interface{}) error diff --git a/internal/db/pg/delete.go b/internal/db/pg/delete.go new file mode 100644 index 000000000..0f288353e --- /dev/null +++ b/internal/db/pg/delete.go @@ -0,0 +1,57 @@ +/* + 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 . +*/ + +package pg + +import ( + "errors" + + "github.com/go-pg/pg/v10" + "github.com/superseriousbusiness/gotosocial/internal/db" +) + +func (ps *postgresService) DeleteByID(id string, i interface{}) error { + if _, err := ps.conn.Model(i).Where("id = ?", id).Delete(); err != nil { + // if there are no rows *anyway* then that's fine + // just return err if there's an actual error + if err != pg.ErrNoRows { + return err + } + } + return nil +} + +func (ps *postgresService) DeleteWhere(where []db.Where, i interface{}) error { + if len(where) == 0 { + return errors.New("no queries provided") + } + + q := ps.conn.Model(i) + for _, w := range where { + q = q.Where("? = ?", pg.Safe(w.Key), w.Value) + } + + if _, err := q.Delete(); err != nil { + // if there are no rows *anyway* then that's fine + // just return err if there's an actual error + if err != pg.ErrNoRows { + return err + } + } + return nil +} diff --git a/internal/db/pg/get.go b/internal/db/pg/get.go new file mode 100644 index 000000000..d48c43520 --- /dev/null +++ b/internal/db/pg/get.go @@ -0,0 +1,75 @@ +/* + 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 . +*/ + +package pg + +import ( + "errors" + + "github.com/go-pg/pg/v10" + "github.com/superseriousbusiness/gotosocial/internal/db" +) + +func (ps *postgresService) GetByID(id string, i interface{}) error { + if err := ps.conn.Model(i).Where("id = ?", id).Select(); err != nil { + if err == pg.ErrNoRows { + return db.ErrNoEntries{} + } + return err + + } + return nil +} + +func (ps *postgresService) GetWhere(where []db.Where, i interface{}) error { + if len(where) == 0 { + return errors.New("no queries provided") + } + + q := ps.conn.Model(i) + for _, w := range where { + + if w.Value == nil { + q = q.Where("? IS NULL", pg.Ident(w.Key)) + } else { + if w.CaseInsensitive { + q = q.Where("LOWER(?) = LOWER(?)", pg.Safe(w.Key), w.Value) + } else { + q = q.Where("? = ?", pg.Safe(w.Key), w.Value) + } + } + } + + if err := q.Select(); err != nil { + if err == pg.ErrNoRows { + return db.ErrNoEntries{} + } + return err + } + return nil +} + +func (ps *postgresService) GetAll(i interface{}) error { + if err := ps.conn.Model(i).Select(); err != nil { + if err == pg.ErrNoRows { + return db.ErrNoEntries{} + } + return err + } + return nil +} diff --git a/internal/db/pg/instance.go b/internal/db/pg/instance.go index 208268ac6..c551b2a49 100644 --- a/internal/db/pg/instance.go +++ b/internal/db/pg/instance.go @@ -1,3 +1,21 @@ +/* + 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 . +*/ + package pg import ( diff --git a/internal/db/pg/pg.go b/internal/db/pg/pg.go index 1050f141e..614968e22 100644 --- a/internal/db/pg/pg.go +++ b/internal/db/pg/pg.go @@ -197,119 +197,6 @@ func (ps *postgresService) CreateSchema(ctx context.Context) error { return nil } -func (ps *postgresService) GetByID(id string, i interface{}) error { - if err := ps.conn.Model(i).Where("id = ?", id).Select(); err != nil { - if err == pg.ErrNoRows { - return db.ErrNoEntries{} - } - return err - - } - return nil -} - -func (ps *postgresService) GetWhere(where []db.Where, i interface{}) error { - if len(where) == 0 { - return errors.New("no queries provided") - } - - q := ps.conn.Model(i) - for _, w := range where { - - if w.Value == nil { - q = q.Where("? IS NULL", pg.Ident(w.Key)) - } else { - if w.CaseInsensitive { - q = q.Where("LOWER(?) = LOWER(?)", pg.Safe(w.Key), w.Value) - } else { - q = q.Where("? = ?", pg.Safe(w.Key), w.Value) - } - } - } - - if err := q.Select(); err != nil { - if err == pg.ErrNoRows { - return db.ErrNoEntries{} - } - return err - } - return nil -} - -func (ps *postgresService) GetAll(i interface{}) error { - if err := ps.conn.Model(i).Select(); err != nil { - if err == pg.ErrNoRows { - return db.ErrNoEntries{} - } - return err - } - return nil -} - -func (ps *postgresService) Put(i interface{}) error { - _, err := ps.conn.Model(i).Insert(i) - if err != nil && strings.Contains(err.Error(), "duplicate key value violates unique constraint") { - return db.ErrAlreadyExists{} - } - return err -} - -func (ps *postgresService) Upsert(i interface{}, conflictColumn string) error { - if _, err := ps.conn.Model(i).OnConflict(fmt.Sprintf("(%s) DO UPDATE", conflictColumn)).Insert(); err != nil { - if err == pg.ErrNoRows { - return db.ErrNoEntries{} - } - return err - } - return nil -} - -func (ps *postgresService) UpdateByID(id string, i interface{}) error { - if _, err := ps.conn.Model(i).Where("id = ?", id).OnConflict("(id) DO UPDATE").Insert(); err != nil { - if err == pg.ErrNoRows { - return db.ErrNoEntries{} - } - return err - } - return nil -} - -func (ps *postgresService) UpdateOneByID(id string, key string, value interface{}, i interface{}) error { - _, err := ps.conn.Model(i).Set("? = ?", pg.Safe(key), value).Where("id = ?", id).Update() - return err -} - -func (ps *postgresService) DeleteByID(id string, i interface{}) error { - if _, err := ps.conn.Model(i).Where("id = ?", id).Delete(); err != nil { - // if there are no rows *anyway* then that's fine - // just return err if there's an actual error - if err != pg.ErrNoRows { - return err - } - } - return nil -} - -func (ps *postgresService) DeleteWhere(where []db.Where, i interface{}) error { - if len(where) == 0 { - return errors.New("no queries provided") - } - - q := ps.conn.Model(i) - for _, w := range where { - q = q.Where("? = ?", pg.Safe(w.Key), w.Value) - } - - if _, err := q.Delete(); err != nil { - // if there are no rows *anyway* then that's fine - // just return err if there's an actual error - if err != pg.ErrNoRows { - return err - } - } - return nil -} - /* HANDY SHORTCUTS */ diff --git a/internal/db/pg/put.go b/internal/db/pg/put.go new file mode 100644 index 000000000..09beca14b --- /dev/null +++ b/internal/db/pg/put.go @@ -0,0 +1,33 @@ +/* + 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 . +*/ + +package pg + +import ( + "strings" + + "github.com/superseriousbusiness/gotosocial/internal/db" +) + +func (ps *postgresService) Put(i interface{}) error { + _, err := ps.conn.Model(i).Insert(i) + if err != nil && strings.Contains(err.Error(), "duplicate key value violates unique constraint") { + return db.ErrAlreadyExists{} + } + return err +} diff --git a/internal/db/pg/statuscontext.go b/internal/db/pg/statuscontext.go index e907a2d6f..732485ab5 100644 --- a/internal/db/pg/statuscontext.go +++ b/internal/db/pg/statuscontext.go @@ -1,3 +1,21 @@ +/* + 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 . +*/ + package pg import ( diff --git a/internal/db/pg/update.go b/internal/db/pg/update.go new file mode 100644 index 000000000..f6bc70ad9 --- /dev/null +++ b/internal/db/pg/update.go @@ -0,0 +1,73 @@ +/* + 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 . +*/ + +package pg + +import ( + "fmt" + + "github.com/go-pg/pg/v10" + "github.com/superseriousbusiness/gotosocial/internal/db" +) + +func (ps *postgresService) Upsert(i interface{}, conflictColumn string) error { + if _, err := ps.conn.Model(i).OnConflict(fmt.Sprintf("(%s) DO UPDATE", conflictColumn)).Insert(); err != nil { + if err == pg.ErrNoRows { + return db.ErrNoEntries{} + } + return err + } + return nil +} + +func (ps *postgresService) UpdateByID(id string, i interface{}) error { + if _, err := ps.conn.Model(i).Where("id = ?", id).OnConflict("(id) DO UPDATE").Insert(); err != nil { + if err == pg.ErrNoRows { + return db.ErrNoEntries{} + } + return err + } + return nil +} + +func (ps *postgresService) UpdateOneByID(id string, key string, value interface{}, i interface{}) error { + _, err := ps.conn.Model(i).Set("? = ?", pg.Safe(key), value).Where("id = ?", id).Update() + return err +} + +func (ps *postgresService) UpdateWhere(where []db.Where, key string, value interface{}, i interface{}) error { + q := ps.conn.Model(i) + + for _, w := range where { + if w.Value == nil { + q = q.Where("? IS NULL", pg.Ident(w.Key)) + } else { + if w.CaseInsensitive { + q = q.Where("LOWER(?) = LOWER(?)", pg.Safe(w.Key), w.Value) + } else { + q = q.Where("? = ?", pg.Safe(w.Key), w.Value) + } + } + } + + q = q.Set("? = ?", pg.Safe(key), value) + + _, err := q.Update() + + return err +} diff --git a/internal/gtsmodel/account.go b/internal/gtsmodel/account.go index cf1bcec0a..e560601b8 100644 --- a/internal/gtsmodel/account.go +++ b/internal/gtsmodel/account.go @@ -115,7 +115,7 @@ type Account struct { CRYPTO FIELDS */ - // Privatekey for validating activitypub requests, will obviously only be defined for local accounts + // Privatekey for validating activitypub requests, will only be defined for local accounts PrivateKey *rsa.PrivateKey // Publickey for encoding activitypub requests, will be defined for both local and remote accounts PublicKey *rsa.PublicKey @@ -134,8 +134,8 @@ type Account struct { SuspendedAt time.Time `pg:"type:timestamp"` // Should we hide this account's collections? HideCollections bool - // id of the user that suspended this account through an admin action - SuspensionOrigin string + // id of the database entry that caused this account to become suspended -- can be an account ID or a domain block ID + SuspensionOrigin string `pg:"type:CHAR(26)"` } // Field represents a key value field on an account, for things like pronouns, website, etc. diff --git a/internal/processing/account/account.go b/internal/processing/account/account.go index 442b6fa9f..efdac5d3e 100644 --- a/internal/processing/account/account.go +++ b/internal/processing/account/account.go @@ -40,7 +40,8 @@ type Processor interface { // Create processes the given form for creating a new account, returning an oauth token for that account if successful. Create(applicationToken oauth2.TokenInfo, application *gtsmodel.Application, form *apimodel.AccountCreateRequest) (*apimodel.Token, error) // Delete deletes an account, and all of that account's statuses, media, follows, notifications, etc etc etc. - Delete(account *gtsmodel.Account, deletedBy string) error + // The origin passed here should be either the ID of the account doing the delete (can be itself), or the ID of a domain block. + Delete(account *gtsmodel.Account, origin string) error // Get processes the given request for account information. Get(requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Account, error) // Update processes the update of an account with the given form diff --git a/internal/processing/account/delete.go b/internal/processing/account/delete.go index c31b67352..65ac02291 100644 --- a/internal/processing/account/delete.go +++ b/internal/processing/account/delete.go @@ -48,7 +48,7 @@ import ( // 16. Delete account's user // 17. Delete account's timeline // 18. Delete account itself -func (p *processor) Delete(account *gtsmodel.Account, deletedBy string) error { +func (p *processor) Delete(account *gtsmodel.Account, origin string) error { l := p.log.WithFields(logrus.Fields{ "func": "Delete", "username": account.Username, @@ -100,6 +100,7 @@ func (p *processor) Delete(account *gtsmodel.Account, deletedBy string) error { // nothing to do here // 4. Delete account's follow requests + // TODO: federate these if necessary l.Debug("deleting account follow requests") // first delete any follow requests that this account created if err := p.db.DeleteWhere([]db.Where{{Key: "account_id", Value: account.ID}}, &[]*gtsmodel.FollowRequest{}); err != nil { @@ -112,6 +113,7 @@ func (p *processor) Delete(account *gtsmodel.Account, deletedBy string) error { } // 5. Delete account's follows + // TODO: federate these if necessary l.Debug("deleting account follows") // first delete any follows that this account created if err := p.db.DeleteWhere([]db.Where{{Key: "account_id", Value: account.ID}}, &[]*gtsmodel.Follow{}); err != nil { @@ -217,6 +219,7 @@ selectStatusesLoop: } // 12. Delete account's faves + // TODO: federate these if necessary l.Debug("deleting account faves") if err := p.db.DeleteWhere([]db.Where{{Key: "account_id", Value: account.ID}}, &[]*gtsmodel.StatusFave{}); err != nil { l.Errorf("error deleting faves created by account: %s", err) @@ -229,6 +232,7 @@ selectStatusesLoop: } // 14. Delete account's streams + // TODO // 15. Delete account's tags // TODO @@ -240,6 +244,7 @@ selectStatusesLoop: } // 17. Delete account's timeline + // TODO // 18. Delete account itself // to prevent the account being created again, set all these fields and update it in the db @@ -259,7 +264,7 @@ selectStatusesLoop: account.UpdatedAt = time.Now() account.SuspendedAt = time.Now() - account.SuspensionOrigin = deletedBy + account.SuspensionOrigin = origin if err := p.db.UpdateByID(account.ID, account); err != nil { return err diff --git a/internal/processing/admin.go b/internal/processing/admin.go index f4741a1b8..9a38f5ec1 100644 --- a/internal/processing/admin.go +++ b/internal/processing/admin.go @@ -33,7 +33,7 @@ func (p *processor) AdminDomainBlockCreate(authed *oauth.Auth, form *apimodel.Do } func (p *processor) AdminDomainBlocksImport(authed *oauth.Auth, form *apimodel.DomainBlockCreateRequest) ([]*apimodel.DomainBlock, gtserror.WithCode) { - return p.adminProcessor.DomainBlocksImport(authed.Account, form.Domains) + return p.adminProcessor.DomainBlocksImport(authed.Account, form.Domains) } func (p *processor) AdminDomainBlocksGet(authed *oauth.Auth, export bool) ([]*apimodel.DomainBlock, gtserror.WithCode) { diff --git a/internal/processing/admin/createdomainblock.go b/internal/processing/admin/createdomainblock.go index 926ddf355..a9c7094e6 100644 --- a/internal/processing/admin/createdomainblock.go +++ b/internal/processing/admin/createdomainblock.go @@ -54,6 +54,7 @@ func (p *processor) DomainBlockCreate(account *gtsmodel.Account, domain string, PrivateComment: privateComment, PublicComment: publicComment, Obfuscate: obfuscate, + SubscriptionID: subscriptionID, } // put the new block in the database @@ -140,7 +141,7 @@ selectAccountsLoop: p.fromClientAPI <- gtsmodel.FromClientAPI{ APObjectType: gtsmodel.ActivityStreamsPerson, APActivityType: gtsmodel.ActivityStreamsDelete, - GTSModel: a, + GTSModel: block, OriginAccount: account, TargetAccount: a, } diff --git a/internal/processing/admin/deletedomainblock.go b/internal/processing/admin/deletedomainblock.go index 3eaf3368f..b41fedd92 100644 --- a/internal/processing/admin/deletedomainblock.go +++ b/internal/processing/admin/deletedomainblock.go @@ -64,5 +64,20 @@ func (p *processor) DomainBlockDelete(account *gtsmodel.Account, id string) (*ap } } + // unsuspend all accounts whose suspension origin was this domain block + // 1. remove the 'suspended_at' entry from their accounts + if err := p.db.UpdateWhere([]db.Where{ + {Key: "suspension_origin", Value: domainBlock.ID}, + }, "suspended_at", nil, &[]*gtsmodel.Account{}); err != nil { + return nil, gtserror.NewErrorInternalError(fmt.Errorf("database error removing suspended_at from accounts: %s", err)) + } + + // 2. remove the 'suspension_origin' entry from their accounts + if err := p.db.UpdateWhere([]db.Where{ + {Key: "suspension_origin", Value: domainBlock.ID}, + }, "suspension_origin", nil, &[]*gtsmodel.Account{}); err != nil { + return nil, gtserror.NewErrorInternalError(fmt.Errorf("database error removing suspension_origin from accounts: %s", err)) + } + return mastoDomainBlock, nil } diff --git a/internal/processing/fromclientapi.go b/internal/processing/fromclientapi.go index 3cb21569a..cfed3b5e4 100644 --- a/internal/processing/fromclientapi.go +++ b/internal/processing/fromclientapi.go @@ -193,17 +193,17 @@ func (p *processor) processFromClientAPI(clientMsg gtsmodel.FromClientAPI) error return p.federateStatusDelete(statusToDelete) case gtsmodel.ActivityStreamsProfile, gtsmodel.ActivityStreamsPerson: // DELETE ACCOUNT/PROFILE - accountToDelete, ok := clientMsg.GTSModel.(*gtsmodel.Account) - if !ok { - return errors.New("account was not parseable as *gtsmodel.Account") - } - var deletedBy string - if clientMsg.OriginAccount != nil { - deletedBy = clientMsg.OriginAccount.ID + // the origin of the delete could be either a domain block, or an action by another (or this) account + var origin string + if domainBlock, ok := clientMsg.GTSModel.(*gtsmodel.DomainBlock); ok { + // origin is a domain block + origin = domainBlock.ID + } else { + // origin is whichever account caused this message + origin = clientMsg.OriginAccount.ID } - - return p.accountProcessor.Delete(accountToDelete, deletedBy) + return p.accountProcessor.Delete(clientMsg.TargetAccount, origin) } } return nil