2021-07-11 16:22:21 +02: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/>.
|
|
|
|
*/
|
|
|
|
|
2021-06-13 18:42:28 +02:00
|
|
|
package timeline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
2021-08-25 15:34:33 +02:00
|
|
|
"context"
|
2021-06-13 18:42:28 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
|
|
|
)
|
|
|
|
|
2021-08-10 16:56:59 +02:00
|
|
|
const retries = 5
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (t *timeline) Get(ctx context.Context, amount int, maxID string, sinceID string, minID string, prepareNext bool) ([]*apimodel.Status, error) {
|
2021-10-11 14:37:33 +02:00
|
|
|
l := logrus.WithFields(logrus.Fields{
|
2021-06-13 18:42:28 +02:00
|
|
|
"func": "Get",
|
|
|
|
"accountID": t.accountID,
|
2021-06-23 18:42:20 +02:00
|
|
|
"amount": amount,
|
|
|
|
"maxID": maxID,
|
|
|
|
"sinceID": sinceID,
|
|
|
|
"minID": minID,
|
2021-06-13 18:42:28 +02:00
|
|
|
})
|
2021-06-23 18:42:20 +02:00
|
|
|
l.Debug("entering get")
|
2021-06-13 18:42:28 +02:00
|
|
|
|
|
|
|
var statuses []*apimodel.Status
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// no params are defined to just fetch from the top
|
2021-08-15 18:43:08 +02:00
|
|
|
// this is equivalent to a user asking for the top x posts from their timeline
|
2021-06-13 18:42:28 +02:00
|
|
|
if maxID == "" && sinceID == "" && minID == "" {
|
2021-08-25 15:34:33 +02:00
|
|
|
statuses, err = t.GetXFromTop(ctx, amount)
|
2021-06-13 18:42:28 +02:00
|
|
|
// aysnchronously prepare the next predicted query so it's ready when the user asks for it
|
|
|
|
if len(statuses) != 0 {
|
|
|
|
nextMaxID := statuses[len(statuses)-1].ID
|
2021-08-15 18:43:08 +02:00
|
|
|
if prepareNext {
|
|
|
|
// already cache the next query to speed up scrolling
|
|
|
|
go func() {
|
2021-08-25 15:34:33 +02:00
|
|
|
// use context.Background() because we don't want the query to abort when the request finishes
|
|
|
|
if err := t.prepareNextQuery(context.Background(), amount, nextMaxID, "", ""); err != nil {
|
2021-08-15 18:43:08 +02:00
|
|
|
l.Errorf("error preparing next query: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// maxID is defined but sinceID isn't so take from behind
|
2021-08-15 18:43:08 +02:00
|
|
|
// this is equivalent to a user asking for the next x posts from their timeline, starting from maxID
|
2021-06-13 18:42:28 +02:00
|
|
|
if maxID != "" && sinceID == "" {
|
2021-08-10 16:56:59 +02:00
|
|
|
attempts := 0
|
2021-08-25 15:34:33 +02:00
|
|
|
statuses, err = t.GetXBehindID(ctx, amount, maxID, &attempts)
|
2021-06-13 18:42:28 +02:00
|
|
|
// aysnchronously prepare the next predicted query so it's ready when the user asks for it
|
|
|
|
if len(statuses) != 0 {
|
|
|
|
nextMaxID := statuses[len(statuses)-1].ID
|
2021-08-15 18:43:08 +02:00
|
|
|
if prepareNext {
|
|
|
|
// already cache the next query to speed up scrolling
|
|
|
|
go func() {
|
2021-08-25 15:34:33 +02:00
|
|
|
// use context.Background() because we don't want the query to abort when the request finishes
|
|
|
|
if err := t.prepareNextQuery(context.Background(), amount, nextMaxID, "", ""); err != nil {
|
2021-08-15 18:43:08 +02:00
|
|
|
l.Errorf("error preparing next query: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// maxID is defined and sinceID || minID are as well, so take a slice between them
|
2021-08-15 18:43:08 +02:00
|
|
|
// this is equivalent to a user asking for posts older than x but newer than y
|
2021-06-13 18:42:28 +02:00
|
|
|
if maxID != "" && sinceID != "" {
|
2021-08-25 15:34:33 +02:00
|
|
|
statuses, err = t.GetXBetweenID(ctx, amount, maxID, minID)
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
if maxID != "" && minID != "" {
|
2021-08-25 15:34:33 +02:00
|
|
|
statuses, err = t.GetXBetweenID(ctx, amount, maxID, minID)
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// maxID isn't defined, but sinceID || minID are, so take x before
|
2021-08-15 18:43:08 +02:00
|
|
|
// this is equivalent to a user asking for posts newer than x (eg., refreshing the top of their timeline)
|
2021-06-13 18:42:28 +02:00
|
|
|
if maxID == "" && sinceID != "" {
|
2021-08-25 15:34:33 +02:00
|
|
|
statuses, err = t.GetXBeforeID(ctx, amount, sinceID, true)
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
if maxID == "" && minID != "" {
|
2021-08-25 15:34:33 +02:00
|
|
|
statuses, err = t.GetXBeforeID(ctx, amount, minID, true)
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return statuses, err
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (t *timeline) GetXFromTop(ctx context.Context, amount int) ([]*apimodel.Status, error) {
|
2021-06-13 18:42:28 +02:00
|
|
|
// make a slice of statuses with the length we need to return
|
|
|
|
statuses := make([]*apimodel.Status, 0, amount)
|
|
|
|
|
|
|
|
if t.preparedPosts.data == nil {
|
|
|
|
t.preparedPosts.data = &list.List{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we have enough posts prepared to return
|
|
|
|
if t.preparedPosts.data.Len() < amount {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := t.PrepareFromTop(ctx, amount); err != nil {
|
2021-06-13 18:42:28 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-08-25 15:34:33 +02:00
|
|
|
func (t *timeline) GetXBehindID(ctx context.Context, amount int, behindID string, attempts *int) ([]*apimodel.Status, error) {
|
2021-10-11 14:37:33 +02:00
|
|
|
l := logrus.WithFields(logrus.Fields{
|
2021-08-15 18:43:08 +02:00
|
|
|
"func": "GetXBehindID",
|
|
|
|
"amount": amount,
|
|
|
|
"behindID": behindID,
|
|
|
|
"attempts": *attempts,
|
|
|
|
})
|
|
|
|
|
2021-08-10 16:56:59 +02:00
|
|
|
newAttempts := *attempts
|
|
|
|
newAttempts = newAttempts + 1
|
|
|
|
attempts = &newAttempts
|
|
|
|
|
2021-06-13 18:42:28 +02:00
|
|
|
// make a slice of statuses with the length we need to return
|
|
|
|
statuses := make([]*apimodel.Status, 0, amount)
|
|
|
|
|
|
|
|
if t.preparedPosts.data == nil {
|
|
|
|
t.preparedPosts.data = &list.List{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate through the modified list until we hit the mark we're looking for
|
|
|
|
var position int
|
|
|
|
var behindIDMark *list.Element
|
|
|
|
|
|
|
|
findMarkLoop:
|
|
|
|
for e := t.preparedPosts.data.Front(); e != nil; e = e.Next() {
|
|
|
|
position = position + 1
|
|
|
|
entry, ok := e.Value.(*preparedPostsEntry)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("GetXBehindID: could not parse e as a preparedPostsEntry")
|
|
|
|
}
|
|
|
|
|
2021-08-15 18:43:08 +02:00
|
|
|
if entry.statusID <= behindID {
|
|
|
|
l.Trace("found behindID mark")
|
2021-06-13 18:42:28 +02:00
|
|
|
behindIDMark = e
|
|
|
|
break findMarkLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we didn't find it, so we need to make sure it's indexed and prepared and then try again
|
2021-08-15 18:43:08 +02:00
|
|
|
// this can happen when a user asks for really old posts
|
2021-06-13 18:42:28 +02:00
|
|
|
if behindIDMark == nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := t.PrepareBehind(ctx, behindID, amount); err != nil {
|
2021-06-13 18:42:28 +02:00
|
|
|
return nil, fmt.Errorf("GetXBehindID: error preparing behind and including ID %s", behindID)
|
|
|
|
}
|
2021-08-25 15:34:33 +02:00
|
|
|
oldestID, err := t.OldestPreparedPostID(ctx)
|
2021-06-13 18:42:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-15 18:43:08 +02:00
|
|
|
if oldestID == "" {
|
|
|
|
l.Tracef("oldestID is empty so we can't return behindID %s", behindID)
|
|
|
|
return statuses, nil
|
|
|
|
}
|
|
|
|
if oldestID == behindID {
|
|
|
|
l.Tracef("given behindID %s is the same as oldestID %s so there's nothing to return behind it", behindID, oldestID)
|
|
|
|
return statuses, nil
|
|
|
|
}
|
|
|
|
if *attempts > retries {
|
|
|
|
l.Tracef("exceeded retries looking for behindID %s", behindID)
|
2021-06-13 18:42:28 +02:00
|
|
|
return statuses, nil
|
|
|
|
}
|
2021-08-15 18:43:08 +02:00
|
|
|
l.Trace("trying GetXBehindID again")
|
2021-08-25 15:34:33 +02:00
|
|
|
return t.GetXBehindID(ctx, amount, behindID, attempts)
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we have enough posts prepared behind it to return what we're being asked for
|
|
|
|
if t.preparedPosts.data.Len() < amount+position {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := t.PrepareBehind(ctx, behindID, amount); err != nil {
|
2021-06-13 18:42:28 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// start serving from the entry right after the mark
|
|
|
|
var served int
|
|
|
|
serveloop:
|
|
|
|
for e := behindIDMark.Next(); e != nil; e = e.Next() {
|
|
|
|
entry, ok := e.Value.(*preparedPostsEntry)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("GetXBehindID: could not parse e as a preparedPostsEntry")
|
|
|
|
}
|
|
|
|
|
|
|
|
// serve up to the amount requested
|
|
|
|
statuses = append(statuses, entry.prepared)
|
|
|
|
served = served + 1
|
|
|
|
if served >= amount {
|
|
|
|
break serveloop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return statuses, nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (t *timeline) GetXBeforeID(ctx context.Context, amount int, beforeID string, startFromTop bool) ([]*apimodel.Status, error) {
|
2021-06-13 18:42:28 +02:00
|
|
|
// make a slice of statuses with the length we need to return
|
|
|
|
statuses := make([]*apimodel.Status, 0, amount)
|
|
|
|
|
|
|
|
if t.preparedPosts.data == nil {
|
|
|
|
t.preparedPosts.data = &list.List{}
|
|
|
|
}
|
|
|
|
|
2021-08-15 18:43:08 +02:00
|
|
|
// iterate through the modified list until we hit the mark we're looking for, or as close as possible to it
|
2021-06-13 18:42:28 +02:00
|
|
|
var beforeIDMark *list.Element
|
|
|
|
findMarkLoop:
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2021-08-15 18:43:08 +02:00
|
|
|
if entry.statusID >= beforeID {
|
2021-06-13 18:42:28 +02:00
|
|
|
beforeIDMark = e
|
2021-08-15 18:43:08 +02:00
|
|
|
} else {
|
2021-06-13 18:42:28 +02:00
|
|
|
break findMarkLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if beforeIDMark == nil {
|
2021-08-15 18:43:08 +02:00
|
|
|
return statuses, nil
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var served int
|
|
|
|
|
|
|
|
if startFromTop {
|
|
|
|
// start serving from the front/top and keep going until we hit mark or get x amount statuses
|
|
|
|
serveloopFromTop:
|
|
|
|
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 {
|
|
|
|
break serveloopFromTop
|
|
|
|
}
|
|
|
|
|
|
|
|
// serve up to the amount requested
|
|
|
|
statuses = append(statuses, entry.prepared)
|
|
|
|
served = served + 1
|
|
|
|
if served >= amount {
|
|
|
|
break serveloopFromTop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if !startFromTop {
|
|
|
|
// start serving from the entry right before the mark
|
|
|
|
serveloopFromBottom:
|
|
|
|
for e := beforeIDMark.Prev(); e != nil; e = e.Prev() {
|
|
|
|
entry, ok := e.Value.(*preparedPostsEntry)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("GetXBeforeID: could not parse e as a preparedPostsEntry")
|
|
|
|
}
|
|
|
|
|
|
|
|
// serve up to the amount requested
|
|
|
|
statuses = append(statuses, entry.prepared)
|
|
|
|
served = served + 1
|
|
|
|
if served >= amount {
|
|
|
|
break serveloopFromBottom
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return statuses, nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (t *timeline) GetXBetweenID(ctx context.Context, amount int, behindID string, beforeID string) ([]*apimodel.Status, error) {
|
2021-06-13 18:42:28 +02:00
|
|
|
// make a slice of statuses with the length we need to return
|
|
|
|
statuses := make([]*apimodel.Status, 0, amount)
|
|
|
|
|
|
|
|
if t.preparedPosts.data == nil {
|
|
|
|
t.preparedPosts.data = &list.List{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate through the modified list until we hit the mark we're looking for
|
|
|
|
var position int
|
|
|
|
var behindIDMark *list.Element
|
|
|
|
findMarkLoop:
|
|
|
|
for e := t.preparedPosts.data.Front(); e != nil; e = e.Next() {
|
|
|
|
position = position + 1
|
|
|
|
entry, ok := e.Value.(*preparedPostsEntry)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("GetXBetweenID: could not parse e as a preparedPostsEntry")
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry.statusID == behindID {
|
|
|
|
behindIDMark = e
|
|
|
|
break findMarkLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we didn't find it
|
|
|
|
if behindIDMark == nil {
|
|
|
|
return nil, fmt.Errorf("GetXBetweenID: couldn't find status with ID %s", behindID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we have enough posts prepared behind it to return what we're being asked for
|
|
|
|
if t.preparedPosts.data.Len() < amount+position {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := t.PrepareBehind(ctx, behindID, amount); err != nil {
|
2021-06-13 18:42:28 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// start serving from the entry right after the mark
|
|
|
|
var served int
|
|
|
|
serveloop:
|
|
|
|
for e := behindIDMark.Next(); e != nil; e = e.Next() {
|
|
|
|
entry, ok := e.Value.(*preparedPostsEntry)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("GetXBetweenID: could not parse e as a preparedPostsEntry")
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry.statusID == beforeID {
|
|
|
|
break serveloop
|
|
|
|
}
|
|
|
|
|
|
|
|
// serve up to the amount requested
|
|
|
|
statuses = append(statuses, entry.prepared)
|
|
|
|
served = served + 1
|
|
|
|
if served >= amount {
|
|
|
|
break serveloop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return statuses, nil
|
|
|
|
}
|