mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-09-28 21:02:28 +02:00
Filter.LimitZero
This commit is contained in:
19
filter.go
19
filter.go
@@ -2,7 +2,6 @@ package nostr
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
"github.com/mailru/easyjson"
|
"github.com/mailru/easyjson"
|
||||||
@@ -19,6 +18,9 @@ type Filter struct {
|
|||||||
Until *Timestamp `json:"until,omitempty"`
|
Until *Timestamp `json:"until,omitempty"`
|
||||||
Limit int `json:"limit,omitempty"`
|
Limit int `json:"limit,omitempty"`
|
||||||
Search string `json:"search,omitempty"`
|
Search string `json:"search,omitempty"`
|
||||||
|
|
||||||
|
// LimitZero is or must be set when there is a "limit":0 in the filter, and not when "limit" is just omitted
|
||||||
|
LimitZero bool `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TagMap map[string][]string
|
type TagMap map[string][]string
|
||||||
@@ -115,16 +117,21 @@ func FilterEqual(a Filter, b Filter) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if a.LimitZero != b.LimitZero {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ef Filter) Clone() Filter {
|
func (ef Filter) Clone() Filter {
|
||||||
clone := Filter{
|
clone := Filter{
|
||||||
IDs: slices.Clone(ef.IDs),
|
IDs: slices.Clone(ef.IDs),
|
||||||
Authors: slices.Clone(ef.Authors),
|
Authors: slices.Clone(ef.Authors),
|
||||||
Kinds: slices.Clone(ef.Kinds),
|
Kinds: slices.Clone(ef.Kinds),
|
||||||
Limit: ef.Limit,
|
Limit: ef.Limit,
|
||||||
Search: ef.Search,
|
Search: ef.Search,
|
||||||
|
LimitZero: ef.LimitZero,
|
||||||
}
|
}
|
||||||
|
|
||||||
if ef.Tags != nil {
|
if ef.Tags != nil {
|
||||||
|
@@ -129,6 +129,9 @@ func easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Filter)
|
|||||||
}
|
}
|
||||||
case "limit":
|
case "limit":
|
||||||
out.Limit = int(in.Int())
|
out.Limit = int(in.Int())
|
||||||
|
if out.Limit == 0 {
|
||||||
|
out.LimitZero = true
|
||||||
|
}
|
||||||
case "search":
|
case "search":
|
||||||
out.Search = string(in.String())
|
out.Search = string(in.String())
|
||||||
default:
|
default:
|
||||||
@@ -243,7 +246,7 @@ func easyjson4d398eaaEncodeGithubComNbdWtfGoNostr(out *jwriter.Writer, in Filter
|
|||||||
}
|
}
|
||||||
out.Int64(int64(*in.Until))
|
out.Int64(int64(*in.Until))
|
||||||
}
|
}
|
||||||
if in.Limit != 0 {
|
if in.Limit != 0 || in.LimitZero {
|
||||||
const prefix string = ",\"limit\":"
|
const prefix string = ",\"limit\":"
|
||||||
if first {
|
if first {
|
||||||
first = false
|
first = false
|
||||||
|
@@ -2,9 +2,8 @@ package nostr
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"testing"
|
|
||||||
|
|
||||||
"slices"
|
"slices"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFilterUnmarshal(t *testing.T) {
|
func TestFilterUnmarshal(t *testing.T) {
|
||||||
@@ -39,6 +38,41 @@ func TestFilterMarshal(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFilterUnmarshalWithLimitZero(t *testing.T) {
|
||||||
|
raw := `{"ids": ["abc"],"#e":["zzz"],"limit":0,"#something":["nothing","bab"],"since":1644254609,"search":"test"}`
|
||||||
|
var f Filter
|
||||||
|
if err := json.Unmarshal([]byte(raw), &f); err != nil {
|
||||||
|
t.Errorf("failed to parse filter json: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.Since == nil ||
|
||||||
|
f.Since.Time().UTC().Format("2006-01-02") != "2022-02-07" ||
|
||||||
|
f.Until != nil ||
|
||||||
|
f.Tags == nil || len(f.Tags) != 2 || !slices.Contains(f.Tags["something"], "bab") ||
|
||||||
|
f.Search != "test" ||
|
||||||
|
f.LimitZero == false {
|
||||||
|
t.Error("failed to parse filter correctly")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilterMarshalWithLimitZero(t *testing.T) {
|
||||||
|
until := Timestamp(12345678)
|
||||||
|
filterj, err := json.Marshal(Filter{
|
||||||
|
Kinds: []int{KindTextNote, KindRecommendServer, KindEncryptedDirectMessage},
|
||||||
|
Tags: TagMap{"fruit": {"banana", "mango"}},
|
||||||
|
Until: &until,
|
||||||
|
LimitZero: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal filter json: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := `{"kinds":[1,2,4],"until":12345678,"limit":0,"#fruit":["banana","mango"]}`
|
||||||
|
if string(filterj) != expected {
|
||||||
|
t.Errorf("filter json was wrong: %s != %s", string(filterj), expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFilterMatchingLive(t *testing.T) {
|
func TestFilterMatchingLive(t *testing.T) {
|
||||||
var filter Filter
|
var filter Filter
|
||||||
var event Event
|
var event Event
|
||||||
|
Reference in New Issue
Block a user