prevent caching of incomplete profile pages.

This commit is contained in:
fiatjaf
2024-10-15 00:08:02 -03:00
parent caca355bbd
commit f9a79f209e
3 changed files with 16 additions and 10 deletions

View File

@@ -152,8 +152,8 @@ func (internal *InternalDB) overwriteFollowListArchive(fla *FollowListArchive) e
}
func (internal *InternalDB) attachRelaysToEvent(eventId string, relays ...string) (allRelays []string) {
idb, _ := hex.DecodeString(eventId[0:16])
if _, err := internal.DB.Upsert("cached-id", idb, TypeCachedEvent, func(t leafdb.DataType, value proto.Message) (proto.Message, error) {
idxkey, _ := hex.DecodeString(eventId[0:16])
if _, err := internal.DB.Upsert("cached-id", idxkey, TypeCachedEvent, func(t leafdb.DataType, value proto.Message) (proto.Message, error) {
var ee *CachedEvent
if value == nil {
ee = &CachedEvent{

View File

@@ -76,7 +76,7 @@ func getEvent(ctx context.Context, code string, withRelays bool) (*nostr.Event,
return evt, allRelays, nil
}
func authorLastNotes(ctx context.Context, pubkey string) []EnhancedEvent {
func authorLastNotes(ctx context.Context, pubkey string) (lastNotes []EnhancedEvent, justFetched bool) {
limit := 100
go sys.FetchProfileMetadata(ctx, pubkey) // fetch this before so the cache is filled for later
@@ -87,7 +87,7 @@ func authorLastNotes(ctx context.Context, pubkey string) []EnhancedEvent {
Limit: limit,
}
lastNotes := make([]EnhancedEvent, 0, filter.Limit)
lastNotes = make([]EnhancedEvent, 0, filter.Limit)
latestTimestamp := nostr.Timestamp(0)
// fetch from local store if available
@@ -107,6 +107,8 @@ func authorLastNotes(ctx context.Context, pubkey string) []EnhancedEvent {
(len(lastNotes) < limit/5 && latestTimestamp > nostr.Now()-60*60*24*2) ||
(len(lastNotes) < limit/2 && latestTimestamp < nostr.Now()-60*60*24*2) {
// if we didn't get enough notes then try to fetch from external relays (but do not wait for it)
justFetched = true
go func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
@@ -138,7 +140,7 @@ func authorLastNotes(ctx context.Context, pubkey string) []EnhancedEvent {
}()
}
return lastNotes
return lastNotes, justFetched
}
func relayLastNotes(ctx context.Context, hostname string, limit int) iter.Seq[*nostr.Event] {

View File

@@ -25,7 +25,7 @@ func renderProfile(ctx context.Context, r *http.Request, w http.ResponseWriter,
profile, err := sys.FetchProfileFromInput(ctx, code)
if err != nil {
log.Warn().Err(err).Str("code", code).Msg("event not found on render_profile")
log.Warn().Err(err).Str("code", code).Msg("error fetching profile on render_profile")
w.Header().Set("Cache-Control", "max-age=60")
w.WriteHeader(http.StatusNotFound)
@@ -41,13 +41,19 @@ func renderProfile(ctx context.Context, r *http.Request, w http.ResponseWriter,
}
var lastNotes []EnhancedEvent
var cacheControl string = "max-age=86400"
if !isEmbed {
lastNotes = authorLastNotes(ctx, profile.PubKey)
var justFetched bool
lastNotes, justFetched = authorLastNotes(ctx, profile.PubKey)
if justFetched && profile.Event != nil {
cacheControl = "only-if-cached"
}
}
w.Header().Set("Cache-Control", cacheControl)
if isSitemap {
w.Header().Add("content-type", "text/xml")
w.Header().Set("Cache-Control", "max-age=86400")
w.Write([]byte(XML_HEADER))
err = SitemapTemplate.Render(w, &SitemapPage{
Host: s.Domain,
@@ -56,7 +62,6 @@ func renderProfile(ctx context.Context, r *http.Request, w http.ResponseWriter,
})
} else if isRSS {
w.Header().Add("content-type", "text/xml")
w.Header().Set("Cache-Control", "max-age=86400")
w.Write([]byte(XML_HEADER))
err = RSSTemplate.Render(w, &RSSPage{
Host: s.Domain,
@@ -66,7 +71,6 @@ func renderProfile(ctx context.Context, r *http.Request, w http.ResponseWriter,
})
} else {
w.Header().Add("content-type", "text/html")
w.Header().Set("Cache-Control", "max-age=86400")
nprofile := profile.Nprofile(ctx, sys, 2)
params := ProfilePageParams{