feedbinctl 0.1.1

Index and search Feedbin entries from the command line
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
use anyhow::{Context, Result, bail};
use directories::BaseDirs;
use rusqlite::{Connection, OptionalExtension, Transaction, params};
use serde::Serialize;
use std::path::{Path, PathBuf};

use crate::api::{FeedEntry, Subscription};

const SCHEMA_VERSION: i64 = 1;

#[derive(Debug, Serialize, PartialEq)]
pub struct EntrySummary {
    pub id: i64,
    pub feed_id: i64,
    pub title: Option<String>,
    pub url: Option<String>,
    pub author: Option<String>,
    pub feed_title: Option<String>,
    pub feed_url: Option<String>,
    pub site_url: Option<String>,
    pub published: String,
    pub created_at: String,
}

pub fn default_path() -> Result<PathBuf> {
    let file_name = if cfg!(debug_assertions) {
        "feedbin-dev.sqlite"
    } else {
        "feedbin.sqlite"
    };
    Ok(path_for_profile(&xdg_data_home()?, file_name))
}

fn xdg_data_home() -> Result<PathBuf> {
    if let Some(value) = std::env::var_os("XDG_DATA_HOME")
        && !value.is_empty()
    {
        let path = PathBuf::from(value);
        if !path.is_absolute() {
            bail!("XDG_DATA_HOME must be an absolute path");
        }
        return Ok(path);
    }

    let dirs = BaseDirs::new().context("could not determine the home directory")?;
    Ok(dirs.home_dir().join(".local").join("share"))
}

fn path_for_profile(data_home: &Path, file_name: &str) -> PathBuf {
    data_home.join("feedbinctl").join(file_name)
}

pub struct Database {
    connection: Connection,
}

