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

Extra event kind checks and reduce max event age #18

Merged
merged 2 commits into from
Jul 6, 2023
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
39 changes: 24 additions & 15 deletions service/app/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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

Expand Down
6 changes: 6 additions & 0 deletions service/app/handler_save_received_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"context"
"fmt"

"github.com/boreq/errors"
"github.com/planetary-social/go-notification-service/internal/logging"
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 14 additions & 1 deletion service/domain/event_kind.go
Original file line number Diff line number Diff line change
@@ -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)
Expand Down