2023-03-12 18:00:57 +03:00
|
|
|
// 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/>.
|
2021-06-13 19:42:28 +03:00
|
|
|
|
|
|
|
package timeline
|
|
|
|
|
|
|
|
import (
|
2021-08-25 16:34:33 +03:00
|
|
|
"context"
|
2021-06-13 19:42:28 +03:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
2022-11-22 21:38:10 +03:00
|
|
|
"time"
|
2021-06-13 19:42:28 +03:00
|
|
|
|
2023-04-06 14:43:13 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2022-07-19 11:47:55 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2021-06-13 19:42:28 +03:00
|
|
|
)
|
|
|
|
|
2023-04-06 14:43:13 +03:00
|
|
|
const (
|
|
|
|
pruneLengthIndexed = 400
|
|
|
|
pruneLengthPrepared = 50
|
|
|
|
)
|
|
|
|
|
2021-06-13 19:42:28 +03:00
|
|
|
// Manager abstracts functions for creating timelines for multiple accounts, and adding, removing, and fetching entries from those timelines.
|
|
|
|
//
|
2022-02-05 14:47:38 +03:00
|
|
|
// By the time a timelineable hits the manager interface, it should already have been filtered and it should be established that the item indeed
|
|
|
|
// belongs in the timeline of the given account ID.
|
2021-06-13 19:42:28 +03:00
|
|
|
//
|
2022-02-05 14:47:38 +03:00
|
|
|
// The manager makes a distinction between *indexed* items and *prepared* items.
|
2021-06-13 19:42:28 +03:00
|
|
|
//
|
2022-02-05 14:47:38 +03:00
|
|
|
// Indexed items consist of just that item's ID (in the database) and the time it was created. An indexed item takes up very little memory, so
|
|
|
|
// it's not a huge priority to keep trimming the indexed items list.
|
2021-06-13 19:42:28 +03:00
|
|
|
//
|
2022-02-05 14:47:38 +03:00
|
|
|
// Prepared items consist of the item's database ID, the time it was created, AND the apimodel representation of that item, for quick serialization.
|
|
|
|
// Prepared items of course take up more memory than indexed items, so they should be regularly pruned if they're not being actively served.
|
2021-06-13 19:42:28 +03:00
|
|
|
type Manager interface {
|
2023-04-06 14:43:13 +03:00
|
|
|
// IngestOne takes one timelineable and indexes it into the timeline for the given account ID, and then immediately prepares it for serving.
|
2022-02-05 14:47:38 +03:00
|
|
|
// This is useful in cases where we know the item will need to be shown at the top of a user's timeline immediately (eg., a new status is created).
|
2021-06-13 19:42:28 +03:00
|
|
|
//
|
2022-02-05 14:47:38 +03:00
|
|
|
// It should already be established before calling this function that the item actually belongs in the timeline!
|
2021-06-19 12:18:55 +03:00
|
|
|
//
|
2022-02-05 14:47:38 +03:00
|
|
|
// The returned bool indicates whether the item was actually put in the timeline. This could be false in cases where
|
|
|
|
// a status is a boost, but a boost of the original status or the status itself already exists recently in the timeline.
|
2023-04-06 14:43:13 +03:00
|
|
|
IngestOne(ctx context.Context, accountID string, item Timelineable) (bool, error)
|
|
|
|
|
2022-02-05 14:47:38 +03:00
|
|
|
// GetTimeline returns limit n amount of prepared entries from the timeline of the given account ID, in descending chronological order.
|
|
|
|
GetTimeline(ctx context.Context, accountID string, maxID string, sinceID string, minID string, limit int, local bool) ([]Preparable, error)
|
2023-04-06 14:43:13 +03:00
|
|
|
|
|
|
|
// GetIndexedLength returns the amount of items that have been indexed for the given account ID.
|
|
|
|
GetIndexedLength(ctx context.Context, accountID string) int
|
|
|
|
|
2022-02-05 14:47:38 +03:00
|
|
|
// GetOldestIndexedID returns the id ID for the oldest item that we have indexed for the given account.
|
2023-04-06 14:43:13 +03:00
|
|
|
// Will be an empty string if nothing is (yet) indexed.
|
|
|
|
GetOldestIndexedID(ctx context.Context, accountID string) string
|
|
|
|
|
2022-02-05 14:47:38 +03:00
|
|
|
// Remove removes one item from the timeline of the given timelineAccountID
|
2023-04-06 14:43:13 +03:00
|
|
|
Remove(ctx context.Context, accountID string, itemID string) (int, error)
|
|
|
|
|
2022-02-05 14:47:38 +03:00
|
|
|
// WipeItemFromAllTimelines removes one item from the index and prepared items of all timelines
|
|
|
|
WipeItemFromAllTimelines(ctx context.Context, itemID string) error
|
2023-04-06 14:43:13 +03:00
|
|
|
|
2022-02-05 14:47:38 +03:00
|
|
|
// WipeStatusesFromAccountID removes all items by the given accountID from the timelineAccountID's timelines.
|
|
|
|
WipeItemsFromAccountID(ctx context.Context, timelineAccountID string, accountID string) error
|
2023-04-06 14:43:13 +03:00
|
|
|
|
2022-11-22 21:38:10 +03:00
|
|
|
// Start starts hourly cleanup jobs for this timeline manager.
|
|
|
|
Start() error
|
2023-04-06 14:43:13 +03:00
|
|
|
|
2022-11-22 21:38:10 +03:00
|
|
|
// Stop stops the timeline manager (currently a stub, doesn't do anything).
|
|
|
|
Stop() error
|
2021-06-13 19:42:28 +03:00
|
|
|
}
|
|
|
|
|
2022-02-05 14:47:38 +03:00
|
|
|
// NewManager returns a new timeline manager.
|
|
|
|
func NewManager(grabFunction GrabFunction, filterFunction FilterFunction, prepareFunction PrepareFunction, skipInsertFunction SkipInsertFunction) Manager {
|
2021-06-13 19:42:28 +03:00
|
|
|
return &manager{
|
2022-02-05 14:47:38 +03:00
|
|
|
accountTimelines: sync.Map{},
|
|
|
|
grabFunction: grabFunction,
|
|
|
|
filterFunction: filterFunction,
|
|
|
|
prepareFunction: prepareFunction,
|
|
|
|
skipInsertFunction: skipInsertFunction,
|
2021-06-13 19:42:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type manager struct {
|
2022-02-05 14:47:38 +03:00
|
|
|
accountTimelines sync.Map
|
|
|
|
grabFunction GrabFunction
|
|
|
|
filterFunction FilterFunction
|
|
|
|
prepareFunction PrepareFunction
|
|
|
|
skipInsertFunction SkipInsertFunction
|
2021-06-13 19:42:28 +03:00
|
|
|
}
|
|
|
|
|
2022-11-22 21:38:10 +03:00
|
|
|
func (m *manager) Start() error {
|
2023-04-06 14:43:13 +03:00
|
|
|
// Start a background goroutine which iterates
|
|
|
|
// through all stored timelines once per hour,
|
|
|
|
// and cleans up old entries if that timeline
|
|
|
|
// hasn't been accessed in the last hour.
|
2022-11-22 21:38:10 +03:00
|
|
|
go func() {
|
|
|
|
for now := range time.NewTicker(1 * time.Hour).C {
|
2023-04-06 14:43:13 +03:00
|
|
|
// Define the range function inside here,
|
|
|
|
// so that we can use the 'now' returned
|
|
|
|
// by the ticker, instead of having to call
|
|
|
|
// time.Now() multiple times.
|
|
|
|
//
|
|
|
|
// Unless it panics, this function always
|
|
|
|
// returns 'true', to continue the Range
|
|
|
|
// call through the sync.Map.
|
|
|
|
f := func(_ any, v any) bool {
|
|
|
|
timeline, ok := v.(Timeline)
|
2022-11-22 21:38:10 +03:00
|
|
|
if !ok {
|
2023-04-06 14:43:13 +03:00
|
|
|
log.Panic(nil, "couldn't parse timeline manager sync map value as Timeline, this should never happen so panic")
|
2022-11-22 21:38:10 +03:00
|
|
|
}
|
|
|
|
|
2023-04-06 14:43:13 +03:00
|
|
|
if now.Sub(timeline.LastGot()) < 1*time.Hour {
|
|
|
|
// Timeline has been fetched in the
|
|
|
|
// last hour, move on to the next one.
|
|
|
|
return true
|
2022-11-22 21:38:10 +03:00
|
|
|
}
|
|
|
|
|
2023-04-06 14:43:13 +03:00
|
|
|
if amountPruned := timeline.Prune(pruneLengthPrepared, pruneLengthIndexed); amountPruned > 0 {
|
|
|
|
log.WithField("accountID", timeline.AccountID()).Infof("pruned %d indexed and prepared items from timeline", amountPruned)
|
2022-11-22 21:38:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2023-04-06 14:43:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the function for each timeline.
|
|
|
|
m.accountTimelines.Range(f)
|
2022-11-22 21:38:10 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *manager) Stop() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-06 14:43:13 +03:00
|
|
|
func (m *manager) IngestOne(ctx context.Context, accountID string, item Timelineable) (bool, error) {
|
|
|
|
return m.getOrCreateTimeline(ctx, accountID).IndexAndPrepareOne(
|
|
|
|
ctx,
|
|
|
|
item.GetID(),
|
|
|
|
item.GetBoostOfID(),
|
|
|
|
item.GetAccountID(),
|
|
|
|
item.GetBoostOfAccountID(),
|
|
|
|
)
|
2021-06-13 19:42:28 +03:00
|
|
|
}
|
|
|
|
|
2023-04-06 14:43:13 +03:00
|
|
|
func (m *manager) Remove(ctx context.Context, accountID string, itemID string) (int, error) {
|
|
|
|
return m.getOrCreateTimeline(ctx, accountID).Remove(ctx, itemID)
|
2021-06-13 19:42:28 +03:00
|
|
|
}
|
|
|
|
|
2023-04-06 14:43:13 +03:00
|
|
|
func (m *manager) GetTimeline(ctx context.Context, accountID string, maxID string, sinceID string, minID string, limit int, local bool) ([]Preparable, error) {
|
|
|
|
return m.getOrCreateTimeline(ctx, accountID).Get(ctx, limit, maxID, sinceID, minID, true)
|
2021-06-13 19:42:28 +03:00
|
|
|
}
|
|
|
|
|
2023-04-06 14:43:13 +03:00
|
|
|
func (m *manager) GetIndexedLength(ctx context.Context, accountID string) int {
|
|
|
|
return m.getOrCreateTimeline(ctx, accountID).Len()
|
2021-06-13 19:42:28 +03:00
|
|
|
}
|
|
|
|
|
2023-04-06 14:43:13 +03:00
|
|
|
func (m *manager) GetOldestIndexedID(ctx context.Context, accountID string) string {
|
|
|
|
return m.getOrCreateTimeline(ctx, accountID).OldestIndexedItemID()
|
2021-06-13 19:42:28 +03:00
|
|
|
}
|
|
|
|
|
2022-02-05 14:47:38 +03:00
|
|
|
func (m *manager) WipeItemFromAllTimelines(ctx context.Context, statusID string) error {
|
2023-04-06 14:43:13 +03:00
|
|
|
errors := gtserror.MultiError{}
|
2021-06-13 19:42:28 +03:00
|
|
|
|
2023-04-06 14:43:13 +03:00
|
|
|
m.accountTimelines.Range(func(_ any, v any) bool {
|
|
|
|
if _, err := v.(Timeline).Remove(ctx, statusID); err != nil {
|
|
|
|
errors.Append(err)
|
2021-06-13 19:42:28 +03:00
|
|
|
}
|
|
|
|
|
2023-04-06 14:43:13 +03:00
|
|
|
return true // always continue range
|
2021-06-13 19:42:28 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
if len(errors) > 0 {
|
2023-04-06 14:43:13 +03:00
|
|
|
return fmt.Errorf("WipeItemFromAllTimelines: one or more errors wiping status %s: %w", statusID, errors.Combine())
|
2021-06-13 19:42:28 +03:00
|
|
|
}
|
|
|
|
|
2023-04-06 14:43:13 +03:00
|
|
|
return nil
|
2021-06-13 19:42:28 +03:00
|
|
|
}
|
|
|
|
|
2022-02-05 14:47:38 +03:00
|
|
|
func (m *manager) WipeItemsFromAccountID(ctx context.Context, timelineAccountID string, accountID string) error {
|
2023-04-06 14:43:13 +03:00
|
|
|
_, err := m.getOrCreateTimeline(ctx, timelineAccountID).RemoveAllByOrBoosting(ctx, accountID)
|
2021-07-11 17:22:21 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-06 14:43:13 +03:00
|
|
|
// getOrCreateTimeline returns a timeline for the given
|
|
|
|
// accountID. If a timeline does not yet exist in the
|
|
|
|
// manager's sync.Map, it will be created and stored.
|
|
|
|
func (m *manager) getOrCreateTimeline(ctx context.Context, accountID string) Timeline {
|
|
|
|
i, ok := m.accountTimelines.Load(accountID)
|
|
|
|
if ok {
|
|
|
|
// Timeline already existed in sync.Map.
|
|
|
|
return i.(Timeline) //nolint:forcetypeassert
|
2021-06-13 19:42:28 +03:00
|
|
|
}
|
|
|
|
|
2023-04-06 14:43:13 +03:00
|
|
|
// Timeline did not yet exist in sync.Map.
|
|
|
|
// Create + store it.
|
|
|
|
timeline := NewTimeline(ctx, accountID, m.grabFunction, m.filterFunction, m.prepareFunction, m.skipInsertFunction)
|
|
|
|
m.accountTimelines.Store(accountID, timeline)
|
|
|
|
|
|
|
|
return timeline
|
2021-06-13 19:42:28 +03:00
|
|
|
}
|