Files
kim 7362d55a35 [feature] add deleted flag to the statuses table to track tombstones (#4716)
# Description

- replaces a bunch of boolean (and like) columns with a single "statuses.flags" column
- adds a potential deleted value for the new "statuses.flags" column
- replaces useless migration.Down functions with empty txs with empty functions

In a future PR this will update our status deletion logic to instead stub-out a particular status row instead of deleting it, where necessary, to maintain status threads. In the process of replacing a bunch of columns with a single bit field this should also free up some storage space on the statuses table! It also replaces a bunch of indices with partials which again should further reduce space! ~~This is yet to be tested, so we shall see :p~~

Just finished testing this on my own instance...
- previous database size: 24827580416
- after database size: 21751169024

So 3GB of savings from a 24GB database, that's pretty damn impressive!

## Checklist

- [x] I/we have read the [GoToSocial contribution guidelines](https://codeberg.org/superseriousbusiness/gotosocial/src/branch/main/CONTRIBUTING.md).
- [x] I/we have not used so-called 'AI' to create the proposed changes.
- [x] I/we have discussed the proposed changes already, either in an issue on the repository, or in the Matrix chat.
- [x] I/we have performed a self-review of added code.
- [x] I/we have written code that is legible and maintainable by others.
- [x] I/we have commented the added code, particularly in hard-to-understand areas.
- [ ] I/we have made any necessary changes to documentation.
- [ ] I/we have added tests that cover new code.
- [x] I/we have run tests and they pass locally with the changes.
- [x] I/we have run `go fmt ./...` and `golangci-lint run`.

Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4716
Co-authored-by: kim <grufwub@gmail.com>
Co-committed-by: kim <grufwub@gmail.com>
2026-03-10 10:15:19 +01:00

239 lines
7.1 KiB
Go

// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// 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 interactionrequests
import (
"context"
"time"
"code.superseriousbusiness.org/gotosocial/internal/ap"
apimodel "code.superseriousbusiness.org/gotosocial/internal/api/model"
"code.superseriousbusiness.org/gotosocial/internal/gtserror"
"code.superseriousbusiness.org/gotosocial/internal/gtsmodel"
"code.superseriousbusiness.org/gotosocial/internal/messages"
"code.superseriousbusiness.org/gotosocial/internal/uris"
"code.superseriousbusiness.org/gotosocial/internal/util"
)
// Accept accepts an interaction request with the given ID,
// on behalf of the given account (whose post it must target).
func (p *Processor) Accept(
ctx context.Context,
acct *gtsmodel.Account,
reqID string,
) (*apimodel.InteractionRequest, gtserror.WithCode) {
req, err := p.state.DB.GetInteractionRequestByID(ctx, reqID)
if err != nil {
err := gtserror.Newf("db error getting interaction request: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
if req.TargetAccountID != acct.ID {
err := gtserror.Newf(
"interaction request %s does not belong to account %s",
reqID, acct.ID,
)
return nil, gtserror.NewErrorNotFound(err)
}
if !req.IsPending() {
err := gtserror.Newf(
"interaction request %s has already been handled",
reqID,
)
return nil, gtserror.NewErrorNotFound(err)
}
// Lock on the interaction req URI to
// ensure nobody else is modifying it rn.
unlock := p.state.ProcessingLocks.Lock(req.InteractionURI)
defer unlock()
// Mark the request as accepted
// and generate URIs for it.
req.AcceptedAt = time.Now()
req.ResponseURI = uris.GenerateURIForAccept(acct.Username, req.ID)
req.AuthorizationURI = uris.GenerateURIForAuthorization(acct.Username, req.ID)
if err := p.state.DB.UpdateInteractionRequest(
ctx,
req,
"accepted_at",
"response_uri",
"authorization_uri",
); err != nil {
err := gtserror.Newf("db error updating interaction request: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
switch req.InteractionType {
case gtsmodel.InteractionLike:
if errWithCode := p.acceptLike(ctx, req); errWithCode != nil {
return nil, errWithCode
}
case gtsmodel.InteractionReply:
if errWithCode := p.acceptReply(ctx, req); errWithCode != nil {
return nil, errWithCode
}
case gtsmodel.InteractionAnnounce:
if errWithCode := p.acceptAnnounce(ctx, req); errWithCode != nil {
return nil, errWithCode
}
default:
err := gtserror.Newf("unknown interaction type for interaction request %s", reqID)
return nil, gtserror.NewErrorInternalError(err)
}
// Return the now-accepted req to the caller so
// they can do something with it if they need to.
apiReq, err := p.converter.InteractionReqToAPIInteractionReq(
ctx,
req,
acct,
)
if err != nil {
err := gtserror.Newf("error converting interaction request: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
return apiReq, nil
}
// Package-internal convenience
// function to accept a like.
func (p *Processor) acceptLike(
ctx context.Context,
req *gtsmodel.InteractionRequest,
) gtserror.WithCode {
// If the Like is missing, that means it's
// probably already been undone by someone,
// so there's nothing to actually accept.
if req.Like == nil {
err := gtserror.Newf("no Like found for interaction request %s", req.ID)
return gtserror.NewErrorNotFound(err)
}
// Update the Like.
req.Like.PendingApproval = util.Ptr(false)
req.Like.PreApproved = false
req.Like.ApprovedByURI = req.AuthorizationURI
if err := p.state.DB.UpdateStatusFave(ctx,
req.Like,
"pending_approval",
"approved_by_uri",
); err != nil {
err := gtserror.Newf("db error updating status fave: %w", err)
return gtserror.NewErrorInternalError(err)
}
// Send the accepted request off through the
// client API processor to handle side effects.
p.state.Workers.Client.Queue.Push(&messages.FromClientAPI{
APObjectType: ap.ActivityLike,
APActivityType: ap.ActivityAccept,
GTSModel: req,
Origin: req.TargetAccount,
Target: req.InteractingAccount,
})
return nil
}
// Package-internal convenience
// function to accept a reply.
func (p *Processor) acceptReply(
ctx context.Context,
req *gtsmodel.InteractionRequest,
) gtserror.WithCode {
// If the Reply is missing, that means it's
// probably already been undone by someone,
// so there's nothing to actually accept.
if req.Reply == nil {
err := gtserror.Newf("no Reply found for interaction request %s", req.ID)
return gtserror.NewErrorNotFound(err)
}
// Update the Reply.
req.Reply.PreApproved = false
req.Reply.Flags.SetPendingApproval(false)
req.Reply.ApprovedByURI = req.AuthorizationURI
if err := p.state.DB.UpdateStatus(ctx,
req.Reply,
"flags",
"approved_by_uri",
); err != nil {
err := gtserror.Newf("db error updating status reply: %w", err)
return gtserror.NewErrorInternalError(err)
}
// Send the accepted request off through the
// client API processor to handle side effects.
p.state.Workers.Client.Queue.Push(&messages.FromClientAPI{
APObjectType: ap.ObjectNote,
APActivityType: ap.ActivityAccept,
GTSModel: req,
Origin: req.TargetAccount,
Target: req.InteractingAccount,
})
return nil
}
// Package-internal convenience
// function to accept an announce.
func (p *Processor) acceptAnnounce(
ctx context.Context,
req *gtsmodel.InteractionRequest,
) gtserror.WithCode {
// If the Announce is missing, that means it's
// probably already been undone by someone,
// so there's nothing to actually accept.
if req.Reply == nil {
err := gtserror.Newf("no Announce found for interaction request %s", req.ID)
return gtserror.NewErrorNotFound(err)
}
// Update the Announce.
req.Announce.PreApproved = false
req.Announce.Flags.SetPendingApproval(false)
req.Announce.ApprovedByURI = req.AuthorizationURI
if err := p.state.DB.UpdateStatus(ctx,
req.Announce,
"flags",
"approved_by_uri",
); err != nil {
err := gtserror.Newf("db error updating status announce: %w", err)
return gtserror.NewErrorInternalError(err)
}
// Send the accepted request off through the
// client API processor to handle side effects.
p.state.Workers.Client.Queue.Push(&messages.FromClientAPI{
APObjectType: ap.ActivityAnnounce,
APActivityType: ap.ActivityAccept,
GTSModel: req,
Origin: req.TargetAccount,
Target: req.InteractingAccount,
})
return nil
}