| 
									
										
										
										
											2021-06-01 13:19:50 +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/>. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package timeline | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"container/list" | 
					
						
							|  |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2021-06-01 20:09:28 +02:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2021-06-01 13:19:50 +02:00
										 |  |  | 	"sync" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/db" | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/typeutils" | 
					
						
							| 
									
										
										
										
											2021-06-01 13:19:50 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-01 21:36:55 +02:00
										 |  |  | const ( | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	preparedPostsMaxLength = desiredPostIndexLength | 
					
						
							| 
									
										
										
										
											2021-06-01 21:36:55 +02:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2021-06-01 20:09:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | type Timeline interface { | 
					
						
							|  |  |  | 	// GetXFromTop returns x amount of posts from the top of the timeline, from newest to oldest. | 
					
						
							|  |  |  | 	GetXFromTop(amount int) ([]*apimodel.Status, error) | 
					
						
							| 
									
										
										
										
											2021-06-03 18:11:25 +02:00
										 |  |  | 	// GetXFromIDOnwards returns x amount of posts from the given id onwards, from newest to oldest. | 
					
						
							| 
									
										
										
										
											2021-06-03 10:57:24 +02:00
										 |  |  | 	// This will include the status with the given ID. | 
					
						
							| 
									
										
										
										
											2021-06-03 18:11:25 +02:00
										 |  |  | 	GetXFromIDOnwards(amount int, fromID string) ([]*apimodel.Status, error) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// GetXBeforeID returns x amount of posts up to the given id, from newest to oldest. | 
					
						
							|  |  |  | 	// This will NOT include the status with the given ID. | 
					
						
							|  |  |  | 	GetXBeforeID(amount int, sinceID string) ([]*apimodel.Status, error) | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// IndexOne puts a status into the timeline at the appropriate place according to its 'createdAt' property. | 
					
						
							|  |  |  | 	IndexOne(statusCreatedAt time.Time, statusID string) error | 
					
						
							| 
									
										
										
										
											2021-06-03 18:11:25 +02:00
										 |  |  | 	// IndexOne puts a status into the timeline at the appropriate place according to its 'createdAt' property, | 
					
						
							|  |  |  | 	// and then immediately prepares it. | 
					
						
							|  |  |  | 	IndexAndPrepareOne(statusCreatedAt time.Time, statusID string) error | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	// Remove removes a status from the timeline. | 
					
						
							|  |  |  | 	Remove(statusID string) error | 
					
						
							|  |  |  | 	// OldestIndexedPostID returns the id of the rearmost (ie., the oldest) indexed post, or an error if something goes wrong. | 
					
						
							|  |  |  | 	// If nothing goes wrong but there's no oldest post, an empty string will be returned so make sure to check for this. | 
					
						
							|  |  |  | 	OldestIndexedPostID() (string, error) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// PrepareXFromTop instructs the timeline to prepare x amount of posts from the top of the timeline. | 
					
						
							|  |  |  | 	PrepareXFromTop(amount int) error | 
					
						
							|  |  |  | 	// PrepareXFromIndex instrucst the timeline to prepare the next amount of entries for serialization, from index onwards. | 
					
						
							|  |  |  | 	PrepareXFromIndex(amount int, index int) error | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// ActualPostIndexLength returns the actual length of the post index at this point in time. | 
					
						
							|  |  |  | 	PostIndexLength() int | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Reset instructs the timeline to reset to its base state -- cache only the minimum amount of posts. | 
					
						
							|  |  |  | 	Reset() error | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-01 13:19:50 +02:00
										 |  |  | type timeline struct { | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	postIndex     *postIndex | 
					
						
							|  |  |  | 	preparedPosts *preparedPosts | 
					
						
							| 
									
										
										
										
											2021-06-01 20:09:28 +02:00
										 |  |  | 	accountID     string | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	account       *gtsmodel.Account | 
					
						
							| 
									
										
										
										
											2021-06-01 20:09:28 +02:00
										 |  |  | 	db            db.DB | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	tc            typeutils.TypeConverter | 
					
						
							|  |  |  | 	sync.Mutex | 
					
						
							| 
									
										
										
										
											2021-06-01 13:19:50 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | func NewTimeline(accountID string, db db.DB, typeConverter typeutils.TypeConverter) Timeline { | 
					
						
							| 
									
										
										
										
											2021-06-01 13:19:50 +02:00
										 |  |  | 	return &timeline{ | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 		postIndex:     &postIndex{}, | 
					
						
							|  |  |  | 		preparedPosts: &preparedPosts{}, | 
					
						
							| 
									
										
										
										
											2021-06-01 20:09:28 +02:00
										 |  |  | 		accountID:     accountID, | 
					
						
							| 
									
										
										
										
											2021-06-01 21:36:55 +02:00
										 |  |  | 		db:            db, | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 		tc:            typeConverter, | 
					
						
							| 
									
										
										
										
											2021-06-01 13:19:50 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | func (t *timeline) PrepareXFromIndex(amount int, index int) error { | 
					
						
							| 
									
										
										
										
											2021-06-01 13:19:50 +02:00
										 |  |  | 	t.Lock() | 
					
						
							|  |  |  | 	defer t.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	var indexed int | 
					
						
							|  |  |  | 	var prepared int | 
					
						
							|  |  |  | 	var preparing bool | 
					
						
							|  |  |  | 	for e := t.postIndex.data.Front(); e != nil; e = e.Next() { | 
					
						
							|  |  |  | 		entry, ok := e.Value.(*postIndexEntry) | 
					
						
							| 
									
										
										
										
											2021-06-01 20:09:28 +02:00
										 |  |  | 		if !ok { | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 			return errors.New("PrepareXFromTop: could not parse e as a postIndexEntry") | 
					
						
							| 
									
										
										
										
											2021-06-01 20:09:28 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 		if !preparing { | 
					
						
							|  |  |  | 			// we haven't hit the index we need to prepare from yet | 
					
						
							|  |  |  | 			if indexed == index { | 
					
						
							|  |  |  | 				preparing = true | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			indexed = indexed + 1 | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			if err := t.prepare(entry.statusID); err != nil { | 
					
						
							|  |  |  | 				return fmt.Errorf("PrepareXFromTop: error preparing status with id %s: %s", entry.statusID, err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			prepared = prepared + 1 | 
					
						
							|  |  |  | 			if prepared >= amount { | 
					
						
							|  |  |  | 				// we're done | 
					
						
							|  |  |  | 				break | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-06-01 20:09:28 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-06-01 20:09:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | func (t *timeline) PrepareXFromTop(amount int) error { | 
					
						
							|  |  |  | 	t.Lock() | 
					
						
							|  |  |  | 	defer t.Unlock() | 
					
						
							| 
									
										
										
										
											2021-06-01 20:09:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	t.preparedPosts.data.Init() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var prepared int | 
					
						
							|  |  |  | 	for e := t.postIndex.data.Front(); e != nil; e = e.Next() { | 
					
						
							|  |  |  | 		entry, ok := e.Value.(*postIndexEntry) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			return errors.New("PrepareXFromTop: could not parse e as a postIndexEntry") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if err := t.prepare(entry.statusID); err != nil { | 
					
						
							|  |  |  | 			return fmt.Errorf("PrepareXFromTop: error preparing status with id %s: %s", entry.statusID, err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		prepared = prepared + 1 | 
					
						
							|  |  |  | 		if prepared >= amount { | 
					
						
							|  |  |  | 			// we're done | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-06-01 20:09:28 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-01 13:19:50 +02:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | func (t *timeline) GetXFromTop(amount int) ([]*apimodel.Status, error) { | 
					
						
							|  |  |  | 	// make a slice of statuses with the length we need to return | 
					
						
							|  |  |  | 	statuses := make([]*apimodel.Status, 0, amount) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// if there are no prepared posts, just return the empty slice | 
					
						
							|  |  |  | 	if t.preparedPosts.data == nil { | 
					
						
							|  |  |  | 		t.preparedPosts.data = &list.List{} | 
					
						
							| 
									
										
										
										
											2021-06-01 20:09:28 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	// make sure we have enough posts prepared to return | 
					
						
							|  |  |  | 	if t.preparedPosts.data.Len() < amount { | 
					
						
							|  |  |  | 		if err := t.PrepareXFromTop(amount); err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-01 20:09:28 +02:00
										 |  |  | 			return nil, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	// 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-06-01 20:09:28 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 18:11:25 +02:00
										 |  |  | func (t *timeline) GetXFromIDOnwards(amount int, fromID string) ([]*apimodel.Status, error) { | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	// make a slice of statuses with the length we need to return | 
					
						
							|  |  |  | 	statuses := make([]*apimodel.Status, 0, amount) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// if there are no prepared posts, just return the empty slice | 
					
						
							|  |  |  | 	if t.preparedPosts.data == nil { | 
					
						
							|  |  |  | 		t.preparedPosts.data = &list.List{} | 
					
						
							| 
									
										
										
										
											2021-06-01 20:09:28 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	// find the position of id | 
					
						
							|  |  |  | 	var position int | 
					
						
							|  |  |  | 	for e := t.preparedPosts.data.Front(); e != nil; e = e.Next() { | 
					
						
							|  |  |  | 		entry, ok := e.Value.(*preparedPostsEntry) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			return nil, errors.New("GetXBehindID: could not parse e as a preparedPostsEntry") | 
					
						
							| 
									
										
										
										
											2021-06-01 20:09:28 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 		if entry.statusID == fromID { | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		position = position + 1 | 
					
						
							| 
									
										
										
										
											2021-06-01 20:09:28 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:57:24 +02:00
										 |  |  | 	// make sure we have enough posts prepared behind it to return what we're being asked for | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	if t.preparedPosts.data.Len() < amount+position { | 
					
						
							|  |  |  | 		if err := t.PrepareXFromIndex(amount, position); err != nil { | 
					
						
							| 
									
										
										
										
											2021-06-01 20:09:28 +02:00
										 |  |  | 			return nil, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	// iterate through the modified list until we hit the fromID again | 
					
						
							|  |  |  | 	var serving bool | 
					
						
							|  |  |  | 	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("GetXBehindID: could not parse e as a preparedPostsEntry") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if !serving { | 
					
						
							| 
									
										
										
										
											2021-06-03 10:57:24 +02:00
										 |  |  | 			// start serving if we've hit the id we're looking for | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 			if entry.statusID == fromID { | 
					
						
							|  |  |  | 				serving = true | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-06-03 10:57:24 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if serving { | 
					
						
							|  |  |  | 			// serve up to the amount requested | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 			statuses = append(statuses, entry.prepared) | 
					
						
							|  |  |  | 			served = served + 1 | 
					
						
							|  |  |  | 			if served >= amount { | 
					
						
							|  |  |  | 				break | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return statuses, nil | 
					
						
							| 
									
										
										
										
											2021-06-01 13:19:50 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 18:11:25 +02:00
										 |  |  | func (t *timeline) GetXBeforeID(amount int, beforeID string) ([]*apimodel.Status, error) { | 
					
						
							|  |  |  | 	// make a slice of statuses with the length we need to return | 
					
						
							|  |  |  | 	statuses := make([]*apimodel.Status, 0, amount) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// if there are no prepared posts, just return the empty slice | 
					
						
							|  |  |  | 	if t.preparedPosts.data == nil { | 
					
						
							|  |  |  | 		t.preparedPosts.data = &list.List{} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// iterate through the modified list until we hit the fromID again | 
					
						
							|  |  |  | 	var served int | 
					
						
							|  |  |  | servloop: | 
					
						
							|  |  |  | 	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 { | 
					
						
							|  |  |  | 			// we're good | 
					
						
							|  |  |  | 			break servloop | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// serve up to the amount requested | 
					
						
							|  |  |  | 		statuses = append(statuses, entry.prepared) | 
					
						
							|  |  |  | 		served = served + 1 | 
					
						
							|  |  |  | 		if served >= amount { | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return statuses, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | func (t *timeline) IndexOne(statusCreatedAt time.Time, statusID string) error { | 
					
						
							| 
									
										
										
										
											2021-06-01 13:19:50 +02:00
										 |  |  | 	t.Lock() | 
					
						
							|  |  |  | 	defer t.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	postIndexEntry := &postIndexEntry{ | 
					
						
							|  |  |  | 		createdAt: statusCreatedAt, | 
					
						
							|  |  |  | 		statusID:  statusID, | 
					
						
							| 
									
										
										
										
											2021-06-01 20:09:28 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-06-03 10:57:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return t.postIndex.insertIndexed(postIndexEntry) | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-06-01 20:09:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 18:11:25 +02:00
										 |  |  | func (t *timeline) IndexAndPrepareOne(statusCreatedAt time.Time, statusID string) error { | 
					
						
							|  |  |  | 	t.Lock() | 
					
						
							|  |  |  | 	defer t.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	postIndexEntry := &postIndexEntry{ | 
					
						
							|  |  |  | 		createdAt: statusCreatedAt, | 
					
						
							|  |  |  | 		statusID:  statusID, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := t.postIndex.insertIndexed(postIndexEntry); err != nil { | 
					
						
							|  |  |  | 		return fmt.Errorf("IndexAndPrepareOne: error inserting indexed: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := t.prepare(statusID); err != nil { | 
					
						
							|  |  |  | 		return fmt.Errorf("IndexAndPrepareOne: error preparing: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | func (t *timeline) Remove(statusID string) error { | 
					
						
							|  |  |  | 	t.Lock() | 
					
						
							|  |  |  | 	defer t.Unlock() | 
					
						
							| 
									
										
										
										
											2021-06-01 13:19:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	// remove the entry from the post index | 
					
						
							|  |  |  | 	for e := t.postIndex.data.Front(); e != nil; e = e.Next() { | 
					
						
							|  |  |  | 		entry, ok := e.Value.(*postIndexEntry) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			return errors.New("Remove: could not parse e as a postIndexEntry") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if entry.statusID == statusID { | 
					
						
							|  |  |  | 			t.postIndex.data.Remove(e) | 
					
						
							| 
									
										
										
										
											2021-06-03 10:57:24 +02:00
										 |  |  | 			break // bail once we found and removed it | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-06-01 13:19:50 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	// remove the entry from prepared posts | 
					
						
							|  |  |  | 	for e := t.preparedPosts.data.Front(); e != nil; e = e.Next() { | 
					
						
							|  |  |  | 		entry, ok := e.Value.(*preparedPostsEntry) | 
					
						
							| 
									
										
										
										
											2021-06-01 13:19:50 +02:00
										 |  |  | 		if !ok { | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 			return errors.New("Remove: could not parse e as a preparedPostsEntry") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if entry.statusID == statusID { | 
					
						
							|  |  |  | 			t.preparedPosts.data.Remove(e) | 
					
						
							| 
									
										
										
										
											2021-06-03 10:57:24 +02:00
										 |  |  | 			break // bail once we found and removed it | 
					
						
							| 
									
										
										
										
											2021-06-01 13:19:50 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *timeline) Reset() error { | 
					
						
							| 
									
										
										
										
											2021-06-01 13:19:50 +02:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | func (t *timeline) PostIndexLength() int { | 
					
						
							|  |  |  | 	if t.postIndex == nil || t.postIndex.data == nil { | 
					
						
							|  |  |  | 		return 0 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return t.postIndex.data.Len() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *timeline) OldestIndexedPostID() (string, error) { | 
					
						
							|  |  |  | 	var id string | 
					
						
							|  |  |  | 	if t.postIndex == nil || t.postIndex.data == nil { | 
					
						
							| 
									
										
										
										
											2021-06-03 10:57:24 +02:00
										 |  |  | 		// return an empty string if postindex hasn't been initialized yet | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 		return id, nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-06-03 10:57:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	e := t.postIndex.data.Back() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if e == nil { | 
					
						
							| 
									
										
										
										
											2021-06-03 10:57:24 +02:00
										 |  |  | 		// return an empty string if there's no back entry (ie., the index list hasn't been initialized yet) | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 		return id, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	entry, ok := e.Value.(*postIndexEntry) | 
					
						
							|  |  |  | 	if !ok { | 
					
						
							|  |  |  | 		return id, errors.New("OldestIndexedPostID: could not parse e as a postIndexEntry") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return entry.statusID, nil | 
					
						
							| 
									
										
										
										
											2021-06-01 13:19:50 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-06-01 21:36:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | func (t *timeline) prepare(statusID string) error { | 
					
						
							| 
									
										
										
										
											2021-06-03 10:57:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// start by getting the status out of the database according to its indexed ID | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	gtsStatus := >smodel.Status{} | 
					
						
							|  |  |  | 	if err := t.db.GetByID(statusID, gtsStatus); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:57:24 +02:00
										 |  |  | 	// if the account pointer hasn't been set on this timeline already, set it lazily here | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	if t.account == nil { | 
					
						
							|  |  |  | 		timelineOwnerAccount := >smodel.Account{} | 
					
						
							|  |  |  | 		if err := t.db.GetByID(t.accountID, timelineOwnerAccount); err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		t.account = timelineOwnerAccount | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:57:24 +02:00
										 |  |  | 	// to convert the status we need relevant accounts from it, so pull them out here | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	relevantAccounts, err := t.db.PullRelevantAccountsFromStatus(gtsStatus) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:57:24 +02:00
										 |  |  | 	// check if this is a boost... | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	var reblogOfStatus *gtsmodel.Status | 
					
						
							|  |  |  | 	if gtsStatus.BoostOfID != "" { | 
					
						
							|  |  |  | 		s := >smodel.Status{} | 
					
						
							|  |  |  | 		if err := t.db.GetByID(gtsStatus.BoostOfID, s); err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		reblogOfStatus = s | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:57:24 +02:00
										 |  |  | 	// serialize the status (or, at least, convert it to a form that's ready to be serialized) | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	apiModelStatus, err := t.tc.StatusToMasto(gtsStatus, relevantAccounts.StatusAuthor, t.account, relevantAccounts.BoostedAccount, relevantAccounts.ReplyToAccount, reblogOfStatus) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 10:57:24 +02:00
										 |  |  | 	// shove it in prepared posts as a prepared posts entry | 
					
						
							| 
									
										
										
										
											2021-06-02 19:52:15 +02:00
										 |  |  | 	preparedPostsEntry := &preparedPostsEntry{ | 
					
						
							|  |  |  | 		createdAt: gtsStatus.CreatedAt, | 
					
						
							|  |  |  | 		statusID:  statusID, | 
					
						
							|  |  |  | 		prepared:  apiModelStatus, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return t.preparedPosts.insertPrepared(preparedPostsEntry) | 
					
						
							| 
									
										
										
										
											2021-06-01 21:36:55 +02:00
										 |  |  | } |