synd-persistence 0.4.0

Persistence adapters for syndicationd
Documentation
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
use std::str::FromStr;

use chrono::{DateTime, Utc};
use sqlx::{QueryBuilder, Sqlite, Transaction};
use synd_feed::{
    entry::EntryId,
    types::{Annotated, Category, FeedUrl, Requirement},
};
use synd_registry::{
    RegistryDbResult,
    db::TimelineDb,
    query::{
        TimelineChange, TimelineChangesPage, TimelineChangesQuery, TimelineEntriesPage,
        TimelineEntriesQuery, TimelineEntry, TimelineEntryCursor,
    },
    subscription::{SubscriberId, SubscriptionKey},
    timeline::TimelineCatchup,
};

use super::{
    codec::{decode_stored_entry, decode_stored_feed_meta},
    error::{DecodeResultExt, IntoDbResult, SqliteError, SqliteResult},
    pagination::PageLimit,
};

const TIMELINE_ENTRY_SELECT: &str = r#"
SELECT
    te.order_time,
    te.entry_id,
    e.entry_json,
    f.url AS feed_url,
    fs.meta_json,
    s.requirement,
    s.category
FROM timeline_entry AS te
INNER JOIN entry AS e
    ON e.entry_id = te.entry_id
INNER JOIN feed AS f
    ON f.pk = e.feed_pk
INNER JOIN feed_snapshot AS fs
    ON fs.feed_pk = f.pk
INNER JOIN feed_subscription AS s
    ON s.subscriber_id = te.subscriber_id
   AND s.feed_pk = f.pk
"#;

async fn ensure_timeline(
    tx: &mut Transaction<'_, Sqlite>,
    subscriber_id: &SubscriberId,
) -> SqliteResult<()> {
    sqlx::query(
        r#"
            INSERT OR IGNORE INTO timeline (subscriber_id)
            VALUES (?)
            "#,
    )
    .bind(subscriber_id.as_str())
    .execute(&mut **tx)
    .await?;
    Ok(())
}

async fn catchup_feed(
    tx: &mut Transaction<'_, Sqlite>,
    subscriber_id: &SubscriberId,
    feed_url: &FeedUrl,
) -> SqliteResult<TimelineCatchup> {
    FeedCatchupCandidates::load(tx, subscriber_id, feed_url)
        .await?
        .apply(tx)
        .await
}

/// Current feed members that may need to be added to one subscriber timeline.
struct FeedCatchupCandidates {
    subscriber_id: SubscriberId,
    feed_url: FeedUrl,
    count: i64,
}

impl FeedCatchupCandidates {
    async fn load(
        tx: &mut Transaction<'_, Sqlite>,
        subscriber_id: &SubscriberId,
        feed_url: &FeedUrl,
    ) -> SqliteResult<Self> {
        let count = sqlx::query_scalar::<_, i64>(
            r#"
                SELECT COUNT(*)
                FROM feed_entry AS fe
                INNER JOIN feed AS f
                    ON f.pk = fe.feed_pk
                WHERE f.url = ?
                "#,
        )
        .bind(feed_url.as_str())
        .fetch_one(&mut **tx)
        .await?;
        Ok(Self {
            subscriber_id: subscriber_id.clone(),
            feed_url: feed_url.clone(),
            count,
        })
    }

    async fn apply(self, tx: &mut Transaction<'_, Sqlite>) -> SqliteResult<TimelineCatchup> {
        if self.count == 0 {
            return Ok(self.outcome(0));
        }

        // Every row gets its own seq: change pagination pages by seq alone, so
        // rows sharing a seq would be lost at page boundaries. Candidates left
        // untouched leave gaps, which is fine because seq only needs to be
        // unique and increasing.
        let base_seq = alloc_seq_range(tx, &self.subscriber_id, self.count).await?;
        let inserted = self.insert(tx, base_seq).await?;
        Ok(self.outcome(inserted))
    }

