Skip to content

Commit

Permalink
Download events
Browse files Browse the repository at this point in the history
  • Loading branch information
boreq committed Jun 20, 2023
1 parent b3919fd commit 92ecbee
Show file tree
Hide file tree
Showing 19 changed files with 612 additions and 158 deletions.
3 changes: 3 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
run:
build-tags:
- wireinject
4 changes: 0 additions & 4 deletions cmd/notification-service/di/inject_adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,3 @@ var firestoreTxAdaptersSet = wire.NewSet(
firestore.NewEventRepository,
wire.Bind(new(app.EventRepository), new(*firestore.EventRepository)),
)

var adaptersSet = wire.NewSet(
// adapters.NewCurrentTimeProvider,
)
134 changes: 0 additions & 134 deletions cmd/notification-service/di/inject_badger.go

This file was deleted.

10 changes: 10 additions & 0 deletions cmd/notification-service/di/inject_downloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package di

import (
"github.com/google/wire"
"github.com/planetary-social/go-notification-service/service/app"
)

var downloaderSet = wire.NewSet(
app.NewDownloader,
)
16 changes: 12 additions & 4 deletions cmd/notification-service/di/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ import (
)

type Service struct {
app app.Application
server http.Server
app app.Application
server http.Server
downloader *app.Downloader
}

func NewService(
app app.Application,
server http.Server,
downloader *app.Downloader,
) Service {
return Service{
app: app,
server: server,
app: app,
server: server,
downloader: downloader,
}
}

Expand All @@ -40,6 +43,11 @@ func (s Service) Run(ctx context.Context) error {
errCh <- s.server.ListenAndServe(ctx)
}()

runners++
go func() {
errCh <- s.downloader.Run(ctx)
}()

var err error
for i := 0; i < runners; i++ {
err = multierror.Append(err, errors.Wrap(<-errCh, "error returned by runner"))
Expand Down
1 change: 1 addition & 0 deletions cmd/notification-service/di/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func BuildService(context.Context, config.Config) (Service, func(), error) {
portsSet,
applicationSet,
firestoreAdaptersSet,
downloaderSet,
)
return Service{}, nil, nil
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/notification-service/di/wire_gen.go

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

100 changes: 97 additions & 3 deletions cmd/notification-service/main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,113 @@
package main

import (
"errors"
"context"
"fmt"
"os"

"github.com/boreq/errors"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19"
"github.com/planetary-social/go-notification-service/cmd/notification-service/di"
"github.com/planetary-social/go-notification-service/internal/fixtures"
"github.com/planetary-social/go-notification-service/service/app"
"github.com/planetary-social/go-notification-service/service/config"
"github.com/planetary-social/go-notification-service/service/domain"
)

func main() {
if err := run(); err != nil {
fmt.Printf("error: %s", err)
os.Exit(1)
}

}

func run() error {
return errors.New("not implemented")
ctx := context.Background()
cfg, err := config.NewConfig("", "test-project-id")
if err != nil {
return errors.Wrap(err, "error creating a config")
}

service, cleanup, err := di.BuildService(ctx, cfg)
if err != nil {
return errors.Wrap(err, "error building a service")
}
defer cleanup()

addMyRegistration(ctx, service) // todo remove

return service.Run(ctx)

}

func addMyRegistration(ctx context.Context, service di.Service) {
nsec := os.Getenv("NSEC")
_, value, err := nip19.Decode(nsec)
if err != nil {
panic(err)
}

secretKey := value.(string)
publicKeyString, err := nostr.GetPublicKey(secretKey)
if err != nil {
panic(err)
}

publicKey, err := domain.NewPublicKey(publicKeyString)
if err != nil {
panic(err)
}

relayAddress, err := domain.NewRelayAddress("wss://relay.damus.io")
if err != nil {
panic(err)
}

libEvent := nostr.Event{
CreatedAt: nostr.Now(),
Kind: 12345,
Tags: nostr.Tags{},
Content: fmt.Sprintf(`
{
"publicKeys": [
{
"publicKey": "%s",
"relays": [
{
"address": "%s"
}
]
}
],
"locale": "%s",
"apnsToken": "%s"
}
`,
publicKey.Hex(),
relayAddress.String(),
fixtures.SomeString(),
fixtures.SomeString()),
}

err = libEvent.Sign(secretKey)
if err != nil {
panic(err)
}

event, err := domain.NewEvent(libEvent)
if err != nil {
panic(err)
}

registration, err := domain.NewRegistrationFromEvent(event)
if err != nil {
panic(err)
}

cmd := app.NewSaveRegistration(registration)
err = service.App().Commands.SaveRegistration.Handle(ctx, cmd)
if err != nil {
panic(err)
}
}
48 changes: 48 additions & 0 deletions internal/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package internal

type Set[T comparable] struct {
values map[T]struct{}
}

func NewEmptySet[T comparable]() *Set[T] {
return &Set[T]{
values: make(map[T]struct{}),
}
}

func NewSet[T comparable](values []T) *Set[T] {
v := NewEmptySet[T]()
for _, value := range values {
v.Put(value)
}
return v
}

func (s *Set[T]) Contains(v T) bool {
_, ok := s.values[v]
return ok
}

func (s *Set[T]) Put(v T) {
s.values[v] = struct{}{}
}

func (s *Set[T]) Clear() {
s.values = make(map[T]struct{})
}

func (s *Set[T]) Delete(v T) {
delete(s.values, v)
}

func (s *Set[T]) List() []T {
var result []T
for v := range s.values {
result = append(result, v)
}
return result
}

func (s *Set[T]) Len() int {
return len(s.values)
}
Loading

0 comments on commit 92ecbee

Please sign in to comment.