gotosocial/internal/federation/dereferencing/announce.go
kim 23fc70f4e6
[feature] add support for receiving federated status edits (#3597)
* add support for extracting Updated field from Statusable implementers

* add support for status edits in the database, and update status dereferencer to handle them

* remove unused AdditionalInfo{}.CreatedAt

* remove unused AdditionalEmojiInfo{}.CreatedAt

* update new mention creation to use status.UpdatedAt

* remove mention.UpdatedAt, fixes related to NewULIDFromTime() change

* add migration to remove Mention{}.UpdatedAt field

* add migration to add the StatusEdit{} table

* start adding tests, add delete function for status edits

* add more of status edit migrations, fill in more of the necessary edit delete functionality

* remove unused function

* allow generating gotosocial compatible ulid via CLI with `go run ./cmd/gen-ulid`

* add StatusEdit{} test models

* fix new statusedits sql

* use model instead of table name

* actually remove the Mention.UpdatedAt field...

* fix tests now new models are added, add more status edit DB tests

* fix panic wording

* add test for deleting status edits

* don't automatically set `updated_at` field on updated statuses

* flesh out more of the dereferencer status edit tests, ensure updated at field set on outgoing AS statuses

* remove media_attachments.updated_at column

* fix up more tests, further complete the dereferencer status edit tests

* update more status serialization tests not expecting 'updated' AS property

* gah!! json serialization tests!!

* undo some gtscontext wrapping changes

* more serialization test fixing 🥲

* more test fixing, ensure the edit.status_id field is actually set 🤦

* fix status edit test

* grrr linter

* add edited_at field to apimodel status

* remove the choice of paging on the timeline public filtered test (otherwise it needs updating every time you add statuses ...)

* ensure that status.updated_at always fits chronologically

* fix more serialization tests ...

* add more code comments

* fix envparsing

* update swagger file

* properly handle media description changes during status edits

* slight formatting tweak

* code comment
2024-12-05 13:35:07 +00:00

127 lines
4.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 dereferencing
import (
"context"
"errors"
"net/url"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id"
"github.com/superseriousbusiness/gotosocial/internal/util"
)
// EnrichAnnounce enriches the given boost wrapper status
// by either fetching from the DB or dereferencing the target
// status, populating the boost wrapper's fields based on the
// target status, and then storing the wrapper in the database.
// The wrapper is then returned to the caller.
//
// The provided boost wrapper status must have BoostOfURI set.
func (d *Dereferencer) EnrichAnnounce(
ctx context.Context,
boost *gtsmodel.Status,
requestUser string,
) (*gtsmodel.Status, error) {
targetURI := boost.BoostOfURI
if targetURI == "" {
// We can't do anything.
return nil, gtserror.Newf("no URI to dereference")
}
// Parse the boost target status URI.
targetURIObj, err := url.Parse(targetURI)
if err != nil {
return nil, gtserror.Newf(
"couldn't parse boost target status URI %s: %w",
targetURI, err,
)
}
// Fetch and dereference status being boosted, noting that
// d.GetStatusByURI handles domain blocks and local statuses.
target, _, err := d.GetStatusByURI(ctx, requestUser, targetURIObj)
if err != nil {
return nil, gtserror.Newf("error fetching boost target %s: %w", targetURI, err)
}
if target.BoostOfID != "" {
// Ensure that the target is not a boost (should not be possible).
err := gtserror.Newf("target status %s is a boost", targetURI)
return nil, err
}
// Set boost_of_uri again in case the
// original URI was an indirect link.
boost.BoostOfURI = target.URI
// Boosts are not considered sensitive even if their target is.
boost.Sensitive = util.Ptr(false)
// Populate remaining fields on
// the boost wrapper using target.
boost.ActivityStreamsType = target.ActivityStreamsType
boost.BoostOfID = target.ID
boost.BoostOf = target
boost.BoostOfAccountID = target.AccountID
boost.BoostOfAccount = target.Account
boost.Visibility = target.Visibility
boost.Federated = target.Federated
// Ensure this Announce is permitted by the Announcee.
permit, err := d.isPermittedStatus(ctx, requestUser, nil, boost, true)
if err != nil {
return nil, gtserror.Newf("error checking permitted status %s: %w", boost.URI, err)
}
if !permit {
// Return a checkable error type that can be ignored.
err := gtserror.Newf("dropping unpermitted status: %s", boost.URI)
return nil, gtserror.SetNotPermitted(err)
}
// Generate an ID for the boost wrapper status.
boost.ID = id.NewULIDFromTime(boost.CreatedAt)
// Store the boost wrapper status in database.
switch err = d.state.DB.PutStatus(ctx, boost); {
case err == nil:
// all groovy.
case errors.Is(err, db.ErrAlreadyExists):
uri := boost.URI
// DATA RACE! We likely lost out to another goroutine
// in a call to db.Put(Status). Look again in DB by URI.
boost, err = d.state.DB.GetStatusByURI(ctx, uri)
if err != nil {
return nil, gtserror.Newf(
"error getting boost wrapper status %s from database after race: %w",
uri, err,
)
}
default: // Proper database error.
return nil, gtserror.Newf("db error inserting status: %w", err)
}
return boost, err
}