| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |    GoToSocial | 
					
						
							| 
									
										
										
										
											2023-01-05 12:43:00 +01:00
										 |  |  |    Copyright (C) 2021-2023 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" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-19 09:47:55 +01:00
										 |  |  | 	"codeberg.org/gruf/go-kv" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/log" | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | func (t *timeline) ItemIndexLength(ctx context.Context) int { | 
					
						
							|  |  |  | 	if t.indexedItems == nil || t.indexedItems.data == nil { | 
					
						
							|  |  |  | 		return 0 | 
					
						
							| 
									
										
										
										
											2021-06-23 18:42:20 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 	return t.indexedItems.data.Len() | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | func (t *timeline) indexBehind(ctx context.Context, itemID string, amount int) error { | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 	l := log.WithContext(ctx). | 
					
						
							|  |  |  | 		WithFields(kv.Fields{{"amount", amount}}...) | 
					
						
							| 
									
										
										
										
											2021-08-15 18:43:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// lazily initialize index if it hasn't been done already | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 	if t.indexedItems.data == nil { | 
					
						
							|  |  |  | 		t.indexedItems.data = &list.List{} | 
					
						
							|  |  |  | 		t.indexedItems.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-11-22 19:38:10 +01:00
										 |  |  | 	for e := t.indexedItems.data.Front(); e != nil; e = e.Next() { | 
					
						
							|  |  |  | 		entry, ok := e.Value.(*indexedItemsEntry) | 
					
						
							| 
									
										
										
										
											2021-08-15 18:43:08 +02:00
										 |  |  | 		if !ok { | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +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) | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 	if t.indexedItems.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 { | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 			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-11-22 19:38:10 +01:00
										 |  |  | 	postIndexEntry := &indexedItemsEntry{ | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 		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-11-22 19:38:10 +01:00
										 |  |  | 	return t.indexedItems.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-11-22 19:38:10 +01:00
										 |  |  | 	postIndexEntry := &indexedItemsEntry{ | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 		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-11-22 19:38:10 +01:00
										 |  |  | 	inserted, err := t.indexedItems.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-11-22 19:38:10 +01:00
										 |  |  | 	if t.indexedItems == nil || t.indexedItems.data == nil || t.indexedItems.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-11-22 19:38:10 +01:00
										 |  |  | 	e := t.indexedItems.data.Back() | 
					
						
							|  |  |  | 	entry, ok := e.Value.(*indexedItemsEntry) | 
					
						
							| 
									
										
										
										
											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-11-22 19:38:10 +01:00
										 |  |  | 	if t.indexedItems == nil || t.indexedItems.data == nil || t.indexedItems.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-11-22 19:38:10 +01:00
										 |  |  | 	e := t.indexedItems.data.Front() | 
					
						
							|  |  |  | 	entry, ok := e.Value.(*indexedItemsEntry) | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } |