oembed discovery.

This commit is contained in:
fiatjaf
2023-09-24 14:12:10 -03:00
parent 892065339d
commit 475b614653
2 changed files with 41 additions and 12 deletions

View File

@@ -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 <blockquotes> we must treat <img>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

View File

@@ -42,8 +42,19 @@
rel="canonical"
href="https://{{.s.CanonicalHost}}/{{.nevent | escapeString }}"
/>
{{template "head_common.html"}}
<!---->
{{ if .oembed }}
<link
rel="alternate"
type="application/json+oembed"
href="{{.oembed}}&format=json"
/>
<link
rel="alternate"
type="text/xml+oembed"
href="{{.oembed}}&format=xml"
/>
{{ end }} {{template "head_common.html"}}
</head>
<body class="note">