From 74fa69274dc2251d22f8315de0725a3da1a183fb Mon Sep 17 00:00:00 2001 From: xuu Date: Wed, 2 Apr 2025 11:55:56 -0600 Subject: [PATCH] chore: add date formatting --- http-api.go | 3 +-- http-html.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++------ http.go | 4 +++- 3 files changed, 63 insertions(+), 10 deletions(-) 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, "
") output += int64(i) - for _, twt := range reverse(r.Twts()) { + for _, twt := range r.Twts() { twter := twt.Twter() uri, err := url.Parse(twter.URI) if err != nil { @@ -149,7 +187,7 @@ func (r *HTWriter) WriteTo(w io.Writer) (int64, error) {
%s
-
%s
+
@@ -160,8 +198,9 @@ func (r *HTWriter) WriteTo(w io.Writer) (int64, error) { `, "/?uri="+twter.URI, twter.Avatar, "/?uri="+twter.URI, twter.Nick, twter.URI, uri.Host, - "/conv/"+twt.Hash(), fmt.Sprintf("", twt.Created().Format(time.RFC3339), twt.Created().Format(time.RFC822)), - time.Since(twt.Created()).Round(time.Second).String(), twt, + "/conv/"+twt.Subject().Tag().Text(), fmt.Sprintf("", twt.Created().Format(time.RFC3339), twt.Created().Format(time.RFC822)), + // time.Since(twt.Created()).Round(time.Minute).String(), + twt, ) output += int64(i) if err != nil { @@ -170,7 +209,20 @@ func (r *HTWriter) WriteTo(w io.Writer) (int64, error) { } - i, err = fmt.Fprintln(w, "
") + i, err = fmt.Fprintln(w, ` + + `) output += int64(i) return output, err diff --git a/http.go b/http.go index 27e77f7..99cec40 100644 --- a/http.go +++ b/http.go @@ -35,9 +35,11 @@ func httpServer(ctx context.Context, app *appState) error { hostname: app.args.Hostname, } - http.HandleFunc("/", html.home) http.HandleFunc("/health", html.healthcheck) + http.HandleFunc("/", html.home) + http.HandleFunc("/conv/{hash}", html.conv) + http.HandleFunc("/api/plain", api.plain) http.HandleFunc("/api/plain/conv/{hash}", api.conv) http.HandleFunc("/api/plain/mentions", api.mentions)