impl Database {
    pub fn open(path: &Path) -> Result<Self> {
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)
                .with_context(|| format!("failed to create {}", parent.display()))?;
        }
        let connection =
            Connection::open(path).with_context(|| format!("failed to open {}", path.display()))?;
        connection.execute_batch("PRAGMA journal_mode = WAL; PRAGMA foreign_keys = ON;")?;
        initialize_schema(&connection)?;
        Ok(Self { connection })
    }

    pub fn cursor(&self) -> Result<Option<String>> {
        self.connection
            .query_row(
                "SELECT value FROM metadata WHERE key = 'entries_cursor'",
                [],
                |row| row.get(0),
            )
            .optional()
            .map_err(Into::into)
    }

    pub fn store_feeds(&mut self, subscriptions: &[Subscription]) -> Result<()> {
        let transaction = self.connection.transaction()?;
        for subscription in subscriptions {
            transaction.execute(
                r#"
                INSERT INTO feeds (feed_id, title, feed_url, site_url)
                VALUES (?1, ?2, ?3, ?4)
                ON CONFLICT(feed_id) DO UPDATE SET
                    title = excluded.title,
                    feed_url = excluded.feed_url,
                    site_url = excluded.site_url
                "#,
                params![
                    subscription.feed_id,
                    subscription.title,
                    subscription.feed_url,
                    subscription.site_url,
                ],
            )?;
        }
        transaction.execute(
            r#"
            UPDATE entries
            SET feed_title = (SELECT title FROM feeds WHERE feeds.feed_id = entries.feed_id)
            WHERE EXISTS (
                SELECT 1 FROM feeds
                WHERE feeds.feed_id = entries.feed_id
                  AND feeds.title IS NOT entries.feed_title
            )
            "#,
            [],
        )?;
        transaction.commit()?;
        Ok(())
    }

    pub fn store_entries(&mut self, entries: &[FeedEntry]) -> Result<()> {
        let transaction = self.connection.transaction()?;
        for entry in entries {
            store_entry(&transaction, entry)?;
        }
        transaction.commit()?;
        Ok(())
    }

    pub fn set_cursor(&self, cursor: &str) -> Result<()> {
        self.connection.execute(
            r#"
            INSERT INTO metadata (key, value) VALUES ('entries_cursor', ?1)
            ON CONFLICT(key) DO UPDATE SET value = excluded.value
            "#,
            [cursor],
        )?;
        Ok(())
    }

    pub fn entries(&self, limit: usize, before: Option<i64>) -> Result<Vec<EntrySummary>> {
        if limit == 0 {
            return Ok(vec![]);
        }

        if let Some(before) = before {
            let anchor: Option<(String, i64)> = self
                .connection
                .query_row(
                    "SELECT created_at, id FROM entries WHERE id = ?1",
                    [before],
                    |row| Ok((row.get(0)?, row.get(1)?)),
                )
                .optional()?;
            let (created_at, id) = anchor
                .with_context(|| format!("entry {before} is not present in the local index"))?;
            let mut statement = self.connection.prepare(&format!(
                "{} WHERE e.created_at < ?1 OR (e.created_at = ?1 AND e.id < ?2) ORDER BY e.created_at DESC, e.id DESC LIMIT ?3",
                summary_select()
            ))?;
            let mut rows = statement.query(params![created_at, id, limit as i64])?;
            collect_summaries(&mut rows)
        } else {
            let mut statement = self.connection.prepare(&format!(
                "{} ORDER BY e.created_at DESC, e.id DESC LIMIT ?1",
                summary_select()
            ))?;
            let mut rows = statement.query([limit as i64])?;
            collect_summaries(&mut rows)
        }
    }

    pub fn search(&self, query: &str, limit: usize) -> Result<Vec<EntrySummary>> {
        if limit == 0 {
            return Ok(vec![]);
        }
        if query.trim().is_empty() {
            bail!("search query must not be empty");
        }

        let mut statement = self.connection.prepare(
            r#"
            SELECT
                e.id, e.feed_id, e.title, e.url, e.author, e.feed_title,
                f.feed_url, f.site_url, e.published, e.created_at
            FROM entries_fts
            JOIN entries e ON e.id = entries_fts.rowid
            LEFT JOIN feeds f ON f.feed_id = e.feed_id
            WHERE entries_fts MATCH ?1
            ORDER BY bm25(entries_fts), e.created_at DESC, e.id DESC
            LIMIT ?2
            "#,
        )?;
        let mut rows = statement.query(params![query, limit as i64])?;
        collect_summaries(&mut rows)
    }

    pub fn checkpoint(&self) -> Result<()> {
        self.connection
            .execute_batch("PRAGMA wal_checkpoint(TRUNCATE);")?;
        Ok(())
    }
}

fn store_entry(transaction: &Transaction<'_>, entry: &FeedEntry) -> Result<()> {
    transaction.execute(
        r#"
        INSERT INTO entries (
            id, feed_id, title, url, author, feed_title, published, created_at
        ) VALUES (
            ?1, ?2, ?3, ?4, ?5,
            (SELECT title FROM feeds WHERE feed_id = ?2),
            ?6, ?7
        )
        ON CONFLICT(id) DO UPDATE SET
            feed_id = excluded.feed_id,
            title = excluded.title,
            url = excluded.url,
            author = excluded.author,
            feed_title = excluded.feed_title,
            published = excluded.published,
            created_at = excluded.created_at
        "#,
        params![
            entry.id,
            entry.feed_id,
            entry.title,
            entry.url,
            entry.author,
            entry.published,
            entry.created_at,
        ],
    )?;
    Ok(())
}

fn summary_select() -> &'static str {
    r#"
    SELECT
        e.id, e.feed_id, e.title, e.url, e.author, e.feed_title,
        f.feed_url, f.site_url, e.published, e.created_at
    FROM entries e
    LEFT JOIN feeds f ON f.feed_id = e.feed_id
    "#
}

