specialkinds.go raw

   1  package app
   2  
   3  import (
   4  	"context"
   5  
   6  	"next.orly.dev/pkg/nostr/encoders/event"
   7  	"next.orly.dev/pkg/nostr/encoders/kind"
   8  	"next.orly.dev/pkg/acl"
   9  	"next.orly.dev/pkg/event/specialkinds"
  10  	"next.orly.dev/pkg/protocol/nip43"
  11  )
  12  
  13  // registerSpecialKindHandlers registers handlers for special event kinds
  14  // that require custom processing before normal storage/delivery.
  15  func (s *Server) registerSpecialKindHandlers() {
  16  	// NIP-43 Join Request handler - signals that special handling is needed
  17  	s.specialKinds.Register(specialkinds.NewHandlerFunc(
  18  		"nip43-join",
  19  		func(ev *event.E) bool {
  20  			return ev.Kind == nip43.KindJoinRequest
  21  		},
  22  		func(ctx context.Context, ev *event.E, hctx *specialkinds.HandlerContext) specialkinds.Result {
  23  			// Signal to handler that this needs NIP-43 join processing
  24  			// The actual processing happens in the Listener which has the connection context
  25  			return specialkinds.Result{
  26  				Handled: true,
  27  				Message: "nip43-join", // Marker for handler
  28  			}
  29  		},
  30  	))
  31  
  32  	// NIP-43 Leave Request handler - signals that special handling is needed
  33  	s.specialKinds.Register(specialkinds.NewHandlerFunc(
  34  		"nip43-leave",
  35  		func(ev *event.E) bool {
  36  			return ev.Kind == nip43.KindLeaveRequest
  37  		},
  38  		func(ctx context.Context, ev *event.E, hctx *specialkinds.HandlerContext) specialkinds.Result {
  39  			return specialkinds.Result{
  40  				Handled: true,
  41  				Message: "nip43-leave", // Marker for handler
  42  			}
  43  		},
  44  	))
  45  
  46  	// Curating config handler (kind 30078 with d-tag "curating-config")
  47  	s.specialKinds.Register(specialkinds.NewHandlerFunc(
  48  		"curating-config",
  49  		func(ev *event.E) bool {
  50  			if ev.Kind != acl.CuratingConfigKind {
  51  				return false
  52  			}
  53  			dTag := ev.Tags.GetFirst([]byte("d"))
  54  			return dTag != nil && string(dTag.Value()) == acl.CuratingConfigDTag
  55  		},
  56  		s.handleCuratingConfig,
  57  	))
  58  
  59  	// Policy config handler (kind 12345)
  60  	s.specialKinds.Register(specialkinds.NewHandlerFunc(
  61  		"policy-config",
  62  		func(ev *event.E) bool {
  63  			return ev.Kind == kind.PolicyConfig.K
  64  		},
  65  		func(ctx context.Context, ev *event.E, hctx *specialkinds.HandlerContext) specialkinds.Result {
  66  			return specialkinds.Result{
  67  				Handled: true,
  68  				Message: "policy-config", // Marker for handler
  69  			}
  70  		},
  71  	))
  72  
  73  }
  74  
  75  // handleCuratingConfig handles curating configuration events
  76  func (s *Server) handleCuratingConfig(ctx context.Context, ev *event.E, hctx *specialkinds.HandlerContext) specialkinds.Result {
  77  	if acl.Registry.Type() != "curating" {
  78  		return specialkinds.ContinueProcessing()
  79  	}
  80  
  81  	for _, aclInstance := range acl.Registry.ACLs() {
  82  		if aclInstance.Type() == "curating" {
  83  			if curating, ok := aclInstance.(*acl.Curating); ok {
  84  				if err := curating.ProcessConfigEvent(ev); err != nil {
  85  					return specialkinds.ErrorResult(err)
  86  				}
  87  				// Save the event and signal success
  88  				return specialkinds.HandledWithSave("curating configuration updated")
  89  			}
  90  		}
  91  	}
  92  
  93  	return specialkinds.ContinueProcessing()
  94  }
  95