mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-07-04 20:44:28 +02:00
sdk: make it so replaceable stuff is automatically reloaded from time to time.
This commit is contained in:
36
sdk/loader_helpers.go
Normal file
36
sdk/loader_helpers.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user