Add option to show inactive items in feed

This commit is contained in:
Tim Van Baak 2025-01-23 11:38:17 -08:00
parent 675cb64f47
commit dde799ff8e
3 changed files with 74 additions and 2 deletions

View File

@ -27,6 +27,7 @@ Available formats:
var feedFormat string
var feedSource string
var feedChannel string
var feedShowInactive bool
func init() {
rootCmd.AddCommand(feedCmd)
@ -35,6 +36,7 @@ func init() {
feedCmd.Flags().StringVarP(&feedSource, "source", "s", "", "Limit to items from source")
feedCmd.Flags().StringVarP(&feedChannel, "channel", "c", "", "Limit to items from channel")
feedCmd.MarkFlagsMutuallyExclusive("source", "channel")
feedCmd.Flags().BoolVar(&feedShowInactive, "all", false, "Show inactive items")
}
func feed() {
@ -47,14 +49,22 @@ func feed() {
var items []core.Item
if feedSource != "" {
items, err = core.GetActiveItemsForSource(db, feedSource)
if feedShowInactive {
items, err = core.GetAllItemsForSource(db, feedSource)
} else {
items, err = core.GetActiveItemsForSource(db, feedSource)
}
if err != nil {
log.Fatalf("error: failed to fetch items from %s", feedSource)
}
} else if feedChannel != "" {
log.Fatal("error: unimplemented")
} else {
items, err = core.GetAllActiveItems(db)
if feedShowInactive {
items, err = core.GetAllItems(db)
} else {
items, err = core.GetAllActiveItems(db)
}
if err != nil {
log.Fatal("error: failed to fetch items")
}

View File

@ -87,6 +87,18 @@ func DeactivateItem(db *DB, source string, id string) (bool, error) {
return active, nil
}
func DeleteItem(db *DB, source string, id string) (int64, error) {
res, err := db.Exec(`
delete from items
where source = ?
and id = ?
`, source, id)
if err != nil {
return 0, err
}
return res.RowsAffected()
}
func getItems(db *DB, query string, args ...any) ([]Item, error) {
rows, err := db.Query(query, args...)
if err != nil {
@ -113,6 +125,14 @@ 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
from items
`)
}
func GetActiveItemsForSource(db *DB, source string) ([]Item, error) {
return getItems(db, `
select
@ -123,3 +143,13 @@ func GetActiveItemsForSource(db *DB, source string) ([]Item, error) {
and active <> 0
`, source)
}
func GetAllItemsForSource(db *DB, source string) ([]Item, error) {
return getItems(db, `
select
source, id, created, active, title, author, body, link, time
from items
where
source = ?
`, source)
}

View File

@ -85,4 +85,36 @@ func TestAddItem(t *testing.T) {
if len(items) != 1 {
t.Fatal("should get one item")
}
items, err = GetAllItemsForSource(db, "test")
if err != nil {
t.Fatal(err)
}
if len(items) != 2 {
t.Fatal("should get two items")
}
deleted, err := DeleteItem(db, "test", "one")
if err != nil {
t.Fatal(err)
}
if deleted != 1 {
t.Fatal("expected one deletion")
}
deleted, err = DeleteItem(db, "test", "one")
if err != nil {
t.Fatal(err)
}
if deleted != 0 {
t.Fatal("expected no deletion")
}
items, err = GetAllItemsForSource(db, "test")
if err != nil {
t.Fatal(err)
}
if len(items) != 1 {
t.Fatal("should get one item")
}
}