2021-07-11 14:22:21 +00:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2021-12-20 17:42:19 +00:00
|
|
|
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
|
2021-07-11 14:22:21 +00:00
|
|
|
|
|
|
|
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 16:42:28 +00:00
|
|
|
package timeline
|
|
|
|
|
|
|
|
import (
|
2021-06-21 10:27:23 +00:00
|
|
|
"container/list"
|
2021-08-25 13:34:33 +00:00
|
|
|
"context"
|
2021-06-13 16:42:28 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2022-07-19 08:47:55 +00:00
|
|
|
"codeberg.org/gruf/go-kv"
|
2021-06-13 16:42:28 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2022-07-19 08:47:55 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2021-06-13 16:42:28 +00:00
|
|
|
)
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (t *timeline) prepareNextQuery(ctx context.Context, amount int, maxID string, sinceID string, minID string) error {
|
2022-07-19 08:47:55 +00:00
|
|
|
l := log.WithFields(kv.Fields{
|
|
|
|
|
|
|
|
{"amount", amount},
|
|
|
|
{"maxID", maxID},
|
|
|
|
{"sinceID", sinceID},
|
|
|
|
{"minID", minID},
|
|
|
|
}...)
|
2021-08-15 16:43:08 +00:00
|
|
|
|
2021-06-13 16:42:28 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
// maxID is defined but sinceID isn't so take from behind
|
|
|
|
if maxID != "" && sinceID == "" {
|
2021-08-15 16:43:08 +00:00
|
|
|
l.Debug("preparing behind maxID")
|
2021-08-25 13:34:33 +00:00
|
|
|
err = t.PrepareBehind(ctx, maxID, amount)
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// maxID isn't defined, but sinceID || minID are, so take x before
|
|
|
|
if maxID == "" && sinceID != "" {
|
2021-08-15 16:43:08 +00:00
|
|
|
l.Debug("preparing before sinceID")
|
2021-08-25 13:34:33 +00:00
|
|
|
err = t.PrepareBefore(ctx, sinceID, false, amount)
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
if maxID == "" && minID != "" {
|
2021-08-15 16:43:08 +00:00
|
|
|
l.Debug("preparing before minID")
|
2021-08-25 13:34:33 +00:00
|
|
|
err = t.PrepareBefore(ctx, minID, false, amount)
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-05 11:47:38 +00:00
|
|
|
func (t *timeline) PrepareBehind(ctx context.Context, itemID string, amount int) error {
|
|
|
|
// lazily initialize prepared items if it hasn't been done already
|
|
|
|
if t.preparedItems.data == nil {
|
|
|
|
t.preparedItems.data = &list.List{}
|
|
|
|
t.preparedItems.data.Init()
|
2021-06-21 10:27:23 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 11:47:38 +00:00
|
|
|
if err := t.IndexBehind(ctx, itemID, amount); err != nil {
|
|
|
|
return fmt.Errorf("PrepareBehind: error indexing behind id %s: %s", itemID, err)
|
2021-08-15 16:43:08 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 11:47:38 +00:00
|
|
|
// if the itemindex is nil, nothing has been indexed yet so there's nothing to prepare
|
|
|
|
if t.itemIndex.data == nil {
|
2021-06-21 10:27:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-13 16:42:28 +00:00
|
|
|
var prepared int
|
|
|
|
var preparing bool
|
2021-08-15 16:43:08 +00:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
2021-06-13 16:42:28 +00:00
|
|
|
prepareloop:
|
2022-02-05 11:47:38 +00:00
|
|
|
for e := t.itemIndex.data.Front(); e != nil; e = e.Next() {
|
|
|
|
entry, ok := e.Value.(*itemIndexEntry)
|
2021-06-13 16:42:28 +00:00
|
|
|
if !ok {
|
2022-02-05 11:47:38 +00:00
|
|
|
return errors.New("PrepareBehind: could not parse e as itemIndexEntry")
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !preparing {
|
|
|
|
// we haven't hit the position we need to prepare from yet
|
2022-02-05 11:47:38 +00:00
|
|
|
if entry.itemID == itemID {
|
2021-06-13 16:42:28 +00:00
|
|
|
preparing = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if preparing {
|
2022-02-05 11:47:38 +00:00
|
|
|
if err := t.prepare(ctx, entry.itemID); err != nil {
|
2021-06-13 16:42:28 +00:00
|
|
|
// there's been an error
|
2021-08-20 10:26:56 +00:00
|
|
|
if err != db.ErrNoEntries {
|
2021-06-13 16:42:28 +00:00
|
|
|
// it's a real error
|
2022-02-05 11:47:38 +00:00
|
|
|
return fmt.Errorf("PrepareBehind: error preparing item with id %s: %s", entry.itemID, err)
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
// the status just doesn't exist (anymore) so continue to the next one
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if prepared == amount {
|
|
|
|
// we're done
|
|
|
|
break prepareloop
|
|
|
|
}
|
2021-11-22 07:46:19 +00:00
|
|
|
prepared++
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (t *timeline) PrepareBefore(ctx context.Context, statusID string, include bool, amount int) error {
|
2021-06-13 16:42:28 +00:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2021-06-21 10:27:23 +00:00
|
|
|
// lazily initialize prepared posts if it hasn't been done already
|
2022-02-05 11:47:38 +00:00
|
|
|
if t.preparedItems.data == nil {
|
|
|
|
t.preparedItems.data = &list.List{}
|
|
|
|
t.preparedItems.data.Init()
|
2021-06-21 10:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if the postindex is nil, nothing has been indexed yet so there's nothing to prepare
|
2022-02-05 11:47:38 +00:00
|
|
|
if t.itemIndex.data == nil {
|
2021-06-21 10:27:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-13 16:42:28 +00:00
|
|
|
var prepared int
|
|
|
|
var preparing bool
|
|
|
|
prepareloop:
|
2022-02-05 11:47:38 +00:00
|
|
|
for e := t.itemIndex.data.Back(); e != nil; e = e.Prev() {
|
|
|
|
entry, ok := e.Value.(*itemIndexEntry)
|
2021-06-13 16:42:28 +00:00
|
|
|
if !ok {
|
|
|
|
return errors.New("PrepareBefore: could not parse e as a postIndexEntry")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !preparing {
|
|
|
|
// we haven't hit the position we need to prepare from yet
|
2022-02-05 11:47:38 +00:00
|
|
|
if entry.itemID == statusID {
|
2021-06-13 16:42:28 +00:00
|
|
|
preparing = true
|
|
|
|
if !include {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if preparing {
|
2022-02-05 11:47:38 +00:00
|
|
|
if err := t.prepare(ctx, entry.itemID); err != nil {
|
2021-06-13 16:42:28 +00:00
|
|
|
// there's been an error
|
2021-08-20 10:26:56 +00:00
|
|
|
if err != db.ErrNoEntries {
|
2021-06-13 16:42:28 +00:00
|
|
|
// it's a real error
|
2022-02-05 11:47:38 +00:00
|
|
|
return fmt.Errorf("PrepareBefore: error preparing status with id %s: %s", entry.itemID, err)
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
// the status just doesn't exist (anymore) so continue to the next one
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if prepared == amount {
|
|
|
|
// we're done
|
|
|
|
break prepareloop
|
|
|
|
}
|
2021-11-22 07:46:19 +00:00
|
|
|
prepared++
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (t *timeline) PrepareFromTop(ctx context.Context, amount int) error {
|
2022-07-19 08:47:55 +00:00
|
|
|
l := log.WithFields(kv.Fields{
|
|
|
|
|
|
|
|
{"amount", amount},
|
|
|
|
}...)
|
2021-06-13 16:42:28 +00:00
|
|
|
|
2021-06-21 10:27:23 +00:00
|
|
|
// lazily initialize prepared posts if it hasn't been done already
|
2022-02-05 11:47:38 +00:00
|
|
|
if t.preparedItems.data == nil {
|
|
|
|
t.preparedItems.data = &list.List{}
|
|
|
|
t.preparedItems.data.Init()
|
2021-06-21 10:27:23 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 16:43:08 +00:00
|
|
|
// if the postindex is nil, nothing has been indexed yet so index from the highest ID possible
|
2022-02-05 11:47:38 +00:00
|
|
|
if t.itemIndex.data == nil {
|
2021-08-15 16:43:08 +00:00
|
|
|
l.Debug("postindex.data was nil, indexing behind highest possible ID")
|
2022-02-05 11:47:38 +00:00
|
|
|
if err := t.IndexBehind(ctx, "ZZZZZZZZZZZZZZZZZZZZZZZZZZ", amount); err != nil {
|
2021-08-15 16:43:08 +00:00
|
|
|
return fmt.Errorf("PrepareFromTop: error indexing behind id %s: %s", "ZZZZZZZZZZZZZZZZZZZZZZZZZZ", err)
|
|
|
|
}
|
2021-06-21 10:27:23 +00:00
|
|
|
}
|
2021-06-13 16:42:28 +00:00
|
|
|
|
2021-08-15 16:43:08 +00:00
|
|
|
l.Trace("entering prepareloop")
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
2021-06-13 16:42:28 +00:00
|
|
|
var prepared int
|
|
|
|
prepareloop:
|
2022-02-05 11:47:38 +00:00
|
|
|
for e := t.itemIndex.data.Front(); e != nil; e = e.Next() {
|
2021-06-21 10:27:23 +00:00
|
|
|
if e == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-02-05 11:47:38 +00:00
|
|
|
entry, ok := e.Value.(*itemIndexEntry)
|
2021-06-13 16:42:28 +00:00
|
|
|
if !ok {
|
|
|
|
return errors.New("PrepareFromTop: could not parse e as a postIndexEntry")
|
|
|
|
}
|
|
|
|
|
2022-02-05 11:47:38 +00:00
|
|
|
if err := t.prepare(ctx, entry.itemID); err != nil {
|
2021-06-13 16:42:28 +00:00
|
|
|
// there's been an error
|
2021-08-20 10:26:56 +00:00
|
|
|
if err != db.ErrNoEntries {
|
2021-06-13 16:42:28 +00:00
|
|
|
// it's a real error
|
2022-02-05 11:47:38 +00:00
|
|
|
return fmt.Errorf("PrepareFromTop: error preparing status with id %s: %s", entry.itemID, err)
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
// the status just doesn't exist (anymore) so continue to the next one
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-11-22 07:46:19 +00:00
|
|
|
prepared++
|
2021-06-13 16:42:28 +00:00
|
|
|
if prepared == amount {
|
|
|
|
// we're done
|
2021-08-15 16:43:08 +00:00
|
|
|
l.Trace("leaving prepareloop")
|
2021-06-13 16:42:28 +00:00
|
|
|
break prepareloop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-15 16:43:08 +00:00
|
|
|
l.Trace("leaving function")
|
2021-06-13 16:42:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-05 11:47:38 +00:00
|
|
|
func (t *timeline) prepare(ctx context.Context, itemID string) error {
|
|
|
|
// trigger the caller-provided prepare function
|
|
|
|
prepared, err := t.prepareFunction(ctx, t.accountID, itemID)
|
2021-06-13 16:42:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-05 11:47:38 +00:00
|
|
|
// shove it in prepared items as a prepared items entry
|
|
|
|
preparedItemsEntry := &preparedItemsEntry{
|
|
|
|
itemID: prepared.GetID(),
|
|
|
|
boostOfID: prepared.GetBoostOfID(),
|
|
|
|
accountID: prepared.GetAccountID(),
|
|
|
|
boostOfAccountID: prepared.GetBoostOfAccountID(),
|
|
|
|
prepared: prepared,
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 11:47:38 +00:00
|
|
|
return t.preparedItems.insertPrepared(ctx, preparedItemsEntry)
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 11:47:38 +00:00
|
|
|
func (t *timeline) OldestPreparedItemID(ctx context.Context) (string, error) {
|
2021-06-13 16:42:28 +00:00
|
|
|
var id string
|
2022-02-05 11:47:38 +00:00
|
|
|
if t.preparedItems == nil || t.preparedItems.data == nil {
|
|
|
|
// return an empty string if prepared items hasn't been initialized yet
|
2021-06-13 16:42:28 +00:00
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2022-02-05 11:47:38 +00:00
|
|
|
e := t.preparedItems.data.Back()
|
2021-06-13 16:42:28 +00:00
|
|
|
if e == nil {
|
|
|
|
// return an empty string if there's no back entry (ie., the index list hasn't been initialized yet)
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2022-02-05 11:47:38 +00:00
|
|
|
entry, ok := e.Value.(*preparedItemsEntry)
|
2021-06-13 16:42:28 +00:00
|
|
|
if !ok {
|
2022-02-05 11:47:38 +00:00
|
|
|
return id, errors.New("OldestPreparedItemID: could not parse e as a preparedItemsEntry")
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
2022-02-05 11:47:38 +00:00
|
|
|
|
|
|
|
return entry.itemID, nil
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|