2025-01-16 22:53:04 +00:00
|
|
|
package core
|
2025-01-16 19:46:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2025-01-24 23:41:52 +00:00
|
|
|
"encoding/json"
|
2025-01-17 06:02:03 +00:00
|
|
|
"errors"
|
2025-01-16 21:46:30 +00:00
|
|
|
"fmt"
|
2025-01-16 19:46:37 +00:00
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
2025-01-31 16:44:09 +00:00
|
|
|
func AddItems(db DB, items []Item) error {
|
2025-01-31 16:53:11 +00:00
|
|
|
return db.Transact(func(tx DB) error {
|
2025-01-23 20:26:21 +00:00
|
|
|
stmt, err := tx.Prepare(`
|
2025-01-29 16:48:12 +00:00
|
|
|
insert into items (source, id, active, title, author, body, link, time, action)
|
|
|
|
values (?, ?, ?, ?, ?, ?, ?, ?, jsonb(?))
|
2025-01-23 20:26:21 +00:00
|
|
|
`)
|
|
|
|
if err != nil {
|
2025-01-29 16:48:12 +00:00
|
|
|
return fmt.Errorf("failed to prepare insert: %v", err)
|
2025-01-23 20:26:21 +00:00
|
|
|
}
|
|
|
|
for _, item := range items {
|
2025-01-29 16:48:12 +00:00
|
|
|
actions, err := json.Marshal(item.Action)
|
2025-01-23 20:26:21 +00:00
|
|
|
if err != nil {
|
2025-01-29 16:48:12 +00:00
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to insert %s/%s: %v", item.Source, item.Id, err)
|
2025-01-23 20:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2025-01-23 22:24:16 +00:00
|
|
|
// Set fields in the new item to match the old item where the new item's fields are zero-valued.
|
|
|
|
// This allows sources to omit fields and let an action set them without a later fetch overwriting
|
|
|
|
// the value from the action, e.g. an on-create action archiving a page and setting the link to
|
|
|
|
// point to the archive.
|
|
|
|
func BackfillItem(new *Item, old *Item) {
|
|
|
|
new.Active = old.Active
|
|
|
|
new.Created = old.Created
|
|
|
|
if new.Author == "" {
|
|
|
|
new.Author = old.Author
|
|
|
|
}
|
|
|
|
if new.Body == "" {
|
|
|
|
new.Body = old.Body
|
|
|
|
}
|
|
|
|
if new.Link == "" {
|
|
|
|
new.Link = old.Link
|
|
|
|
}
|
|
|
|
if new.Time == 0 {
|
|
|
|
new.Time = old.Time
|
|
|
|
}
|
|
|
|
if new.Title == "" {
|
|
|
|
new.Title = old.Title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-31 16:44:09 +00:00
|
|
|
func UpdateItems(db DB, items []Item) error {
|
2025-01-31 16:53:11 +00:00
|
|
|
return db.Transact(func(tx DB) error {
|
2025-01-23 22:24:16 +00:00
|
|
|
stmt, err := tx.Prepare(`
|
|
|
|
update items
|
|
|
|
set
|
|
|
|
title = ?,
|
|
|
|
author = ?,
|
|
|
|
body = ?,
|
|
|
|
link = ?,
|
2025-01-29 16:48:12 +00:00
|
|
|
time = ?,
|
|
|
|
action = jsonb(?)
|
2025-01-23 22:24:16 +00:00
|
|
|
where source = ?
|
|
|
|
and id = ?
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, item := range items {
|
2025-01-29 16:48:12 +00:00
|
|
|
actions, err := json.Marshal(item.Action)
|
|
|
|
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)
|
2025-01-23 22:24:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2025-01-17 06:02:03 +00:00
|
|
|
// Deactivate an item, returning its previous active state.
|
2025-01-31 16:44:09 +00:00
|
|
|
func DeactivateItem(db DB, source string, id string) (bool, error) {
|
2025-01-17 06:02:03 +00:00
|
|
|
row := db.QueryRow(`
|
|
|
|
select active
|
|
|
|
from items
|
|
|
|
where source = ? and id = ?
|
|
|
|
`, source, id)
|
|
|
|
var active bool
|
|
|
|
err := row.Scan(&active)
|
|
|
|
if err != nil && errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return false, fmt.Errorf("item %s/%s not found", source, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec(`
|
2025-01-16 21:46:30 +00:00
|
|
|
update items
|
|
|
|
set active = 0
|
|
|
|
where source = ? and id = ?
|
|
|
|
`, source, id)
|
|
|
|
if err != nil {
|
2025-01-17 06:02:03 +00:00
|
|
|
return false, err
|
2025-01-16 21:46:30 +00:00
|
|
|
}
|
2025-01-17 06:02:03 +00:00
|
|
|
return active, nil
|
2025-01-16 21:46:30 +00:00
|
|
|
}
|
|
|
|
|
2025-01-31 16:44:09 +00:00
|
|
|
func DeleteItem(db DB, source string, id string) (int64, error) {
|
2025-01-23 19:38:17 +00:00
|
|
|
res, err := db.Exec(`
|
|
|
|
delete from items
|
|
|
|
where source = ?
|
|
|
|
and id = ?
|
|
|
|
`, source, id)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return res.RowsAffected()
|
|
|
|
}
|
|
|
|
|
2025-01-31 16:44:09 +00:00
|
|
|
func getItems(db DB, query string, args ...any) ([]Item, error) {
|
2025-01-23 19:34:44 +00:00
|
|
|
rows, err := db.Query(query, args...)
|
2025-01-17 15:05:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2025-02-01 00:01:47 +00:00
|
|
|
defer rows.Close()
|
2025-01-17 15:05:57 +00:00
|
|
|
var items []Item
|
|
|
|
for rows.Next() {
|
|
|
|
var item Item
|
2025-01-29 16:48:12 +00:00
|
|
|
err = rows.Scan(&item.Source, &item.Id, &item.Created, &item.Active, &item.Title, &item.Author, &item.Body, &item.Link, &item.Time, &item.Action)
|
2025-01-23 17:08:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2025-01-17 15:05:57 +00:00
|
|
|
items = append(items, item)
|
|
|
|
}
|
2025-02-01 00:01:47 +00:00
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2025-01-17 15:05:57 +00:00
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
2025-01-31 16:44:09 +00:00
|
|
|
func GetItem(db DB, source string, id string) (Item, error) {
|
2025-01-23 21:32:09 +00:00
|
|
|
items, err := getItems(db, `
|
2025-01-29 16:48:12 +00:00
|
|
|
select source, id, created, active, title, author, body, link, time, json(action)
|
2025-01-23 21:32:09 +00:00
|
|
|
from items
|
|
|
|
where source = ?
|
|
|
|
and id = ?
|
2025-01-30 06:41:50 +00:00
|
|
|
order by case when time = 0 then created else time end, id
|
2025-01-23 21:32:09 +00:00
|
|
|
`, source, id)
|
|
|
|
if err != nil {
|
|
|
|
return Item{}, err
|
|
|
|
}
|
|
|
|
if len(items) == 0 {
|
|
|
|
return Item{}, fmt.Errorf("no item in %s with id %s", source, id)
|
|
|
|
}
|
|
|
|
return items[0], nil
|
|
|
|
}
|
|
|
|
|
2025-01-31 16:44:09 +00:00
|
|
|
func GetAllActiveItems(db DB) ([]Item, error) {
|
2025-01-23 19:34:44 +00:00
|
|
|
return getItems(db, `
|
|
|
|
select
|
2025-01-29 16:48:12 +00:00
|
|
|
source, id, created, active, title, author, body, link, time, json(action)
|
2025-01-23 19:34:44 +00:00
|
|
|
from items
|
|
|
|
where active <> 0
|
2025-01-30 06:41:50 +00:00
|
|
|
order by case when time = 0 then created else time end, id
|
2025-01-23 19:34:44 +00:00
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
2025-01-31 16:44:09 +00:00
|
|
|
func GetAllItems(db DB) ([]Item, error) {
|
2025-01-23 19:38:17 +00:00
|
|
|
return getItems(db, `
|
|
|
|
select
|
2025-01-29 16:48:12 +00:00
|
|
|
source, id, created, active, title, author, body, link, time, json(action)
|
2025-01-23 19:38:17 +00:00
|
|
|
from items
|
2025-01-30 06:41:50 +00:00
|
|
|
order by case when time = 0 then created else time end, id
|
2025-01-23 19:38:17 +00:00
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
2025-01-31 16:44:09 +00:00
|
|
|
func GetActiveItemsForSource(db DB, source string) ([]Item, error) {
|
2025-01-23 19:34:44 +00:00
|
|
|
return getItems(db, `
|
2025-01-16 21:46:30 +00:00
|
|
|
select
|
2025-01-29 16:48:12 +00:00
|
|
|
source, id, created, active, title, author, body, link, time, json(action)
|
2025-01-16 21:46:30 +00:00
|
|
|
from items
|
|
|
|
where
|
|
|
|
source = ?
|
|
|
|
and active <> 0
|
2025-01-30 06:41:50 +00:00
|
|
|
order by case when time = 0 then created else time end, id
|
2025-01-16 21:46:30 +00:00
|
|
|
`, source)
|
|
|
|
}
|
2025-01-23 19:38:17 +00:00
|
|
|
|
2025-01-31 16:44:09 +00:00
|
|
|
func GetAllItemsForSource(db DB, source string) ([]Item, error) {
|
2025-01-23 19:38:17 +00:00
|
|
|
return getItems(db, `
|
|
|
|
select
|
2025-01-29 16:48:12 +00:00
|
|
|
source, id, created, active, title, author, body, link, time, json(action)
|
2025-01-23 19:38:17 +00:00
|
|
|
from items
|
|
|
|
where
|
|
|
|
source = ?
|
2025-01-30 06:41:50 +00:00
|
|
|
order by case when time = 0 then created else time end, id
|
2025-01-23 19:38:17 +00:00
|
|
|
`, source)
|
|
|
|
}
|