summaryrefslogtreecommitdiff
path: root/internal/timeline/get.go
blob: f9a5444fc54b72ad92b44423cd48b98c5f5aaf55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
   GoToSocial
   Copyright (C) 2021-2023 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"
	"context"
	"errors"
	"fmt"
	"time"

	"codeberg.org/gruf/go-kv"
	"github.com/superseriousbusiness/gotosocial/internal/log"
)

const retries = 5

func (t *timeline) LastGot() time.Time {
	t.Lock()
	defer t.Unlock()
	return t.lastGot
}

func (t *timeline) Get(ctx context.Context, amount int, maxID string, sinceID string, minID string, prepareNext bool) ([]Preparable, error) {
	l := log.WithContext(ctx).
		WithFields(kv.Fields{
			{"accountID", t.accountID},
			{"amount", amount},
			{"maxID", maxID},
			{"sinceID", sinceID},
			{"minID", minID},
		}...)
	l.Trace("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()

	var items []Preparable
	var err error

	// no params are defined to just fetch from the top
	// this is equivalent to a user asking for the top x items from their timeline
	if maxID == "" && sinceID == "" && minID == "" {
		items, err = t.getXFromTop(ctx, amount)
		// aysnchronously prepare the next predicted query so it's ready when the user asks for it
		if len(items) != 0 {
			nextMaxID := items[len(items)-1].GetID()
			if prepareNext {
				// already cache the next query to speed up scrolling
				go func() {
					// 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 {
						l.Errorf("error preparing next query: %s", err)
					}
				}()
			}
		}
	}

	// maxID is defined but sinceID isn't so take from behind
	// this is equivalent to a user asking for the next x items from their timeline, starting from maxID
	if maxID != "" && sinceID == "" {
		attempts := 0
		items, err = t.getXBehindID(ctx, amount, maxID, &attempts)
		// aysnchronously prepare the next predicted query so it's ready when the user asks for it
		if len(items) != 0 {
			nextMaxID := items[len(items)-1].GetID()
			if prepareNext {
				// already cache the next query to speed up scrolling
				go func() {
					// 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 {
						l.Errorf("error preparing next query: %s", err)
					}
				}()
			}
		}
	}

	// maxID is defined and sinceID || minID are as well, so take a slice between them
	// this is equivalent to a user asking for items older than x but newer than y
	if maxID != "" && sinceID != "" {
		items, err = t.getXBetweenID(ctx, amount, maxID, minID)
	}
	if maxID != "" && minID != "" {
		items, err = t.getXBetweenID(ctx, amount, maxID, minID)
	}

	// maxID isn't defined, but sinceID || minID are, so take x before
	// this is equivalent to a user asking for items newer than x (eg., refreshing the top of their timeline)
	if maxID == "" && sinceID != "" {
		items, err = t.getXBeforeID(ctx, amount, sinceID, true)
	}
	if maxID == "" && minID != "" {
		items, err = t.getXBeforeID(ctx, amount, minID, true)
	}

	return items, err
}

// 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) {
	// make a slice of preparedItems with the length we need to return
	preparedItems := make([]Preparable, 0, amount)

	if t.preparedItems.data == nil {
		t.preparedItems.data = &list.List{}
	}

	// make sure we have enough items prepared to return
	if t.preparedItems.data.Len() < amount {
		if err := t.PrepareFromTop(ctx, amount); err != nil {
			return nil, err
		}
	}

	// work through the prepared items from the top and return
	var served int
	for e := t.preparedItems.data.Front(); e != nil; e = e.Next() {
		entry, ok := e.Value.(*preparedItemsEntry)
		if !ok {
			return nil, errors.New("getXFromTop: could not parse e as a preparedItemsEntry")
		}
		preparedItems = append(preparedItems, entry.prepared)
		served++
		if served >= amount {
			break
		}
	}

	return preparedItems, nil
}

// 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) {
	l := log.WithContext(ctx).
		WithFields(kv.Fields{
			{"amount", amount},
			{"behindID", behindID},
			{"attempts", attempts},
		}...)

	newAttempts := *attempts
	newAttempts++
	attempts = &newAttempts

	// make a slice of items with the length we need to return
	items := make([]Preparable, 0, amount)

	if t.preparedItems.data == nil {
		t.preparedItems.data = &list.List{}
	}

	// iterate through the modified list until we hit the mark we're looking for
	var position int
	var behindIDMark *list.Element

