gotosocial/internal/timeline/timeline.go

158 lines
5.3 KiB
Go
Raw Normal View History

/*
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 (
"errors"
"sync"
"time"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/db"
2021-06-02 17:52:15 +00:00
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
)
2021-06-01 19:36:55 +00:00
const (
2021-06-02 17:52:15 +00:00
preparedPostsMaxLength = desiredPostIndexLength
2021-06-01 19:36:55 +00:00
)
2021-06-01 18:09:28 +00:00
2021-06-03 16:16:33 +00:00
// Timeline represents a timeline for one account, and contains indexed and prepared posts.
2021-06-02 17:52:15 +00:00
type Timeline interface {
2021-06-03 16:16:33 +00:00
/*
RETRIEVAL FUNCTIONS
*/
2021-06-02 17:52:15 +00:00
// GetXFromTop returns x amount of posts from the top of the timeline, from newest to oldest.
GetXFromTop(amount int) ([]*apimodel.Status, error)
// GetXBehindID returns x amount of posts from the given id onwards, from newest to oldest.
// This will NOT include the status with the given ID.
//
// This corresponds to an api call to /timelines/home?max_id=WHATEVER
GetXBehindID(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.
//
// This corresponds to an api call to /timelines/home?since_id=WHATEVER
2021-06-06 16:02:03 +00:00
GetXBeforeID(amount int, sinceID string, startFromTop bool) ([]*apimodel.Status, error)
// GetXBetweenID returns x amount of posts from the given maxID, up to the given id, from newest to oldest.
// This will NOT include the status with the given IDs.
//
// This corresponds to an api call to /timelines/home?since_id=WHATEVER&max_id=WHATEVER_ELSE
GetXBetweenID(amount int, maxID string, sinceID string) ([]*apimodel.Status, error)
2021-06-02 17:52:15 +00:00
2021-06-03 16:16:33 +00:00
/*
INDEXING FUNCTIONS
*/
2021-06-02 17:52:15 +00: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-06 10:01:20 +00:00
// Remove removes a status from both the index and prepared posts.
//
// If a status has multiple entries in a timeline, they will all be removed.
//
// The returned int indicates the amount of entries that were removed.
Remove(statusID string) (int, error)
2021-06-02 17:52:15 +00:00
// 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)
2021-06-03 16:16:33 +00:00
/*
PREPARATION FUNCTIONS
*/
2021-06-02 17:52:15 +00:00
// PrepareXFromTop instructs the timeline to prepare x amount of posts from the top of the timeline.
2021-06-06 16:02:03 +00:00
PrepareFromTop(amount int) error
// PrepareBehind instructs the timeline to prepare the next amount of entries for serialization, from position onwards.
// If include is true, then the given status ID will also be prepared, otherwise only entries behind it will be prepared.
PrepareBehind(statusID string, include bool, amount int) error
2021-06-03 16:16:33 +00: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
/*
INFO FUNCTIONS
*/
2021-06-02 17:52:15 +00:00
// ActualPostIndexLength returns the actual length of the post index at this point in time.
PostIndexLength() int
2021-06-03 16:16:33 +00:00
/*
UTILITY FUNCTIONS
*/
2021-06-02 17:52:15 +00:00
// Reset instructs the timeline to reset to its base state -- cache only the minimum amount of posts.
Reset() error
}
2021-06-03 16:16:33 +00:00
// timeline fulfils the Timeline interface
type timeline struct {
2021-06-02 17:52:15 +00:00
postIndex *postIndex
preparedPosts *preparedPosts
2021-06-01 18:09:28 +00:00
accountID string
2021-06-02 17:52:15 +00:00
account *gtsmodel.Account
2021-06-01 18:09:28 +00:00
db db.DB
2021-06-02 17:52:15 +00:00
tc typeutils.TypeConverter
sync.Mutex
}
2021-06-03 16:16:33 +00:00
// NewTimeline returns a new Timeline for the given account ID
2021-06-02 17:52:15 +00:00
func NewTimeline(accountID string, db db.DB, typeConverter typeutils.TypeConverter) Timeline {
return &timeline{
2021-06-02 17:52:15 +00:00
postIndex: &postIndex{},
preparedPosts: &preparedPosts{},
2021-06-01 18:09:28 +00:00
accountID: accountID,
2021-06-01 19:36:55 +00:00
db: db,
2021-06-02 17:52:15 +00:00
tc: typeConverter,
}
}
2021-06-02 17:52:15 +00:00
func (t *timeline) Reset() error {
return nil
}
2021-06-02 17:52:15 +00: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 08:57:24 +00:00
// return an empty string if postindex hasn't been initialized yet
2021-06-02 17:52:15 +00:00
return id, nil
}
2021-06-03 08:57:24 +00:00
2021-06-02 17:52:15 +00:00
e := t.postIndex.data.Back()
if e == nil {
2021-06-03 08:57:24 +00:00
// return an empty string if there's no back entry (ie., the index list hasn't been initialized yet)
2021-06-02 17:52:15 +00: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
}