delete_app_consumer.go raw

   1  package service
   2  
   3  import (
   4  	"context"
   5  
   6  	"github.com/getAlby/go-nostr"
   7  	"github.com/getAlby/hub/events"
   8  	"github.com/getAlby/hub/logger"
   9  )
  10  
  11  type deleteAppConsumer struct {
  12  	events.EventSubscriber
  13  	walletPubkey       string
  14  	pool               *nostr.SimplePool
  15  	cancelSubscription func()
  16  	svc                *service
  17  }
  18  
  19  // When an app is deleted, unsubscribe from events for that app on the relay
  20  // and publish a deletion event for that app's info event
  21  func (s *deleteAppConsumer) ConsumeEvent(ctx context.Context, event *events.Event, globalProperties map[string]interface{}) {
  22  	if event.Event != "nwc_app_deleted" {
  23  		return
  24  	}
  25  	properties, ok := event.Properties.(map[string]interface{})
  26  	if !ok {
  27  		logger.Logger.WithField("event", event).Error("Failed to cast event.Properties to map")
  28  		return
  29  	}
  30  	// Note: for legacy apps the deleted app's WalletPubkey is empty and will
  31  	// not match the master key used for the legacy app subscription, so the
  32  	// subscription is preserved for any remaining legacy apps.
  33  	walletPubKey, _ := properties["walletPubkey"].(string)
  34  	if walletPubKey == "" || walletPubKey != s.walletPubkey {
  35  		return
  36  	}
  37  	id, ok := properties["id"].(uint)
  38  	if !ok {
  39  		logger.Logger.WithField("event", event).Error("missing id in properties event")
  40  		return
  41  	}
  42  
  43  	// no longer need to listen to events for this wallet
  44  	s.cancelSubscription()
  45  
  46  	// remove this consumer as subscriber in eventPublisher
  47  	s.svc.eventPublisher.RemoveSubscriber(s)
  48  
  49  	walletPrivKey, err := s.svc.keys.GetAppWalletKey(id)
  50  	if err != nil {
  51  		logger.Logger.WithError(err).WithField("id", id).Error("Failed to calculate app wallet priv key")
  52  		return
  53  	}
  54  
  55  	// try to delete info event from relays (non-critical if it fails)
  56  	// get nip47 event info for this app wallet key
  57  	nip47InfoEvent, err := s.svc.GetNip47Service().GetNip47Info(ctx, s.pool, s.walletPubkey)
  58  	if err != nil {
  59  		logger.Logger.WithError(err).Error("Could not get nip47 info event")
  60  		return
  61  	}
  62  	if nip47InfoEvent != nil {
  63  		err = s.svc.nip47Service.PublishNip47InfoDeletion(ctx, s.pool, walletPubKey, walletPrivKey, nip47InfoEvent.ID)
  64  		if err != nil {
  65  			logger.Logger.WithError(err).WithField("event", event).Error("Failed to publish nip47 info deletion")
  66  		}
  67  	}
  68  }
  69