chore: add date formatting
This commit is contained in:
parent
6b8ad143fe
commit
74fa69274d
@ -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 {
|
||||
|
66
http-html.go
66
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; }
|
||||
</style
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<body onload="setTimestamps()">
|
||||
<pre class='preamble'>
|
||||
`)
|
||||
output += int64(i)
|
||||
@ -117,7 +155,7 @@ func (r *HTWriter) WriteTo(w io.Writer) (int64, error) {
|
||||
i, err = fmt.Fprintln(w, "</pre><main>")
|
||||
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) {
|
||||
|
||||
<div class="date">
|
||||
<div><a class="u-url" href="%s">%s</a></div>
|
||||
<div><small>%s</small></div>
|
||||
<div><small> </small></div>
|
||||
</div>
|
||||
</header>
|
||||
<section>
|
||||
@ -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("<time datetime='%s'>%s</time>", 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("<time datetime='%s'>%s</time>", 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, "</main></body>")
|
||||
i, err = fmt.Fprintln(w, `</main>
|
||||
<script>
|
||||
function setTimestamps() {
|
||||
document.querySelectorAll("time").forEach((e) => {
|
||||
const OneDay = new Date(new Date().getTime() - (1 * 24 * 60 * 60 * 1000))
|
||||
const d = new Date(e.hasAttributes() && e.attributes.getNamedItem('datetime'). value);
|
||||
if (d > OneDay)
|
||||
e.innerHTML = d.toLocaleTimeString('en-US',{ hour: '2-digit', minute: '2-digit' });
|
||||
else
|
||||
e.innerHTML = d.toLocaleDateString('en-US',{ hour: '2-digit', minute: '2-digit' });
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>`)
|
||||
output += int64(i)
|
||||
|
||||
return output, err
|
||||
|
4
http.go
4
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user