    async fn insert(&self, tx: &mut Transaction<'_, Sqlite>, base_seq: i64) -> SqliteResult<u64> {
        // Insert missing entries and revive tombstoned ones(resubscribe).
        // Live rows are left untouched so they emit no sync change.
        let result = sqlx::query(
            r#"
                INSERT INTO timeline_entry (
                    subscriber_id,
                    entry_id,
                    order_time,
                    seq
                )
                SELECT
                    ?,
                    e.entry_id,
                    e.order_time,
                    ? + ROW_NUMBER() OVER (ORDER BY e.entry_id)
                FROM feed_entry AS fe
                INNER JOIN entry AS e
                    ON e.feed_pk = fe.feed_pk
                   AND e.entry_id = fe.entry_id
                INNER JOIN feed AS f
                    ON f.pk = fe.feed_pk
                WHERE f.url = ?
                ON CONFLICT (subscriber_id, entry_id) DO UPDATE SET
                    seq = excluded.seq,
                    deleted = 0
                WHERE timeline_entry.deleted != 0
                "#,
        )
        .bind(self.subscriber_id.as_str())
        .bind(base_seq)
        .bind(self.feed_url.as_str())
        .execute(&mut **tx)
        .await?;
        Ok(result.rows_affected())
    }

    fn outcome(&self, added: u64) -> TimelineCatchup {
        TimelineCatchup::new(self.subscriber_id.clone(), self.feed_url.clone(), added)
    }
}

async fn apply_entry_to_timelines(
    tx: &mut Transaction<'_, Sqlite>,
    feed_url: &FeedUrl,
    entry_id: &EntryId,
    content_changed: bool,
) -> SqliteResult<Vec<SubscriberId>> {
    ensure_subscriber_timelines(tx, feed_url).await?;

    let mut affected = Vec::new();
    for target in load_entry_timeline_targets(tx, feed_url, entry_id).await? {
        let touched = match &target.existing {
            None => {
                let seq = next_seq(tx, &target.subscriber_id).await?;
                insert_timeline_entry(tx, &target, seq).await?;
                true
            }
            // order_time is frozen; the seq bump only tells syncing clients
            // to re-read the entry content or that a tombstone was revived
            Some(existing) if content_changed || existing.deleted => {
                let seq = next_seq(tx, &target.subscriber_id).await?;
                bump_timeline_entry(tx, &target, seq).await?;
                true
            }
            Some(_) => false,
        };
        if touched {
            affected.push(target.subscriber_id);
        }
    }
    Ok(affected)
}

async fn apply_feed_unsubscribed(
    tx: &mut Transaction<'_, Sqlite>,
    subscription: &SubscriptionKey,
) -> SqliteResult<Option<SubscriberId>> {
    let subscriber_id = &subscription.subscriber_id;
    if !timeline_exists(tx, subscriber_id).await? {
        return Ok(None);
    }
    let candidates = sqlx::query_scalar::<_, i64>(
        r#"
            SELECT COUNT(*)
            FROM timeline_entry AS te
            WHERE te.subscriber_id = ?
              AND te.deleted = 0
              AND te.entry_id IN (
                SELECT e.entry_id
                FROM entry AS e
                INNER JOIN feed AS f
                    ON f.pk = e.feed_pk
                WHERE f.url = ?
              )
            "#,
    )
    .bind(subscriber_id.as_str())
    .bind(subscription.feed_url.as_str())
    .fetch_one(&mut **tx)
    .await?;
    if candidates == 0 {
        return Ok(None);
    }

    // Removal keeps the row as a tombstone so syncing clients observe it
    // through the seq bump. Every row gets its own seq: change pagination
    // pages by seq alone, so rows sharing a seq would be lost at page
    // boundaries
    let base_seq = alloc_seq_range(tx, subscriber_id, candidates).await?;
    sqlx::query(
        r#"
            UPDATE timeline_entry
            SET
                deleted = 1,
                seq = ? + ranked.rn
            FROM (
                SELECT
                    te.entry_id,
                    ROW_NUMBER() OVER (ORDER BY te.entry_id) AS rn
                FROM timeline_entry AS te
                WHERE te.subscriber_id = ?
                  AND te.deleted = 0
                  AND te.entry_id IN (
                    SELECT e.entry_id
                    FROM entry AS e
                    INNER JOIN feed AS f
                        ON f.pk = e.feed_pk
                    WHERE f.url = ?
                  )
            ) AS ranked
            WHERE timeline_entry.subscriber_id = ?
              AND timeline_entry.entry_id = ranked.entry_id
            "#,
    )
    .bind(base_seq)
    .bind(subscriber_id.as_str())
    .bind(subscription.feed_url.as_str())
    .bind(subscriber_id.as_str())
    .execute(&mut **tx)
    .await?;

    Ok(Some(subscriber_id.clone()))
}