findMarkLoop:
	for e := t.preparedItems.data.Front(); e != nil; e = e.Next() {
		position++
		entry, ok := e.Value.(*preparedItemsEntry)
		if !ok {
			return nil, errors.New("getXBehindID: could not parse e as a preparedPostsEntry")
		}

		if entry.itemID <= behindID {
			l.Trace("found behindID mark")
			behindIDMark = e
			break findMarkLoop
		}
	}

	// we didn't find it, so we need to make sure it's indexed and prepared and then try again
	// this can happen when a user asks for really old items
	if behindIDMark == nil {
		if err := t.prepareBehind(ctx, behindID, amount); err != nil {
			return nil, fmt.Errorf("getXBehindID: error preparing behind and including ID %s", behindID)
		}
		oldestID, err := t.oldestPreparedItemID(ctx)
		if err != nil {
			return nil, err
		}
		if oldestID == "" {
			l.Tracef("oldestID is empty so we can't return behindID %s", behindID)
			return items, nil
		}
		if oldestID == behindID {
			l.Tracef("given behindID %s is the same as oldestID %s so there's nothing to return behind it", behindID, oldestID)
			return items, nil
		}
		if *attempts > retries {
			l.Tracef("exceeded retries looking for behindID %s", behindID)
			return items, nil
		}
		l.Trace("trying getXBehindID again")
		return t.getXBehindID(ctx, amount, behindID, attempts)
	}

	// make sure we have enough items prepared behind it to return what we're being asked for
	if t.preparedItems.data.Len() < amount+position {
		if err := t.prepareBehind(ctx, behindID, amount); err != nil {
			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() {
		entry, ok := e.Value.(*preparedItemsEntry)
		if !ok {
			return nil, errors.New("getXBehindID: could not parse e as a preparedPostsEntry")
		}

		// serve up to the amount requested
		items = append(items, entry.prepared)
		served++
		if served >= amount {
			break serveloop
		}
	}

	return items, nil
}

// 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) {
	// make a slice of items with the length we need to return
	items := make([]Preparable, 0, amount)

	if t.preparedItems.data == nil {
		t.preparedItems.data = &list.List{}
	}

	// iterate through the modified list until we hit the mark we're looking for, or as close as possible to it
	var beforeIDMark *list.Element
findMarkLoop:
	for e := t.preparedItems.data.Front(); e != nil; e = e.Next() {
		entry, ok := e.Value.(*preparedItemsEntry)
		if !ok {
			return nil, errors.New("getXBeforeID: could not parse e as a preparedPostsEntry")
		}

		if entry.itemID >= beforeID {
			beforeIDMark = e
		} else {
			break findMarkLoop
		}
	}

	if beforeIDMark == nil {
		return items, nil
	}

	var served int

	if startFromTop {
		// start serving from the front/top and keep going until we hit mark or get x amount items
	serveloopFromTop:
		for e := t.preparedItems.data.Front(); e != nil; e = e.Next() {
			entry, ok := e.Value.(*preparedItemsEntry)
			if !ok {
				return nil, errors.New("getXBeforeID: could not parse e as a preparedPostsEntry")
			}

			if entry.itemID == beforeID {
				break serveloopFromTop
			}

			// serve up to the amount requested
			items = append(items, entry.prepared)
			served++
			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() {
			entry, ok := e.Value.(*preparedItemsEntry)
			if !ok {
				return nil, errors.New("getXBeforeID: could not parse e as a preparedPostsEntry")
			}

			// serve up to the amount requested
			items = append(items, entry.prepared)
			served++
			if served >= amount {
				break serveloopFromBottom
			}
		}
	}

	return items, nil
}

// 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) {
	// make a slice of items with the length we need to return
	items := make([]Preparable, 0, amount)

	if t.preparedItems.data == nil {
		t.preparedItems.data = &list.List{}
	}

	// iterate through the modified list until we hit the mark we're looking for
	var position int
	var behindIDMark *list.Element
findMarkLoop:
	for e := t.preparedItems.data.Front(); e != nil; e = e.Next() {
		position++
		entry, ok := e.Value.(*preparedItemsEntry)
		if !ok {
			return nil, errors.New("getXBetweenID: could not parse e as a preparedPostsEntry")
		}

		if entry.itemID == behindID {
			behindIDMark = e
			break findMarkLoop
		}
	}

	// we didn't find it
	if behindIDMark == nil {
		return nil, fmt.Errorf("getXBetweenID: couldn't find item with ID %s", behindID)
	}

	// make sure we have enough items prepared behind it to return what we're being asked for
	if t.preparedItems.data.Len() < amount+position {
		if err := t.prepareBehind(ctx, behindID, amount); err != nil {
			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() {
		entry, ok := e.Value.(*preparedItemsEntry)
		if !ok {
			return nil, errors.New("getXBetweenID: could not parse e as a preparedPostsEntry")
		}

		if entry.itemID == beforeID {
			break serveloop
		}

		// serve up to the amount requested
		items = append(items, entry.prepared)
		served++
		if served >= amount {
			break serveloop
		}
	}

	return items, nil
}