Skip to content

Commit

Permalink
Validate if event should have been downloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
boreq committed Nov 16, 2023
1 parent 5647546 commit d0bb4b8
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 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/nos-event-service/internal/logging"
Expand Down Expand Up @@ -46,9 +47,11 @@ func (h *SaveReceivedEventHandler) Handle(ctx context.Context, cmd SaveReceivedE
WithField("event.kind", cmd.event.Kind().Int()).
Message("saving received event")

if err := h.transactionProvider.Transact(ctx, func(ctx context.Context, adapters Adapters) error {
// todo check if event should be saved
if !h.shouldBeGloballyDownloaded(cmd.event.Kind()) {
return fmt.Errorf("event shouldn't have been downloaded, relay '%s' may be misbehaving", cmd.relay.String())
}

if err := h.transactionProvider.Transact(ctx, func(ctx context.Context, adapters Adapters) error {
if err := adapters.Events.Save(ctx, cmd.event); err != nil {
return errors.Wrap(err, "error saving the event")
}
Expand All @@ -64,3 +67,12 @@ func (h *SaveReceivedEventHandler) Handle(ctx context.Context, cmd SaveReceivedE

return nil
}

func (h *SaveReceivedEventHandler) shouldBeGloballyDownloaded(kind domain.EventKind) bool {
for _, v := range globalEventTypesToDownload {
if v == kind {
return true
}
}
return false
}

0 comments on commit d0bb4b8

Please sign in to comment.