diff --git a/http-api.go b/http-api.go index 7f3335e..b001578 100644 --- a/http-api.go +++ b/http-api.go @@ -202,9 +202,8 @@ func addKey(preamble lextwt.Comments, key, value string, v ...any) lextwt.Commen return append(preamble, lextwt.NewCommentValue(comment, key, value)) } - func mkPreamble(hostname, uri, path string, limit int, length, offset, end int64) lextwt.Comments { - uri += path + hostname += path preamble := addKey(mkPreambleDocs(hostname), "twt range", "1 %d", end) preamble = addKey(preamble, "self", "%s%s", hostname, mkqry(uri, limit, offset)) if next := offset + length; next < end { diff --git a/http-html.go b/http-html.go index 09c46ce..7c434df 100644 --- a/http-html.go +++ b/http-html.go @@ -56,6 +56,44 @@ func (a *HTML) home(w http.ResponseWriter, r *http.Request) { preamble := mkPreamble(a.hostname, uri, "", limit, int64(len(twts)), offset, end) + reg := &HTWriter{ + lextwt.NewTwtRegistry(preamble, reverse(twts)), + } + + reg.WriteTo(w) +} + +func (a *HTML) conv(w http.ResponseWriter, r *http.Request) { + ctx, span := otel.Span(r.Context()) + defer span.End() + + w.Header().Set("Content-Type", "text/html; charset=utf-8") + + hash := r.PathValue("hash") + if (len(hash) < 6 || len(hash) > 8) && !notAny(hash, "abcdefghijklmnopqrstuvwxyz234567") { + w.WriteHeader(http.StatusBadRequest) + return + } + + limit := 100 + if v, ok := strconv.Atoi(r.URL.Query().Get("limit")); ok == nil { + limit = v + } + + var offset int64 = 0 + if v, ok := strconv.ParseInt(r.URL.Query().Get("offset"), 10, 64); ok == nil { + offset = v + } + + twts, offset, end, err := fetchConv(ctx, a.db, hash, limit, offset) + span.RecordError(err) + if err != nil { + http.Error(w, "ERR", 500) + return + } + + preamble := mkPreamble(a.hostname, "", "/conv/"+hash, limit, int64(len(twts)), offset, end) + reg := &HTWriter{ lextwt.NewTwtRegistry(preamble, twts), } @@ -90,9 +128,9 @@ func (r *HTWriter) WriteTo(w io.Writer) (int64, error) { .h-card .u-photo { width: 32px; } .h-card .date { text-align: right;} section { overflow: scroll; } - -
+`) output += int64(i) @@ -117,7 +155,7 @@ func (r *HTWriter) WriteTo(w io.Writer) (int64, error) { i, err = fmt.Fprintln(w, "