async fn list_entries(
    tx: &mut Transaction<'_, Sqlite>,
    query: TimelineEntriesQuery,
) -> SqliteResult<TimelineEntriesPage> {
    Ok(StoredTimelineEntriesPage::load(tx, query)
        .await?
        .into_page())
}

/// Overfetched timeline entries paired with the snapshot sequence they represent.
struct StoredTimelineEntriesPage {
    nodes: Vec<TimelineEntry>,
    limit: PageLimit,
    seq: i64,
}

impl StoredTimelineEntriesPage {
    async fn load(
        tx: &mut Transaction<'_, Sqlite>,
        query: TimelineEntriesQuery,
    ) -> SqliteResult<Self> {
        let limit = PageLimit::new(query.first);
        let Some(seq) = load_last_seq(tx, &query.subscriber_id).await? else {
            return Ok(Self {
                nodes: Vec::new(),
                limit,
                seq: 0,
            });
        };
        let nodes = load_timeline_entries(tx, &query, limit).await?;
        Ok(Self { nodes, limit, seq })
    }

    fn into_page(mut self) -> TimelineEntriesPage {
        let has_next_page = self.limit.truncate_overfetch(&mut self.nodes);
        let end_cursor = self.nodes.last().map(|node| node.cursor.clone());
        TimelineEntriesPage {
            nodes: self.nodes,
            has_next_page,
            end_cursor,
            seq: self.seq,
        }
    }
}

async fn load_timeline_entries(
    tx: &mut Transaction<'_, Sqlite>,
    query: &TimelineEntriesQuery,
    limit: PageLimit,
) -> SqliteResult<Vec<TimelineEntry>> {
    let mut sql = QueryBuilder::<Sqlite>::new(TIMELINE_ENTRY_SELECT);
    sql.push(" WHERE te.subscriber_id = ");
    sql.push_bind(query.subscriber_id.as_str());
    sql.push(" AND te.deleted = 0");
    if let Some(after) = query.after.as_ref() {
        sql.push(" AND (te.order_time, te.entry_id) < (");
        sql.push_bind(after.order_time());
        sql.push(", ");
        sql.push_bind(after.entry_id().as_str());
        sql.push(")");
    }

    sql.push(" ORDER BY te.order_time DESC, te.entry_id DESC LIMIT ");
    sql.push_bind(limit.sql_limit());

    let rows = sql
        .build_query_as::<TimelineEntryRow>()
        .fetch_all(&mut **tx)
        .await?;
    rows.into_iter().map(TimelineEntry::try_from).collect()
}

async fn list_changes(
    tx: &mut Transaction<'_, Sqlite>,
    query: TimelineChangesQuery,
) -> SqliteResult<TimelineChangesPage> {
    StoredTimelineChangesPage::load(tx, query)
        .await?
        .into_page()
}

/// Overfetched timeline change rows paired with their sequence window.
struct StoredTimelineChangesPage {
    rows: Vec<TimelineChangeRow>,
    limit: PageLimit,
    since: i64,
    last_seq: i64,
}

