Skip to content

Commit

Permalink
fixed names and shorter saner context alias
Browse files Browse the repository at this point in the history
  • Loading branch information
mleku committed Jan 5, 2024
1 parent 0e6077c commit b865923
Show file tree
Hide file tree
Showing 19 changed files with 49 additions and 40 deletions.
9 changes: 9 additions & 0 deletions pkg/context/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package context

import "context"

type (
T = context.Context
F = context.CancelFunc
C = context.CancelCauseFunc
)
2 changes: 1 addition & 1 deletion pkg/go-nostr/binary/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func UnmarshalBinary(data []byte, evt *Event) (e error) {
copy(evt.ID[:], data[0:32])
copy(evt.PubKey[:], data[32:64])
copy(evt.Sig[:], data[64:128])
evt.CreatedAt = timestamp.Timestamp(binary.BigEndian.Uint32(data[128:132]))
evt.CreatedAt = timestamp.T(binary.BigEndian.Uint32(data[128:132]))
evt.Kind = binary.BigEndian.Uint16(data[132:134])
contentLength := int(binary.BigEndian.Uint16(data[134:136]))
evt.Content = string(data[136 : 136+contentLength])
Expand Down
2 changes: 1 addition & 1 deletion pkg/go-nostr/binary/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestBinaryPartialGet(t *testing.T) {
if sig := hex.EncodeToString(bevt[64:128]); sig != evt.Sig {
t.Fatalf("partial sig wrong. got %v, expected %v", sig, evt.Sig)
}
if createdAt := timestamp.Timestamp(binary.BigEndian.Uint32(bevt[128:132])); createdAt != evt.CreatedAt {
if createdAt := timestamp.T(binary.BigEndian.Uint32(bevt[128:132])); createdAt != evt.CreatedAt {
t.Fatalf("partial created_at wrong. got %v, expected %v", createdAt, evt.CreatedAt)
}
if kind := int(binary.BigEndian.Uint16(bevt[132:134])); kind != evt.Kind {
Expand Down
2 changes: 1 addition & 1 deletion pkg/go-nostr/binary/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Event struct {
Sig [64]byte
ID [32]byte
Kind uint16
CreatedAt timestamp.Timestamp
CreatedAt timestamp.T
Content string
Tags tags.Tags
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/go-nostr/binary/hybrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Unmarshal(data []byte, evt *event.T) (e error) {
evt.ID = hex.EncodeToString(data[0:32])
evt.PubKey = hex.EncodeToString(data[32:64])
evt.Sig = hex.EncodeToString(data[64:128])
evt.CreatedAt = timestamp.Timestamp(binary.BigEndian.Uint32(data[128:132]))
evt.CreatedAt = timestamp.T(binary.BigEndian.Uint32(data[128:132]))
evt.Kind = int(binary.BigEndian.Uint16(data[132:134]))
contentLength := int(binary.BigEndian.Uint16(data[134:136]))
evt.Content = string(data[136 : 136+contentLength])
Expand Down
14 changes: 7 additions & 7 deletions pkg/go-nostr/close/close.go → pkg/go-nostr/closer/close.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package close
package closer

import (
"encoding/json"
Expand All @@ -13,28 +13,28 @@ var _ envelopes.Envelope = (*Envelope)(nil)

type Envelope string

func (_ Envelope) Label() string { return "CLOSE" }
func (c Envelope) String() string {
func (_ *Envelope) Label() string { return "CLOSE" }
func (c *Envelope) String() string {
v, _ := json.Marshal(c)
return string(v)
}

func (v *Envelope) UnmarshalJSON(data []byte) error {
func (c *Envelope) UnmarshalJSON(data []byte) error {
r := gjson.ParseBytes(data)
arr := r.Array()
switch len(arr) {
case 2:
*v = Envelope(arr[1].Str)
*c = Envelope(arr[1].Str)
return nil
default:
return fmt.Errorf("failed to decode CLOSE envelope")
}
}

func (v Envelope) MarshalJSON() ([]byte, error) {
func (c *Envelope) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
w.RawString(`["CLOSE",`)
w.Raw(json.Marshal(string(v)))
w.Raw(json.Marshal(string(*c)))
w.RawString(`]`)
return w.BuildBytes()
}
2 changes: 1 addition & 1 deletion pkg/go-nostr/envelope/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (

"github.com/Hubmakerlabs/replicatr/pkg/go-nostr/OK"
"github.com/Hubmakerlabs/replicatr/pkg/go-nostr/auth"
close2 "github.com/Hubmakerlabs/replicatr/pkg/go-nostr/close"
"github.com/Hubmakerlabs/replicatr/pkg/go-nostr/closed"
close2 "github.com/Hubmakerlabs/replicatr/pkg/go-nostr/closer"
"github.com/Hubmakerlabs/replicatr/pkg/go-nostr/count"
"github.com/Hubmakerlabs/replicatr/pkg/go-nostr/envelopes"
"github.com/Hubmakerlabs/replicatr/pkg/go-nostr/eose"
Expand Down
6 changes: 3 additions & 3 deletions pkg/go-nostr/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (

type T struct {
ID string `json:"id"`
PubKey string `json:"pubkey"`
CreatedAt timestamp.Timestamp `json:"created_at"`
Kind int `json:"kind"`
PubKey string `json:"pubkey"`
CreatedAt timestamp.T `json:"created_at"`
Kind int `json:"kind"`
Tags tags.Tags `json:"tags"`
Content string `json:"content"`
Sig string `json:"sig"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/go-nostr/event/event_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/go-nostr/event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestEventSerialization(t *testing.T) {
ID: "92570b321da503eac8014b23447301eb3d0bbdfbace0d11a4e4072e72bb7205d",
PubKey: "e9142f724955c5854de36324dab0434f97b15ec6b33464d56ebe491e3f559d1b",
Kind: KindEncryptedDirectMessage,
CreatedAt: timestamp.Timestamp(1671028682),
CreatedAt: timestamp.T(1671028682),
Tags: tags.Tags{{"p", "f8340b2bde651576b75af61aa26c80e13c65029f00f7f64004eece679bf7059f"}},
Content: "you say yes, I say no",
Sig: "ed08d2dd5b0f7b6a3cdc74643d4adee3158ddede9cc848e8cd97630c097001acc2d052d2d3ec2b7ac4708b2314b797106d1b3c107322e61b5e5cc2116e099b79",
Expand Down Expand Up @@ -126,7 +126,7 @@ func TestEventSerializationWithExtraFields(t *testing.T) {
ID: "92570b321da503eac8014b23447301eb3d0bbdfbace0d11a4e4072e72bb7205d",
PubKey: "e9142f724955c5854de36324dab0434f97b15ec6b33464d56ebe491e3f559d1b",
Kind: KindReaction,
CreatedAt: timestamp.Timestamp(1671028682),
CreatedAt: timestamp.T(1671028682),
Content: "there is an extra field here",
Sig: "ed08d2dd5b0f7b6a3cdc74643d4adee3158ddede9cc848e8cd97630c097001acc2d052d2d3ec2b7ac4708b2314b797106d1b3c107322e61b5e5cc2116e099b79",
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/go-nostr/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ type T struct {
IDs []string `json:"ids,omitempty"`
Kinds []int `json:"kinds,omitempty"`
Authors []string `json:"authors,omitempty"`
Tags TagMap `json:"-,omitempty"`
Since *timestamp.Timestamp `json:"since,omitempty"`
Until *timestamp.Timestamp `json:"until,omitempty"`
Limit int `json:"limit,omitempty"`
Tags TagMap `json:"-,omitempty"`
Since *timestamp.T `json:"since,omitempty"`
Until *timestamp.T `json:"until,omitempty"`
Limit int `json:"limit,omitempty"`
Search string `json:"search,omitempty"`
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/go-nostr/filter/filter_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/go-nostr/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestFilterUnmarshal(t *testing.T) {
}

func TestFilterMarshal(t *testing.T) {
until := timestamp.Timestamp(12345678)
until := timestamp.T(12345678)
filterj, e := json.Marshal(T{
Kinds: []int{event.KindTextNote, event.KindRecommendServer, event.KindEncryptedDirectMessage},
Tags: TagMap{"fruit": {"banana", "mango"}},
Expand Down
2 changes: 1 addition & 1 deletion pkg/go-nostr/nip29/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Group struct {
Private bool
Closed bool

LastMetadataUpdate timestamp.Timestamp
LastMetadataUpdate timestamp.T
}

func (group Group) ToMetadataEvent() *event.T {
Expand Down
6 changes: 3 additions & 3 deletions pkg/go-nostr/nson/nson.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Unmarshal(data string, evt *event.T) (e error) {
evt.PubKey = data[PUBKEY_START:PUBKEY_END]
evt.Sig = data[SIG_START:SIG_END]
ts, _ := strconv.ParseUint(data[CREATED_AT_START:CREATED_AT_END], 10, 64)
evt.CreatedAt = timestamp.Timestamp(ts)
evt.CreatedAt = timestamp.T(ts)

// dynamic fields
// kind
Expand Down Expand Up @@ -214,9 +214,9 @@ func (ne *Event) parseDescriptors() {
func (ne Event) GetID() string { return ne.data[ID_START:ID_END] }
func (ne Event) GetPubkey() string { return ne.data[PUBKEY_START:PUBKEY_END] }
func (ne Event) GetSig() string { return ne.data[SIG_START:SIG_END] }
func (ne Event) GetCreatedAt() timestamp.Timestamp {
func (ne Event) GetCreatedAt() timestamp.T {
ts, _ := strconv.ParseUint(ne.data[CREATED_AT_START:CREATED_AT_END], 10, 64)
return timestamp.Timestamp(ts)
return timestamp.T(ts)
}

func (ne *Event) GetKind() int {
Expand Down
6 changes: 3 additions & 3 deletions pkg/go-nostr/pools/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (pool *SimplePool) subMany(ctx context.Context, urls []string, filters filt
ctx, cancel := context.WithCancel(ctx)
_ = cancel // do this so `go vet` will stop complaining
events := make(chan IncomingEvent)
seenAlready := xsync.NewMapOf[timestamp.Timestamp]()
seenAlready := xsync.NewMapOf[timestamp.T]()
ticker := time.NewTicker(seenAlreadyDropTick)

eose := false
Expand Down Expand Up @@ -178,8 +178,8 @@ func (pool *SimplePool) subMany(ctx context.Context, urls []string, filters filt
}
case <-ticker.C:
if eose {
old := timestamp.Timestamp(time.Now().Add(-seenAlreadyDropTick).Unix())
seenAlready.Range(func(id string, value timestamp.Timestamp) bool {
old := timestamp.T(time.Now().Add(-seenAlreadyDropTick).Unix())
seenAlready.Range(func(id string, value timestamp.T) bool {
if value < old {
seenAlready.Delete(id)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/go-nostr/relays/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestPublish(t *testing.T) {
textNote := event.T{
Kind: event.KindTextNote,
Content: "hello",
CreatedAt: timestamp.Timestamp(1672068534), // random fixed timestamp
CreatedAt: timestamp.T(1672068534), // random fixed timestamp
Tags: tags.Tags{[]string{"foo", "bar"}},
PubKey: pub,
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/go-nostr/relays/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync"
"sync/atomic"

close2 "github.com/Hubmakerlabs/replicatr/pkg/go-nostr/close"
close2 "github.com/Hubmakerlabs/replicatr/pkg/go-nostr/closer"
"github.com/Hubmakerlabs/replicatr/pkg/go-nostr/count"
"github.com/Hubmakerlabs/replicatr/pkg/go-nostr/event"
"github.com/Hubmakerlabs/replicatr/pkg/go-nostr/filters"
Expand Down
8 changes: 4 additions & 4 deletions pkg/go-nostr/timestamp/timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package timestamp

import "time"

type Timestamp int64
type T int64

func Now() Timestamp {
return Timestamp(time.Now().Unix())
func Now() T {
return T(time.Now().Unix())
}

func (t Timestamp) Time() time.Time {
func (t T) Time() time.Time {
return time.Unix(int64(t), 0)
}

0 comments on commit b865923

Please sign in to comment.