Compare commits

..

3 Commits

Author SHA1 Message Date
xuu
a0614435ff
chore: deps 2025-03-26 18:58:59 -06:00
xuu
fe28b7c2ad
chore: go fmt 2025-03-26 18:57:09 -06:00
xuu
b34c9bc99f
chore: changes to feed 2025-03-26 18:54:44 -06:00
2 changed files with 56 additions and 18 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
) )
} }
} }

53
http.go
View File

@ -109,24 +109,25 @@ 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 != "" {
feed_id := urlNS.UUID5(uri)
uriarg = "and feed_id = ?"
args = append(args, feed_id)
}
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 := []any{limit, offset}
uriqry := ""
if u := r.URL.Query().Get("uri"); u != "" {
feed_id := urlNS.UUID5(u)
uriqry = "and feed_id = ?"
args = append([]any{feed_id}, 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,
@ -145,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 ?
@ -180,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)
@ -280,3 +286,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, "&")
}