Skip to content

Commit

Permalink
prevent caching of incomplete profile pages.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Oct 15, 2024
1 parent caca355 commit f9a79f2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions internaldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
8 changes: 5 additions & 3 deletions nostr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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] {
Expand Down
14 changes: 9 additions & 5 deletions render_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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{
Expand Down

0 comments on commit f9a79f2

Please sign in to comment.