publisher.go raw
1 package publish
2
3 import (
4 "time"
5
6 "github.com/gorilla/websocket"
7 "next.orly.dev/pkg/nostr/encoders/event"
8 "next.orly.dev/pkg/interfaces/publisher"
9 "next.orly.dev/pkg/interfaces/typer"
10 )
11
12 // WriteRequest represents a write operation to be performed by the write worker
13 type WriteRequest struct {
14 Data []byte
15 MsgType int
16 IsControl bool
17 Deadline time.Time
18 IsPing bool // Special marker for ping messages
19 }
20
21 // WriteChanSetter defines the interface for setting write channels
22 type WriteChanSetter interface {
23 SetWriteChan(*websocket.Conn, chan WriteRequest)
24 GetWriteChan(*websocket.Conn) (chan WriteRequest, bool)
25 }
26
27 // NIP46SignerChecker defines the interface for checking active NIP-46 signers
28 type NIP46SignerChecker interface {
29 HasActiveNIP46Signer(signerPubkey []byte) bool
30 }
31
32 // S is the control structure for the subscription management scheme.
33 type S struct {
34 publisher.Publishers
35 }
36
37 // New creates a new publish.S.
38 func New(p ...publisher.I) (s *S) {
39 s = &S{Publishers: p}
40 return
41 }
42
43 var _ publisher.I = &S{}
44
45 func (s *S) Type() string { return "publish" }
46
47 func (s *S) Deliver(ev *event.E) {
48 for _, p := range s.Publishers {
49 p.Deliver(ev)
50 }
51 }
52
53 func (s *S) Receive(msg typer.T) {
54 t := msg.Type()
55 for _, p := range s.Publishers {
56 if p.Type() == t {
57 p.Receive(msg)
58 return
59 }
60 }
61 }
62
63 // GetSocketPublisher returns the socketapi publisher instance
64 func (s *S) GetSocketPublisher() WriteChanSetter {
65 for _, p := range s.Publishers {
66 if p.Type() == "socketapi" {
67 if socketPub, ok := p.(WriteChanSetter); ok {
68 return socketPub
69 }
70 }
71 }
72 return nil
73 }
74