Skip to content

Commit

Permalink
Add and endpoint which can be used for listing events
Browse files Browse the repository at this point in the history
  • Loading branch information
boreq committed Nov 30, 2023
1 parent bb53523 commit 587986d
Show file tree
Hide file tree
Showing 20 changed files with 685 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ nos-notification-service-*.json
database.sqlite
database.sqlite-journal
run.sh
_testdata
1 change: 1 addition & 0 deletions cmd/event-service/di/inject_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ var applicationSet = wire.NewSet(
app.NewAddPublicKeyToMonitorHandler,
app.NewGetEventHandler,
app.NewGetPublicKeyInfoHandler,
app.NewGetEventsHandler,
)
15 changes: 15 additions & 0 deletions cmd/event-service/di/inject_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package di

import (
"github.com/google/wire"
"github.com/planetary-social/nos-event-service/internal/logging"
"github.com/planetary-social/nos-event-service/service/config"
)

var extractConfigSet = wire.NewSet(
logLevelFromConfig,
)

func logLevelFromConfig(conf config.Config) logging.Level {
return conf.LogLevel()
}
7 changes: 3 additions & 4 deletions cmd/event-service/di/inject_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/boreq/errors"
"github.com/google/wire"
"github.com/planetary-social/nos-event-service/internal/logging"
"github.com/planetary-social/nos-event-service/service/config"
"github.com/sirupsen/logrus"
)

Expand All @@ -16,13 +15,13 @@ var loggingSet = wire.NewSet(
wire.Bind(new(watermill.LoggerAdapter), new(*logging.WatermillAdapter)),
)

