Add paging links to web feed

This commit is contained in:
Tim Van Baak 2025-02-07 08:17:51 -08:00
parent 3874abf8bd
commit b434c1acfe
7 changed files with 64 additions and 14 deletions

View File

@ -98,7 +98,7 @@ Parity features
* [x] web feed supports item TTS
* [x] item punt
* [ ] web feed paging
* [x] web feed paging
* [ ] web fetch
* [ ] set a working directory for item actions
* [ ] crontab integration

View File

@ -37,5 +37,12 @@ tmp/intake channel add -c all -s spook
tmp/intake channel add -c none -s nothing
tmp/intake source add -s page
tmp/intake item add -s page --id 1 --title "Item 1" --body "This is the body of item 1"
for i in $(seq 2 211); do
tmp/intake item add -s page --id $i --title "Item $i" --body "This is the body of item $i" 2>/dev/null
done
tmp/intake item add -s page --id 212 --title "Item 212" --body "This is the body of item 212"
echo "hello" | tmp/intake passwd --stdin
echo "hello" | tmp/intake passwd --stdin --verify

View File

@ -10,13 +10,16 @@ import (
func (env *Env) getChannel(writer http.ResponseWriter, req *http.Request) {
channel := req.PathValue("channel")
page := getQueryInt(req, "page", 1)
count := getQueryInt(req, "count", core.DefaultFeedLimit)
showHidden := getQueryInt(req, "hidden", 0)
var items []core.Item
var err error
inactive := req.URL.Query().Get("inactive") == "1"
if inactive {
items, err = core.GetAllItemsForChannel(env.db, channel, 0, core.DefaultFeedLimit)
if showHidden != 0 {
items, err = core.GetAllItemsForChannel(env.db, channel, (page-1)*count, count)
} else {
items, err = core.GetActiveItemsForChannel(env.db, channel, 0, core.DefaultFeedLimit)
items, err = core.GetActiveItemsForChannel(env.db, channel, (page-1)*count, count)
}
if err != nil {
http.Error(writer, err.Error(), 500)
@ -25,6 +28,9 @@ func (env *Env) getChannel(writer http.ResponseWriter, req *http.Request) {
data := html.FeedData{
Items: items,
ShowHidden: showHidden,
Page: page,
Count: count,
}
html.Feed(writer, data)
}

View File

@ -2,9 +2,17 @@
{{ define "content" -}}
<article class="center">
<span class="feed-controls">
<span class="feed-controls" style="display: flex; justify-content: space-between;">
<a href="?hidden={{ .ShowHidden }}&page={{ page .Page -1 }}&count={{ .Count }}">&lt;--</a>
<a href="/">Home</a>
[<a href="?inactive=0">Active</a> | <a href="?inactive=1">All</a>]
<span>
{{ if .ShowHidden -}}
[ <a href="?hidden=0&page={{ .Page }}&count={{ .Count }}">Active</a> | All ]
{{- else -}}
[ Active | <a href="?hidden=1&page={{ .Page }}&count={{ .Count }}">All</a> ]
{{- end }}
</span>
<a href="?hidden={{ .ShowHidden }}&page={{ page .Page 1 }}&count={{ .Count }}">--&gt;</a>
</span>
</article>

View File

@ -61,12 +61,21 @@ func massDeactivateVals(items []core.Item) string {
return string(vals)
}
func page(i int, delta int) int {
i = i + delta
if i < 1 {
return 1
}
return i
}
var funcs = template.FuncMap{
"raw": rawHtml,
"dateFormat": dateFormat,
"tsToDate": tsToDate,
"until": until,
"massDeacVars": massDeactivateVals,
"page": page,
}
//go:embed intake.css
@ -109,6 +118,9 @@ var feed = load("feed.html", "item.html")
type FeedData struct {
Items []core.Item
ShowHidden int
Page int
Count int
}
func Feed(writer io.Writer, data FeedData) {

View File

@ -4,6 +4,7 @@ import (
"log"
"net"
"net/http"
"strconv"
"github.com/Jaculabilis/intake/core"
)
@ -48,3 +49,13 @@ func RunServer(db core.DB, addr string, port string) {
log.Fatal(http.ListenAndServe(bind, nil))
}
func getQueryInt(req *http.Request, name string, def int) int {
s := req.URL.Query().Get(name)
i, err := strconv.Atoi(s)
if err != nil {
return def
} else {
return i
}
}

View File

@ -14,13 +14,16 @@ func (env *Env) getSource(writer http.ResponseWriter, req *http.Request) {
return
}
page := getQueryInt(req, "page", 1)
count := getQueryInt(req, "count", core.DefaultFeedLimit)
showHidden := getQueryInt(req, "hidden", 0)
var items []core.Item
var err error
inactive := req.URL.Query().Get("inactive") == "1"
if inactive {
items, err = core.GetAllItemsForSource(env.db, source, 0, core.DefaultFeedLimit)
if showHidden != 0 {
items, err = core.GetAllItemsForSource(env.db, source, (page-1)*count, count)
} else {
items, err = core.GetActiveItemsForSource(env.db, source, 0, core.DefaultFeedLimit)
items, err = core.GetActiveItemsForSource(env.db, source, (page-1)*count, count)
}
if err != nil {
http.Error(writer, err.Error(), 500)
@ -29,6 +32,9 @@ func (env *Env) getSource(writer http.ResponseWriter, req *http.Request) {
data := html.FeedData{
Items: items,
ShowHidden: showHidden,
Page: page,
Count: count,
}
html.Feed(writer, data)
}