fn collect_summaries(rows: &mut rusqlite::Rows<'_>) -> Result<Vec<EntrySummary>> {
    let mut entries = Vec::new();
    while let Some(row) = rows.next()? {
        entries.push(EntrySummary {
            id: row.get(0)?,
            feed_id: row.get(1)?,
            title: row.get(2)?,
            url: row.get(3)?,
            author: row.get(4)?,
            feed_title: row.get(5)?,
            feed_url: row.get(6)?,
            site_url: row.get(7)?,
            published: row.get(8)?,
            created_at: row.get(9)?,
        });
    }
    Ok(entries)
}

fn initialize_schema(connection: &Connection) -> Result<()> {
    let version: i64 = connection.query_row("PRAGMA user_version", [], |row| row.get(0))?;
    match version {
        SCHEMA_VERSION => Ok(()),
        0 if table_exists(connection, "entries")? => migrate_prototype_schema(connection),
        0 => create_schema(connection),
        other => bail!(
            "database schema version {other} is newer than this feedbinctl supports ({SCHEMA_VERSION})"
        ),
    }
}

fn table_exists(connection: &Connection, table: &str) -> Result<bool> {
    Ok(connection
        .query_row(
            "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?1",
            [table],
            |_| Ok(()),
        )
        .optional()?
        .is_some())
}

fn create_schema(connection: &Connection) -> Result<()> {
    connection.execute_batch(&format!(
        r#"
        CREATE TABLE feeds (
            feed_id INTEGER PRIMARY KEY,
            title TEXT NOT NULL,
            feed_url TEXT NOT NULL,
            site_url TEXT NOT NULL
        );

        CREATE TABLE entries (
            id INTEGER PRIMARY KEY,
            feed_id INTEGER NOT NULL,
            title TEXT,
            url TEXT,
            author TEXT,
            feed_title TEXT,
            published TEXT NOT NULL,
            created_at TEXT NOT NULL
        );
        CREATE INDEX entries_created_at ON entries(created_at DESC, id DESC);

        CREATE VIRTUAL TABLE entries_fts USING fts5(
            title, url, author, feed_title,
            content = 'entries', content_rowid = 'id'
        );

        CREATE TRIGGER entries_ai AFTER INSERT ON entries BEGIN
            INSERT INTO entries_fts(rowid, title, url, author, feed_title)
            VALUES (new.id, new.title, new.url, new.author, new.feed_title);
        END;
        CREATE TRIGGER entries_ad AFTER DELETE ON entries BEGIN
            INSERT INTO entries_fts(entries_fts, rowid, title, url, author, feed_title)
            VALUES ('delete', old.id, old.title, old.url, old.author, old.feed_title);
        END;
        CREATE TRIGGER entries_au AFTER UPDATE ON entries BEGIN
            INSERT INTO entries_fts(entries_fts, rowid, title, url, author, feed_title)
            VALUES ('delete', old.id, old.title, old.url, old.author, old.feed_title);
            INSERT INTO entries_fts(rowid, title, url, author, feed_title)
            VALUES (new.id, new.title, new.url, new.author, new.feed_title);
        END;

        CREATE TABLE metadata (
            key TEXT PRIMARY KEY,
            value TEXT NOT NULL
        );

        PRAGMA user_version = {SCHEMA_VERSION};
        "#
    ))?;
    Ok(())
}