func newLogger(conf config.Config) (logging.Logger, error) {
if conf.LogLevel() == logging.LevelDisabled {
func newLogger(level logging.Level) (logging.Logger, error) {
if level == logging.LevelDisabled {
return logging.NewDevNullLogger(), nil
}

v := logrus.New()
switch conf.LogLevel() {
switch level {
case logging.LevelTrace:
v.SetLevel(logrus.TraceLevel)
case logging.LevelDebug:
Expand Down
44 changes: 44 additions & 0 deletions cmd/event-service/di/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/google/wire"
"github.com/planetary-social/nos-event-service/internal/fixtures"
"github.com/planetary-social/nos-event-service/internal/logging"
"github.com/planetary-social/nos-event-service/service/adapters/mocks"
"github.com/planetary-social/nos-event-service/service/adapters/sqlite"
"github.com/planetary-social/nos-event-service/service/app"
"github.com/planetary-social/nos-event-service/service/config"
Expand All @@ -33,6 +34,7 @@ func BuildService(context.Context, config.Config) (Service, func(), error) {
migrationsAdaptersSet,
domainSet,
externalPubsubSet,
extractConfigSet,
)
return Service{}, nil, nil
}
Expand All @@ -46,10 +48,52 @@ func BuildTestAdapters(context.Context, testing.TB) (sqlite.TestedItems, func(),
loggingSet,
newTestAdaptersConfig,
migrationsAdaptersSet,
extractConfigSet,
)
return sqlite.TestedItems{}, nil, nil
}

type TestApplication struct {
GetEventsHandler *app.GetEventsHandler

EventRepository *mocks.EventRepository
}

func BuildTestApplication(context.Context, testing.TB) (TestApplication, error) {
wire.Build(
wire.Struct(new(TestApplication), "*"),

wire.Struct(new(app.Adapters), "*"),

mocks.NewTransactionProvider,
wire.Bind(new(app.TransactionProvider), new(*mocks.TransactionProvider)),

mocks.NewEventRepository,
wire.Bind(new(app.EventRepository), new(*mocks.EventRepository)),

mocks.NewRelayRepository,
wire.Bind(new(app.RelayRepository), new(*mocks.RelayRepository)),

mocks.NewContactRepository,
wire.Bind(new(app.ContactRepository), new(*mocks.ContactRepository)),

mocks.NewPublicKeysToMonitorRepository,
wire.Bind(new(app.PublicKeysToMonitorRepository), new(*mocks.PublicKeysToMonitorRepository)),

mocks.NewPublisher,
wire.Bind(new(app.Publisher), new(*mocks.Publisher)),

mocks.NewMetrics,
wire.Bind(new(app.Metrics), new(*mocks.Metrics)),

applicationSet,
loggingSet,

wire.Value(logging.LevelError),
)
return TestApplication{}, nil
}

func newTestAdaptersConfig(tb testing.TB) (config.Config, error) {
return config.NewConfig(
fixtures.SomeString(),
Expand Down
47 changes: 45 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.

9 changes: 9 additions & 0 deletions internal/fixtures/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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/stretchr/testify/require"
)

func SomePublicKey() domain.PublicKey {
Expand Down Expand Up @@ -198,3 +199,11 @@ func randSeq(n int) string {
func somePrivateKeyHex() string {
return nostr.GeneratePrivateKey()
}

func RequireEqualEventSlices(tb testing.TB, a, b []domain.Event) {
require.Equal(tb, len(a), len(b))
for i := 0; i < len(a); i++ {
require.Equal(tb, a[i].Id(), b[i].Id())
require.Equal(tb, a[i].Raw(), b[i].Raw())
}
}
38 changes: 38 additions & 0 deletions service/adapters/mocks/contact_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package mocks

import (
"context"

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

type ContactRepository struct {
}

func NewContactRepository() *ContactRepository {
return &ContactRepository{}
}

func (c ContactRepository) GetCurrentContactsEvent(ctx context.Context, author domain.PublicKey) (domain.Event, error) {
panic("implement me")
}

func (c ContactRepository) SetContacts(ctx context.Context, event domain.Event, contacts []domain.PublicKey) error {
panic("implement me")
}

func (c ContactRepository) GetFollowees(ctx context.Context, publicKey domain.PublicKey) ([]domain.PublicKey, error) {
panic("implement me")
}

func (c ContactRepository) IsFolloweeOfMonitoredPublicKey(ctx context.Context, publicKey domain.PublicKey) (bool, error) {
panic("implement me")
}

func (c ContactRepository) CountFollowers(ctx context.Context, publicKey domain.PublicKey) (int, error) {
panic("implement me")
}

func (c ContactRepository) CountFollowees(ctx context.Context, publicKey domain.PublicKey) (int, error) {
panic("implement me")
}
45 changes: 45 additions & 0 deletions service/adapters/mocks/event_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package mocks

import (
"context"

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

type EventRepository struct {
ListCalls []EventRepositoryListCall
ListReturnValue []domain.Event
}

func NewEventRepository() *EventRepository {
return &EventRepository{}
}

func (e EventRepository) Save(ctx context.Context, event domain.Event) error {
panic("implement me")
}

func (e EventRepository) Get(ctx context.Context, eventID domain.EventId) (domain.Event, error) {
panic("implement me")
}

func (e EventRepository) Exists(ctx context.Context, eventID domain.EventId) (bool, error) {
panic("implement me")
}

func (e EventRepository) Count(ctx context.Context) (int, error) {
panic("implement me")
}

func (e *EventRepository) List(ctx context.Context, after *domain.EventId, limit int) ([]domain.Event, error) {
e.ListCalls = append(e.ListCalls, EventRepositoryListCall{
After: after,
Limit: limit,
})
return e.ListReturnValue, nil
}

type EventRepositoryListCall struct {
After *domain.EventId
Limit int
}
50 changes: 50 additions & 0 deletions service/adapters/mocks/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package mocks

import (
"time"

"github.com/planetary-social/nos-event-service/service/app"
"github.com/planetary-social/nos-event-service/service/domain"
)

type Metrics struct {
}

func NewMetrics() *Metrics {
return &Metrics{}
}

func (m Metrics) StartApplicationCall(handlerName string) app.ApplicationCall {
return NewApplicationCall()
}

func (m Metrics) ReportNumberOfRelayDownloaders(n int) {
}

func (m Metrics) ReportReceivedEvent(address domain.RelayAddress) {
}

func (m Metrics) ReportQueueLength(topic string, n int) {
}

func (m Metrics) ReportQueueOldestMessageAge(topic string, age time.Duration) {
}

func (m Metrics) ReportNumberOfStoredRelayAddresses(n int) {
}

func (m Metrics) ReportNumberOfStoredEvents(n int) {
}

func (m Metrics) ReportEventSentToRelay(address domain.RelayAddress, decision app.SendEventToRelayDecision, result app.SendEventToRelayResult) {
}

type ApplicationCall struct {
}

func NewApplicationCall() *ApplicationCall {
return &ApplicationCall{}
}

func (a ApplicationCall) End(err *error) {
}
26 changes: 26 additions & 0 deletions service/adapters/mocks/public_keys_to_monitor_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package mocks

import (
"context"

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

type PublicKeysToMonitorRepository struct {
}

func NewPublicKeysToMonitorRepository() *PublicKeysToMonitorRepository {
return &PublicKeysToMonitorRepository{}
}

func (p PublicKeysToMonitorRepository) Save(ctx context.Context, publicKeyToMonitor domain.PublicKeyToMonitor) error {
panic("implement me")
}

func (p PublicKeysToMonitorRepository) List(ctx context.Context) ([]domain.PublicKeyToMonitor, error) {
panic("implement me")
}

func (p PublicKeysToMonitorRepository) Get(ctx context.Context, publicKey domain.PublicKey) (domain.PublicKeyToMonitor, error) {
panic("implement me")
}
Loading

0 comments on commit 587986d

Please sign in to comment.