impl StoredTimelineChangesPage {
    async fn load(
        tx: &mut Transaction<'_, Sqlite>,
        query: TimelineChangesQuery,
    ) -> SqliteResult<Self> {
        let limit = PageLimit::new(query.limit);
        let Some(last_seq) = load_last_seq(tx, &query.subscriber_id).await? else {
            return Ok(Self {
                rows: Vec::new(),
                limit,
                since: query.since,
                last_seq: 0,
            });
        };
        let rows = load_timeline_change_rows(tx, &query, limit).await?;
        Ok(Self {
            rows,
            limit,
            since: query.since,
            last_seq,
        })
    }

    fn into_page(mut self) -> SqliteResult<TimelineChangesPage> {
        let has_more = self.limit.truncate_overfetch(&mut self.rows);
        let seq = if has_more {
            self.rows.last().map_or(self.since, |row| row.seq)
        } else {
            self.last_seq
        };
        let changes = self
            .rows
            .into_iter()
            .map(TimelineChange::try_from)
            .collect::<SqliteResult<Vec<_>>>()?;
        Ok(TimelineChangesPage {
            changes,
            seq,
            has_more,
        })
    }
}

async fn load_timeline_change_rows(
    tx: &mut Transaction<'_, Sqlite>,
    query: &TimelineChangesQuery,
    limit: PageLimit,
) -> SqliteResult<Vec<TimelineChangeRow>> {
    // Tombstoned entries have lost their subscription, so subscription and
    // snapshot columns are joined optionally and their absence also means
    // removal
    let rows = sqlx::query_as::<_, TimelineChangeRow>(
        r#"
            SELECT
                te.order_time,
                te.entry_id,
                te.seq,
                (te.deleted != 0 OR s.subscriber_id IS NULL) AS removed,
                e.entry_json,
                f.url AS feed_url,
                fs.meta_json,
                s.requirement,
                s.category
            FROM timeline_entry AS te
            INNER JOIN entry AS e
                ON e.entry_id = te.entry_id
            INNER JOIN feed AS f
                ON f.pk = e.feed_pk
            LEFT JOIN feed_snapshot AS fs
                ON fs.feed_pk = f.pk
            LEFT JOIN feed_subscription AS s
                ON s.subscriber_id = te.subscriber_id
               AND s.feed_pk = f.pk
            WHERE te.subscriber_id = ?
              AND te.seq > ?
            ORDER BY te.seq ASC
            LIMIT ?
            "#,
    )
    .bind(query.subscriber_id.as_str())
    .bind(query.since)
    .bind(limit.sql_limit())
    .fetch_all(&mut **tx)
    .await?;
    Ok(rows)
}

async fn timeline_exists(
    tx: &mut Transaction<'_, Sqlite>,
    subscriber_id: &SubscriberId,
) -> SqliteResult<bool> {
    Ok(load_last_seq(tx, subscriber_id).await?.is_some())
}

async fn load_last_seq(
    tx: &mut Transaction<'_, Sqlite>,
    subscriber_id: &SubscriberId,
) -> SqliteResult<Option<i64>> {
    let row = sqlx::query_scalar::<_, i64>(
        r#"
            SELECT last_seq
            FROM timeline
            WHERE subscriber_id = ?
            "#,
    )
    .bind(subscriber_id.as_str())
    .fetch_optional(&mut **tx)
    .await?;

    Ok(row)
}

/// Takes the next change seq of one timeline.
async fn next_seq(
    tx: &mut Transaction<'_, Sqlite>,
    subscriber_id: &SubscriberId,
) -> SqliteResult<i64> {
    Ok(alloc_seq_range(tx, subscriber_id, 1).await? + 1)
}

/// Allocates `count` change seqs of one timeline in one contiguous range.
/// The allocated seqs are `base + 1 ..= base + count` where `base` is the
/// returned value.
async fn alloc_seq_range(
    tx: &mut Transaction<'_, Sqlite>,
    subscriber_id: &SubscriberId,
    count: i64,
) -> SqliteResult<i64> {
    let last_seq = sqlx::query_scalar::<_, i64>(
        r#"
            UPDATE timeline
            SET last_seq = last_seq + ?
            WHERE subscriber_id = ?
            RETURNING last_seq
            "#,
    )
    .bind(count)
    .bind(subscriber_id.as_str())
    .fetch_one(&mut **tx)
    .await?;
    Ok(last_seq - count)
}

