2021-06-13 16:42:28 +00:00
|
|
|
/*
|
|
|
|
GoToSocial
|
|
|
|
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
|
|
|
|
|
|
|
|
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 timeline
|
|
|
|
|
|
|
|
import (
|
2021-08-25 13:34:33 +00:00
|
|
|
"context"
|
2021-06-13 16:42:28 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
desiredPostIndexLength = 400
|
|
|
|
)
|
|
|
|
|
|
|
|
// Manager abstracts functions for creating timelines for multiple accounts, and adding, removing, and fetching entries from those timelines.
|
|
|
|
//
|
|
|
|
// By the time a status hits the manager interface, it should already have been filtered and it should be established that the status indeed
|
|
|
|
// belongs in the home timeline of the given account ID.
|
|
|
|
//
|
|
|
|
// The manager makes a distinction between *indexed* posts and *prepared* posts.
|
|
|
|
//
|
|
|
|
// Indexed posts consist of just that post's ID (in the database) and the time it was created. An indexed post takes up very little memory, so
|
|
|
|
// it's not a huge priority to keep trimming the indexed posts list.
|
|
|
|
//
|
|
|
|
// Prepared posts consist of the post's database ID, the time it was created, AND the apimodel representation of that post, for quick serialization.
|
|
|
|
// Prepared posts of course take up more memory than indexed posts, so they should be regularly pruned if they're not being actively served.
|
|
|
|
type Manager interface {
|
|
|
|
// Ingest takes one status and indexes it into the timeline for the given account ID.
|
|
|
|
//
|
|
|
|
// It should already be established before calling this function that the status/post actually belongs in the timeline!
|
2021-06-19 09:18:55 +00:00
|
|
|
//
|
|
|
|
// The returned bool indicates whether the status was actually put in the timeline. This could be false in cases where
|
|
|
|
// the status is a boost, but a boost of the original post or the post itself already exists recently in the timeline.
|
2021-08-25 13:34:33 +00:00
|
|
|
Ingest(ctx context.Context, status *gtsmodel.Status, timelineAccountID string) (bool, error)
|
2021-06-13 16:42:28 +00:00
|
|
|
// IngestAndPrepare takes one status and indexes it into the timeline for the given account ID, and then immediately prepares it for serving.
|
|
|
|
// This is useful in cases where we know the status will need to be shown at the top of a user's timeline immediately (eg., a new status is created).
|
|
|
|
//
|
|
|
|
// It should already be established before calling this function that the status/post actually belongs in the timeline!
|
2021-06-19 09:18:55 +00:00
|
|
|
//
|
|
|
|
// The returned bool indicates whether the status was actually put in the timeline. This could be false in cases where
|
|
|
|
// the status is a boost, but a boost of the original post or the post itself already exists recently in the timeline.
|
2021-08-25 13:34:33 +00:00
|
|
|
IngestAndPrepare(ctx context.Context, status *gtsmodel.Status, timelineAccountID string) (bool, error)
|
2021-06-13 16:42:28 +00:00
|
|
|
// HomeTimeline returns limit n amount of entries from the home timeline of the given account ID, in descending chronological order.
|
|
|
|
// If maxID is provided, it will return entries from that maxID onwards, inclusive.
|
2021-08-25 13:34:33 +00:00
|
|
|
HomeTimeline(ctx context.Context, accountID string, maxID string, sinceID string, minID string, limit int, local bool) ([]*apimodel.Status, error)
|
2021-06-13 16:42:28 +00:00
|
|
|
// GetIndexedLength returns the amount of posts/statuses that have been *indexed* for the given account ID.
|
2021-08-25 13:34:33 +00:00
|
|
|
GetIndexedLength(ctx context.Context, timelineAccountID string) int
|
2021-06-13 16:42:28 +00:00
|
|
|
// GetDesiredIndexLength returns the amount of posts that we, ideally, index for each user.
|
2021-08-25 13:34:33 +00:00
|
|
|
GetDesiredIndexLength(ctx context.Context) int
|
2021-06-13 16:42:28 +00:00
|
|
|
// GetOldestIndexedID returns the status ID for the oldest post that we have indexed for the given account.
|
2021-08-25 13:34:33 +00:00
|
|
|
GetOldestIndexedID(ctx context.Context, timelineAccountID string) (string, error)
|
2021-06-13 16:42:28 +00:00
|
|
|
// PrepareXFromTop prepares limit n amount of posts, based on their indexed representations, from the top of the index.
|
2021-08-25 13:34:33 +00:00
|
|
|
PrepareXFromTop(ctx context.Context, timelineAccountID string, limit int) error
|
2021-06-21 13:56:00 +00:00
|
|
|
// Remove removes one status from the timeline of the given timelineAccountID
|
2021-08-25 13:34:33 +00:00
|
|
|
Remove(ctx context.Context, timelineAccountID string, statusID string) (int, error)
|
2021-06-21 13:56:00 +00:00
|
|
|
// WipeStatusFromAllTimelines removes one status from the index and prepared posts of all timelines
|
2021-08-25 13:34:33 +00:00
|
|
|
WipeStatusFromAllTimelines(ctx context.Context, statusID string) error
|
2021-07-11 14:22:21 +00:00
|
|
|
// WipeStatusesFromAccountID removes all statuses by the given accountID from the timelineAccountID's timelines.
|
2021-08-25 13:34:33 +00:00
|
|
|
WipeStatusesFromAccountID(ctx context.Context, timelineAccountID string, accountID string) error
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewManager returns a new timeline manager with the given database, typeconverter, config, and log.
|
2021-12-07 12:31:39 +00:00
|
|
|
func NewManager(db db.DB, tc typeutils.TypeConverter) Manager {
|
2021-06-13 16:42:28 +00:00
|
|
|
return &manager{
|
|
|
|
accountTimelines: sync.Map{},
|
|
|
|
db: db,
|
|
|
|
tc: tc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type manager struct {
|
|
|
|
accountTimelines sync.Map
|
|
|
|
db db.DB
|
|
|
|
tc typeutils.TypeConverter
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (m *manager) Ingest(ctx context.Context, status *gtsmodel.Status, timelineAccountID string) (bool, error) {
|
2021-10-11 12:37:33 +00:00
|
|
|
l := logrus.WithFields(logrus.Fields{
|
2021-06-13 16:42:28 +00:00
|
|
|
"func": "Ingest",
|
|
|
|
"timelineAccountID": timelineAccountID,
|
|
|
|
"statusID": status.ID,
|
|
|
|
})
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
t, err := m.getOrCreateTimeline(ctx, timelineAccountID)
|
2021-06-23 16:42:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-06-13 16:42:28 +00:00
|
|
|
|
|
|
|
l.Trace("ingesting status")
|
2021-08-25 13:34:33 +00:00
|
|
|
return t.IndexOne(ctx, status.CreatedAt, status.ID, status.BoostOfID, status.AccountID, status.BoostOfAccountID)
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (m *manager) IngestAndPrepare(ctx context.Context, status *gtsmodel.Status, timelineAccountID string) (bool, error) {
|
2021-10-11 12:37:33 +00:00
|
|
|
l := logrus.WithFields(logrus.Fields{
|
2021-06-13 16:42:28 +00:00
|
|
|
"func": "IngestAndPrepare",
|
|
|
|
"timelineAccountID": timelineAccountID,
|
|
|
|
"statusID": status.ID,
|
|
|
|
})
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
t, err := m.getOrCreateTimeline(ctx, timelineAccountID)
|
2021-06-23 16:42:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-06-13 16:42:28 +00:00
|
|
|
|
|
|
|
l.Trace("ingesting status")
|
2021-08-25 13:34:33 +00:00
|
|
|
return t.IndexAndPrepareOne(ctx, status.CreatedAt, status.ID, status.BoostOfID, status.AccountID, status.BoostOfAccountID)
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (m *manager) Remove(ctx context.Context, timelineAccountID string, statusID string) (int, error) {
|
2021-10-11 12:37:33 +00:00
|
|
|
l := logrus.WithFields(logrus.Fields{
|
2021-06-13 16:42:28 +00:00
|
|
|
"func": "Remove",
|
|
|
|
"timelineAccountID": timelineAccountID,
|
|
|
|
"statusID": statusID,
|
|
|
|
})
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
t, err := m.getOrCreateTimeline(ctx, timelineAccountID)
|
2021-06-23 16:42:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-06-13 16:42:28 +00:00
|
|
|
|
|
|
|
l.Trace("removing status")
|
2021-08-25 13:34:33 +00:00
|
|
|
return t.Remove(ctx, statusID)
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (m *manager) HomeTimeline(ctx context.Context, timelineAccountID string, maxID string, sinceID string, minID string, limit int, local bool) ([]*apimodel.Status, error) {
|
2021-10-11 12:37:33 +00:00
|
|
|
l := logrus.WithFields(logrus.Fields{
|
2021-06-13 16:42:28 +00:00
|
|
|
"func": "HomeTimelineGet",
|
|
|
|
"timelineAccountID": timelineAccountID,
|
|
|
|
})
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
t, err := m.getOrCreateTimeline(ctx, timelineAccountID)
|
2021-06-23 16:42:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-13 16:42:28 +00:00
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
statuses, err := t.Get(ctx, limit, maxID, sinceID, minID, true)
|
2021-06-13 16:42:28 +00:00
|
|
|
if err != nil {
|
|
|
|
l.Errorf("error getting statuses: %s", err)
|
|
|
|
}
|
|
|
|
return statuses, nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (m *manager) GetIndexedLength(ctx context.Context, timelineAccountID string) int {
|
|
|
|
t, err := m.getOrCreateTimeline(ctx, timelineAccountID)
|
2021-06-23 16:42:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
2021-06-13 16:42:28 +00:00
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
return t.PostIndexLength(ctx)
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (m *manager) GetDesiredIndexLength(ctx context.Context) int {
|
2021-06-13 16:42:28 +00:00
|
|
|
return desiredPostIndexLength
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (m *manager) GetOldestIndexedID(ctx context.Context, timelineAccountID string) (string, error) {
|
|
|
|
t, err := m.getOrCreateTimeline(ctx, timelineAccountID)
|
2021-06-23 16:42:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-06-13 16:42:28 +00:00
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
return t.OldestIndexedPostID(ctx)
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (m *manager) PrepareXFromTop(ctx context.Context, timelineAccountID string, limit int) error {
|
|
|
|
t, err := m.getOrCreateTimeline(ctx, timelineAccountID)
|
2021-06-23 16:42:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-13 16:42:28 +00:00
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
return t.PrepareFromTop(ctx, limit)
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (m *manager) WipeStatusFromAllTimelines(ctx context.Context, statusID string) error {
|
2021-06-13 16:42:28 +00:00
|
|
|
errors := []string{}
|
|
|
|
m.accountTimelines.Range(func(k interface{}, i interface{}) bool {
|
|
|
|
t, ok := i.(Timeline)
|
|
|
|
if !ok {
|
|
|
|
panic("couldn't parse entry as Timeline, this should never happen so panic")
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
if _, err := t.Remove(ctx, statusID); err != nil {
|
2021-06-13 16:42:28 +00:00
|
|
|
errors = append(errors, err.Error())
|
|
|
|
}
|
|
|
|
|
2021-06-21 13:56:00 +00:00
|
|
|
return true
|
2021-06-13 16:42:28 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if len(errors) > 0 {
|
|
|
|
err = fmt.Errorf("one or more errors removing status %s from all timelines: %s", statusID, strings.Join(errors, ";"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (m *manager) WipeStatusesFromAccountID(ctx context.Context, timelineAccountID string, accountID string) error {
|
|
|
|
t, err := m.getOrCreateTimeline(ctx, timelineAccountID)
|
2021-07-11 14:22:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
_, err = t.RemoveAllBy(ctx, accountID)
|
2021-07-11 14:22:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (m *manager) getOrCreateTimeline(ctx context.Context, timelineAccountID string) (Timeline, error) {
|
2021-06-13 16:42:28 +00:00
|
|
|
var t Timeline
|
|
|
|
i, ok := m.accountTimelines.Load(timelineAccountID)
|
|
|
|
if !ok {
|
2021-06-23 16:42:20 +00:00
|
|
|
var err error
|
2021-10-11 12:37:33 +00:00
|
|
|
t, err = NewTimeline(ctx, timelineAccountID, m.db, m.tc)
|
2021-06-23 16:42:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-13 16:42:28 +00:00
|
|
|
m.accountTimelines.Store(timelineAccountID, t)
|
|
|
|
} else {
|
|
|
|
t, ok = i.(Timeline)
|
|
|
|
if !ok {
|
|
|
|
panic("couldn't parse entry as Timeline, this should never happen so panic")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-23 16:42:20 +00:00
|
|
|
return t, nil
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|