| 
									
										
										
										
											2021-07-11 16:22:21 +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/>. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | package timeline | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-06-21 12:27:23 +02:00
										 |  |  | 	"container/list" | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	"errors" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-15 18:43:08 +02:00
										 |  |  | 	"github.com/sirupsen/logrus" | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/db" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *timeline) prepareNextQuery(amount int, maxID string, sinceID string, minID string) error { | 
					
						
							| 
									
										
										
										
											2021-08-15 18:43:08 +02:00
										 |  |  | 	l := t.log.WithFields(logrus.Fields{ | 
					
						
							|  |  |  | 		"func":    "prepareNextQuery", | 
					
						
							|  |  |  | 		"amount":  amount, | 
					
						
							|  |  |  | 		"maxID":   maxID, | 
					
						
							|  |  |  | 		"sinceID": sinceID, | 
					
						
							|  |  |  | 		"minID":   minID, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	var err error | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// maxID is defined but sinceID isn't so take from behind | 
					
						
							|  |  |  | 	if maxID != "" && sinceID == "" { | 
					
						
							| 
									
										
										
										
											2021-08-15 18:43:08 +02:00
										 |  |  | 		l.Debug("preparing behind maxID") | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		err = t.PrepareBehind(maxID, amount) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// maxID isn't defined, but sinceID || minID are, so take x before | 
					
						
							|  |  |  | 	if maxID == "" && sinceID != "" { | 
					
						
							| 
									
										
										
										
											2021-08-15 18:43:08 +02:00
										 |  |  | 		l.Debug("preparing before sinceID") | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		err = t.PrepareBefore(sinceID, false, amount) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if maxID == "" && minID != "" { | 
					
						
							| 
									
										
										
										
											2021-08-15 18:43:08 +02:00
										 |  |  | 		l.Debug("preparing before minID") | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		err = t.PrepareBefore(minID, false, amount) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *timeline) PrepareBehind(statusID string, amount int) error { | 
					
						
							| 
									
										
										
										
											2021-06-21 12:27:23 +02:00
										 |  |  | 	// lazily initialize prepared posts if it hasn't been done already | 
					
						
							|  |  |  | 	if t.preparedPosts.data == nil { | 
					
						
							|  |  |  | 		t.preparedPosts.data = &list.List{} | 
					
						
							|  |  |  | 		t.preparedPosts.data.Init() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-15 18:43:08 +02:00
										 |  |  | 	if err := t.IndexBehind(statusID, true, amount); err != nil { | 
					
						
							|  |  |  | 		return fmt.Errorf("PrepareBehind: error indexing behind id %s: %s", statusID, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-21 12:27:23 +02:00
										 |  |  | 	// if the postindex is nil, nothing has been indexed yet so there's nothing to prepare | 
					
						
							|  |  |  | 	if t.postIndex.data == nil { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	var prepared int | 
					
						
							|  |  |  | 	var preparing bool | 
					
						
							| 
									
										
										
										
											2021-08-15 18:43:08 +02:00
										 |  |  | 	t.Lock() | 
					
						
							|  |  |  | 	defer t.Unlock() | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | prepareloop: | 
					
						
							|  |  |  | 	for e := t.postIndex.data.Front(); e != nil; e = e.Next() { | 
					
						
							|  |  |  | 		entry, ok := e.Value.(*postIndexEntry) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			return errors.New("PrepareBehind: could not parse e as a postIndexEntry") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if !preparing { | 
					
						
							|  |  |  | 			// we haven't hit the position we need to prepare from yet | 
					
						
							|  |  |  | 			if entry.statusID == statusID { | 
					
						
							|  |  |  | 				preparing = true | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if preparing { | 
					
						
							|  |  |  | 			if err := t.prepare(entry.statusID); err != nil { | 
					
						
							|  |  |  | 				// there's been an error | 
					
						
							|  |  |  | 				if _, ok := err.(db.ErrNoEntries); !ok { | 
					
						
							|  |  |  | 					// it's a real error | 
					
						
							|  |  |  | 					return fmt.Errorf("PrepareBehind: error preparing status with id %s: %s", entry.statusID, err) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				// the status just doesn't exist (anymore) so continue to the next one | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if prepared == amount { | 
					
						
							|  |  |  | 				// we're done | 
					
						
							|  |  |  | 				break prepareloop | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			prepared = prepared + 1 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *timeline) PrepareBefore(statusID string, include bool, amount int) error { | 
					
						
							|  |  |  | 	t.Lock() | 
					
						
							|  |  |  | 	defer t.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-21 12:27:23 +02:00
										 |  |  | 	// lazily initialize prepared posts if it hasn't been done already | 
					
						
							|  |  |  | 	if t.preparedPosts.data == nil { | 
					
						
							|  |  |  | 		t.preparedPosts.data = &list.List{} | 
					
						
							|  |  |  | 		t.preparedPosts.data.Init() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// if the postindex is nil, nothing has been indexed yet so there's nothing to prepare | 
					
						
							|  |  |  | 	if t.postIndex.data == nil { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	var prepared int | 
					
						
							|  |  |  | 	var preparing bool | 
					
						
							|  |  |  | prepareloop: | 
					
						
							|  |  |  | 	for e := t.postIndex.data.Back(); e != nil; e = e.Prev() { | 
					
						
							|  |  |  | 		entry, ok := e.Value.(*postIndexEntry) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			return errors.New("PrepareBefore: could not parse e as a postIndexEntry") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if !preparing { | 
					
						
							|  |  |  | 			// we haven't hit the position we need to prepare from yet | 
					
						
							|  |  |  | 			if entry.statusID == statusID { | 
					
						
							|  |  |  | 				preparing = true | 
					
						
							|  |  |  | 				if !include { | 
					
						
							|  |  |  | 					continue | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if preparing { | 
					
						
							|  |  |  | 			if err := t.prepare(entry.statusID); err != nil { | 
					
						
							|  |  |  | 				// there's been an error | 
					
						
							|  |  |  | 				if _, ok := err.(db.ErrNoEntries); !ok { | 
					
						
							|  |  |  | 					// it's a real error | 
					
						
							|  |  |  | 					return fmt.Errorf("PrepareBefore: error preparing status with id %s: %s", entry.statusID, err) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				// the status just doesn't exist (anymore) so continue to the next one | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if prepared == amount { | 
					
						
							|  |  |  | 				// we're done | 
					
						
							|  |  |  | 				break prepareloop | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			prepared = prepared + 1 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *timeline) PrepareFromTop(amount int) error { | 
					
						
							| 
									
										
										
										
											2021-08-15 18:43:08 +02:00
										 |  |  | 	l := t.log.WithFields(logrus.Fields{ | 
					
						
							|  |  |  | 		"func":   "PrepareFromTop", | 
					
						
							|  |  |  | 		"amount": amount, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-21 12:27:23 +02:00
										 |  |  | 	// lazily initialize prepared posts if it hasn't been done already | 
					
						
							|  |  |  | 	if t.preparedPosts.data == nil { | 
					
						
							|  |  |  | 		t.preparedPosts.data = &list.List{} | 
					
						
							|  |  |  | 		t.preparedPosts.data.Init() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-15 18:43:08 +02:00
										 |  |  | 	// if the postindex is nil, nothing has been indexed yet so index from the highest ID possible | 
					
						
							| 
									
										
										
										
											2021-06-21 12:27:23 +02:00
										 |  |  | 	if t.postIndex.data == nil { | 
					
						
							| 
									
										
										
										
											2021-08-15 18:43:08 +02:00
										 |  |  | 		l.Debug("postindex.data was nil, indexing behind highest possible ID") | 
					
						
							|  |  |  | 		if err := t.IndexBehind("ZZZZZZZZZZZZZZZZZZZZZZZZZZ", false, amount); err != nil { | 
					
						
							|  |  |  | 			return fmt.Errorf("PrepareFromTop: error indexing behind id %s: %s", "ZZZZZZZZZZZZZZZZZZZZZZZZZZ", err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-06-21 12:27:23 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-15 18:43:08 +02:00
										 |  |  | 	l.Trace("entering prepareloop") | 
					
						
							|  |  |  | 	t.Lock() | 
					
						
							|  |  |  | 	defer t.Unlock() | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	var prepared int | 
					
						
							|  |  |  | prepareloop: | 
					
						
							|  |  |  | 	for e := t.postIndex.data.Front(); e != nil; e = e.Next() { | 
					
						
							| 
									
										
										
										
											2021-06-21 12:27:23 +02:00
										 |  |  | 		if e == nil { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 		entry, ok := e.Value.(*postIndexEntry) | 
					
						
							|  |  |  | 		if !ok { | 
					
						
							|  |  |  | 			return errors.New("PrepareFromTop: could not parse e as a postIndexEntry") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if err := t.prepare(entry.statusID); err != nil { | 
					
						
							|  |  |  | 			// there's been an error | 
					
						
							|  |  |  | 			if _, ok := err.(db.ErrNoEntries); !ok { | 
					
						
							|  |  |  | 				// it's a real error | 
					
						
							|  |  |  | 				return fmt.Errorf("PrepareFromTop: error preparing status with id %s: %s", entry.statusID, err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			// the status just doesn't exist (anymore) so continue to the next one | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		prepared = prepared + 1 | 
					
						
							|  |  |  | 		if prepared == amount { | 
					
						
							|  |  |  | 			// we're done | 
					
						
							| 
									
										
										
										
											2021-08-15 18:43:08 +02:00
										 |  |  | 			l.Trace("leaving prepareloop") | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 			break prepareloop | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-15 18:43:08 +02:00
										 |  |  | 	l.Trace("leaving function") | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *timeline) prepare(statusID string) error { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// start by getting the status out of the database according to its indexed ID | 
					
						
							|  |  |  | 	gtsStatus := >smodel.Status{} | 
					
						
							|  |  |  | 	if err := t.db.GetByID(statusID, gtsStatus); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// if the account pointer hasn't been set on this timeline already, set it lazily here | 
					
						
							|  |  |  | 	if t.account == nil { | 
					
						
							|  |  |  | 		timelineOwnerAccount := >smodel.Account{} | 
					
						
							|  |  |  | 		if err := t.db.GetByID(t.accountID, timelineOwnerAccount); err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		t.account = timelineOwnerAccount | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// serialize the status (or, at least, convert it to a form that's ready to be serialized) | 
					
						
							| 
									
										
										
										
											2021-06-17 18:02:33 +02:00
										 |  |  | 	apiModelStatus, err := t.tc.StatusToMasto(gtsStatus, t.account) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// shove it in prepared posts as a prepared posts entry | 
					
						
							|  |  |  | 	preparedPostsEntry := &preparedPostsEntry{ | 
					
						
							| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | 		statusID:         gtsStatus.ID, | 
					
						
							|  |  |  | 		boostOfID:        gtsStatus.BoostOfID, | 
					
						
							|  |  |  | 		accountID:        gtsStatus.AccountID, | 
					
						
							|  |  |  | 		boostOfAccountID: gtsStatus.BoostOfAccountID, | 
					
						
							|  |  |  | 		prepared:         apiModelStatus, | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return t.preparedPosts.insertPrepared(preparedPostsEntry) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *timeline) OldestPreparedPostID() (string, error) { | 
					
						
							|  |  |  | 	var id string | 
					
						
							|  |  |  | 	if t.preparedPosts == nil || t.preparedPosts.data == nil { | 
					
						
							|  |  |  | 		// return an empty string if prepared posts hasn't been initialized yet | 
					
						
							|  |  |  | 		return id, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	e := t.preparedPosts.data.Back() | 
					
						
							|  |  |  | 	if e == nil { | 
					
						
							|  |  |  | 		// return an empty string if there's no back entry (ie., the index list hasn't been initialized yet) | 
					
						
							|  |  |  | 		return id, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	entry, ok := e.Value.(*preparedPostsEntry) | 
					
						
							|  |  |  | 	if !ok { | 
					
						
							|  |  |  | 		return id, errors.New("OldestPreparedPostID: could not parse e as a preparedPostsEntry") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return entry.statusID, nil | 
					
						
							|  |  |  | } |