/// Ensures every subscriber of the feed has a timeline row to allocate
/// seqs from.
async fn ensure_subscriber_timelines(
    tx: &mut Transaction<'_, Sqlite>,
    feed_url: &FeedUrl,
) -> SqliteResult<()> {
    sqlx::query(
        r#"
            INSERT OR IGNORE INTO timeline (subscriber_id)
            SELECT s.subscriber_id
            FROM feed_subscription AS s
            INNER JOIN feed AS f
                ON f.pk = s.feed_pk
            WHERE f.url = ?
            "#,
    )
    .bind(feed_url.as_str())
    .execute(&mut **tx)
    .await?;
    Ok(())
}

async fn load_entry_timeline_targets(
    tx: &mut Transaction<'_, Sqlite>,
    feed_url: &FeedUrl,
    entry_id: &EntryId,
) -> SqliteResult<Vec<TimelineEntryTarget>> {
    let rows = sqlx::query_as::<_, TimelineEntryTargetRow>(
        r#"
            SELECT
                s.subscriber_id,
                e.entry_id,
                e.order_time AS entry_order_time,
                te.entry_id IS NOT NULL AS entry_exists,
                COALESCE(te.deleted, 0) != 0 AS entry_deleted
            FROM feed_subscription AS s
            INNER JOIN feed AS f
                ON f.pk = s.feed_pk
            INNER JOIN feed_entry AS fe
                ON fe.feed_pk = f.pk
            INNER JOIN entry AS e
                ON e.feed_pk = fe.feed_pk
               AND e.entry_id = fe.entry_id
            LEFT JOIN timeline_entry AS te
                ON te.subscriber_id = s.subscriber_id
               AND te.entry_id = e.entry_id
            WHERE f.url = ?
              AND e.entry_id = ?
            ORDER BY s.subscriber_id
            "#,
    )
    .bind(feed_url.as_str())
    .bind(entry_id.as_str())
    .fetch_all(&mut **tx)
    .await?;

    Ok(rows.into_iter().map(TimelineEntryTarget::from).collect())
}

async fn insert_timeline_entry(
    tx: &mut Transaction<'_, Sqlite>,
    target: &TimelineEntryTarget,
    seq: i64,
) -> SqliteResult<()> {
    sqlx::query(
        r#"
            INSERT INTO timeline_entry (
                subscriber_id,
                entry_id,
                order_time,
                seq
            )
            VALUES (?, ?, ?, ?)
            "#,
    )
    .bind(target.subscriber_id.as_str())
    .bind(&target.entry_id)
    .bind(target.entry_order_time)
    .bind(seq)
    .execute(&mut **tx)
    .await?;
    Ok(())
}

async fn bump_timeline_entry(
    tx: &mut Transaction<'_, Sqlite>,
    target: &TimelineEntryTarget,
    seq: i64,
) -> SqliteResult<()> {
    sqlx::query(
        r#"
            UPDATE timeline_entry
            SET
                seq = ?,
                deleted = 0
            WHERE subscriber_id = ?
              AND entry_id = ?
            "#,
    )
    .bind(seq)
    .bind(target.subscriber_id.as_str())
    .bind(&target.entry_id)
    .execute(&mut **tx)
    .await?;
    Ok(())
}

struct TimelineEntryTarget {
    subscriber_id: SubscriberId,
    entry_id: String,
    entry_order_time: DateTime<Utc>,
    existing: Option<ExistingTimelineEntry>,
}

struct ExistingTimelineEntry {
    deleted: bool,
}

#[derive(sqlx::FromRow)]
struct TimelineEntryTargetRow {
    subscriber_id: String,
    entry_id: String,
    entry_order_time: DateTime<Utc>,
    entry_exists: bool,
    entry_deleted: bool,
}

