sdk: make it so replaceable stuff is automatically reloaded from time to time.

This commit is contained in:
fiatjaf
2025-01-17 18:21:19 -03:00
parent 3fd33ce281
commit 6cffcc3b47
4 changed files with 185 additions and 35 deletions

36
sdk/loader_helpers.go Normal file
View File

@ -0,0 +1,36 @@
package sdk
import (
"encoding/binary"
"encoding/hex"
"github.com/nbd-wtf/go-nostr"
)
var kvStoreLastFetchPrefix = byte('f')
func makeLastFetchKey(kind int, pubkey string) []byte {
buf := make([]byte, 1+5+32)
buf[0] = kvStoreLastFetchPrefix
binary.LittleEndian.PutUint32(buf[1:], uint32(kind))
hex.Decode(buf[5:], []byte(pubkey))
return buf
}
// encodeTimestamp encodes a unix timestamp as 4 bytes
func encodeTimestamp(t nostr.Timestamp) []byte {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, uint32(t))
return b
}
// decodeTimestamp decodes a 4-byte timestamp into unix seconds
func decodeTimestamp(b []byte) nostr.Timestamp {
return nostr.Timestamp(binary.BigEndian.Uint32(b))
}
// shouldRefreshFromNetwork checks if we should try fetching from network
func shouldRefreshFromNetwork(lastFetchData []byte) bool {
lastFetch := decodeTimestamp(lastFetchData)
return nostr.Now()-lastFetch > 7*24*60*60
}