Add tt{l,d,s} fields to item

This commit is contained in:
Tim Van Baak 2025-02-04 22:56:26 -08:00
parent ff943704c4
commit cb7bd6e3ba
6 changed files with 55 additions and 19 deletions

View File

@ -45,8 +45,8 @@ func TestChannel(t *testing.T) {
// Items on both sources appear in the channel // Items on both sources appear in the channel
if err := AddItems(db, []Item{ if err := AddItems(db, []Item{
{"one", "a", 0, true, "", "", "", "", 0, nil}, {Source: "one", Id: "a"},
{"two", "b", 0, true, "", "", "", "", 0, nil}, {Source: "two", Id: "b"},
}); err != nil { }); err != nil {
t.Fatalf("failed to add items to one: %v", err) t.Fatalf("failed to add items to one: %v", err)
} }

View File

@ -93,8 +93,8 @@ func TestDeleteSourceCascade(t *testing.T) {
t.Fatalf("failed to add source2: %v", err) t.Fatalf("failed to add source2: %v", err)
} }
if err := AddItems(db, []Item{ if err := AddItems(db, []Item{
{"source1", "item1", 0, true, "", "", "", "", 0, nil}, {Source: "source1", Id: "item1"},
{"source2", "item2", 0, true, "", "", "", "", 0, nil}, {Source: "source2", Id: "item2"},
}); err != nil { }); err != nil {
t.Fatalf("failed to add items: %v", err) 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)) 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 { if err == nil {
t.Fatal("Unexpected success adding item for nonexistent source") t.Fatal("Unexpected success adding item for nonexistent source")
} }

View File

@ -27,6 +27,9 @@ type Item struct {
Body string `json:"body"` Body string `json:"body"`
Link string `json:"link"` Link string `json:"link"`
Time int `json:"time"` Time int `json:"time"`
Ttl int `json:"ttl"`
Ttd int `json:"ttd"`
Tts int `json:"tts"`
Action Actions `json:"action"` Action Actions `json:"action"`
} }

View File

@ -12,8 +12,8 @@ import (
func AddItems(db DB, items []Item) error { func AddItems(db DB, items []Item) error {
return db.Transact(func(tx DB) error { return db.Transact(func(tx DB) error {
stmt, err := tx.Prepare(` stmt, err := tx.Prepare(`
insert into items (source, id, active, title, author, body, link, time, action) insert into items (source, id, active, title, author, body, link, time, ttl, ttd, tts, action)
values (?, ?, ?, ?, ?, ?, ?, ?, jsonb(?)) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, jsonb(?))
`) `)
if err != nil { if err != nil {
return fmt.Errorf("failed to prepare insert: %v", err) return fmt.Errorf("failed to prepare insert: %v", err)
@ -23,7 +23,9 @@ func AddItems(db DB, items []Item) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to marshal actions for %s/%s: %v", item.Source, item.Id, err) 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 { if err != nil {
return fmt.Errorf("failed to insert %s/%s: %v", item.Source, item.Id, err) 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 == "" { if new.Title == "" {
new.Title = old.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 { func UpdateItems(db DB, items []Item) error {
@ -66,6 +77,9 @@ func UpdateItems(db DB, items []Item) error {
body = ?, body = ?,
link = ?, link = ?,
time = ?, time = ?,
ttl = ?,
ttd = ?,
tts = ?,
action = jsonb(?) action = jsonb(?)
where source = ? where source = ?
and id = ? and id = ?
@ -78,7 +92,9 @@ func UpdateItems(db DB, items []Item) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to marshal actions for %s/%s: %v", item.Source, item.Id, err) 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 { if err != nil {
return err return err
} }
@ -132,7 +148,21 @@ func getItems(db DB, query string, args ...any) ([]Item, error) {
var items []Item var items []Item
for rows.Next() { for rows.Next() {
var item Item 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 { if err != nil {
return nil, err 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) { func GetItem(db DB, source string, id string) (Item, error) {
items, err := getItems(db, ` 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 from items
where source = ? where source = ?
and id = ? and id = ?
@ -164,7 +194,7 @@ func GetItem(db DB, source string, id string) (Item, error) {
func GetAllActiveItems(db DB) ([]Item, error) { func GetAllActiveItems(db DB) ([]Item, error) {
return getItems(db, ` return getItems(db, `
select 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 from items
where active <> 0 where active <> 0
order by case when time = 0 then created else time end, id 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) { func GetAllItems(db DB) ([]Item, error) {
return getItems(db, ` return getItems(db, `
select 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 from items
order by case when time = 0 then created else time end, id 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) { func GetActiveItemsForSource(db DB, source string) ([]Item, error) {
return getItems(db, ` return getItems(db, `
select 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 from items
where where
source = ? source = ?
@ -195,7 +225,7 @@ func GetActiveItemsForSource(db DB, source string) ([]Item, error) {
func GetAllItemsForSource(db DB, source string) ([]Item, error) { func GetAllItemsForSource(db DB, source string) ([]Item, error) {
return getItems(db, ` return getItems(db, `
select 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 from items
where where
source = ? source = ?
@ -206,7 +236,7 @@ func GetAllItemsForSource(db DB, source string) ([]Item, error) {
func GetActiveItemsForChannel(db DB, channel string) ([]Item, error) { func GetActiveItemsForChannel(db DB, channel string) ([]Item, error) {
return getItems(db, ` return getItems(db, `
select 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 from items i
join channels c on i.source = c.source join channels c on i.source = c.source
where where
@ -219,7 +249,7 @@ func GetActiveItemsForChannel(db DB, channel string) ([]Item, error) {
func GetAllItemsForChannel(db DB, channel string) ([]Item, error) { func GetAllItemsForChannel(db DB, channel string) ([]Item, error) {
return getItems(db, ` return getItems(db, `
select 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 from items i
join channels c on i.source = c.source join channels c on i.source = c.source
where where

View File

@ -31,8 +31,8 @@ func TestAddItem(t *testing.T) {
} }
if err := AddItems(db, []Item{ if err := AddItems(db, []Item{
{"test", "one", 0, true, "", "", "", "", 0, nil}, {Source: "test", Id: "one", Active: true},
{"test", "two", 0, true, "title", "author", "body", "link", 123456, nil}, {"test", "two", 0, true, "title", "author", "body", "link", 123456, 1, 2, 3, nil},
}); err != nil { }); err != nil {
t.Fatalf("failed to add items: %v", err) t.Fatalf("failed to add items: %v", err)
} }

View File

@ -27,6 +27,9 @@ create table items(
body text, body text,
link text, link text,
time int, time int,
ttl int,
ttd int,
tts int,
action blob, action blob,
primary key (source, id), primary key (source, id),
foreign key (source) references sources (name) on delete cascade foreign key (source) references sources (name) on delete cascade