Skip to content

Commit

Permalink
Download tagged events not events from a public key
Browse files Browse the repository at this point in the history
I confused myself.
  • Loading branch information
boreq committed Jun 21, 2023
1 parent 5f42409 commit 678069a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 47 deletions.
4 changes: 0 additions & 4 deletions service/adapters/firestore/repository_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
40 changes: 0 additions & 40 deletions service/adapters/firestore/repository_relays.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package firestore
import (
"context"
"encoding/hex"
"time"

"cloud.google.com/go/firestore"
"github.com/boreq/errors"
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions service/app/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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,
}},
}

Expand Down

0 comments on commit 678069a

Please sign in to comment.