mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-04-13 06:09:18 +02:00
37 lines
928 B
Go
37 lines
928 B
Go
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
|
|
}
|