impl From<TimelineEntryTargetRow> for TimelineEntryTarget {
    fn from(row: TimelineEntryTargetRow) -> Self {
        Self {
            subscriber_id: SubscriberId::new(row.subscriber_id),
            entry_id: row.entry_id,
            entry_order_time: row.entry_order_time,
            existing: row.entry_exists.then_some(ExistingTimelineEntry {
                deleted: row.entry_deleted,
            }),
        }
    }
}

#[derive(sqlx::FromRow)]
struct TimelineChangeRow {
    #[sqlx(flatten)]
    entry: TimelineEntryColumns,
    seq: i64,
    removed: bool,
    meta_json: Option<String>,
}

impl TryFrom<TimelineChangeRow> for TimelineChange {
    type Error = SqliteError;

    fn try_from(row: TimelineChangeRow) -> Result<Self, Self::Error> {
        if row.removed {
            let entry_id = EntryId::parse(row.entry.entry_id).decode()?;
            return Ok(TimelineChange::Remove { entry_id });
        }

        let meta_json = row.meta_json.ok_or_else(|| {
            SqliteError::decode_message("timeline change row without feed snapshot")
        })?;
        let entry = TimelineEntry::try_from(TimelineEntryRow {
            entry: row.entry,
            meta_json,
        })?;
        Ok(TimelineChange::Upsert(Box::new(entry)))
    }
}

#[derive(sqlx::FromRow)]
struct TimelineEntryRow {
    #[sqlx(flatten)]
    entry: TimelineEntryColumns,
    meta_json: String,
}

#[derive(sqlx::FromRow)]
struct TimelineEntryColumns {
    order_time: DateTime<Utc>,
    entry_id: String,
    entry_json: String,
    feed_url: String,
    requirement: Option<String>,
    category: Option<String>,
}

impl TryFrom<TimelineEntryRow> for TimelineEntry {
    type Error = SqliteError;

    fn try_from(row: TimelineEntryRow) -> Result<Self, Self::Error> {
        let entry = decode_stored_entry(&row.entry.entry_id, &row.entry.entry_json)?;
        let feed_meta = decode_stored_feed_meta(&row.entry.feed_url, &row.meta_json)?;
        let requirement = row
            .entry
            .requirement
            .as_deref()
            .map(Requirement::from_str)
            .transpose()
            .decode()?;
        let category = row.entry.category.map(Category::new).transpose().decode()?;
        let cursor = TimelineEntryCursor::new(row.entry.order_time, entry.id().clone());
        let feed_meta = Annotated {
            feed: feed_meta,
            requirement,
            category,
        };

        Ok(TimelineEntry {
            entry,
            feed_meta,
            cursor,
        })
    }
}

impl TimelineDb for super::SqliteRegistryTx<'_> {
    async fn list_timeline_entries(
        &mut self,
        query: TimelineEntriesQuery,
    ) -> RegistryDbResult<TimelineEntriesPage> {
        list_entries(&mut self.tx, query).await.db()
    }

    async fn list_timeline_changes(
        &mut self,
        query: TimelineChangesQuery,
    ) -> RegistryDbResult<TimelineChangesPage> {
        list_changes(&mut self.tx, query).await.db()
    }

    async fn catchup_subscribed_feed(
        &mut self,
        subscriber_id: &SubscriberId,
        feed_url: &FeedUrl,
    ) -> RegistryDbResult<TimelineCatchup> {
        ensure_timeline(&mut self.tx, subscriber_id).await.db()?;
        catchup_feed(&mut self.tx, subscriber_id, feed_url)
            .await
            .db()
    }

    async fn apply_entry_to_timelines(
        &mut self,
        feed_url: &FeedUrl,
        entry_id: &EntryId,
        content_changed: bool,
    ) -> RegistryDbResult<Vec<SubscriberId>> {
        apply_entry_to_timelines(&mut self.tx, feed_url, entry_id, content_changed)
            .await
            .db()
    }

    async fn apply_feed_unsubscribed(
        &mut self,
        subscription: &SubscriptionKey,
    ) -> RegistryDbResult<Option<SubscriberId>> {
        apply_feed_unsubscribed(&mut self.tx, subscription)
            .await
            .db()
    }
}

#[cfg(test)]
mod tests;