diff --git a/render.go b/render.go index 45ea236..e435ccb 100644 --- a/render.go +++ b/render.go @@ -7,6 +7,7 @@ import ( "fmt" "html" "net/http" + "net/url" "regexp" "strings" "time" @@ -397,9 +398,35 @@ func render(w http.ResponseWriter, r *http.Request) { // we must do this because inside we must treat s different when telegram_instant_view } + if data.typ == "telegram_instant_view" { + w.Header().Set("Cache-Control", "no-cache") + } else if strings.Contains(data.typ, "profile") && len(data.renderableLastNotes) != 0 { + w.Header().Set("Cache-Control", "max-age=3600") + } else if !strings.Contains(data.typ, "profile") && len(data.content) != 0 { + w.Header().Set("Cache-Control", "max-age=604800") + } else { + w.Header().Set("Cache-Control", "max-age=60") + } + // pretty JSON eventJSON, _ := json.MarshalIndent(data.event, "", " ") + // oembed discovery + oembed := "" + if data.typ == "note" { + oembed = (&url.URL{ + Scheme: "https", + Host: host, + Path: "/services/oembed", + RawQuery: (url.Values{ + "url": {fmt.Sprintf("https://%s/%s", host, code)}, + }).Encode(), + }).String() + w.Header().Add("Link", "<"+oembed+"&format=json>; rel=\"alternate\"; type=\"application/json+oembed\"") + w.Header().Add("Link", "<"+oembed+"&format=xml>; rel=\"alternate\"; type=\"text/xml+oembed\"") + } + + // template params := map[string]any{ "createdAt": data.createdAt, "modifiedAt": data.modifiedAt, @@ -430,6 +457,7 @@ func render(w http.ResponseWriter, r *http.Request) { "lastNotes": data.renderableLastNotes, "parentNevent": data.parentNevent, "authorRelays": data.authorRelays, + "oembed": oembed, "s": s, } @@ -439,16 +467,6 @@ func render(w http.ResponseWriter, r *http.Request) { currentTemplate = "other.html" } - if data.typ == "telegram_instant_view" { - w.Header().Set("Cache-Control", "no-cache") - } else if strings.Contains(data.typ, "profile") && len(data.renderableLastNotes) != 0 { - w.Header().Set("Cache-Control", "max-age=3600") - } else if !strings.Contains(data.typ, "profile") && len(data.content) != 0 { - w.Header().Set("Cache-Control", "max-age=604800") - } else { - w.Header().Set("Cache-Control", "max-age=60") - } - if err := tmpl.ExecuteTemplate(w, currentTemplate, params); err != nil { log.Error().Err(err).Msg("error rendering") return diff --git a/templates/note.html b/templates/note.html index df9c342..312917a 100644 --- a/templates/note.html +++ b/templates/note.html @@ -42,8 +42,19 @@ rel="canonical" href="https://{{.s.CanonicalHost}}/{{.nevent | escapeString }}" /> - - {{template "head_common.html"}} + + {{ if .oembed }} + + + {{ end }} {{template "head_common.html"}}