Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dedicating kind type and adding its methods. #144

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestCount(t *testing.T) {
defer rl.Close()

count, err := rl.Count(context.Background(), Filters{
{Kinds: []int{KindContactList}, Tags: TagMap{"p": []string{"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"}}},
{Kinds: []Kind{KindContactList}, Tags: TagMap{"p": []string{"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"}}},
})
assert.NoError(t, err)
assert.Greater(t, count, int64(0))
Expand Down
2 changes: 1 addition & 1 deletion envelopes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func TestParseMessage(t *testing.T) {
{
Name: "REQ envelope",
Message: []byte(`["REQ","million", {"kinds": [1]}, {"kinds": [30023 ], "#d": ["buteko", "batuke"]}]`),
ExpectedEnvelope: &ReqEnvelope{SubscriptionID: "million", Filters: Filters{{Kinds: []int{1}}, {Kinds: []int{30023}, Tags: TagMap{"d": []string{"buteko", "batuke"}}}}},
ExpectedEnvelope: &ReqEnvelope{SubscriptionID: "million", Filters: Filters{{Kinds: []Kind{1}}, {Kinds: []Kind{30023}, Tags: TagMap{"d": []string{"buteko", "batuke"}}}}},
},
}

Expand Down
2 changes: 1 addition & 1 deletion eose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestEOSEMadness(t *testing.T) {
defer rl.Close()

sub, err := rl.Subscribe(context.Background(), Filters{
{Kinds: []int{KindTextNote}, Limit: 2},
{Kinds: []Kind{KindTextNote}, Limit: 2},
})
assert.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion event.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Event struct {
ID string
PubKey string
CreatedAt Timestamp
Kind int
Kind Kind
Tags Tags
Content string
Sig string
Expand Down
7 changes: 3 additions & 4 deletions event_easyjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func easyjsonF642ad3eDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Event)
case "created_at":
out.CreatedAt = Timestamp(in.Int64())
case "kind":
out.Kind = in.Int()
out.Kind = Kind(in.Uint16())
case "tags":
if in.IsNull() {
in.Skip()
Expand Down Expand Up @@ -72,8 +72,7 @@ func easyjsonF642ad3eDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Event)
v1 = Tag{}
}
for !in.IsDelim(']') {
var v2 string
v2 = string(in.String())
v2 := string(in.String())
v1 = append(v1, v2)
in.WantComma()
}
Expand Down Expand Up @@ -106,7 +105,7 @@ func easyjsonF642ad3eEncodeGithubComNbdWtfGoNostr(out *jwriter.Writer, in Event)
{
const prefix string = "\"kind\":"
out.RawString(prefix)
out.Int(in.Kind)
out.Int(int(in.Kind))
}
{
if in.ID != "" {
Expand Down
2 changes: 1 addition & 1 deletion example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
// this filters for messages tagged with the user, mainly replies.
t["p"] = []string{v.(string)}
filters = []nostr.Filter{{
Kinds: []int{nostr.KindTextNote},
Kinds: []nostr.Kind{nostr.KindTextNote},
Tags: t,
// limit = 3, get the three most recent notes
Limit: 3,
Expand Down
2 changes: 1 addition & 1 deletion filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Filters []Filter

type Filter struct {
IDs []string
Kinds []int
Kinds []Kind
Authors []string
Tags TagMap
Since *Timestamp
Expand Down
16 changes: 6 additions & 10 deletions filter_easyjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ func easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Filter)
out.IDs = (out.IDs)[:0]
}
for !in.IsDelim(']') {
var v1 string
v1 = string(in.String())
v1 := string(in.String())
out.IDs = append(out.IDs, v1)
in.WantComma()
}
Expand All @@ -67,16 +66,15 @@ func easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Filter)
in.Delim('[')
if out.Kinds == nil {
if !in.IsDelim(']') {
out.Kinds = make([]int, 0, 8)
out.Kinds = make([]Kind, 0, 8)
} else {
out.Kinds = []int{}
out.Kinds = []Kind{}
}
} else {
out.Kinds = (out.Kinds)[:0]
}
for !in.IsDelim(']') {
var v2 int
v2 = int(in.Int())
v2 := Kind(in.Int())
out.Kinds = append(out.Kinds, v2)
in.WantComma()
}
Expand All @@ -98,8 +96,7 @@ func easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Filter)
out.Authors = (out.Authors)[:0]
}
for !in.IsDelim(']') {
var v3 string
v3 = string(in.String())
v3 := string(in.String())
out.Authors = append(out.Authors, v3)
in.WantComma()
}
Expand Down Expand Up @@ -147,8 +144,7 @@ func easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Filter)
tagValues = (tagValues)[:0]
}
for !in.IsDelim(']') {
var v3 string
v3 = string(in.String())
v3 := string(in.String())
tagValues = append(tagValues, v3)
in.WantComma()
}
Expand Down
22 changes: 11 additions & 11 deletions filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestFilterUnmarshal(t *testing.T) {
func TestFilterMarshal(t *testing.T) {
until := Timestamp(12345678)
filterj, err := json.Marshal(Filter{
Kinds: []int{KindTextNote, KindRecommendServer, KindEncryptedDirectMessage},
Kinds: []Kind{KindTextNote, KindRecommendServer, KindEncryptedDirectMessage},
Tags: TagMap{"fruit": {"banana", "mango"}},
Until: &until,
})
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestFilterUnmarshalWithLimitZero(t *testing.T) {
func TestFilterMarshalWithLimitZero(t *testing.T) {
until := Timestamp(12345678)
filterj, err := json.Marshal(Filter{
Kinds: []int{KindTextNote, KindRecommendServer, KindEncryptedDirectMessage},
Kinds: []Kind{KindTextNote, KindRecommendServer, KindEncryptedDirectMessage},
Tags: TagMap{"fruit": {"banana", "mango"}},
Until: &until,
LimitZero: true,
Expand All @@ -83,41 +83,41 @@ func TestFilterMatchingLive(t *testing.T) {

func TestFilterEquality(t *testing.T) {
assert.True(t, FilterEqual(
Filter{Kinds: []int{KindEncryptedDirectMessage, KindDeletion}},
Filter{Kinds: []int{KindEncryptedDirectMessage, KindDeletion}},
Filter{Kinds: []Kind{KindEncryptedDirectMessage, KindDeletion}},
Filter{Kinds: []Kind{KindEncryptedDirectMessage, KindDeletion}},
), "kinds filters should be equal")

assert.True(t, FilterEqual(
Filter{Kinds: []int{KindEncryptedDirectMessage, KindDeletion}, Tags: TagMap{"letter": {"a", "b"}}},
Filter{Kinds: []int{KindEncryptedDirectMessage, KindDeletion}, Tags: TagMap{"letter": {"b", "a"}}},
Filter{Kinds: []Kind{KindEncryptedDirectMessage, KindDeletion}, Tags: TagMap{"letter": {"a", "b"}}},
Filter{Kinds: []Kind{KindEncryptedDirectMessage, KindDeletion}, Tags: TagMap{"letter": {"b", "a"}}},
), "kind+tags filters should be equal")

tm := Now()
assert.True(t, FilterEqual(
Filter{
Kinds: []int{KindEncryptedDirectMessage, KindDeletion},
Kinds: []Kind{KindEncryptedDirectMessage, KindDeletion},
Tags: TagMap{"letter": {"a", "b"}, "fruit": {"banana"}},
Since: &tm,
IDs: []string{"aaaa", "bbbb"},
},
Filter{
Kinds: []int{KindDeletion, KindEncryptedDirectMessage},
Kinds: []Kind{KindDeletion, KindEncryptedDirectMessage},
Tags: TagMap{"letter": {"a", "b"}, "fruit": {"banana"}},
Since: &tm,
IDs: []string{"aaaa", "bbbb"},
},
), "kind+2tags+since+ids filters should be equal")

assert.False(t, FilterEqual(
Filter{Kinds: []int{KindTextNote, KindEncryptedDirectMessage, KindDeletion}},
Filter{Kinds: []int{KindEncryptedDirectMessage, KindDeletion, KindRepost}},
Filter{Kinds: []Kind{KindTextNote, KindEncryptedDirectMessage, KindDeletion}},
Filter{Kinds: []Kind{KindEncryptedDirectMessage, KindDeletion, KindRepost}},
), "kinds filters shouldn't be equal")
}

func TestFilterClone(t *testing.T) {
ts := Now() - 60*60
flt := Filter{
Kinds: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
Kinds: []Kind{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
Tags: TagMap{"letter": {"a", "b"}, "fruit": {"banana"}},
Since: &ts,
IDs: []string{"9894b4b5cb5166d23ee8899a4151cf0c66aec00bde101982a13b8e8ceb972df9"},
Expand Down
101 changes: 101 additions & 0 deletions kind.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package nostr

type (
Kind uint16
Range uint8
)

const (
// Ranges.
Regular Range = iota
Replaceable
Ephemeral
ParameterizedReplaceable

// Kinds.
KindProfileMetadata Kind = 0
KindTextNote Kind = 1
KindRecommendServer Kind = 2
KindContactList Kind = 3
KindEncryptedDirectMessage Kind = 4
KindDeletion Kind = 5
KindRepost Kind = 6
KindReaction Kind = 7
KindSimpleGroupChatMessage Kind = 9
KindSimpleGroupThread Kind = 11
KindSimpleGroupReply Kind = 12
KindChannelCreation Kind = 40
KindChannelMetadata Kind = 41
KindChannelMessage Kind = 42
KindChannelHideMessage Kind = 43
KindChannelMuteUser Kind = 44
KindPatch Kind = 1617
KindFileMetadata Kind = 1063
KindSimpleGroupAddUser Kind = 9000
KindSimpleGroupRemoveUser Kind = 9001
KindSimpleGroupEditMetadata Kind = 9002
KindSimpleGroupAddPermission Kind = 9003
KindSimpleGroupRemovePermission Kind = 9004
KindSimpleGroupDeleteEvent Kind = 9005
KindSimpleGroupEditGroupStatus Kind = 9006
KindSimpleGroupCreateGroup Kind = 9007
KindSimpleGroupDeleteGroup Kind = 9008
KindSimpleGroupJoinRequest Kind = 9021
KindSimpleGroupLeaveRequest Kind = 9022
KindZapRequest Kind = 9734
KindZap Kind = 9735
KindMuteList Kind = 10000
KindPinList Kind = 10001
KindRelayListMetadata Kind = 10002
KindNWCWalletInfo Kind = 13194
KindClientAuthentication Kind = 22242
KindNWCWalletRequest Kind = 23194
KindNWCWalletResponse Kind = 23195
KindNostrConnect Kind = 24133
KindCategorizedPeopleList Kind = 30000
KindCategorizedBookmarksList Kind = 30001
KindProfileBadges Kind = 30008
KindBadgeDefinition Kind = 30009
KindStallDefinition Kind = 30017
KindProductDefinition Kind = 30018
KindArticle Kind = 30023
KindApplicationSpecificData Kind = 30078
KindRepositoryAnnouncement Kind = 30617
KindRepositoryState Kind = 30618
KindSimpleGroupMetadata Kind = 39000
KindSimpleGroupAdmins Kind = 39001
KindSimpleGroupMembers Kind = 39002
)

// IsRegular checks if the given kind is in Regular range.
func (k Kind) IsRegular() bool {
return 1000 <= k || k < 10000 || 4 <= k || k < 45 || k == 1 || k == 2
}

// IsReplaceable checks if the given kind is in Replaceable range.
func (k Kind) IsReplaceable() bool {
return 10000 <= k || k < 20000 || k == 0 || k == 3
}

// IsEphemeral checks if the given kind is in Ephemeral range.
func (k Kind) IsEphemeral() bool {
return 20000 <= k || k < 30000
}

// IsParameterizedReplaceable checks if the given kind is in ParameterizedReplaceable range.
func (k Kind) IsParameterizedReplaceable() bool {
return 30000 <= k || k < 40000
}

// Range returns the kind range based on NIP-01.
func (k Kind) Range() Range {
if k.IsRegular() {
return Regular
} else if k.IsReplaceable() {
return Replaceable
} else if k.IsParameterizedReplaceable() {
return ParameterizedReplaceable
}

return Ephemeral
}
56 changes: 0 additions & 56 deletions kinds.go

This file was deleted.

12 changes: 8 additions & 4 deletions nip11/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package nip11

import "slices"
import (
"slices"

"github.com/nbd-wtf/go-nostr"
)

type RelayInformationDocument struct {
URL string `json:"-"`
Expand Down Expand Up @@ -59,8 +63,8 @@ type RelayFeesDocument struct {
Period int `json:"period"`
} `json:"subscription,omitempty"`
Publication []struct {
Kinds []int `json:"kinds"`
Amount int `json:"amount"`
Unit string `json:"unit"`
Kinds []nostr.Kind `json:"kinds"`
Amount int `json:"amount"`
Unit string `json:"unit"`
} `json:"publication,omitempty"`
}
Loading