forked from fiatjaf/njump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_profile.go
91 lines (82 loc) · 2.42 KB
/
render_profile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"fmt"
"html"
"html/template"
"net/http"
"strings"
)
func renderProfile(w http.ResponseWriter, r *http.Request, code string) {
fmt.Println(r.URL.Path, "@.", r.Header.Get("user-agent"))
isSitemap := false
if strings.HasSuffix(code, ".xml") {
code = code[:len(code)-4]
isSitemap = true
}
isRSS := false
if strings.HasSuffix(code, ".rss") {
code = code[:len(code)-4]
isRSS = true
}
data, err := grabData(r.Context(), code, isSitemap)
if err != nil {
w.Header().Set("Cache-Control", "max-age=60")
errorPage := &ErrorPage{
Errors: err.Error(),
}
errorPage.TemplateText()
w.WriteHeader(http.StatusNotFound)
ErrorTemplate.Render(w, errorPage)
return
}
if len(data.renderableLastNotes) != 0 {
w.Header().Set("Cache-Control", "max-age=3600")
}
if isSitemap {
w.Header().Add("content-type", "text/xml")
w.Write([]byte(XML_HEADER))
SitemapTemplate.Render(w, &SitemapPage{
Host: s.Domain,
ModifiedAt: data.modifiedAt,
Npub: data.npub,
LastNotes: data.renderableLastNotes,
})
} else if isRSS {
w.Header().Add("content-type", "text/xml")
w.Write([]byte(XML_HEADER))
RSSTemplate.Render(w, &RSSPage{
Host: s.Domain,
ModifiedAt: data.modifiedAt,
Npub: data.npub,
Metadata: data.metadata,
LastNotes: data.renderableLastNotes,
})
} else {
w.Header().Add("content-type", "text/html")
err = ProfileTemplate.Render(w, &ProfilePage{
HeadCommonPartial: HeadCommonPartial{IsProfile: true, TailwindDebugStuff: tailwindDebugStuff},
DetailsPartial: DetailsPartial{
HideDetails: true,
CreatedAt: data.createdAt,
KindDescription: data.kindDescription,
KindNIP: data.kindNIP,
EventJSON: eventToHTML(data.event),
Kind: data.event.Kind,
},
ClientsPartial: ClientsPartial{
Clients: generateClientList(getPreviewStyle(r), data.nprofile, data.event),
},
Metadata: data.metadata,
NormalizedAuthorWebsiteURL: normalizeWebsiteURL(data.metadata.Website),
RenderedAuthorAboutText: template.HTML(basicFormatting(html.EscapeString(data.metadata.About), false, false, false)),
Npub: data.npub,
Nprofile: data.nprofile,
AuthorRelays: data.authorRelays,
LastNotes: data.renderableLastNotes,
})
}
if err != nil {
log.Error().Err(err).Msg("error rendering tmpl")
}
return
}