From ed837221bba320b1ff6656b4e58e3387ff8cda6d Mon Sep 17 00:00:00 2001 From: boreq Date: Thu, 6 Jul 2023 20:18:38 +0200 Subject: [PATCH 1/2] Add event kind checks to the handler which saves events --- service/app/downloader.go | 37 ++++++++++++++-------- service/app/handler_save_received_event.go | 6 ++++ service/domain/event_kind.go | 15 ++++++++- 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/service/app/downloader.go b/service/app/downloader.go index daaf15e..5af2a74 100644 --- a/service/app/downloader.go +++ b/service/app/downloader.go @@ -25,8 +25,6 @@ const ( storeMetricsEvery = 10 * time.Second ) -var eventKindsToDownload = []int{domain.EventKindNote.Int()} - type ReceivedEventPublisher interface { Publish(relay domain.RelayAddress, event domain.Event) } @@ -362,18 +360,7 @@ func (d *RelayDownloader) updateSubs( WithField("publicKey", publicKey). Message("opening subscription") - t := nostr.Timestamp(time.Now().Add(-howFarIntoThePastToLook).Unix()) - - envelope := nostr.ReqEnvelope{ - SubscriptionID: publicKey.Hex(), - Filters: nostr.Filters{nostr.Filter{ - Kinds: eventKindsToDownload, - Tags: map[string][]string{ - "p": {publicKey.Hex()}, - }, - Since: &t, - }}, - } + envelope := d.createRequest(publicKey) envelopeJSON, err := envelope.MarshalJSON() if err != nil { @@ -391,6 +378,28 @@ func (d *RelayDownloader) updateSubs( return nil } +func (d *RelayDownloader) createRequest(publicKey domain.PublicKey) nostr.ReqEnvelope { + t := nostr.Timestamp(time.Now().Add(-howFarIntoThePastToLook).Unix()) + + var eventKindsToDownload []int + for _, eventKind := range domain.EventKindsToDownload() { + eventKindsToDownload = append(eventKindsToDownload, eventKind.Int()) + } + + envelope := nostr.ReqEnvelope{ + SubscriptionID: publicKey.Hex(), + Filters: nostr.Filters{nostr.Filter{ + Kinds: eventKindsToDownload, + Tags: map[string][]string{ + "p": {publicKey.Hex()}, + }, + Since: &t, + }}, + } + + return envelope +} + func (d *RelayDownloader) getPublicKeys(ctx context.Context) (*internal.Set[domain.PublicKey], error) { var publicKeys []domain.PublicKey diff --git a/service/app/handler_save_received_event.go b/service/app/handler_save_received_event.go index 3ee9106..99317a2 100644 --- a/service/app/handler_save_received_event.go +++ b/service/app/handler_save_received_event.go @@ -2,6 +2,7 @@ package app import ( "context" + "fmt" "github.com/boreq/errors" "github.com/planetary-social/go-notification-service/internal/logging" @@ -38,11 +39,16 @@ func NewSaveReceivedEventHandler( func (h *SaveReceivedEventHandler) Handle(ctx context.Context, cmd SaveReceivedEvent) error { defer h.metrics.TrackApplicationCall("saveReceivedEvent").End() + if !domain.ShouldDownloadEventKind(cmd.event.Kind()) { + return fmt.Errorf("event '%s' shouldn't have been downloaded", cmd.event.String()) + } + h.logger.Debug(). WithField("relay", cmd.relay.String()). WithField("event.id", cmd.event.Id().Hex()). WithField("event.kind", cmd.event.Kind().Int()). WithField("size", len(cmd.event.Raw())). + WithField("number_of_tags", len(cmd.event.Tags())). Message("saving received event") if err := h.transactionProvider.Transact(ctx, func(ctx context.Context, adapters Adapters) error { diff --git a/service/domain/event_kind.go b/service/domain/event_kind.go index 520d23f..f98be67 100644 --- a/service/domain/event_kind.go +++ b/service/domain/event_kind.go @@ -1,6 +1,19 @@ package domain -import "github.com/boreq/errors" +import ( + "github.com/boreq/errors" + "github.com/planetary-social/go-notification-service/internal" +) + +var eventKindsToDownload = internal.NewSet([]EventKind{EventKindNote}) + +func EventKindsToDownload() []EventKind { + return eventKindsToDownload.List() +} + +func ShouldDownloadEventKind(eventKind EventKind) bool { + return eventKindsToDownload.Contains(eventKind) +} var ( EventKindNote = MustNewEventKind(1) From 13a47e42aebfb5390afc379b61025e2199bd8660 Mon Sep 17 00:00:00 2001 From: boreq Date: Thu, 6 Jul 2023 20:41:37 +0200 Subject: [PATCH 2/2] Only request events from the last 24h --- service/app/downloader.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/app/downloader.go b/service/app/downloader.go index 5af2a74..15838ae 100644 --- a/service/app/downloader.go +++ b/service/app/downloader.go @@ -20,7 +20,7 @@ const ( reconnectEvery = 1 * time.Minute manageSubscriptionsEvery = 1 * time.Minute - howFarIntoThePastToLook = 7 * 24 * time.Hour + howFarIntoThePastToLook = 24 * time.Hour storeMetricsEvery = 10 * time.Second )