mirror of
https://github.com/aljazceru/njump.git
synced 2025-12-17 22:34:25 +01:00
move redirectors, renderRelay and renderProfile out of render.
This commit is contained in:
122
render.go
122
render.go
@@ -18,35 +18,25 @@ func render(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println(r.URL.Path, "#/", r.Header.Get("user-agent"))
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
|
||||
code := r.URL.Path[1:]
|
||||
isProfileSitemap := false
|
||||
code := r.URL.Path[1:] // hopefully a nip19 code
|
||||
|
||||
if strings.HasPrefix(code, "e/") {
|
||||
code, _ = nip19.EncodeEvent(code[2:], []string{}, "")
|
||||
} else if strings.HasPrefix(code, "p/") {
|
||||
if urlSuffixMatcher.MatchString(code) {
|
||||
// it's a nip05
|
||||
code = code[2:]
|
||||
} else {
|
||||
// it's a hex pubkey
|
||||
code, _ = nip19.EncodePublicKey(code[2:])
|
||||
}
|
||||
} else if strings.HasPrefix(code, "r/") {
|
||||
hostname := code[2:]
|
||||
if strings.HasPrefix(hostname, "wss:/") || strings.HasPrefix(hostname, "ws:/") {
|
||||
hostname = trimProtocol(hostname)
|
||||
http.Redirect(w, r, "/r/"+hostname, http.StatusFound)
|
||||
} else {
|
||||
renderRelayPage(w, r)
|
||||
}
|
||||
// it's the homepage
|
||||
if code == "" {
|
||||
renderHomepage(w, r)
|
||||
return
|
||||
} else if strings.HasPrefix(code, "nostr:") {
|
||||
http.Redirect(w, r, "/"+code[6:], http.StatusFound)
|
||||
} else if strings.HasPrefix(code, "npub") && strings.HasSuffix(code, ".xml") {
|
||||
isProfileSitemap = true
|
||||
code = code[:len(code)-4]
|
||||
}
|
||||
|
||||
if strings.HasPrefix(code, "nostr:") {
|
||||
// remove the "nostr:" prefix
|
||||
http.Redirect(w, r, "/"+code[6:], http.StatusFound)
|
||||
return
|
||||
} else if strings.HasPrefix(code, "npub") || strings.HasPrefix(code, "nprofile") {
|
||||
// it's a profile
|
||||
renderProfile(w, r, code)
|
||||
return
|
||||
}
|
||||
|
||||
// force note1 to become nevent1
|
||||
if strings.HasPrefix(code, "note1") {
|
||||
_, redirectHex, err := nip19.Decode(code)
|
||||
if err != nil {
|
||||
@@ -58,15 +48,10 @@ func render(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/"+redirectNevent, http.StatusFound)
|
||||
}
|
||||
|
||||
if code == "" {
|
||||
renderHomepage(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
host := r.Header.Get("X-Forwarded-Host")
|
||||
style := getPreviewStyle(r)
|
||||
|
||||
data, err := grabData(r.Context(), code, isProfileSitemap)
|
||||
data, err := grabData(r.Context(), code, false)
|
||||
if err != nil {
|
||||
w.Header().Set("Cache-Control", "max-age=60")
|
||||
http.Error(w, "error fetching event: "+err.Error(), 404)
|
||||
@@ -194,20 +179,18 @@ func render(w http.ResponseWriter, r *http.Request) {
|
||||
data.content = strings.ReplaceAll(data.content, placeholderTag, "nostr:"+nreplace)
|
||||
}
|
||||
if data.event.Kind == 30023 || data.event.Kind == 30024 {
|
||||
data.content = mdToHTML(data.content, data.typ == "telegram_instant_view")
|
||||
data.content = mdToHTML(data.content, data.templateId == TelegramInstantView)
|
||||
} else {
|
||||
// first we run basicFormatting, which turns URLs into their appropriate HTML tags
|
||||
data.content = basicFormatting(html.EscapeString(data.content), true, false)
|
||||
// then we render quotes as HTML, which will also apply basicFormatting to all the internal quotes
|
||||
data.content = renderQuotesAsHTML(r.Context(), data.content, data.typ == "telegram_instant_view")
|
||||
data.content = renderQuotesAsHTML(r.Context(), data.content, data.templateId == TelegramInstantView)
|
||||
// we must do this because inside <blockquotes> we must treat <img>s different when telegram_instant_view
|
||||
}
|
||||
|
||||
if data.typ == "telegram_instant_view" {
|
||||
if data.templateId == TelegramInstantView {
|
||||
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 {
|
||||
} else if len(data.content) != 0 {
|
||||
w.Header().Set("Cache-Control", "max-age=604800")
|
||||
} else {
|
||||
w.Header().Set("Cache-Control", "max-age=60")
|
||||
@@ -231,49 +214,6 @@ func render(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Link", "<"+oembed+"&format=xml>; rel=\"alternate\"; type=\"text/xml+oembed\"")
|
||||
}
|
||||
|
||||
if currentTemplate, ok := templateMapping[data.typ]; ok {
|
||||
// template stuff
|
||||
params := map[string]any{
|
||||
"style": style,
|
||||
"createdAt": data.createdAt,
|
||||
"modifiedAt": data.modifiedAt,
|
||||
"clients": generateClientList(code, data.event),
|
||||
"type": data.typ,
|
||||
"title": title,
|
||||
"titleizedContent": titleizedContent,
|
||||
"twitterTitle": twitterTitle,
|
||||
"npub": data.npub,
|
||||
"npubShort": data.npubShort,
|
||||
"nevent": data.nevent,
|
||||
"naddr": data.naddr,
|
||||
"metadata": data.metadata,
|
||||
"authorLong": data.authorLong,
|
||||
"subject": subject,
|
||||
"description": description,
|
||||
"summary": summary,
|
||||
"event": data.event,
|
||||
"eventJSON": string(eventJSON),
|
||||
"content": data.content,
|
||||
"textImageURL": textImageURL,
|
||||
"videoType": data.videoType,
|
||||
"image": data.image,
|
||||
"video": data.video,
|
||||
"proxy": "https://" + host + "/njump/proxy?src=",
|
||||
"kindDescription": data.kindDescription,
|
||||
"kindNIP": data.kindNIP,
|
||||
"lastNotes": data.renderableLastNotes,
|
||||
"seenOn": data.relays,
|
||||
"parentNevent": data.parentNevent,
|
||||
"authorRelays": data.authorRelays,
|
||||
"oembed": oembed,
|
||||
}
|
||||
|
||||
if err := tmpls.ExecuteTemplate(w, currentTemplate, params); err != nil {
|
||||
log.Error().Err(err).Msg("error rendering")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// migrating to templ
|
||||
switch data.templateId {
|
||||
case TelegramInstantView:
|
||||
@@ -328,28 +268,6 @@ func render(w http.ResponseWriter, r *http.Request) {
|
||||
Video: data.video,
|
||||
VideoType: data.videoType,
|
||||
})
|
||||
case Profile:
|
||||
err = ProfileTemplate.Render(w, &ProfilePage{
|
||||
HeadCommonPartial: HeadCommonPartial{IsProfile: true},
|
||||
DetailsPartial: DetailsPartial{
|
||||
HideDetails: true,
|
||||
CreatedAt: data.createdAt,
|
||||
KindDescription: data.kindDescription,
|
||||
KindNIP: data.kindNIP,
|
||||
EventJSON: string(eventJSON),
|
||||
Kind: data.event.Kind,
|
||||
},
|
||||
ClientsPartial: ClientsPartial{
|
||||
Clients: generateClientList(code, data.event),
|
||||
},
|
||||
|
||||
Metadata: data.metadata,
|
||||
NormalizedAuthorWebsiteURL: normalizeWebsiteURL(data.metadata.Website),
|
||||
RenderedAuthorAboutText: template.HTML(basicFormatting(html.EscapeString(data.metadata.About), false, false)),
|
||||
Npub: data.npub,
|
||||
AuthorRelays: data.authorRelays,
|
||||
LastNotes: data.renderableLastNotes,
|
||||
})
|
||||
case Other:
|
||||
err = OtherTemplate.Render(w, &OtherPage{
|
||||
HeadCommonPartial: HeadCommonPartial{IsProfile: false},
|
||||
|
||||
Reference in New Issue
Block a user