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

add WithSkipEventFunc #117

Merged
merged 1 commit into from
Jul 14, 2024
Merged
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
add WithSkipEventFunc
  • Loading branch information
mattn committed Jul 13, 2024
commit 1d1d5408d44ec50345d4e07551b8a6fdbb3b7627
3 changes: 3 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ func (s *Server) doReq(ctx context.Context, ws *WebSocket, request []json.RawMes
}
i := 0
for event := range events {
if s.options.skipEventFunc != nil && s.options.skipEventFunc(event) {
continue
}
ws.WriteJSON(nostr.EventEnvelope{SubscriptionID: &id, Event: *event})
i++
if i > filter.Limit {
Expand Down
8 changes: 8 additions & 0 deletions start.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/fasthttp/websocket"
"github.com/nbd-wtf/go-nostr"
"github.com/rs/cors"
"golang.org/x/time/rate"
)
Expand Down Expand Up @@ -158,6 +159,7 @@ type Option func(*Options)

type Options struct {
perConnectionLimiter *rate.Limiter
skipEventFunc func(*nostr.Event) bool
}

func DefaultOptions() *Options {
Expand All @@ -170,6 +172,12 @@ func WithPerConnectionLimiter(rps rate.Limit, burst int) Option {
}
}

func WithSkipEventFunc(skipEventFunc func(*nostr.Event) bool) Option {
return func(o *Options) {
o.skipEventFunc = skipEventFunc
}
}

func defaultLogger(prefix string) Logger {
l := log.New(os.Stderr, "", log.LstdFlags|log.Lmsgprefix)
l.SetPrefix(prefix)
Expand Down