Skip to content

Commit d1c8a44

Browse files
committed
chore: remove duplicated nip-47 notifier
we were starting a nip-47 notifier for each subscription rather than once per relay. I also removed some unused dependencies from the notifier.
1 parent 1cf8e5b commit d1c8a44

File tree

4 files changed

+23
-30
lines changed

4 files changed

+23
-30
lines changed

nip47/nip47_service.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type nip47Service struct {
3232

3333
type Nip47Service interface {
3434
events.EventSubscriber
35-
StartNotifier(ctx context.Context, relay *nostr.Relay, lnClient lnclient.LNClient)
35+
StartNotifier(ctx context.Context, relay *nostr.Relay)
3636
HandleEvent(ctx context.Context, relay nostrmodels.Relay, event *nostr.Event, lnClient lnclient.LNClient)
3737
GetNip47Info(ctx context.Context, relay *nostr.Relay, appWalletPubKey string) (*nostr.Event, error)
3838
PublishNip47Info(ctx context.Context, relay nostrmodels.Relay, appWalletPubKey string, appWalletPrivKey string, lnClient lnclient.LNClient) (*nostr.Event, error)
@@ -58,13 +58,16 @@ func (svc *nip47Service) ConsumeEvent(ctx context.Context, event *events.Event,
5858
svc.nip47NotificationQueue.AddToQueue(event)
5959
}
6060

61-
func (svc *nip47Service) StartNotifier(ctx context.Context, relay *nostr.Relay, lnClient lnclient.LNClient) {
62-
nip47Notifier := notifications.NewNip47Notifier(relay, svc.db, svc.cfg, svc.keys, svc.permissionsService, svc.transactionsService, lnClient)
61+
// The notifier is decoupled from the notification queue
62+
// so that if Alby Hub disconnects from the relay, it will wait to reconnect
63+
// to send notifications rather than dropping them
64+
func (svc *nip47Service) StartNotifier(ctx context.Context, relay *nostr.Relay) {
65+
nip47Notifier := notifications.NewNip47Notifier(relay, svc.db, svc.cfg, svc.keys, svc.permissionsService)
6366
go func() {
6467
for {
6568
select {
6669
case <-ctx.Done():
67-
// subscription ended
70+
// relay disconnected
6871
return
6972
case event := <-svc.nip47NotificationQueue.Channel():
7073
nip47Notifier.ConsumeEvent(ctx, event)

nip47/notifications/nip47_notifier.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,32 @@ import (
88
"github.com/getAlby/hub/constants"
99
"github.com/getAlby/hub/db"
1010
"github.com/getAlby/hub/events"
11-
"github.com/getAlby/hub/lnclient"
1211
"github.com/getAlby/hub/logger"
1312
"github.com/getAlby/hub/nip47/cipher"
1413
"github.com/getAlby/hub/nip47/models"
1514
"github.com/getAlby/hub/nip47/permissions"
1615
nostrmodels "github.com/getAlby/hub/nostr/models"
1716
"github.com/getAlby/hub/service/keys"
18-
"github.com/getAlby/hub/transactions"
1917
"github.com/nbd-wtf/go-nostr"
2018
"github.com/sirupsen/logrus"
2119
"gorm.io/gorm"
2220
)
2321

2422
type Nip47Notifier struct {
25-
relay nostrmodels.Relay
26-
cfg config.Config
27-
keys keys.Keys
28-
lnClient lnclient.LNClient
29-
db *gorm.DB
30-
permissionsSvc permissions.PermissionsService
31-
transactionsService transactions.TransactionsService
23+
relay nostrmodels.Relay
24+
cfg config.Config
25+
keys keys.Keys
26+
db *gorm.DB
27+
permissionsSvc permissions.PermissionsService
3228
}
3329

34-
func NewNip47Notifier(relay nostrmodels.Relay, db *gorm.DB, cfg config.Config, keys keys.Keys, permissionsSvc permissions.PermissionsService, transactionsService transactions.TransactionsService, lnClient lnclient.LNClient) *Nip47Notifier {
30+
func NewNip47Notifier(relay nostrmodels.Relay, db *gorm.DB, cfg config.Config, keys keys.Keys, permissionsSvc permissions.PermissionsService) *Nip47Notifier {
3531
return &Nip47Notifier{
36-
relay: relay,
37-
cfg: cfg,
38-
db: db,
39-
lnClient: lnClient,
40-
permissionsSvc: permissionsSvc,
41-
transactionsService: transactionsService,
42-
keys: keys,
32+
relay: relay,
33+
cfg: cfg,
34+
db: db,
35+
permissionsSvc: permissionsSvc,
36+
keys: keys,
4337
}
4438
}
4539

nip47/notifications/nip47_notifier_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/getAlby/hub/lnclient"
1717
"github.com/getAlby/hub/nip47/permissions"
1818
"github.com/getAlby/hub/tests"
19-
"github.com/getAlby/hub/transactions"
2019
)
2120

2221
type mockConsumer struct {
@@ -80,9 +79,8 @@ func doTestSendNotificationPaymentReceived(t *testing.T, svc *tests.TestService,
8079
relay := tests.NewMockRelay()
8180

8281
permissionsSvc := permissions.NewPermissionsService(svc.DB, svc.EventPublisher)
83-
transactionsSvc := transactions.NewTransactionsService(svc.DB, svc.EventPublisher)
8482

85-
notifier := NewNip47Notifier(relay, svc.DB, svc.Cfg, svc.Keys, permissionsSvc, transactionsSvc, svc.LNClient)
83+
notifier := NewNip47Notifier(relay, svc.DB, svc.Cfg, svc.Keys, permissionsSvc)
8684
notifier.ConsumeEvent(ctx, receivedEvent)
8785

8886
var publishedEvent *nostr.Event
@@ -195,9 +193,8 @@ func doTestSendNotificationPaymentSent(t *testing.T, svc *tests.TestService, cre
195193
relay := tests.NewMockRelay()
196194

197195
permissionsSvc := permissions.NewPermissionsService(svc.DB, svc.EventPublisher)
198-
transactionsSvc := transactions.NewTransactionsService(svc.DB, svc.EventPublisher)
199196

200-
notifier := NewNip47Notifier(relay, svc.DB, svc.Cfg, svc.Keys, permissionsSvc, transactionsSvc, svc.LNClient)
197+
notifier := NewNip47Notifier(relay, svc.DB, svc.Cfg, svc.Keys, permissionsSvc)
201198
notifier.ConsumeEvent(ctx, receivedEvent)
202199

203200
var publishedEvent *nostr.Event
@@ -289,9 +286,8 @@ func doTestSendNotificationNoPermission(t *testing.T, svc *tests.TestService) {
289286
relay := tests.NewMockRelay()
290287

291288
permissionsSvc := permissions.NewPermissionsService(svc.DB, svc.EventPublisher)
292-
transactionsSvc := transactions.NewTransactionsService(svc.DB, svc.EventPublisher)
293289

294-
notifier := NewNip47Notifier(relay, svc.DB, svc.Cfg, svc.Keys, permissionsSvc, transactionsSvc, svc.LNClient)
290+
notifier := NewNip47Notifier(relay, svc.DB, svc.Cfg, svc.Keys, permissionsSvc)
295291
notifier.ConsumeEvent(ctx, receivedEvent)
296292

297293
assert.Nil(t, relay.PublishedEvents)

service/start.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ func (svc *service) startNostr(ctx context.Context) error {
9090
}).Info("Connected to the relay")
9191
waitToReconnectSeconds = 0
9292

93+
svc.nip47Service.StartNotifier(relay.Context(), relay)
94+
9395
// register a subscriber for events of "nwc_app_created" which handles creation of nostr subscription for new app
9496
if createAppEventListener != nil {
9597
svc.eventPublisher.RemoveSubscriber(createAppEventListener)
@@ -201,8 +203,6 @@ func (svc *service) startAppWalletSubscription(ctx context.Context, relay *nostr
201203
}
202204

203205
func (svc *service) StartSubscription(ctx context.Context, sub *nostr.Subscription) error {
204-
svc.nip47Service.StartNotifier(ctx, sub.Relay, svc.lnClient)
205-
206206
go func() {
207207
// loop through incoming events
208208
for event := range sub.Events {

0 commit comments

Comments
 (0)