mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-12-22 18:22:11 +00:00
84279f6a6a
* improvements to caching for lists and relationship to accounts / follows * fix nil panic in AddToList() * ensure list related caches are correctly invalidated * ensure returned ID lists are ordered correctly * bump go-structr to v0.8.9 (returns early if zero uncached keys to be loaded) * remove zero checks in uncached key load functions (go-structr now handles this) * fix issues after rebase on upstream/main * update the expected return order of CSV exports (since list entries are now down by entry creation date) * rename some funcs, allow deleting list entries for multiple follow IDs at a time, fix up more tests * use returning statements on delete to get cache invalidation info * fixes to recent database delete changes * fix broken list entries delete sql * remove unused db function * update remainder of delete functions to behave in similar way, some other small tweaks * fix delete user sql, allow returning on err no entries * uncomment + fix list database tests * update remaining list tests * update envparsing test * add comments to each specific key being invalidated * add more cache invalidation explanatory comments * whoops; actually delete poll votes from database in the DeletePollByID() func * remove added but-commented-out field * improved comment regarding paging being disabled * make cache invalidation comments match what's actually happening * fix up delete query comments to match what is happening * rename function to read a bit better * don't use ErrNoEntries on delete when not needed (it's only needed for a RETURNING call) * update function name in test * move list exclusivity check to AFTER eligibility check. use log.Panic() instead of panic() * use the poll_id column in poll_votes for selecting votes in poll ID * fix function name
251 lines
5.7 KiB
Go
251 lines
5.7 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 bundb
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
"github.com/superseriousbusiness/gotosocial/internal/state"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
type moveDB struct {
|
|
db *bun.DB
|
|
state *state.State
|
|
}
|
|
|
|
func (m *moveDB) GetMoveByID(
|
|
ctx context.Context,
|
|
id string,
|
|
) (*gtsmodel.Move, error) {
|
|
return m.getMove(
|
|
ctx,
|
|
"ID",
|
|
func(move *gtsmodel.Move) error {
|
|
return m.db.
|
|
NewSelect().
|
|
Model(move).
|
|
Where("? = ?", bun.Ident("move.id"), id).
|
|
Scan(ctx)
|
|
},
|
|
id,
|
|
)
|
|
}
|
|
|
|
func (m *moveDB) GetMoveByURI(
|
|
ctx context.Context,
|
|
uri string,
|
|
) (*gtsmodel.Move, error) {
|
|
return m.getMove(
|
|
ctx,
|
|
"URI",
|
|
func(move *gtsmodel.Move) error {
|
|
return m.db.
|
|
NewSelect().
|
|
Model(move).
|
|
Where("? = ?", bun.Ident("move.uri"), uri).
|
|
Scan(ctx)
|
|
},
|
|
uri,
|
|
)
|
|
}
|
|
|
|
func (m *moveDB) GetMoveByOriginTarget(
|
|
ctx context.Context,
|
|
originURI string,
|
|
targetURI string,
|
|
) (*gtsmodel.Move, error) {
|
|
return m.getMove(
|
|
ctx,
|
|
"OriginURI,TargetURI",
|
|
func(move *gtsmodel.Move) error {
|
|
return m.db.
|
|
NewSelect().
|
|
Model(move).
|
|
Where("? = ?", bun.Ident("move.origin_uri"), originURI).
|
|
Where("? = ?", bun.Ident("move.target_uri"), targetURI).
|
|
Scan(ctx)
|
|
},
|
|
originURI, targetURI,
|
|
)
|
|
}
|
|
|
|
func (m *moveDB) GetLatestMoveSuccessInvolvingURIs(
|
|
ctx context.Context,
|
|
uri1 string,
|
|
uri2 string,
|
|
) (time.Time, error) {
|
|
// Get at most 1 latest Move
|
|
// involving the provided URIs.
|
|
var moves []*gtsmodel.Move
|
|
err := m.db.
|
|
NewSelect().
|
|
Model(&moves).
|
|
Column("succeeded_at").
|
|
Where("? = ?", bun.Ident("move.origin_uri"), uri1).
|
|
WhereOr("? = ?", bun.Ident("move.origin_uri"), uri2).
|
|
WhereOr("? = ?", bun.Ident("move.target_uri"), uri1).
|
|
WhereOr("? = ?", bun.Ident("move.target_uri"), uri2).
|
|
Order("id DESC").
|
|
Limit(1).
|
|
Scan(ctx)
|
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
return time.Time{}, err
|
|
}
|
|
|
|
if len(moves) != 1 {
|
|
return time.Time{}, nil
|
|
}
|
|
|
|
return moves[0].SucceededAt, nil
|
|
}
|
|
|
|
func (m *moveDB) GetLatestMoveAttemptInvolvingURIs(
|
|
ctx context.Context,
|
|
uri1 string,
|
|
uri2 string,
|
|
) (time.Time, error) {
|
|
// Get at most 1 latest Move
|
|
// involving the provided URIs.
|
|
var moves []*gtsmodel.Move
|
|
err := m.db.
|
|
NewSelect().
|
|
Model(&moves).
|
|
Column("attempted_at").
|
|
Where("? = ?", bun.Ident("move.origin_uri"), uri1).
|
|
WhereOr("? = ?", bun.Ident("move.origin_uri"), uri2).
|
|
WhereOr("? = ?", bun.Ident("move.target_uri"), uri1).
|
|
WhereOr("? = ?", bun.Ident("move.target_uri"), uri2).
|
|
Order("id DESC").
|
|
Limit(1).
|
|
Scan(ctx)
|
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
return time.Time{}, err
|
|
}
|
|
|
|
if len(moves) != 1 {
|
|
return time.Time{}, nil
|
|
}
|
|
|
|
return moves[0].AttemptedAt, nil
|
|
}
|
|
|
|
func (m *moveDB) getMove(
|
|
ctx context.Context,
|
|
lookup string,
|
|
dbQuery func(*gtsmodel.Move) error,
|
|
keyParts ...any,
|
|
) (*gtsmodel.Move, error) {
|
|
move, err := m.state.Caches.DB.Move.LoadOne(lookup, func() (*gtsmodel.Move, error) {
|
|
var move gtsmodel.Move
|
|
|
|
// Not cached! Perform database query.
|
|
if err := dbQuery(&move); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &move, nil
|
|
}, keyParts...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if gtscontext.Barebones(ctx) {
|
|
return move, nil
|
|
}
|
|
|
|
// Populate the Move by parsing out the URIs.
|
|
if err := m.PopulateMove(ctx, move); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return move, nil
|
|
}
|
|
|
|
func (m *moveDB) PopulateMove(ctx context.Context, move *gtsmodel.Move) error {
|
|
if move.Origin == nil {
|
|
var err error
|
|
move.Origin, err = url.Parse(move.OriginURI)
|
|
if err != nil {
|
|
return fmt.Errorf("error parsing Move originURI: %w", err)
|
|
}
|
|
}
|
|
|
|
if move.Target == nil {
|
|
var err error
|
|
move.Target, err = url.Parse(move.TargetURI)
|
|
if err != nil {
|
|
return fmt.Errorf("error parsing Move targetURI: %w", err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (m *moveDB) PutMove(ctx context.Context, move *gtsmodel.Move) error {
|
|
return m.state.Caches.DB.Move.Store(move, func() error {
|
|
_, err := m.db.
|
|
NewInsert().
|
|
Model(move).
|
|
Exec(ctx)
|
|
return err
|
|
})
|
|
}
|
|
|
|
func (m *moveDB) UpdateMove(ctx context.Context, move *gtsmodel.Move, columns ...string) error {
|
|
move.UpdatedAt = time.Now()
|
|
if len(columns) > 0 {
|
|
// If we're updating by column,
|
|
// ensure "updated_at" is included.
|
|
columns = append(columns, "updated_at")
|
|
}
|
|
|
|
return m.state.Caches.DB.Move.Store(move, func() error {
|
|
_, err := m.db.
|
|
NewUpdate().
|
|
Model(move).
|
|
Column(columns...).
|
|
Where("? = ?", bun.Ident("move.id"), move.ID).
|
|
Exec(ctx)
|
|
return err
|
|
})
|
|
}
|
|
|
|
func (m *moveDB) DeleteMoveByID(ctx context.Context, id string) error {
|
|
// Delete move with given ID.
|
|
if _, err := m.db.NewDelete().
|
|
TableExpr("? AS ?", bun.Ident("moves"), bun.Ident("move")).
|
|
Where("? = ?", bun.Ident("move.id"), id).
|
|
Exec(ctx); err != nil &&
|
|
!errors.Is(err, db.ErrNoEntries) {
|
|
return nil
|
|
}
|
|
|
|
// Invalidate the cached move model with ID.
|
|
m.state.Caches.DB.Move.Invalidate("ID", id)
|
|
|
|
return nil
|
|
}
|