Skip to content

Commit

Permalink
Publish selected event kinds to relay.nos.social
Browse files Browse the repository at this point in the history
The idea is to make it work like https://purplepag.es/what.
  • Loading branch information
boreq committed Nov 23, 2023
1 parent 6b51f07 commit a71b912
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 35 deletions.
4 changes: 2 additions & 2 deletions cmd/event-service/di/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions service/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/boreq/errors"
"github.com/planetary-social/nos-event-service/service/domain"
"github.com/planetary-social/nos-event-service/service/domain/relays"
)

var (
Expand Down Expand Up @@ -121,3 +122,8 @@ type ContactsExtractor interface {
type Subscriber interface {
EventSavedQueueLength(ctx context.Context) (int, error)
}

type RelayConnections interface {
GetEvents(ctx context.Context, relayAddress domain.RelayAddress, filter domain.Filter) (<-chan relays.EventOrEndOfSavedEvents, error)
SendEvent(ctx context.Context, relayAddress domain.RelayAddress, event domain.Event) error
}
5 changes: 0 additions & 5 deletions service/app/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/planetary-social/nos-event-service/internal"
"github.com/planetary-social/nos-event-service/internal/logging"
"github.com/planetary-social/nos-event-service/service/domain"
"github.com/planetary-social/nos-event-service/service/domain/relays"
)

const (
Expand Down Expand Up @@ -42,10 +41,6 @@ type RelaySource interface {
GetRelays(ctx context.Context) ([]domain.RelayAddress, error)
}

type RelayConnections interface {
GetEvents(ctx context.Context, relayAddress domain.RelayAddress, filter domain.Filter) (<-chan relays.EventOrEndOfSavedEvents, error)
}

type PublicKeySource interface {
GetPublicKeys(ctx context.Context) ([]domain.PublicKey, error)
}
Expand Down
24 changes: 24 additions & 0 deletions service/app/handler_process_saved_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@ import (
"context"

"github.com/boreq/errors"
"github.com/planetary-social/nos-event-service/internal"
"github.com/planetary-social/nos-event-service/internal/logging"
"github.com/planetary-social/nos-event-service/service/domain"
)

var (
nosRelayAddress = domain.MustNewRelayAddress("wss://relay.nos.social")
eventKindsWhichShouldBeSentToRelay = internal.NewSetVariadic(
domain.EventKindMetadata,
domain.EventKindContacts,
domain.EventKindRelayListMetadata,
)
)

type ProcessSavedEvent struct {
id domain.EventId
}
Expand All @@ -21,6 +31,7 @@ type ProcessSavedEventHandler struct {
relaysExtractor RelaysExtractor
contactsExtractor ContactsExtractor
externalEventPublisher ExternalEventPublisher
relayConnections RelayConnections
logger logging.Logger
metrics Metrics
}
Expand All @@ -30,6 +41,7 @@ func NewProcessSavedEventHandler(
relaysExtractor RelaysExtractor,
contactsExtractor ContactsExtractor,
externalEventPublisher ExternalEventPublisher,
relayConnections RelayConnections,
logger logging.Logger,
metrics Metrics,
) *ProcessSavedEventHandler {
Expand All @@ -38,6 +50,7 @@ func NewProcessSavedEventHandler(
relaysExtractor: relaysExtractor,
contactsExtractor: contactsExtractor,
externalEventPublisher: externalEventPublisher,
relayConnections: relayConnections,
logger: logger.New("processSavedEventHandler"),
metrics: metrics,
}
Expand All @@ -59,6 +72,10 @@ func (h *ProcessSavedEventHandler) Handle(ctx context.Context, cmd ProcessSavedE
return errors.Wrap(err, "error publishing the external event")
}

if err := h.maybeSendEventToRelay(ctx, event); err != nil {
return errors.Wrap(err, "error sending the event to relay")
}

return nil
}

Expand Down Expand Up @@ -138,3 +155,10 @@ func (h *ProcessSavedEventHandler) shouldReplaceContacts(ctx context.Context, ad

return domain.ShouldReplaceContactsEvent(oldEvent, newEvent)
}

func (h *ProcessSavedEventHandler) maybeSendEventToRelay(ctx context.Context, event domain.Event) error {
if !eventKindsWhichShouldBeSentToRelay.Contains(event.Kind()) {
return nil
}
return h.relayConnections.SendEvent(ctx, nosRelayAddress, event)
}
Loading

0 comments on commit a71b912

Please sign in to comment.