2021-07-05 12:23:03 +01:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2023-01-05 11:43:00 +00:00
|
|
|
Copyright (C) 2021-2023 GoToSocial Authors admin@gotosocial.org
|
2021-07-05 12:23:03 +01:00
|
|
|
|
|
|
|
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 account
|
|
|
|
|
|
|
|
import (
|
2021-08-25 14:34:33 +01:00
|
|
|
"context"
|
2022-03-15 15:12:35 +00:00
|
|
|
"errors"
|
2021-07-05 12:23:03 +01:00
|
|
|
"time"
|
|
|
|
|
2022-07-19 09:47:55 +01:00
|
|
|
"codeberg.org/gruf/go-kv"
|
2021-08-31 14:59:12 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
2022-03-15 15:12:35 +00:00
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
2021-07-05 12:23:03 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2022-03-15 15:12:35 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2021-07-05 12:23:03 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2022-07-19 09:47:55 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2021-08-31 14:59:12 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/messages"
|
2022-03-15 15:12:35 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2021-07-05 12:23:03 +01:00
|
|
|
)
|
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
// Delete deletes an account, and all of that account's statuses, media, follows, notifications, etc etc etc.
|
|
|
|
// 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.
|
|
|
|
func (p *Processor) Delete(ctx context.Context, account *gtsmodel.Account, origin string) gtserror.WithCode {
|
2022-11-20 14:57:19 +00:00
|
|
|
fields := kv.Fields{{"username", account.Username}}
|
|
|
|
|
2021-09-30 09:56:02 +01:00
|
|
|
if account.Domain != "" {
|
2022-07-19 09:47:55 +01:00
|
|
|
fields = append(fields, kv.Field{
|
|
|
|
"domain", account.Domain,
|
|
|
|
})
|
2021-09-30 09:56:02 +01:00
|
|
|
}
|
2021-07-05 12:23:03 +01:00
|
|
|
|
2023-02-17 11:02:29 +00:00
|
|
|
l := log.WithContext(ctx).WithFields(fields...)
|
2022-11-20 14:57:19 +00:00
|
|
|
l.Trace("beginning account delete process")
|
2021-07-05 12:23:03 +01:00
|
|
|
|
|
|
|
// 1. Delete account's application(s), clients, and oauth tokens
|
|
|
|
// we only need to do this step for local account since remote ones won't have any tokens or applications on our server
|
2022-10-03 09:46:11 +01:00
|
|
|
var user *gtsmodel.User
|
2021-07-05 12:23:03 +01:00
|
|
|
if account.Domain == "" {
|
|
|
|
// see if we can get a user for this account
|
2022-10-03 09:46:11 +01:00
|
|
|
var err error
|
|
|
|
if user, err = p.db.GetUserByAccountID(ctx, account.ID); err == nil {
|
2021-07-05 12:23:03 +01:00
|
|
|
// we got one! select all tokens with the user's ID
|
2021-09-01 10:45:01 +01:00
|
|
|
tokens := []*gtsmodel.Token{}
|
2022-10-03 09:46:11 +01:00
|
|
|
if err := p.db.GetWhere(ctx, []db.Where{{Key: "user_id", Value: user.ID}}, &tokens); err == nil {
|
2021-07-05 12:23:03 +01:00
|
|
|
// we have some tokens to delete
|
|
|
|
for _, t := range tokens {
|
|
|
|
// delete client(s) associated with this token
|
2021-09-01 10:45:01 +01:00
|
|
|
if err := p.db.DeleteByID(ctx, t.ClientID, >smodel.Client{}); err != nil {
|
2021-07-05 12:23:03 +01:00
|
|
|
l.Errorf("error deleting oauth client: %s", err)
|
|
|
|
}
|
|
|
|
// delete application(s) associated with this token
|
2021-08-25 14:34:33 +01:00
|
|
|
if err := p.db.DeleteWhere(ctx, []db.Where{{Key: "client_id", Value: t.ClientID}}, >smodel.Application{}); err != nil {
|
2021-07-05 12:23:03 +01:00
|
|
|
l.Errorf("error deleting application: %s", err)
|
|
|
|
}
|
|
|
|
// delete the token itself
|
2021-08-25 14:34:33 +01:00
|
|
|
if err := p.db.DeleteByID(ctx, t.ID, t); err != nil {
|
2021-07-05 12:23:03 +01:00
|
|
|
l.Errorf("error deleting oauth token: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Delete account's blocks
|
2022-11-20 14:57:19 +00:00
|
|
|
l.Trace("deleting account blocks")
|
2021-07-05 12:23:03 +01:00
|
|
|
// first delete any blocks that this account created
|
2022-11-20 16:33:49 +00:00
|
|
|
if err := p.db.DeleteBlocksByOriginAccountID(ctx, account.ID); err != nil {
|
2021-07-05 12:23:03 +01:00
|
|
|
l.Errorf("error deleting blocks created by account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// now delete any blocks that target this account
|
2022-11-20 16:33:49 +00:00
|
|
|
if err := p.db.DeleteBlocksByTargetAccountID(ctx, account.ID); err != nil {
|
2021-07-05 12:23:03 +01:00
|
|
|
l.Errorf("error deleting blocks targeting account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. Delete account's emoji
|
|
|
|
// nothing to do here
|
|
|
|
|
|
|
|
// 4. Delete account's follow requests
|
2021-07-06 12:29:11 +01:00
|
|
|
// TODO: federate these if necessary
|
2022-11-20 14:57:19 +00:00
|
|
|
l.Trace("deleting account follow requests")
|
2021-07-05 12:23:03 +01:00
|
|
|
// first delete any follow requests that this account created
|
2021-08-25 14:34:33 +01:00
|
|
|
if err := p.db.DeleteWhere(ctx, []db.Where{{Key: "account_id", Value: account.ID}}, &[]*gtsmodel.FollowRequest{}); err != nil {
|
2021-07-05 12:23:03 +01:00
|
|
|
l.Errorf("error deleting follow requests created by account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// now delete any follow requests that target this account
|
2021-08-25 14:34:33 +01:00
|
|
|
if err := p.db.DeleteWhere(ctx, []db.Where{{Key: "target_account_id", Value: account.ID}}, &[]*gtsmodel.FollowRequest{}); err != nil {
|
2021-07-05 12:23:03 +01:00
|
|
|
l.Errorf("error deleting follow requests targeting account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5. Delete account's follows
|
2021-07-06 12:29:11 +01:00
|
|
|
// TODO: federate these if necessary
|
2022-11-20 14:57:19 +00:00
|
|
|
l.Trace("deleting account follows")
|
2021-07-05 12:23:03 +01:00
|
|
|
// first delete any follows that this account created
|
2021-08-25 14:34:33 +01:00
|
|
|
if err := p.db.DeleteWhere(ctx, []db.Where{{Key: "account_id", Value: account.ID}}, &[]*gtsmodel.Follow{}); err != nil {
|
2021-07-05 12:23:03 +01:00
|
|
|
l.Errorf("error deleting follows created by account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// now delete any follows that target this account
|
2021-08-25 14:34:33 +01:00
|
|
|
if err := p.db.DeleteWhere(ctx, []db.Where{{Key: "target_account_id", Value: account.ID}}, &[]*gtsmodel.Follow{}); err != nil {
|
2021-07-05 12:23:03 +01:00
|
|
|
l.Errorf("error deleting follows targeting account: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-11-20 14:57:19 +00:00
|
|
|
var maxID string
|
|
|
|
|
2021-07-05 12:23:03 +01:00
|
|
|
// 6. Delete account's statuses
|
2022-11-20 14:57:19 +00:00
|
|
|
l.Trace("deleting account statuses")
|
|
|
|
|
2021-07-05 12:23:03 +01:00
|
|
|
// we'll select statuses 20 at a time so we don't wreck the db, and pass them through to the client api channel
|
|
|
|
// Deleting the statuses in this way also handles 7. Delete account's media attachments, 8. Delete account's mentions, and 9. Delete account's polls,
|
|
|
|
// since these are all attached to statuses.
|
2022-11-20 14:57:19 +00:00
|
|
|
|
2021-07-05 12:23:03 +01:00
|
|
|
for {
|
2022-11-20 14:57:19 +00:00
|
|
|
// Fetch next block of account statuses from database
|
2023-02-25 12:16:30 +00:00
|
|
|
statuses, err := p.db.GetAccountStatuses(ctx, account.ID, 20, false, false, maxID, "", false, false)
|
2021-07-05 12:23:03 +01:00
|
|
|
if err != nil {
|
2022-11-20 14:57:19 +00:00
|
|
|
if !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
// an actual error has occurred
|
|
|
|
l.Errorf("Delete: db error selecting statuses for account %s: %s", account.Username, err)
|
2021-07-05 12:23:03 +01:00
|
|
|
}
|
2022-11-20 14:57:19 +00:00
|
|
|
break
|
2021-07-05 12:23:03 +01:00
|
|
|
}
|
|
|
|
|
2023-01-08 10:33:03 +00:00
|
|
|
if len(statuses) == 0 {
|
|
|
|
break // reached end
|
|
|
|
}
|
|
|
|
|
2022-11-20 14:57:19 +00:00
|
|
|
for _, status := range statuses {
|
|
|
|
// Ensure account is set
|
|
|
|
status.Account = account
|
2022-07-10 16:18:21 +01:00
|
|
|
|
2022-11-20 14:57:19 +00:00
|
|
|
l.Tracef("queue client API status delete: %s", status.ID)
|
|
|
|
|
|
|
|
// pass the status delete through the client api channel for processing
|
2022-04-28 13:23:11 +01:00
|
|
|
p.clientWorker.Queue(messages.FromClientAPI{
|
2021-08-31 14:59:12 +01:00
|
|
|
APObjectType: ap.ObjectNote,
|
|
|
|
APActivityType: ap.ActivityDelete,
|
2022-11-20 14:57:19 +00:00
|
|
|
GTSModel: status,
|
2021-07-05 12:23:03 +01:00
|
|
|
OriginAccount: account,
|
|
|
|
TargetAccount: account,
|
2022-04-28 13:23:11 +01:00
|
|
|
})
|
2021-07-05 12:23:03 +01:00
|
|
|
|
2022-11-20 14:57:19 +00:00
|
|
|
// Look for any boosts of this status in DB
|
|
|
|
boosts, err := p.db.GetStatusReblogs(ctx, status)
|
|
|
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
l.Errorf("error fetching status reblogs for %q: %v", status.ID, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, boost := range boosts {
|
|
|
|
if boost.Account == nil {
|
|
|
|
// Fetch the relevant account for this status boost
|
|
|
|
boostAcc, err := p.db.GetAccountByID(ctx, boost.AccountID)
|
|
|
|
if err != nil {
|
|
|
|
l.Errorf("error fetching boosted status account for %q: %v", boost.AccountID, err)
|
|
|
|
continue
|
2021-07-05 12:23:03 +01:00
|
|
|
}
|
2022-07-10 16:18:21 +01:00
|
|
|
|
2022-11-20 14:57:19 +00:00
|
|
|
// Set account model
|
|
|
|
boost.Account = boostAcc
|
2021-07-05 12:23:03 +01:00
|
|
|
}
|
|
|
|
|
2022-11-20 14:57:19 +00:00
|
|
|
l.Tracef("queue client API boost delete: %s", status.ID)
|
|
|
|
|
|
|
|
// pass the boost delete through the client api channel for processing
|
|
|
|
p.clientWorker.Queue(messages.FromClientAPI{
|
|
|
|
APObjectType: ap.ActivityAnnounce,
|
|
|
|
APActivityType: ap.ActivityUndo,
|
|
|
|
GTSModel: status,
|
|
|
|
OriginAccount: boost.Account,
|
|
|
|
TargetAccount: account,
|
|
|
|
})
|
2021-07-05 12:23:03 +01:00
|
|
|
}
|
|
|
|
}
|
2022-11-20 14:57:19 +00:00
|
|
|
|
|
|
|
// Update next maxID from last status
|
|
|
|
maxID = statuses[len(statuses)-1].ID
|
2021-07-05 12:23:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 10. Delete account's notifications
|
2022-11-20 14:57:19 +00:00
|
|
|
l.Trace("deleting account notifications")
|
2021-09-30 09:56:02 +01:00
|
|
|
// first notifications created by account
|
2021-08-25 14:34:33 +01:00
|
|
|
if err := p.db.DeleteWhere(ctx, []db.Where{{Key: "origin_account_id", Value: account.ID}}, &[]*gtsmodel.Notification{}); err != nil {
|
2021-07-05 12:23:03 +01:00
|
|
|
l.Errorf("error deleting notifications created by account: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-09-30 09:56:02 +01:00
|
|
|
// now notifications targeting account
|
|
|
|
if err := p.db.DeleteWhere(ctx, []db.Where{{Key: "target_account_id", Value: account.ID}}, &[]*gtsmodel.Notification{}); err != nil {
|
|
|
|
l.Errorf("error deleting notifications targeting account: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-07-05 12:23:03 +01:00
|
|
|
// 11. Delete account's bookmarks
|
2022-11-20 14:57:19 +00:00
|
|
|
l.Trace("deleting account bookmarks")
|
2021-08-25 14:34:33 +01:00
|
|
|
if err := p.db.DeleteWhere(ctx, []db.Where{{Key: "account_id", Value: account.ID}}, &[]*gtsmodel.StatusBookmark{}); err != nil {
|
2021-07-05 12:23:03 +01:00
|
|
|
l.Errorf("error deleting bookmarks created by account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 12. Delete account's faves
|
2021-07-06 12:29:11 +01:00
|
|
|
// TODO: federate these if necessary
|
2022-11-20 14:57:19 +00:00
|
|
|
l.Trace("deleting account faves")
|
2021-08-25 14:34:33 +01:00
|
|
|
if err := p.db.DeleteWhere(ctx, []db.Where{{Key: "account_id", Value: account.ID}}, &[]*gtsmodel.StatusFave{}); err != nil {
|
2021-07-05 12:23:03 +01:00
|
|
|
l.Errorf("error deleting faves created by account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 13. Delete account's mutes
|
2022-11-20 14:57:19 +00:00
|
|
|
l.Trace("deleting account mutes")
|
2021-08-25 14:34:33 +01:00
|
|
|
if err := p.db.DeleteWhere(ctx, []db.Where{{Key: "account_id", Value: account.ID}}, &[]*gtsmodel.StatusMute{}); err != nil {
|
2021-07-05 12:23:03 +01:00
|
|
|
l.Errorf("error deleting status mutes created by account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 14. Delete account's streams
|
2021-07-06 12:29:11 +01:00
|
|
|
// TODO
|
2021-07-05 12:23:03 +01:00
|
|
|
|
|
|
|
// 15. Delete account's tags
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
// 16. Delete account's user
|
2022-10-03 09:46:11 +01:00
|
|
|
if user != nil {
|
2022-11-20 14:57:19 +00:00
|
|
|
l.Trace("deleting account user")
|
2022-10-03 09:46:11 +01:00
|
|
|
if err := p.db.DeleteUserByID(ctx, user.ID); err != nil {
|
|
|
|
return gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
2021-07-05 12:23:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 17. Delete account's timeline
|
2021-07-06 12:29:11 +01:00
|
|
|
// TODO
|
2021-07-05 12:23:03 +01:00
|
|
|
|
|
|
|
// 18. Delete account itself
|
|
|
|
// to prevent the account being created again, set all these fields and update it in the db
|
|
|
|
// the account won't actually be *removed* from the database but it will be set to just a stub
|
|
|
|
account.Note = ""
|
|
|
|
account.DisplayName = ""
|
|
|
|
account.AvatarMediaAttachmentID = ""
|
|
|
|
account.AvatarRemoteURL = ""
|
|
|
|
account.HeaderMediaAttachmentID = ""
|
|
|
|
account.HeaderRemoteURL = ""
|
|
|
|
account.Reason = ""
|
2022-09-26 10:56:01 +01:00
|
|
|
account.Emojis = []*gtsmodel.Emoji{}
|
|
|
|
account.EmojiIDs = []string{}
|
2021-07-05 12:23:03 +01:00
|
|
|
account.Fields = []gtsmodel.Field{}
|
2022-08-15 11:35:05 +01:00
|
|
|
hideCollections := true
|
|
|
|
account.HideCollections = &hideCollections
|
|
|
|
discoverable := false
|
|
|
|
account.Discoverable = &discoverable
|
2021-07-05 12:23:03 +01:00
|
|
|
account.SuspendedAt = time.Now()
|
2021-07-06 12:29:11 +01:00
|
|
|
account.SuspensionOrigin = origin
|
2022-11-15 18:45:15 +00:00
|
|
|
err := p.db.UpdateAccount(ctx, account)
|
2021-08-25 14:34:33 +01:00
|
|
|
if err != nil {
|
2022-03-15 15:12:35 +00:00
|
|
|
return gtserror.NewErrorInternalError(err)
|
2021-07-05 12:23:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
l.Infof("deleted account with username %s from domain %s", account.Username, account.Domain)
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-15 15:12:35 +00:00
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
// DeleteLocal is like Delete, but specifically for deletion of local accounts rather than federated ones.
|
|
|
|
// Unlike Delete, it will propagate the deletion out across the federating API to other instances.
|
|
|
|
func (p *Processor) DeleteLocal(ctx context.Context, account *gtsmodel.Account, form *apimodel.AccountDeleteRequest) gtserror.WithCode {
|
2022-03-15 15:12:35 +00:00
|
|
|
fromClientAPIMessage := messages.FromClientAPI{
|
|
|
|
APObjectType: ap.ActorPerson,
|
|
|
|
APActivityType: ap.ActivityDelete,
|
|
|
|
TargetAccount: account,
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.DeleteOriginID == account.ID {
|
|
|
|
// the account owner themself has requested deletion via the API, get their user from the db
|
2022-10-03 09:46:11 +01:00
|
|
|
user, err := p.db.GetUserByAccountID(ctx, account.ID)
|
|
|
|
if err != nil {
|
2022-03-15 15:12:35 +00:00
|
|
|
return gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// now check that the password they supplied is correct
|
|
|
|
// make sure a password is actually set and bail if not
|
|
|
|
if user.EncryptedPassword == "" {
|
|
|
|
return gtserror.NewErrorForbidden(errors.New("user password was not set"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// compare the provided password with the encrypted one from the db, bail if they don't match
|
|
|
|
if err := bcrypt.CompareHashAndPassword([]byte(user.EncryptedPassword), []byte(form.Password)); err != nil {
|
|
|
|
return gtserror.NewErrorForbidden(errors.New("invalid password"))
|
|
|
|
}
|
|
|
|
|
|
|
|
fromClientAPIMessage.OriginAccount = account
|
|
|
|
} else {
|
|
|
|
// the delete has been requested by some other account, grab it;
|
|
|
|
// if we've reached this point we know it has permission already
|
|
|
|
requestingAccount, err := p.db.GetAccountByID(ctx, form.DeleteOriginID)
|
|
|
|
if err != nil {
|
|
|
|
return gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fromClientAPIMessage.OriginAccount = requestingAccount
|
|
|
|
}
|
|
|
|
|
|
|
|
// put the delete in the processor queue to handle the rest of it asynchronously
|
2022-04-28 13:23:11 +01:00
|
|
|
p.clientWorker.Queue(fromClientAPIMessage)
|
2022-03-15 15:12:35 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|