chore: changes to feed

This commit is contained in:
xuu 2025-03-26 18:54:44 -06:00
parent 5c97bfb182
commit b34c9bc99f
Signed by: xuu
GPG Key ID: 8B3B0604F164E04F
2 changed files with 56 additions and 17 deletions

11
feed.go
View File

@ -322,9 +322,16 @@ func storeFeed(ctx context.Context, db db, f types.TwtFile) error {
_, part, ok := strings.Cut(prev.Value(), " ") _, part, ok := strings.Cut(prev.Value(), " ")
if ok { if ok {
uri:= f.Twter().URI uri:= f.Twter().URI
if u, ok := f.Info().GetN("url", 0); ok {
uri = u.Value()
}
if u, ok := f.Info().GetN("uri", 0); ok {
uri = u.Value()
}
part = uri[:strings.LastIndex(uri, "/")+1] + part part = uri[:strings.LastIndex(uri, "/")+1] + part
childID := urlNS.UUID5(part) childID := urlNS.UUID5(part)
fmt.Println("found prev", uri, part)
args = append(args, args = append(args,
childID, // feed_id childID, // feed_id
feedID, // parent_id feedID, // parent_id
@ -332,7 +339,7 @@ func storeFeed(ctx context.Context, db db, f types.TwtFile) error {
part, // uri part, // uri
"once", // state "once", // state
nil, // last_scan_on nil, // last_scan_on
refreshRate, // refresh_rate 0, // refresh_rate
) )
} }
} }

62
http.go
View File

@ -109,34 +109,36 @@ func httpServer(c *console, app *appState) error {
ctx, span := otel.Span(r.Context()) ctx, span := otel.Span(r.Context())
defer span.End() defer span.End()
args := make([]any, 0, 3)
uriarg := ""
uri := r.URL.Query().Get("uri")
if uri != "" {
uriarg = "and uri = ?"
args = append(args, uri)
}
limit := 100 limit := 100
if v, ok := strconv.Atoi(r.URL.Query().Get("limit")); ok == nil { if v, ok := strconv.Atoi(r.URL.Query().Get("limit")); ok == nil {
limit = v limit = v
} }
offset := 0 offset := 0
if v, ok := strconv.Atoi(r.URL.Query().Get("offset")); ok == nil { if v, ok := strconv.Atoi(r.URL.Query().Get("offset")); ok == nil {
offset = v offset = v
} }
args = append(args, limit, offset)
args := []any{limit, offset}
uriqry := ""
if u := r.URL.Query().Get("uri"); u != "" {
uriqry = "and uri = ?"
args = append([]any{u}, args...)
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8") w.Header().Set("Content-Type", "text/plain; charset=utf-8")
rows, err := db.QueryContext( rows, err := db.QueryContext(
ctx, ctx,
`SELECT `SELECT
feed_id, feed_id,
hash, hash,
conv, conv,
nick, nick,
uri, uri,
text text
FROM twts FROM twts
JOIN ( JOIN (
SELECT SELECT
feed_id, feed_id,
@ -144,7 +146,7 @@ func httpServer(c *console, app *appState) error {
uri uri
FROM feeds FROM feeds
where state not in ('frozen', 'permanantly-dead') where state not in ('frozen', 'permanantly-dead')
`+uriqry+` `+uriarg+`
) using (feed_id) ) using (feed_id)
order by ulid desc order by ulid desc
limit ? limit ?
@ -179,7 +181,12 @@ func httpServer(c *console, app *appState) error {
twts = append(twts, twt) twts = append(twts, twt)
} }
var preamble lextwt.Comments var preamble lextwt.Comments
preamble = append(preamble, lextwt.NewComment("# self = /api/plain/twts")) preamble = append(preamble, lextwt.NewComment("# I am the Watcher. I am your guide through this vast new twtiverse."))
preamble = append(preamble, lextwt.NewComment("# self = /api/plain/twts"+mkqry(uri, limit, offset)))
preamble = append(preamble, lextwt.NewComment("# next = /api/plain/twts"+mkqry(uri, limit, offset+len(twts))))
if offset > 0 {
preamble = append(preamble, lextwt.NewComment("# prev = /api/plain/twts"+mkqry(uri, limit, offset-limit)))
}
reg := lextwt.NewTwtRegistry(preamble, twts) reg := lextwt.NewTwtRegistry(preamble, twts)
reg.WriteTo(w) reg.WriteTo(w)
@ -271,3 +278,28 @@ func notAny(s string, chars string) bool {
} }
return true return true
} }
func mkqry(uri string, limit, offset int) string {
qry := make([]string, 0, 3)
if uri != "" {
qry = append(qry, "uri=" + uri)
}
limit = min(100, max(1, limit))
if limit != 100 {
qry = append(qry, fmt.Sprint("limit=", limit))
}
offset = max(0, offset)
if offset != 0 {
qry = append(qry, fmt.Sprint("offset=", offset))
}
if len(qry) == 0 {
return ""
}
return "?" + strings.Join(qry, "&")
}