Skip to content

Commit

Permalink
[bugfix/chore] Refactor timeline code (#1656)
Browse files Browse the repository at this point in the history
* start poking timelines

* OK yes we're refactoring, but it's nothing like the last time so don't worry

* more fiddling

* update tests, simplify Get

* thanks linter, you're the best, mwah mwah kisses

* do a bit more tidying up

* start buggering about with the prepare function

* fix little oopsie

* start merging lists into 1

* ik heb een heel zwaar leven
nee nee echt waar

* hey it works we did it reddit

* regenerate swagger docs

* tidy up a wee bit

* adjust paging

* fix little error, remove unused functions
  • Loading branch information
tsmethurst authored Apr 6, 2023
1 parent c54510b commit 3510454
Show file tree
Hide file tree
Showing 22 changed files with 1,251 additions and 1,297 deletions.
4 changes: 2 additions & 2 deletions docs/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5582,11 +5582,11 @@ paths:
in: query
name: max_id
type: string
- description: Return only statuses *NEWER* than the given since status ID. The status with the specified ID will not be included in the response.
- description: Return only statuses *newer* than the given since status ID. The status with the specified ID will not be included in the response.
in: query
name: since_id
type: string
- description: Return only statuses *NEWER* than the given since status ID. The status with the specified ID will not be included in the response.
- description: Return only statuses *immediately newer* than the given since status ID. The status with the specified ID will not be included in the response.
in: query
name: min_id
type: string
Expand Down
4 changes: 2 additions & 2 deletions internal/api/client/timelines/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ import (
// name: since_id
// type: string
// description: >-
// Return only statuses *NEWER* than the given since status ID.
// Return only statuses *newer* than the given since status ID.
// The status with the specified ID will not be included in the response.
// in: query
// -
// name: min_id
// type: string
// description: >-
// Return only statuses *NEWER* than the given since status ID.
// Return only statuses *immediately newer* than the given since status ID.
// The status with the specified ID will not be included in the response.
// in: query
// required: false
Expand Down
36 changes: 30 additions & 6 deletions internal/db/bundb/timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ func (t *timelineDB) GetHomeTimeline(ctx context.Context, accountID string, maxI
}

// Make educated guess for slice size
statusIDs := make([]string, 0, limit)
var (
statusIDs = make([]string, 0, limit)
frontToBack = true
)

q := t.conn.
NewSelect().
Expand All @@ -56,11 +59,9 @@ func (t *timelineDB) GetHomeTimeline(ctx context.Context, accountID string, maxI
bun.Ident("follow.target_account_id"),
bun.Ident("status.account_id"),
bun.Ident("follow.account_id"),
accountID).
// Sort by highest ID (newest) to lowest ID (oldest)
Order("status.id DESC")
accountID)

if maxID == "" {
if maxID == "" || maxID == id.Highest {
const future = 24 * time.Hour

var err error
Expand All @@ -83,6 +84,9 @@ func (t *timelineDB) GetHomeTimeline(ctx context.Context, accountID string, maxI
if minID != "" {
// return only statuses HIGHER (ie., newer) than minID
q = q.Where("? > ?", bun.Ident("status.id"), minID)

// page up
frontToBack = false
}

if local {
Expand All @@ -95,6 +99,14 @@ func (t *timelineDB) GetHomeTimeline(ctx context.Context, accountID string, maxI
q = q.Limit(limit)
}

if frontToBack {
// Page down.
q = q.Order("status.id DESC")
} else {
// Page up.
q = q.Order("status.id ASC")
}

// Use a WhereGroup here to specify that we want EITHER statuses posted by accounts that accountID follows,
// OR statuses posted by accountID itself (since a user should be able to see their own statuses).
//
Expand All @@ -110,8 +122,20 @@ func (t *timelineDB) GetHomeTimeline(ctx context.Context, accountID string, maxI
return nil, t.conn.ProcessError(err)
}

statuses := make([]*gtsmodel.Status, 0, len(statusIDs))
if len(statusIDs) == 0 {
return nil, nil
}

// If we're paging up, we still want statuses
// to be sorted by ID desc, so reverse ids slice.
// https://zchee.github.io/golang-wiki/SliceTricks/#reversing
if !frontToBack {
for l, r := 0, len(statusIDs)-1; l < r; l, r = l+1, r-1 {
statusIDs[l], statusIDs[r] = statusIDs[r], statusIDs[l]
}
}

statuses := make([]*gtsmodel.Status, 0, len(statusIDs))
for _, id := range statusIDs {
// Fetch status from db for ID
status, err := t.state.DB.GetStatusByID(ctx, id)
Expand Down
26 changes: 26 additions & 0 deletions internal/db/bundb/timeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,32 @@ func (suite *TimelineTestSuite) TestGetHomeTimelineWithFutureStatus() {
suite.Len(s, 16)
}

func (suite *TimelineTestSuite) TestGetHomeTimelineBackToFront() {
ctx := context.Background()

viewingAccount := suite.testAccounts["local_account_1"]

s, err := suite.db.GetHomeTimeline(ctx, viewingAccount.ID, "", "", id.Lowest, 5, false)
suite.NoError(err)

suite.Len(s, 5)
suite.Equal("01F8MHAYFKS4KMXF8K5Y1C0KRN", s[0].ID)
suite.Equal("01F8MH75CBF9JFX4ZAD54N0W0R", s[len(s)-1].ID)
}

func (suite *TimelineTestSuite) TestGetHomeTimelineFromHighest() {
ctx := context.Background()

viewingAccount := suite.testAccounts["local_account_1"]

s, err := suite.db.GetHomeTimeline(ctx, viewingAccount.ID, id.Highest, "", "", 5, false)
suite.NoError(err)

suite.Len(s, 5)
suite.Equal("01G36SF3V6Y6V5BF9P4R7PQG7G", s[0].ID)
suite.Equal("01FCTA44PW9H1TB328S9AQXKDS", s[len(s)-1].ID)
}

func getFutureStatus() *gtsmodel.Status {
theDistantFuture := time.Now().Add(876600 * time.Hour)
id, err := id.NewULIDFromTime(theDistantFuture)
Expand Down
4 changes: 2 additions & 2 deletions internal/processing/fromcommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,8 @@ func (p *Processor) timelineStatusForAccount(ctx context.Context, account *gtsmo
return nil
}

// stick the status in the timeline for the account and then immediately prepare it so they can see it right away
if inserted, err := p.statusTimelines.IngestAndPrepare(ctx, status, account.ID); err != nil {
// stick the status in the timeline for the account
if inserted, err := p.statusTimelines.IngestOne(ctx, account.ID, status); err != nil {
return fmt.Errorf("timelineStatusForAccount: error ingesting status %s: %w", status.ID, err)
} else if !inserted {
return nil
Expand Down
Loading

0 comments on commit 3510454

Please sign in to comment.