gotosocial/internal/timeline/timeline.go

467 lines
13 KiB
Go
Raw Normal View History

/*
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 (
"container/list"
"errors"
2021-06-01 18:09:28 +00:00
"fmt"
"sync"
"time"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/db"
2021-06-02 17:52:15 +00:00
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
)
2021-06-01 19:36:55 +00:00
const (
2021-06-02 17:52:15 +00:00
preparedPostsMaxLength = desiredPostIndexLength
2021-06-01 19:36:55 +00:00
)
2021-06-01 18:09:28 +00:00
2021-06-03 16:16:33 +00:00
// Timeline represents a timeline for one account, and contains indexed and prepared posts.
2021-06-02 17:52:15 +00:00
type Timeline interface {
2021-06-03 16:16:33 +00:00
/*
RETRIEVAL FUNCTIONS
*/
2021-06-02 17:52:15 +00:00
// GetXFromTop returns x amount of posts from the top of the timeline, from newest to oldest.
GetXFromTop(amount int) ([]*apimodel.Status, error)
// GetXBehindID returns x amount of posts from the given id onwards, from newest to oldest.
// This will NOT include the status with the given ID.
//
// This corresponds to an api call to /timelines/home?max_id=WHATEVER
GetXBehindID(amount int, fromID string) ([]*apimodel.Status, error)
// GetXBeforeID returns x amount of posts up to the given id, from newest to oldest.
// This will NOT include the status with the given ID.
//
// This corresponds to an api call to /timelines/home?since_id=WHATEVER
GetXBeforeID(amount int, sinceID string) ([]*apimodel.Status, error)
// GetXBetweenID returns x amount of posts from the given maxID, up to the given id, from newest to oldest.
// This will NOT include the status with the given IDs.
//
// This corresponds to an api call to /timelines/home?since_id=WHATEVER&max_id=WHATEVER_ELSE
GetXBetweenID(amount int, maxID string, sinceID string) ([]*apimodel.Status, error)
2021-06-02 17:52:15 +00:00
2021-06-03 16:16:33 +00:00
/*
INDEXING FUNCTIONS
*/
2021-06-02 17:52:15 +00:00
// IndexOne puts a status into the timeline at the appropriate place according to its 'createdAt' property.
IndexOne(statusCreatedAt time.Time, statusID string) error
// Remove removes a status from the timeline.
Remove(statusID string) error
// OldestIndexedPostID returns the id of the rearmost (ie., the oldest) indexed post, or an error if something goes wrong.
// If nothing goes wrong but there's no oldest post, an empty string will be returned so make sure to check for this.
OldestIndexedPostID() (string, error)
2021-06-03 16:16:33 +00:00
/*
PREPARATION FUNCTIONS
*/
2021-06-02 17:52:15 +00:00
// PrepareXFromTop instructs the timeline to prepare x amount of posts from the top of the timeline.
PrepareXFromTop(amount int) error
// PrepareXFromPosition instrucst the timeline to prepare the next amount of entries for serialization, from position onwards.
PrepareXFromPosition(amount int, position int) error
2021-06-03 16:16:33 +00:00
// IndexOne puts a status into the timeline at the appropriate place according to its 'createdAt' property,
// and then immediately prepares it.
IndexAndPrepareOne(statusCreatedAt time.Time, statusID string) error
/*
INFO FUNCTIONS
*/
2021-06-02 17:52:15 +00:00
// ActualPostIndexLength returns the actual length of the post index at this point in time.
PostIndexLength() int
2021-06-03 16:16:33 +00:00
/*
UTILITY FUNCTIONS
*/
2021-06-02 17:52:15 +00:00
// Reset instructs the timeline to reset to its base state -- cache only the minimum amount of posts.
Reset() error
}
2021-06-03 16:16:33 +00:00
// timeline fulfils the Timeline interface
type timeline struct {
2021-06-02 17:52:15 +00:00
postIndex *postIndex
preparedPosts *preparedPosts
2021-06-01 18:09:28 +00:00
accountID string
2021-06-02 17:52:15 +00:00
account *gtsmodel.Account
2021-06-01 18:09:28 +00:00
db db.DB
2021-06-02 17:52:15 +00:00
tc typeutils.TypeConverter
sync.Mutex
}
2021-06-03 16:16:33 +00:00
// NewTimeline returns a new Timeline for the given account ID
2021-06-02 17:52:15 +00:00
func NewTimeline(accountID string, db db.DB, typeConverter typeutils.TypeConverter) Timeline {
return &timeline{
2021-06-02 17:52:15 +00:00
postIndex: &postIndex{},
preparedPosts: &preparedPosts{},
2021-06-01 18:09:28 +00:00
accountID: accountID,
2021-06-01 19:36:55 +00:00
db: db,
2021-06-02 17:52:15 +00:00
tc: typeConverter,
}
}
func (t *timeline) PrepareXFromPosition(amount int, desiredPosition int) error {
t.Lock()
defer t.Unlock()
var position int
2021-06-02 17:52:15 +00:00
var prepared int
var preparing bool
for e := t.postIndex.data.Front(); e != nil; e = e.Next() {
entry, ok := e.Value.(*postIndexEntry)
2021-06-01 18:09:28 +00:00
if !ok {
2021-06-02 17:52:15 +00:00
return errors.New("PrepareXFromTop: could not parse e as a postIndexEntry")
2021-06-01 18:09:28 +00:00
}
2021-06-02 17:52:15 +00:00
if !preparing {
// we haven't hit the position we need to prepare from yet
position = position + 1
if position == desiredPosition {
2021-06-02 17:52:15 +00:00
preparing = true
continue
2021-06-02 17:52:15 +00:00
}
} else {
if err := t.prepare(entry.statusID); err != nil {
return fmt.Errorf("PrepareXFromTop: error preparing status with id %s: %s", entry.statusID, err)
}
prepared = prepared + 1
if prepared >= amount {
// we're done
break
}
2021-06-01 18:09:28 +00:00
}
}
2021-06-02 17:52:15 +00:00
return nil
}
2021-06-01 18:09:28 +00:00
2021-06-02 17:52:15 +00:00
func (t *timeline) PrepareXFromTop(amount int) error {
t.Lock()
defer t.Unlock()
2021-06-01 18:09:28 +00:00
2021-06-02 17:52:15 +00:00
t.preparedPosts.data.Init()
var prepared int
for e := t.postIndex.data.Front(); e != nil; e = e.Next() {
entry, ok := e.Value.(*postIndexEntry)
if !ok {
return errors.New("PrepareXFromTop: could not parse e as a postIndexEntry")
}
if err := t.prepare(entry.statusID); err != nil {
return fmt.Errorf("PrepareXFromTop: error preparing status with id %s: %s", entry.statusID, err)
}
prepared = prepared + 1
if prepared >= amount {
// we're done
break
}
2021-06-01 18:09:28 +00:00
}
return nil
}
2021-06-02 17:52:15 +00:00
func (t *timeline) GetXFromTop(amount int) ([]*apimodel.Status, error) {
// make a slice of statuses with the length we need to return
statuses := make([]*apimodel.Status, 0, amount)
// if there are no prepared posts, just return the empty slice
if t.preparedPosts.data == nil {
t.preparedPosts.data = &list.List{}
2021-06-01 18:09:28 +00:00
}
2021-06-02 17:52:15 +00:00
// make sure we have enough posts prepared to return
if t.preparedPosts.data.Len() < amount {
if err := t.PrepareXFromTop(amount); err != nil {
2021-06-01 18:09:28 +00:00
return nil, err
}
}
2021-06-02 17:52:15 +00:00
// work through the prepared posts from the top and return
var served int
for e := t.preparedPosts.data.Front(); e != nil; e = e.Next() {
entry, ok := e.Value.(*preparedPostsEntry)
if !ok {
return nil, errors.New("GetXFromTop: could not parse e as a preparedPostsEntry")
}
statuses = append(statuses, entry.prepared)
served = served + 1
if served >= amount {
break
}
}
return statuses, nil
2021-06-01 18:09:28 +00:00
}
func (t *timeline) GetXBehindID(amount int, behindID string) ([]*apimodel.Status, error) {
2021-06-02 17:52:15 +00:00
// make a slice of statuses with the length we need to return
statuses := make([]*apimodel.Status, 0, amount)
// if there are no prepared posts, just return the empty slice
if t.preparedPosts.data == nil {
t.preparedPosts.data = &list.List{}
2021-06-01 18:09:28 +00:00
}
2021-06-02 17:52:15 +00:00
// find the position of id
var position int
for e := t.preparedPosts.data.Front(); e != nil; e = e.Next() {
entry, ok := e.Value.(*preparedPostsEntry)
if !ok {
return nil, errors.New("GetXBehindID: could not parse e as a preparedPostsEntry")
2021-06-01 18:09:28 +00:00
}
if entry.statusID == behindID {
2021-06-02 17:52:15 +00:00
break
}
position = position + 1
2021-06-01 18:09:28 +00:00
}
2021-06-03 08:57:24 +00:00
// make sure we have enough posts prepared behind it to return what we're being asked for
2021-06-02 17:52:15 +00:00
if t.preparedPosts.data.Len() < amount+position {
if err := t.PrepareXFromPosition(amount, position); err != nil {
2021-06-01 18:09:28 +00:00
return nil, err
}
}
2021-06-02 17:52:15 +00:00
// iterate through the modified list until we hit the fromID again
var serving bool
var served int
for e := t.preparedPosts.data.Front(); e != nil; e = e.Next() {
entry, ok := e.Value.(*preparedPostsEntry)
if !ok {
return nil, errors.New("GetXBehindID: could not parse e as a preparedPostsEntry")
}
if !serving {
2021-06-03 08:57:24 +00:00
// start serving if we've hit the id we're looking for
if entry.statusID == behindID {
2021-06-02 17:52:15 +00:00
serving = true
continue
2021-06-02 17:52:15 +00:00
}
} else {
2021-06-03 08:57:24 +00:00
// serve up to the amount requested
2021-06-02 17:52:15 +00:00
statuses = append(statuses, entry.prepared)
served = served + 1
if served >= amount {
break
}
}
}
return statuses, nil
}
func (t *timeline) GetXBeforeID(amount int, beforeID string) ([]*apimodel.Status, error) {
// make a slice of statuses with the length we need to return
statuses := make([]*apimodel.Status, 0, amount)
// if there are no prepared posts, just return the empty slice
if t.preparedPosts.data == nil {
t.preparedPosts.data = &list.List{}
}
// iterate through the modified list until we hit the fromID again
var served int
servloop:
for e := t.preparedPosts.data.Front(); e != nil; e = e.Next() {
entry, ok := e.Value.(*preparedPostsEntry)
if !ok {
return nil, errors.New("GetXBeforeID: could not parse e as a preparedPostsEntry")
}
if entry.statusID == beforeID {
// we're good
break servloop
}
// serve up to the amount requested
statuses = append(statuses, entry.prepared)
served = served + 1
if served >= amount {
break
}
}
return statuses, nil
}
func (t *timeline) GetXBetweenID(amount int, maxID string, sinceID string) ([]*apimodel.Status, error) {
// make a slice of statuses with the length we need to return
statuses := make([]*apimodel.Status, 0, amount)
// if there are no prepared posts, just return the empty slice
if t.preparedPosts.data == nil {
t.preparedPosts.data = &list.List{}
}
}
2021-06-02 17:52:15 +00:00
func (t *timeline) IndexOne(statusCreatedAt time.Time, statusID string) error {
t.Lock()
defer t.Unlock()
2021-06-02 17:52:15 +00:00
postIndexEntry := &postIndexEntry{
createdAt: statusCreatedAt,
statusID: statusID,
2021-06-01 18:09:28 +00:00
}
2021-06-03 08:57:24 +00:00
return t.postIndex.insertIndexed(postIndexEntry)
2021-06-02 17:52:15 +00:00
}
2021-06-01 18:09:28 +00:00
func (t *timeline) IndexAndPrepareOne(statusCreatedAt time.Time, statusID string) error {
t.Lock()
defer t.Unlock()
postIndexEntry := &postIndexEntry{
createdAt: statusCreatedAt,
statusID: statusID,
}
if err := t.postIndex.insertIndexed(postIndexEntry); err != nil {
return fmt.Errorf("IndexAndPrepareOne: error inserting indexed: %s", err)
}
if err := t.prepare(statusID); err != nil {
return fmt.Errorf("IndexAndPrepareOne: error preparing: %s", err)
}
return nil
}
2021-06-02 17:52:15 +00:00
func (t *timeline) Remove(statusID string) error {
t.Lock()
defer t.Unlock()
2021-06-04 14:35:58 +00:00
if t.postIndex != nil && t.postIndex.data != nil {
// remove the entry from the post index
for e := t.postIndex.data.Front(); e != nil; e = e.Next() {
entry, ok := e.Value.(*postIndexEntry)
if !ok {
return errors.New("Remove: could not parse e as a postIndexEntry")
}
if entry.statusID == statusID {
t.postIndex.data.Remove(e)
break // bail once we found and removed it
}
2021-06-02 17:52:15 +00:00
}
}
2021-06-02 17:52:15 +00:00
// remove the entry from prepared posts
2021-06-04 14:35:58 +00:00
if t.preparedPosts != nil && t.preparedPosts.data != nil {
for e := t.preparedPosts.data.Front(); e != nil; e = e.Next() {
entry, ok := e.Value.(*preparedPostsEntry)
if !ok {
return errors.New("Remove: could not parse e as a preparedPostsEntry")
}
if entry.statusID == statusID {
t.preparedPosts.data.Remove(e)
break // bail once we found and removed it
}
}
2021-06-02 17:52:15 +00:00
}
return nil
}
func (t *timeline) Reset() error {
return nil
}
2021-06-02 17:52:15 +00:00
func (t *timeline) PostIndexLength() int {
if t.postIndex == nil || t.postIndex.data == nil {
return 0
}
return t.postIndex.data.Len()
}
func (t *timeline) OldestIndexedPostID() (string, error) {
var id string
if t.postIndex == nil || t.postIndex.data == nil {
2021-06-03 08:57:24 +00:00
// return an empty string if postindex hasn't been initialized yet
2021-06-02 17:52:15 +00:00
return id, nil
}
2021-06-03 08:57:24 +00:00
2021-06-02 17:52:15 +00:00
e := t.postIndex.data.Back()
if e == nil {
2021-06-03 08:57:24 +00:00
// return an empty string if there's no back entry (ie., the index list hasn't been initialized yet)
2021-06-02 17:52:15 +00:00
return id, nil
}
entry, ok := e.Value.(*postIndexEntry)
if !ok {
return id, errors.New("OldestIndexedPostID: could not parse e as a postIndexEntry")
}
return entry.statusID, nil
}
2021-06-01 19:36:55 +00:00
2021-06-02 17:52:15 +00:00
func (t *timeline) prepare(statusID string) error {
2021-06-03 08:57:24 +00:00
// start by getting the status out of the database according to its indexed ID
2021-06-02 17:52:15 +00:00
gtsStatus := &gtsmodel.Status{}
if err := t.db.GetByID(statusID, gtsStatus); err != nil {
return err
}
2021-06-03 08:57:24 +00:00
// if the account pointer hasn't been set on this timeline already, set it lazily here
2021-06-02 17:52:15 +00:00
if t.account == nil {
timelineOwnerAccount := &gtsmodel.Account{}
if err := t.db.GetByID(t.accountID, timelineOwnerAccount); err != nil {
return err
}
t.account = timelineOwnerAccount
}
2021-06-03 08:57:24 +00:00
// to convert the status we need relevant accounts from it, so pull them out here
2021-06-02 17:52:15 +00:00
relevantAccounts, err := t.db.PullRelevantAccountsFromStatus(gtsStatus)
if err != nil {
return err
}
2021-06-03 08:57:24 +00:00
// check if this is a boost...
2021-06-02 17:52:15 +00:00
var reblogOfStatus *gtsmodel.Status
if gtsStatus.BoostOfID != "" {
s := &gtsmodel.Status{}
if err := t.db.GetByID(gtsStatus.BoostOfID, s); err != nil {
return err
}
reblogOfStatus = s
}
2021-06-03 08:57:24 +00:00
// serialize the status (or, at least, convert it to a form that's ready to be serialized)
2021-06-02 17:52:15 +00:00
apiModelStatus, err := t.tc.StatusToMasto(gtsStatus, relevantAccounts.StatusAuthor, t.account, relevantAccounts.BoostedAccount, relevantAccounts.ReplyToAccount, reblogOfStatus)
if err != nil {
return err
}
2021-06-03 08:57:24 +00:00
// shove it in prepared posts as a prepared posts entry
2021-06-02 17:52:15 +00:00
preparedPostsEntry := &preparedPostsEntry{
createdAt: gtsStatus.CreatedAt,
statusID: statusID,
prepared: apiModelStatus,
}
return t.preparedPosts.insertPrepared(preparedPostsEntry)
2021-06-01 19:36:55 +00:00
}