-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathrender_profile.go
More file actions
177 lines (156 loc) · 5.22 KB
/
Copy pathrender_profile.go
File metadata and controls
177 lines (156 loc) · 5.22 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"bytes"
"context"
"html"
"html/template"
"net/http"
"strings"
"time"
"fiatjaf.com/nostr/sdk"
)
func renderProfile(ctx context.Context, r *http.Request, w http.ResponseWriter, code string) {
isEmbed := r.URL.Query().Get("embed") != ""
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
}
pp := sdk.InputToProfile(ctx, code)
if pp == nil {
log.Warn().Str("code", code).Msg("invalid profile code")
w.Header().Set("Cache-Control", "public, immutable, s-maxage=86400, max-age=86400")
w.WriteHeader(http.StatusNotFound)
errorTemplate(ErrorPageParams{Errors: "invalid profile code", Clients: generateClientList(999999, code)}).Render(ctx, w)
return
}
if banned, reason := isPubkeyBanned(pp.PublicKey); banned {
deleteAllEventsFromPubKey(pp.PublicKey)
w.Header().Set("Cache-Control", "public, immutable, s-maxage=604800, max-age=604800")
log.Warn().Str("pubkey", pp.PublicKey.Hex()).Str("reason", reason).Msg("pubkey banned")
http.Error(w, "pubkey banned", http.StatusNotFound)
return
}
// Give metadata fetch only 5 seconds to avoid timing out the whole page
profileCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
profile := sys.FetchProfileMetadata(profileCtx, pp.PublicKey)
// Check if profile metadata is missing
profileMissing := profile.Event == nil
if profileMissing {
// Trigger background fetch to populate cache for next request
go sys.FetchProfileMetadata(context.Background(), pp.PublicKey)
}
if isMaliciousBridged(profile) {
deleteAllEventsFromPubKey(pp.PublicKey)
w.Header().Set("Cache-Control", "public, immutable, s-maxage=604800, max-age=604800")
log.Warn().Str("pubkey", pp.PublicKey.Hex()).Msg("pubkey malicious bridged blocked")
http.Error(w, "profile is malicious", http.StatusNotFound)
return
}
if is, _ := isExplicitContent(ctx, profile.Picture); is {
deleteAllEventsFromPubKey(pp.PublicKey)
w.Header().Set("Cache-Control", "public, immutable, s-maxage=604800, max-age=604800")
log.Warn().Str("pubkey", pp.PublicKey.Hex()).Msg("pubkey explicit content blocked")
http.Error(w, "profile is not allowed", http.StatusNotFound)
return
}
var createdAt string
if profile.Event != nil {
createdAt = profile.Event.CreatedAt.Time().Format("2006-01-02T15:04:05Z07:00")
w.Header().Set("ETag", profile.Event.ID.Hex())
}
var lastNotes []EnhancedEvent
var justFetched bool
if !isEmbed {
lastNotes, justFetched = authorLastNotes(ctx, profile.PubKey)
}
// Use short cache if notes were just fetched or profile metadata is missing
if justFetched || profileMissing {
w.Header().Set("Cache-Control", "public, s-maxage=5, max-age=5")
} else {
w.Header().Set("Cache-Control", "public, s-maxage=1800, max-age=1800, stale-while-revalidate=31536000")
}
var err error
if isSitemap {
w.Header().Add("content-type", "text/xml")
var buf bytes.Buffer
buf.WriteString(XML_HEADER)
err = SitemapTemplate.Render(&buf, &SitemapPage{
Host: s.Domain,
ModifiedAt: createdAt,
Metadata: profile,
LastNotes: lastNotes,
})
if err == nil {
w.Write(buf.Bytes())
}
} else if isRSS {
w.Header().Add("content-type", "text/xml")
var buf bytes.Buffer
buf.WriteString(XML_HEADER)
err = RSSTemplate.Render(&buf, &RSSPage{
Host: s.Domain,
ModifiedAt: createdAt,
Metadata: profile,
LastNotes: lastNotes,
})
if err == nil {
w.Write(buf.Bytes())
}
} else {
w.Header().Add("content-type", "text/html")
nprofile := profile.Nprofile(ctx, sys, 2)
originalPath := strings.Split(strings.Split(r.URL.Path, "?")[0], "#")[0]
params := ProfilePageParams{
HeadParams: HeadParams{IsProfile: true},
Details: DetailsParams{
HideDetails: true,
CreatedAt: createdAt,
KindDescription: kindNames[0],
KindNIP: kindNIPs[0],
EventJSON: toJSONHTML(profile.Event),
Kind: 0,
Metadata: profile,
},
Metadata: profile,
NormalizedAuthorWebsiteURL: normalizeWebsiteURL(profile.Website),
RenderedAuthorAboutText: template.HTML(basicFormatting(html.EscapeString(profile.About), false, false, false)),
Nprofile: nprofile,
OriginalPath: originalPath,
AuthorRelays: relaysPretty(ctx, profile.PubKey),
LastNotes: lastNotes,
FetchingNotes: len(lastNotes) == 0 && justFetched,
Clients: generateClientList(0, nprofile,
func(c ClientReference, s string) string {
if c.ID == "nostrudel" {
s = strings.Replace(s, "/n/", "/u/", 1)
}
if c.ID == "primal-web" {
s = strings.Replace(
strings.Replace(s, "/e/", "/p/", 1),
nprofile, profile.Npub(), 1)
}
return s
},
),
}
// give this global context a timeout because it may used inside the template to validate the nip05 address
ctx, cancel := context.WithTimeout(ctx, time.Second*3)
defer cancel()
if isEmbed {
err = embeddedProfileTemplate(params).Render(ctx, w)
} else {
err = profileTemplate(params).Render(ctx, w)
}
}
if err != nil {
log.Warn().Err(err).Msg("error rendering tmpl")
}
return
}