diff --git a/service/app/downloader.go b/service/app/downloader.go index daaf15e..15838ae 100644 --- a/service/app/downloader.go +++ b/service/app/downloader.go @@ -20,13 +20,11 @@ const ( reconnectEvery = 1 * time.Minute manageSubscriptionsEvery = 1 * time.Minute - howFarIntoThePastToLook = 7 * 24 * time.Hour + howFarIntoThePastToLook = 24 * time.Hour 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)