From cb7bd6e3ba391cedc4c1a745bd7f9a4da98d5f87 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Tue, 4 Feb 2025 22:56:26 -0800 Subject: [PATCH] Add tt{l,d,s} fields to item --- core/channel_test.go | 4 +-- core/db_test.go | 6 ++-- core/item.go | 3 ++ core/items.go | 54 +++++++++++++++++++++++++------- core/items_test.go | 4 +-- core/sql/0001_initial_schema.sql | 3 ++ 6 files changed, 55 insertions(+), 19 deletions(-) diff --git a/core/channel_test.go b/core/channel_test.go index bcea9a1..3814ae8 100644 --- a/core/channel_test.go +++ b/core/channel_test.go @@ -45,8 +45,8 @@ func TestChannel(t *testing.T) { // Items on both sources appear in the channel if err := AddItems(db, []Item{ - {"one", "a", 0, true, "", "", "", "", 0, nil}, - {"two", "b", 0, true, "", "", "", "", 0, nil}, + {Source: "one", Id: "a"}, + {Source: "two", Id: "b"}, }); err != nil { t.Fatalf("failed to add items to one: %v", err) } diff --git a/core/db_test.go b/core/db_test.go index de50635..7dda336 100644 --- a/core/db_test.go +++ b/core/db_test.go @@ -93,8 +93,8 @@ func TestDeleteSourceCascade(t *testing.T) { t.Fatalf("failed to add source2: %v", err) } if err := AddItems(db, []Item{ - {"source1", "item1", 0, true, "", "", "", "", 0, nil}, - {"source2", "item2", 0, true, "", "", "", "", 0, nil}, + {Source: "source1", Id: "item1"}, + {Source: "source2", Id: "item2"}, }); err != nil { t.Fatalf("failed to add items: %v", err) } @@ -118,7 +118,7 @@ func TestDeleteSourceCascade(t *testing.T) { t.Fatalf("Expected only 1 item after source delete, got %d", len(items)) } - err = AddItems(db, []Item{{"source1", "item3", 0, true, "", "", "", "", 0, nil}}) + err = AddItems(db, []Item{{Source: "source1", Id: "item3"}}) if err == nil { t.Fatal("Unexpected success adding item for nonexistent source") } diff --git a/core/item.go b/core/item.go index 2002d3b..37aa7ac 100644 --- a/core/item.go +++ b/core/item.go @@ -27,6 +27,9 @@ type Item struct { Body string `json:"body"` Link string `json:"link"` Time int `json:"time"` + Ttl int `json:"ttl"` + Ttd int `json:"ttd"` + Tts int `json:"tts"` Action Actions `json:"action"` } diff --git a/core/items.go b/core/items.go index de5051e..9251e34 100644 --- a/core/items.go +++ b/core/items.go @@ -12,8 +12,8 @@ import ( func AddItems(db DB, items []Item) error { return db.Transact(func(tx DB) error { stmt, err := tx.Prepare(` - insert into items (source, id, active, title, author, body, link, time, action) - values (?, ?, ?, ?, ?, ?, ?, ?, jsonb(?)) + insert into items (source, id, active, title, author, body, link, time, ttl, ttd, tts, action) + values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, jsonb(?)) `) if err != nil { return fmt.Errorf("failed to prepare insert: %v", err) @@ -23,7 +23,9 @@ func AddItems(db DB, items []Item) error { if err != nil { return fmt.Errorf("failed to marshal actions for %s/%s: %v", item.Source, item.Id, err) } - _, err = stmt.Exec(item.Source, item.Id, true, item.Title, item.Author, item.Body, item.Link, item.Time, actions) + _, err = stmt.Exec( + item.Source, item.Id, true, item.Title, item.Author, item.Body, item.Link, item.Time, item.Ttl, item.Ttd, item.Tts, actions, + ) if err != nil { return fmt.Errorf("failed to insert %s/%s: %v", item.Source, item.Id, err) } @@ -54,6 +56,15 @@ func BackfillItem(new *Item, old *Item) { if new.Title == "" { new.Title = old.Title } + if new.Ttl == 0 { + new.Tts = old.Tts + } + if new.Ttd == 0 { + new.Ttd = old.Ttd + } + if new.Tts == 0 { + new.Tts = old.Tts + } } func UpdateItems(db DB, items []Item) error { @@ -66,6 +77,9 @@ func UpdateItems(db DB, items []Item) error { body = ?, link = ?, time = ?, + ttl = ?, + ttd = ?, + tts = ?, action = jsonb(?) where source = ? and id = ? @@ -78,7 +92,9 @@ func UpdateItems(db DB, items []Item) error { if err != nil { return fmt.Errorf("failed to marshal actions for %s/%s: %v", item.Source, item.Id, err) } - _, err = stmt.Exec(item.Title, item.Author, item.Body, item.Link, item.Time, actions, item.Source, item.Id) + _, err = stmt.Exec( + item.Title, item.Author, item.Body, item.Link, item.Time, item.Ttl, item.Ttd, item.Tts, actions, item.Source, item.Id, + ) if err != nil { return err } @@ -132,7 +148,21 @@ func getItems(db DB, query string, args ...any) ([]Item, error) { var items []Item for rows.Next() { var item Item - err = rows.Scan(&item.Source, &item.Id, &item.Created, &item.Active, &item.Title, &item.Author, &item.Body, &item.Link, &item.Time, &item.Action) + err = rows.Scan( + &item.Source, + &item.Id, + &item.Created, + &item.Active, + &item.Title, + &item.Author, + &item.Body, + &item.Link, + &item.Time, + &item.Ttl, + &item.Ttd, + &item.Tts, + &item.Action, + ) if err != nil { return nil, err } @@ -146,7 +176,7 @@ func getItems(db DB, query string, args ...any) ([]Item, error) { func GetItem(db DB, source string, id string) (Item, error) { items, err := getItems(db, ` - select source, id, created, active, title, author, body, link, time, json(action) + select source, id, created, active, title, author, body, link, time, ttl, ttd, tts, json(action) from items where source = ? and id = ? @@ -164,7 +194,7 @@ func GetItem(db DB, source string, id string) (Item, error) { func GetAllActiveItems(db DB) ([]Item, error) { return getItems(db, ` select - source, id, created, active, title, author, body, link, time, json(action) + source, id, created, active, title, author, body, link, time, ttl, ttd, tts, json(action) from items where active <> 0 order by case when time = 0 then created else time end, id @@ -174,7 +204,7 @@ func GetAllActiveItems(db DB) ([]Item, error) { func GetAllItems(db DB) ([]Item, error) { return getItems(db, ` select - source, id, created, active, title, author, body, link, time, json(action) + source, id, created, active, title, author, body, link, time, ttl, ttd, tts, json(action) from items order by case when time = 0 then created else time end, id `) @@ -183,7 +213,7 @@ func GetAllItems(db DB) ([]Item, error) { func GetActiveItemsForSource(db DB, source string) ([]Item, error) { return getItems(db, ` select - source, id, created, active, title, author, body, link, time, json(action) + source, id, created, active, title, author, body, link, time, ttl, ttd, tts, json(action) from items where source = ? @@ -195,7 +225,7 @@ func GetActiveItemsForSource(db DB, source string) ([]Item, error) { func GetAllItemsForSource(db DB, source string) ([]Item, error) { return getItems(db, ` select - source, id, created, active, title, author, body, link, time, json(action) + source, id, created, active, title, author, body, link, time, ttl, ttd, tts, json(action) from items where source = ? @@ -206,7 +236,7 @@ func GetAllItemsForSource(db DB, source string) ([]Item, error) { func GetActiveItemsForChannel(db DB, channel string) ([]Item, error) { return getItems(db, ` select - i.source, i.id, i.created, i.active, i.title, i.author, i.body, i.link, i.time, json(i.action) + i.source, i.id, i.created, i.active, i.title, i.author, i.body, i.link, i.time, i.ttl, i.ttd, i.tts, json(i.action) from items i join channels c on i.source = c.source where @@ -219,7 +249,7 @@ func GetActiveItemsForChannel(db DB, channel string) ([]Item, error) { func GetAllItemsForChannel(db DB, channel string) ([]Item, error) { return getItems(db, ` select - i.source, i.id, i.created, i.active, i.title, i.author, i.body, i.link, i.time, json(i.action) + i.source, i.id, i.created, i.active, i.title, i.author, i.body, i.link, i.time, i.ttl, i.ttd, i.tts, json(i.action) from items i join channels c on i.source = c.source where diff --git a/core/items_test.go b/core/items_test.go index 1ca07bc..7c29618 100644 --- a/core/items_test.go +++ b/core/items_test.go @@ -31,8 +31,8 @@ func TestAddItem(t *testing.T) { } if err := AddItems(db, []Item{ - {"test", "one", 0, true, "", "", "", "", 0, nil}, - {"test", "two", 0, true, "title", "author", "body", "link", 123456, nil}, + {Source: "test", Id: "one", Active: true}, + {"test", "two", 0, true, "title", "author", "body", "link", 123456, 1, 2, 3, nil}, }); err != nil { t.Fatalf("failed to add items: %v", err) } diff --git a/core/sql/0001_initial_schema.sql b/core/sql/0001_initial_schema.sql index c05081b..a4d587d 100644 --- a/core/sql/0001_initial_schema.sql +++ b/core/sql/0001_initial_schema.sql @@ -27,6 +27,9 @@ create table items( body text, link text, time int, + ttl int, + ttd int, + tts int, action blob, primary key (source, id), foreign key (source) references sources (name) on delete cascade