From 678069aa4e1bc072a6fd1ae5b93f0fc6356dc5d0 Mon Sep 17 00:00:00 2001 From: boreq Date: Wed, 21 Jun 2023 17:22:37 +0200 Subject: [PATCH] Download tagged events not events from a public key I confused myself. --- .../adapters/firestore/repository_event.go | 4 -- .../adapters/firestore/repository_relays.go | 40 ------------------- service/app/downloader.go | 10 +++-- 3 files changed, 7 insertions(+), 47 deletions(-) diff --git a/service/adapters/firestore/repository_event.go b/service/adapters/firestore/repository_event.go index 7f3633d..2d8251b 100644 --- a/service/adapters/firestore/repository_event.go +++ b/service/adapters/firestore/repository_event.go @@ -29,10 +29,6 @@ func NewEventRepository( } func (e *EventRepository) Save(relay domain.RelayAddress, event domain.Event) error { - if err := e.relayRepository.UpdateLastEventTime(relay, event); err != nil { - return errors.Wrap(err, "error saving under events") - } - if err := e.saveUnderEvents(event); err != nil { return errors.Wrap(err, "error saving under events") } diff --git a/service/adapters/firestore/repository_relays.go b/service/adapters/firestore/repository_relays.go index 9e6c5fc..702a348 100644 --- a/service/adapters/firestore/repository_relays.go +++ b/service/adapters/firestore/repository_relays.go @@ -3,7 +3,6 @@ package firestore import ( "context" "encoding/hex" - "time" "cloud.google.com/go/firestore" "github.com/boreq/errors" @@ -48,45 +47,6 @@ func (r *RelayRepository) Save(registration domain.Registration) error { return nil } -const fieldLastEventTimestamp = "lastEventTimestamp" - -func (r *RelayRepository) UpdateLastEventTime(relay domain.RelayAddress, event domain.Event) error { - doc, err := r.tx.Get( - r.client. - Collection(collectionRelays). - Doc(relayAddressAsKey(relay)). - Collection(collectionRelaysPublicKeys). - Doc(event.PubKey().Hex()), - ) - if err != nil { - return errors.Wrap(err, "error getting the document") - } - - data := make(map[string]any) - - if err := doc.DataTo(&data); err != nil { - return errors.Wrap(err, "error loading document data") - } - - lastEventTimestamp, ok := data[fieldLastEventTimestamp].(time.Time) - if !ok { - lastEventTimestamp = time.Time{} - } - - if lastEventTimestamp.Before(event.CreatedAt()) { - if err := r.tx.Update(doc.Ref, []firestore.Update{ - { - Path: fieldLastEventTimestamp, - Value: event.CreatedAt(), - }, - }, firestore.Exists); err != nil { - return errors.Wrap(err, "error updating the last event timestamp") - } - } - - return nil -} - func (r *RelayRepository) GetRelays(ctx context.Context) ([]domain.RelayAddress, error) { // todo do it in transaction? emulator doesn't support it iter := r.client.Collection(collectionRelays).Documents(ctx) diff --git a/service/app/downloader.go b/service/app/downloader.go index 7d5c611..effa906 100644 --- a/service/app/downloader.go +++ b/service/app/downloader.go @@ -204,6 +204,8 @@ func (d *RelayDownloader) manageSubs( } } +const howFarIntoThePastToLook = 7 * 24 * time.Hour + func (d *RelayDownloader) updateSubs( conn *websocket.Conn, activeSubscriptions *internal.Set[domain.PublicKey], @@ -232,13 +234,15 @@ func (d *RelayDownloader) updateSubs( for _, publicKey := range publicKeys.List() { if ok := activeSubscriptions.Contains(publicKey); !ok { + t := nostr.Timestamp(time.Now().Add(-howFarIntoThePastToLook).Unix()) + envelope := nostr.ReqEnvelope{ SubscriptionID: publicKey.Hex(), Filters: nostr.Filters{nostr.Filter{ - Authors: []string{ - publicKey.Hex(), + Tags: map[string][]string{ + "p": {publicKey.Hex()}, }, - Since: nil, // todo filter based on already received events + Since: &t, }}, }