fn migrate_prototype_schema(connection: &Connection) -> Result<()> {
    connection.execute_batch(
        r#"
        BEGIN IMMEDIATE;
        DROP TRIGGER IF EXISTS entries_ai;
        DROP TRIGGER IF EXISTS entries_ad;
        DROP TRIGGER IF EXISTS entries_au;
        DROP TABLE IF EXISTS entries_fts;
        ALTER TABLE entries RENAME TO entries_prototype;
        ALTER TABLE subscriptions RENAME TO subscriptions_prototype;
        ALTER TABLE metadata RENAME TO metadata_prototype;
        COMMIT;
        "#,
    )?;
    create_schema(connection)?;
    connection.execute_batch(
        r#"
        BEGIN IMMEDIATE;
        INSERT INTO feeds (feed_id, title, feed_url, site_url)
        SELECT feed_id, title, feed_url, site_url FROM subscriptions_prototype;

        INSERT INTO entries (
            id, feed_id, title, url, author, feed_title, published, created_at
        )
        SELECT
            e.id, e.feed_id, e.title, e.url, e.author, s.title,
            e.published, e.created_at
        FROM entries_prototype e
        LEFT JOIN subscriptions_prototype s ON s.feed_id = e.feed_id;

        INSERT INTO metadata (key, value)
        SELECT 'entries_cursor', value
        FROM metadata_prototype
        WHERE key = 'entries_since';

        DROP TABLE entries_prototype;
        DROP TABLE subscriptions_prototype;
        DROP TABLE metadata_prototype;
        COMMIT;
        "#,
    )?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn separates_development_and_production_databases() {
        let data_home = Path::new("/home/example/.local/share");
        assert_eq!(
            path_for_profile(data_home, "feedbin-dev.sqlite"),
            data_home.join("feedbinctl/feedbin-dev.sqlite")
        );
        assert_eq!(
            path_for_profile(data_home, "feedbin.sqlite"),
            data_home.join("feedbinctl/feedbin.sqlite")
        );
    }

    fn subscription() -> Subscription {
        Subscription {
            id: 1,
            feed_id: 2,
            title: "Example Feed".to_string(),
            feed_url: "https://example.com/feed".to_string(),
            site_url: "https://example.com".to_string(),
            created_at: "2026-01-01T00:00:00Z".to_string(),
        }
    }

    fn entry(id: i64, created_at: &str, title: &str) -> FeedEntry {
        FeedEntry {
            id,
            feed_id: 2,
            title: Some(title.to_string()),
            author: Some("Example Author".to_string()),
            summary: Some("not stored".to_string()),
            content: Some("large HTML is not stored".to_string()),
            url: (id != 2).then(|| format!("https://example.com/{id}")),
            extracted_content_url: None,
            published: created_at.to_string(),
            created_at: created_at.to_string(),
        }
    }

    fn in_memory_database() -> Database {
        let connection = Connection::open_in_memory().unwrap();
        create_schema(&connection).unwrap();
        Database { connection }
    }

    #[test]
    fn stores_compact_entries_searches_feed_names_and_pages_stably() {
        let mut database = in_memory_database();
        database.store_feeds(&[subscription()]).unwrap();
        database
            .store_entries(&[
                entry(1, "2026-01-03T00:00:00Z", "Distributed soup"),
                entry(2, "2026-01-02T00:00:00Z", "Title without URL"),
                entry(3, "2026-01-01T00:00:00Z", "Oldest"),
            ])
            .unwrap();

        let newest = database.entries(2, None).unwrap();
        assert_eq!(
            newest.iter().map(|entry| entry.id).collect::<Vec<_>>(),
            [1, 2]
        );
        assert_eq!(newest[1].url, None);
        let older = database.entries(2, Some(2)).unwrap();
        assert_eq!(older.iter().map(|entry| entry.id).collect::<Vec<_>>(), [3]);

        let matches = database.search("distributed", 10).unwrap();
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].feed_title.as_deref(), Some("Example Feed"));
        let feed_matches = database.search("feed_title:Example", 10).unwrap();
        assert_eq!(feed_matches.len(), 3);

        database.set_cursor("2026-01-03T00:00:00Z").unwrap();
        assert_eq!(
            database.cursor().unwrap().as_deref(),
            Some("2026-01-03T00:00:00Z")
        );
    }

    #[test]
    fn before_uses_id_to_disambiguate_equal_timestamps() {
        let mut database = in_memory_database();
        database.store_feeds(&[subscription()]).unwrap();
        database
            .store_entries(&[
                entry(2, "2026-01-01T00:00:00Z", "Second"),
                entry(1, "2026-01-01T00:00:00Z", "First"),
            ])
            .unwrap();

        assert_eq!(database.entries(1, None).unwrap()[0].id, 2);
        assert_eq!(database.entries(1, Some(2)).unwrap()[0].id, 1);
    }
}