2021-07-11 16:22:21 +02:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2021-12-20 18:42:19 +01:00
|
|
|
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
|
2021-07-11 16:22:21 +02: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 18:42:28 +02:00
|
|
|
package timeline
|
|
|
|
|
|
|
|
import (
|
2021-08-15 18:43:08 +02:00
|
|
|
"container/list"
|
2021-08-25 15:34:33 +02:00
|
|
|
"context"
|
2021-06-13 18:42:28 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2021-08-15 18:43:08 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-06-13 18:42:28 +02:00
|
|
|
)
|
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
func (t *timeline) IndexBefore(ctx context.Context, itemID string, amount int) error {
|
|
|
|
l := logrus.WithFields(logrus.Fields{
|
|
|
|
"func": "IndexBefore",
|
|
|
|
"amount": amount,
|
|
|
|
})
|
|
|
|
|
2021-08-15 18:43:08 +02:00
|
|
|
// lazily initialize index if it hasn't been done already
|
2022-02-05 12:47:38 +01:00
|
|
|
if t.itemIndex.data == nil {
|
|
|
|
t.itemIndex.data = &list.List{}
|
|
|
|
t.itemIndex.data.Init()
|
2021-08-15 18:43:08 +02:00
|
|
|
}
|
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
toIndex := []Timelineable{}
|
|
|
|
offsetID := itemID
|
2021-06-23 18:42:20 +02:00
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
l.Trace("entering grabloop")
|
2021-06-23 18:42:20 +02:00
|
|
|
grabloop:
|
2022-02-05 12:47:38 +01:00
|
|
|
for i := 0; len(toIndex) < amount && i < 5; i++ { // try the grabloop 5 times only
|
|
|
|
// first grab items using the caller-provided grab function
|
|
|
|
l.Trace("grabbing...")
|
|
|
|
items, stop, err := t.grabFunction(ctx, t.accountID, "", "", offsetID, amount)
|
2021-06-23 18:42:20 +02:00
|
|
|
if err != nil {
|
2022-02-05 12:47:38 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if stop {
|
|
|
|
break grabloop
|
2021-06-23 18:42:20 +02:00
|
|
|
}
|
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
l.Trace("filtering...")
|
|
|
|
// now filter each item using the caller-provided filter function
|
|
|
|
for _, item := range items {
|
|
|
|
shouldIndex, err := t.filterFunction(ctx, t.accountID, item)
|
2021-06-23 18:42:20 +02:00
|
|
|
if err != nil {
|
2022-02-05 12:47:38 +01:00
|
|
|
return err
|
2021-06-23 18:42:20 +02:00
|
|
|
}
|
2022-02-05 12:47:38 +01:00
|
|
|
if shouldIndex {
|
|
|
|
toIndex = append(toIndex, item)
|
2021-06-23 18:42:20 +02:00
|
|
|
}
|
2022-02-05 12:47:38 +01:00
|
|
|
offsetID = item.GetID()
|
2021-06-23 18:42:20 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-05 12:47:38 +01:00
|
|
|
l.Trace("left grabloop")
|
2021-06-23 18:42:20 +02:00
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
// index the items we got
|
|
|
|
for _, s := range toIndex {
|
|
|
|
if _, err := t.IndexOne(ctx, s.GetID(), s.GetBoostOfID(), s.GetAccountID(), s.GetBoostOfAccountID()); err != nil {
|
|
|
|
return fmt.Errorf("IndexBehind: error indexing item with id %s: %s", s.GetID(), err)
|
2021-06-23 18:42:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-13 18:42:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
func (t *timeline) IndexBehind(ctx context.Context, itemID string, amount int) error {
|
2021-10-11 14:37:33 +02:00
|
|
|
l := logrus.WithFields(logrus.Fields{
|
2022-02-05 12:47:38 +01:00
|
|
|
"func": "IndexBehind",
|
|
|
|
"amount": amount,
|
2021-08-15 18:43:08 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// lazily initialize index if it hasn't been done already
|
2022-02-05 12:47:38 +01:00
|
|
|
if t.itemIndex.data == nil {
|
|
|
|
t.itemIndex.data = &list.List{}
|
|
|
|
t.itemIndex.data.Init()
|
2021-08-15 18:43:08 +02:00
|
|
|
}
|
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
// If we're already indexedBehind given itemID by the required amount, we can return nil.
|
|
|
|
// First find position of itemID (or as near as possible).
|
2021-08-15 18:43:08 +02:00
|
|
|
var position int
|
|
|
|
positionLoop:
|
2022-02-05 12:47:38 +01:00
|
|
|
for e := t.itemIndex.data.Front(); e != nil; e = e.Next() {
|
|
|
|
entry, ok := e.Value.(*itemIndexEntry)
|
2021-08-15 18:43:08 +02:00
|
|
|
if !ok {
|
2022-02-05 12:47:38 +01:00
|
|
|
return errors.New("IndexBehind: could not parse e as an itemIndexEntry")
|
2021-08-15 18:43:08 +02:00
|
|
|
}
|
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
if entry.itemID <= itemID {
|
2021-08-15 18:43:08 +02:00
|
|
|
// we've found it
|
|
|
|
break positionLoop
|
|
|
|
}
|
|
|
|
position++
|
|
|
|
}
|
2022-02-05 12:47:38 +01:00
|
|
|
|
|
|
|
// now check if the length of indexed items exceeds the amount of items required (position of itemID, plus amount of posts requested after that)
|
|
|
|
if t.itemIndex.data.Len() > position+amount {
|
2021-08-15 18:43:08 +02:00
|
|
|
// we have enough indexed behind already to satisfy amount, so don't need to make db calls
|
2022-02-05 12:47:38 +01:00
|
|
|
l.Trace("returning nil since we already have enough items indexed")
|
2021-08-15 18:43:08 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
toIndex := []Timelineable{}
|
|
|
|
offsetID := itemID
|
2021-06-13 18:42:28 +02:00
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
l.Trace("entering grabloop")
|
2021-06-13 18:42:28 +02:00
|
|
|
grabloop:
|
2022-02-05 12:47:38 +01:00
|
|
|
for i := 0; len(toIndex) < amount && i < 5; i++ { // try the grabloop 5 times only
|
|
|
|
// first grab items using the caller-provided grab function
|
|
|
|
l.Trace("grabbing...")
|
|
|
|
items, stop, err := t.grabFunction(ctx, t.accountID, offsetID, "", "", amount)
|
2021-06-13 18:42:28 +02:00
|
|
|
if err != nil {
|
2022-02-05 12:47:38 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if stop {
|
|
|
|
break grabloop
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
l.Trace("filtering...")
|
|
|
|
// now filter each item using the caller-provided filter function
|
|
|
|
for _, item := range items {
|
|
|
|
shouldIndex, err := t.filterFunction(ctx, t.accountID, item)
|
2021-06-13 18:42:28 +02:00
|
|
|
if err != nil {
|
2022-02-05 12:47:38 +01:00
|
|
|
return err
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
2022-02-05 12:47:38 +01:00
|
|
|
if shouldIndex {
|
|
|
|
toIndex = append(toIndex, item)
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
2022-02-05 12:47:38 +01:00
|
|
|
offsetID = item.GetID()
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-15 18:43:08 +02:00
|
|
|
l.Trace("left grabloop")
|
2021-06-13 18:42:28 +02:00
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
// index the items we got
|
|
|
|
for _, s := range toIndex {
|
|
|
|
if _, err := t.IndexOne(ctx, s.GetID(), s.GetBoostOfID(), s.GetAccountID(), s.GetBoostOfAccountID()); err != nil {
|
|
|
|
return fmt.Errorf("IndexBehind: error indexing item with id %s: %s", s.GetID(), err)
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
func (t *timeline) IndexOne(ctx context.Context, itemID string, boostOfID string, accountID string, boostOfAccountID string) (bool, error) {
|
2021-06-13 18:42:28 +02:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
postIndexEntry := &itemIndexEntry{
|
|
|
|
itemID: itemID,
|
2021-07-11 16:22:21 +02:00
|
|
|
boostOfID: boostOfID,
|
|
|
|
accountID: accountID,
|
|
|
|
boostOfAccountID: boostOfAccountID,
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
return t.itemIndex.insertIndexed(ctx, postIndexEntry)
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
func (t *timeline) IndexAndPrepareOne(ctx context.Context, statusID string, boostOfID string, accountID string, boostOfAccountID string) (bool, error) {
|
2021-06-13 18:42:28 +02:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
postIndexEntry := &itemIndexEntry{
|
|
|
|
itemID: statusID,
|
2021-07-11 16:22:21 +02:00
|
|
|
boostOfID: boostOfID,
|
|
|
|
accountID: accountID,
|
|
|
|
boostOfAccountID: boostOfAccountID,
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
inserted, err := t.itemIndex.insertIndexed(ctx, postIndexEntry)
|
2021-06-19 11:18:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return inserted, fmt.Errorf("IndexAndPrepareOne: error inserting indexed: %s", err)
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 11:18:55 +02:00
|
|
|
if inserted {
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := t.prepare(ctx, statusID); err != nil {
|
2021-06-19 11:18:55 +02:00
|
|
|
return inserted, fmt.Errorf("IndexAndPrepareOne: error preparing: %s", err)
|
|
|
|
}
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 11:18:55 +02:00
|
|
|
return inserted, nil
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
func (t *timeline) OldestIndexedItemID(ctx context.Context) (string, error) {
|
2021-06-13 18:42:28 +02:00
|
|
|
var id string
|
2022-02-05 12:47:38 +01:00
|
|
|
if t.itemIndex == nil || t.itemIndex.data == nil || t.itemIndex.data.Back() == nil {
|
2021-06-13 18:42:28 +02:00
|
|
|
// return an empty string if postindex hasn't been initialized yet
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
e := t.itemIndex.data.Back()
|
|
|
|
entry, ok := e.Value.(*itemIndexEntry)
|
2021-08-15 18:43:08 +02:00
|
|
|
if !ok {
|
2022-02-05 12:47:38 +01:00
|
|
|
return id, errors.New("OldestIndexedItemID: could not parse e as itemIndexEntry")
|
2021-08-15 18:43:08 +02:00
|
|
|
}
|
2022-02-05 12:47:38 +01:00
|
|
|
return entry.itemID, nil
|
2021-08-15 18:43:08 +02:00
|
|
|
}
|
2021-06-13 18:42:28 +02:00
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
func (t *timeline) NewestIndexedItemID(ctx context.Context) (string, error) {
|
2021-08-15 18:43:08 +02:00
|
|
|
var id string
|
2022-02-05 12:47:38 +01:00
|
|
|
if t.itemIndex == nil || t.itemIndex.data == nil || t.itemIndex.data.Front() == nil {
|
2021-08-15 18:43:08 +02:00
|
|
|
// return an empty string if postindex hasn't been initialized yet
|
2021-06-13 18:42:28 +02:00
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2022-02-05 12:47:38 +01:00
|
|
|
e := t.itemIndex.data.Front()
|
|
|
|
entry, ok := e.Value.(*itemIndexEntry)
|
2021-06-13 18:42:28 +02:00
|
|
|
if !ok {
|
2022-02-05 12:47:38 +01:00
|
|
|
return id, errors.New("NewestIndexedItemID: could not parse e as itemIndexEntry")
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
2022-02-05 12:47:38 +01:00
|
|
|
return entry.itemID, nil
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|