| 
									
										
										
										
											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 ( | 
					
						
							|  |  |  | 	"container/list" | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	"errors" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-10 16:56:59 +02:00
										 |  |  | const retries = 5 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | func (t *timeline) LastGot() time.Time { | 
					
						
							|  |  |  | 	t.Lock() | 
					
						
							|  |  |  | 	defer t.Unlock() | 
					
						
							|  |  |  | 	return t.lastGot | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | func (t *timeline) Get(ctx context.Context, amount int, maxID string, sinceID string, minID string, prepareNext bool) ([]Preparable, error) { | 
					
						
							| 
									
										
										
										
											2022-07-19 09:47:55 +01:00
										 |  |  | 	l := log.WithFields(kv.Fields{ | 
					
						
							|  |  |  | 		{"accountID", t.accountID}, | 
					
						
							|  |  |  | 		{"amount", amount}, | 
					
						
							|  |  |  | 		{"maxID", maxID}, | 
					
						
							|  |  |  | 		{"sinceID", sinceID}, | 
					
						
							|  |  |  | 		{"minID", minID}, | 
					
						
							|  |  |  | 	}...) | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 	l.Debug("entering get and updating t.lastGot") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// regardless of what happens below, update the | 
					
						
							|  |  |  | 	// last time Get was called for this timeline | 
					
						
							|  |  |  | 	t.Lock() | 
					
						
							|  |  |  | 	t.lastGot = time.Now() | 
					
						
							|  |  |  | 	t.Unlock() | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	var items []Preparable | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	var err error | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// no params are defined to just fetch from the top | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	// this is equivalent to a user asking for the top x items from their timeline | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	if maxID == "" && sinceID == "" && minID == "" { | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 		items, 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 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 		if len(items) != 0 { | 
					
						
							|  |  |  | 			nextMaxID := items[len(items)-1].GetID() | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	// this is equivalent to a user asking for the next x items 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 | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 		items, 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 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 		if len(items) != 0 { | 
					
						
							|  |  |  | 			nextMaxID := items[len(items)-1].GetID() | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	// this is equivalent to a user asking for items older than x but newer than y | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	if maxID != "" && sinceID != "" { | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 		items, err = t.getXBetweenID(ctx, amount, maxID, minID) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if maxID != "" && minID != "" { | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 		items, 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 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	// this is equivalent to a user asking for items newer than x (eg., refreshing the top of their timeline) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	if maxID == "" && sinceID != "" { | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 		items, err = t.getXBeforeID(ctx, amount, sinceID, true) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if maxID == "" && minID != "" { | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 		items, err = t.getXBeforeID(ctx, amount, minID, true) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	return items, err | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | // getXFromTop returns x amount of items from the top of the timeline, from newest to oldest. | 
					
						
							|  |  |  | func (t *timeline) getXFromTop(ctx context.Context, amount int) ([]Preparable, error) { | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	// make a slice of preparedItems with the length we need to return | 
					
						
							|  |  |  | 	preparedItems := make([]Preparable, 0, amount) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	if t.preparedItems.data == nil { | 
					
						
							|  |  |  | 		t.preparedItems.data = &list.List{} | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	// make sure we have enough items prepared to return | 
					
						
							|  |  |  | 	if t.preparedItems.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 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	// work through the prepared items from the top and return | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	var served int | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	for e := t.preparedItems.data.Front(); e != nil; e = e.Next() { | 
					
						
							|  |  |  | 		entry, ok := e.Value.(*preparedItemsEntry) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		if !ok { | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 			return nil, errors.New("getXFromTop: could not parse e as a preparedItemsEntry") | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 		preparedItems = append(preparedItems, entry.prepared) | 
					
						
							| 
									
										
										
										
											2021-11-22 08:46:19 +01:00
										 |  |  | 		served++ | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		if served >= amount { | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	return preparedItems, nil | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | // getXBehindID returns x amount of items from the given id onwards, from newest to oldest. | 
					
						
							|  |  |  | // This will NOT include the item with the given ID. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This corresponds to an api call to /timelines/home?max_id=WHATEVER | 
					
						
							|  |  |  | func (t *timeline) getXBehindID(ctx context.Context, amount int, behindID string, attempts *int) ([]Preparable, error) { | 
					
						
							| 
									
										
										
										
											2022-07-19 09:47:55 +01:00
										 |  |  | 	l := log.WithFields(kv.Fields{ | 
					
						
							|  |  |  | 		{"amount", amount}, | 
					
						
							|  |  |  | 		{"behindID", behindID}, | 
					
						
							|  |  |  | 		{"attempts", attempts}, | 
					
						
							|  |  |  | 	}...) | 
					
						
							| 
									
										
										
										
											2021-08-15 18:43:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-10 16:56:59 +02:00
										 |  |  | 	newAttempts := *attempts | 
					
						
							| 
									
										
										
										
											2021-11-22 08:46:19 +01:00
										 |  |  | 	newAttempts++ | 
					
						
							| 
									
										
										
										
											2021-08-10 16:56:59 +02:00
										 |  |  | 	attempts = &newAttempts | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	// make a slice of items with the length we need to return | 
					
						
							|  |  |  | 	items := make([]Preparable, 0, amount) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	if t.preparedItems.data == nil { | 
					
						
							|  |  |  | 		t.preparedItems.data = &list.List{} | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// iterate through the modified list until we hit the mark we're looking for | 
					
						
							|  |  |  | 	var position int | 
					
						
							|  |  |  | 	var behindIDMark *list.Element | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | findMarkLoop: | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	for e := t.preparedItems.data.Front(); e != nil; e = e.Next() { | 
					
						
							| 
									
										
										
										
											2021-11-22 08:46:19 +01:00
										 |  |  | 		position++ | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 		entry, ok := e.Value.(*preparedItemsEntry) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		if !ok { | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 			return nil, errors.New("getXBehindID: could not parse e as a preparedPostsEntry") | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 		if entry.itemID <= behindID { | 
					
						
							| 
									
										
										
										
											2021-08-15 18:43:08 +02:00
										 |  |  | 			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 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	// this can happen when a user asks for really old items | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	if behindIDMark == nil { | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 		if err := t.prepareBehind(ctx, behindID, amount); err != nil { | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("getXBehindID: error preparing behind and including ID %s", behindID) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 		oldestID, err := t.oldestPreparedItemID(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) | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 			return items, nil | 
					
						
							| 
									
										
										
										
											2021-08-15 18:43:08 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if oldestID == behindID { | 
					
						
							|  |  |  | 			l.Tracef("given behindID %s is the same as oldestID %s so there's nothing to return behind it", behindID, oldestID) | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 			return items, nil | 
					
						
							| 
									
										
										
										
											2021-08-15 18:43:08 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if *attempts > retries { | 
					
						
							|  |  |  | 			l.Tracef("exceeded retries looking for behindID %s", behindID) | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 			return items, nil | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 		l.Trace("trying getXBehindID again") | 
					
						
							|  |  |  | 		return t.getXBehindID(ctx, amount, behindID, attempts) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	// make sure we have enough items prepared behind it to return what we're being asked for | 
					
						
							|  |  |  | 	if t.preparedItems.data.Len() < amount+position { | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01: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() { | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 		entry, ok := e.Value.(*preparedItemsEntry) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		if !ok { | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 			return nil, errors.New("getXBehindID: could not parse e as a preparedPostsEntry") | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// serve up to the amount requested | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 		items = append(items, entry.prepared) | 
					
						
							| 
									
										
										
										
											2021-11-22 08:46:19 +01:00
										 |  |  | 		served++ | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		if served >= amount { | 
					
						
							|  |  |  | 			break serveloop | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	return items, nil | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | // getXBeforeID returns x amount of items up to the given id, from newest to oldest. | 
					
						
							|  |  |  | // This will NOT include the item with the given ID. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This corresponds to an api call to /timelines/home?since_id=WHATEVER | 
					
						
							|  |  |  | func (t *timeline) getXBeforeID(ctx context.Context, amount int, beforeID string, startFromTop bool) ([]Preparable, error) { | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	// make a slice of items with the length we need to return | 
					
						
							|  |  |  | 	items := make([]Preparable, 0, amount) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	if t.preparedItems.data == nil { | 
					
						
							|  |  |  | 		t.preparedItems.data = &list.List{} | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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: | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	for e := t.preparedItems.data.Front(); e != nil; e = e.Next() { | 
					
						
							|  |  |  | 		entry, ok := e.Value.(*preparedItemsEntry) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		if !ok { | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 			return nil, errors.New("getXBeforeID: could not parse e as a preparedPostsEntry") | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 		if entry.itemID >= 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 { | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 		return items, nil | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var served int | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if startFromTop { | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 		// start serving from the front/top and keep going until we hit mark or get x amount items | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	serveloopFromTop: | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 		for e := t.preparedItems.data.Front(); e != nil; e = e.Next() { | 
					
						
							|  |  |  | 			entry, ok := e.Value.(*preparedItemsEntry) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 			if !ok { | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 				return nil, errors.New("getXBeforeID: could not parse e as a preparedPostsEntry") | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 			if entry.itemID == beforeID { | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 				break serveloopFromTop | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// serve up to the amount requested | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 			items = append(items, entry.prepared) | 
					
						
							| 
									
										
										
										
											2021-11-22 08:46:19 +01:00
										 |  |  | 			served++ | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 			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() { | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 			entry, ok := e.Value.(*preparedItemsEntry) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 			if !ok { | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 				return nil, errors.New("getXBeforeID: could not parse e as a preparedPostsEntry") | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// serve up to the amount requested | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 			items = append(items, entry.prepared) | 
					
						
							| 
									
										
										
										
											2021-11-22 08:46:19 +01:00
										 |  |  | 			served++ | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 			if served >= amount { | 
					
						
							|  |  |  | 				break serveloopFromBottom | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	return items, nil | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | // getXBetweenID returns x amount of items from the given maxID, up to the given id, from newest to oldest. | 
					
						
							|  |  |  | // This will NOT include the item with the given IDs. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This corresponds to an api call to /timelines/home?since_id=WHATEVER&max_id=WHATEVER_ELSE | 
					
						
							|  |  |  | func (t *timeline) getXBetweenID(ctx context.Context, amount int, behindID string, beforeID string) ([]Preparable, error) { | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	// make a slice of items with the length we need to return | 
					
						
							|  |  |  | 	items := make([]Preparable, 0, amount) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	if t.preparedItems.data == nil { | 
					
						
							|  |  |  | 		t.preparedItems.data = &list.List{} | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// iterate through the modified list until we hit the mark we're looking for | 
					
						
							|  |  |  | 	var position int | 
					
						
							|  |  |  | 	var behindIDMark *list.Element | 
					
						
							|  |  |  | findMarkLoop: | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	for e := t.preparedItems.data.Front(); e != nil; e = e.Next() { | 
					
						
							| 
									
										
										
										
											2021-11-22 08:46:19 +01:00
										 |  |  | 		position++ | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 		entry, ok := e.Value.(*preparedItemsEntry) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		if !ok { | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 			return nil, errors.New("getXBetweenID: could not parse e as a preparedPostsEntry") | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 		if entry.itemID == behindID { | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 			behindIDMark = e | 
					
						
							|  |  |  | 			break findMarkLoop | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// we didn't find it | 
					
						
							|  |  |  | 	if behindIDMark == nil { | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 		return nil, fmt.Errorf("getXBetweenID: couldn't find item with ID %s", behindID) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	// make sure we have enough items prepared behind it to return what we're being asked for | 
					
						
							|  |  |  | 	if t.preparedItems.data.Len() < amount+position { | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01: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() { | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 		entry, ok := e.Value.(*preparedItemsEntry) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		if !ok { | 
					
						
							| 
									
										
										
										
											2022-11-22 19:38:10 +01:00
										 |  |  | 			return nil, errors.New("getXBetweenID: could not parse e as a preparedPostsEntry") | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 		if entry.itemID == beforeID { | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 			break serveloop | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// serve up to the amount requested | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 		items = append(items, entry.prepared) | 
					
						
							| 
									
										
										
										
											2021-11-22 08:46:19 +01:00
										 |  |  | 		served++ | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		if served >= amount { | 
					
						
							|  |  |  | 			break serveloop | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 12:47:38 +01:00
										 |  |  | 	return items, nil | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | } |