chore: helper function for preamble

This commit is contained in:
xuu committed 2025-03-28 07:21:34 -06:00
1 parent 00c97eb011
commit b863a2786e
4 files changed
+48 -33

No files matched your search

+44 -31
View File
@@ -19,6 +19,8 @@ import (
"go.sour.is/xt/internal/otel"
)
const iAmTheWatcher = "I am the Watcher. I am your guide through this vast new twtiverse."
func httpServer(ctx context.Context, app *appState) error {
ctx, span := otel.Span(ctx)
defer span.End()
@@ -52,27 +54,26 @@ func httpServer(ctx context.Context, app *appState) error {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
rows, err := db.QueryContext(
ctx,
`SELECT
feed_id,
hash,
conv,
ctx, `
SELECT
feed_id,
hash,
conv,
nick,
uri,
text
FROM twts
JOIN (
SELECT
feed_id,
nick,
uri,
text
FROM twts
JOIN (
SELECT
feed_id,
nick,
uri
FROM feeds
) using (feed_id)
WHERE
hash = $1 or
conv = $1
order by ulid asc`,
hash,
uri
FROM feeds
) using (feed_id)
WHERE
hash = $1 or
conv = $1
order by ulid asc`, hash,
)
if err != nil {
span.RecordError(err)
@@ -102,7 +103,7 @@ func httpServer(ctx context.Context, app *appState) error {
twts = append(twts, twt)
}
var preamble lextwt.Comments
preamble = append(preamble, lextwt.NewComment("# self = /conv/"+hash))
preamble = add(preamble, "self = /conv/%s", hash)
reg := lextwt.NewTwtRegistry(preamble, twts)
reg.WriteTo(w)
@@ -205,20 +206,19 @@ func httpServer(ctx context.Context, app *appState) error {
return
}
twter := types.NewTwter(o.Nick, o.URI)
o.Text = strings.ReplaceAll(o.Text, "\n", "\u2028")
twt, _ := lextwt.ParseLine(o.Text, &twter)
twts = append(twts, twt)
}
var preamble lextwt.Comments
preamble = append(preamble, lextwt.NewComment("# I am the Watcher. I am your guide through this vast new twtiverse."))
preamble = append(preamble, lextwt.NewComment(fmt.Sprint("# range = 1 ", end)))
preamble = append(preamble, lextwt.NewComment("# self = /api/plain/twts"+mkqry(uri, limit, offset)))
preamble = add(preamble, iAmTheWatcher)
preamble = add(preamble, "")
preamble = add(preamble, "range = 1 %d", end)
preamble = add(preamble, "self = /api/plain/twts%s", mkqry(uri, limit, offset))
if next := offset + int64(len(twts)); next < end {
preamble = append(preamble, lextwt.NewComment("# next = /api/plain/twts"+mkqry(uri, limit, next)))
preamble = add(preamble, "next = /api/plain/twts%s", mkqry(uri, limit, next))
}
if prev := offset - int64(limit); prev > 0 {
preamble = append(preamble, lextwt.NewComment("# prev = /api/plain/twts"+mkqry(uri, limit, prev)))
preamble = add(preamble, "prev = /api/plain/twts%s", mkqry(uri, limit, prev))
}
reg := lextwt.NewTwtRegistry(preamble, twts)
@@ -249,8 +249,10 @@ func httpServer(ctx context.Context, app *appState) error {
last_twt_on
FROM feeds
left join (
select feed_id, max(strftime('%Y-%m-%dT%H:%M:%fZ', (substring(text, 1, instr(text, ' ')-1)))) last_twt_on
from twts group by feed_id
select
feed_id,
max(strftime('%Y-%m-%dT%H:%M:%fZ', (substring(text, 1, instr(text, ' ')-1)))) last_twt_on
from twts group by feed_id
) using (feed_id)
`+where+`
order by nick, uri
@@ -282,7 +284,11 @@ func httpServer(ctx context.Context, app *appState) error {
nil,
))
}
reg := lextwt.NewTwtRegistry(nil, twts)
var preamble lextwt.Comments
preamble = add(preamble, iAmTheWatcher)
reg := lextwt.NewTwtRegistry(preamble, twts)
reg.WriteTo(w)
})
@@ -341,3 +347,10 @@ func mkqry(uri string, limit int, offset int64) string {
return "?" + strings.Join(qry, "&")
}
func add(preamble lextwt.Comments, text string, v ...any) lextwt.Comments {
if len(v) > 0 {
text = fmt.Sprintf(text, v...)
}
return append(preamble, lextwt.NewComment("# "+text))
}