initial broken draft of relays page.

This commit is contained in:
fiatjaf
2023-07-11 18:19:11 -03:00
parent bfa2f5f537
commit d734b189d9
7 changed files with 174 additions and 33 deletions

View File

@@ -2,27 +2,18 @@ package main
import (
"context"
"embed"
_ "embed"
"encoding/json"
"fmt"
"html"
"net/http"
"regexp"
"strings"
"text/template"
"time"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19"
)
//go:embed static/*
var static embed.FS
//go:embed templates/*
var templates embed.FS
type Event struct {
Nevent string
Content string
@@ -50,8 +41,15 @@ func render(w http.ResponseWriter, r *http.Request) {
hostname := r.Header.Get("X-Forwarded-Host")
style := getPreviewStyle(r)
// code can be a nevent, nprofile, npub or nip05 identifier, in which case we try to fetch the associated event
event, err := getEvent(r.Context(), code)
if err != nil {
// this will fail if code is a relay URL, in which case we will handle it differently
if urlMatcher.MatchString(code) {
renderRelayPage(w, r)
return
}
http.Error(w, "error fetching event: "+err.Error(), 404)
return
}
@@ -275,30 +273,11 @@ func render(w http.ResponseWriter, r *http.Request) {
"parentNevent": parentNevent,
}
// Use a mapping to expressly link the templates and share them between more kinds/types
templateMapping := make(map[string]string)
templateMapping["profile"] = "profile.html"
templateMapping["note"] = "note.html"
templateMapping["address"] = "other.html"
// If a mapping is not found fallback to raw
// if a mapping is not found fallback to raw
if templateMapping[typ] == "" {
templateMapping[typ] = "other.html"
}
funcMap := template.FuncMap{
"basicFormatting": basicFormatting,
"mdToHTML": mdToHTML,
"escapeString": html.EscapeString,
"sanitizeXSS": sanitizeXSS,
}
tmpl := template.Must(
template.New("tmpl").
Funcs(funcMap).
ParseFS(templates, "templates/*"),
)
w.Header().Set("Cache-Control", "max-age=604800")
if err := tmpl.ExecuteTemplate(w, templateMapping[typ], params); err != nil {
log.Error().Err(err).Msg("error rendering")