@@ -13,46 +13,67 @@ import (
13
13
"github.com/xaionaro-go/streamctl/pkg/streamcontrol"
14
14
)
15
15
16
- type ChatClient interface {
16
+ type ChatClientOBSOLETE interface {
17
17
GetChatMessagesV2 (
18
18
ctx context.Context ,
19
19
channelID uint64 ,
20
20
cursor uint64 ,
21
21
) (* kickcom.ChatMessagesV2Reply , error )
22
22
}
23
23
24
- type ChatHandler struct {
24
+ type ChatHandlerOBSOLETE struct {
25
25
currentCursor uint64
26
26
channelID uint64
27
27
lastMessageID string
28
- client ReverseEngClient
28
+ client ChatClientOBSOLETE
29
29
cancelFunc context.CancelFunc
30
30
messagesOutChan chan streamcontrol.ChatMessage
31
+ onClose func (context.Context )
31
32
}
32
33
33
34
func (k * Kick ) newChatHandler (
34
35
ctx context.Context ,
35
- channelID uint64 ,
36
- ) (* ChatHandler , error ) {
37
- return NewChatHandler (ctx , k .ReverseEngClient , channelID )
36
+ channelSlug string ,
37
+ onClose func (context.Context ),
38
+ ) (* ChatHandlerOBSOLETE , error ) {
39
+ reverseEngClient , err := kickcom .New ()
40
+ if err != nil {
41
+ return nil , fmt .Errorf ("unable to initialize a client to Kick: %w" , err )
42
+ }
43
+ resp , err := reverseEngClient .GetChannelV1 (ctx , channelSlug )
44
+ if err != nil {
45
+ return nil , fmt .Errorf ("unable to get channel '%s' info: %w" , channelSlug , err )
46
+ }
47
+ return NewChatHandlerOBSOLETE (ctx , reverseEngClient , resp .ID , onClose )
38
48
}
39
49
40
- func NewChatHandler (
50
+ func NewChatHandlerOBSOLETE (
41
51
ctx context.Context ,
42
- chatClient ReverseEngClient ,
52
+ chatClient ChatClientOBSOLETE ,
43
53
channelID uint64 ,
44
- ) (* ChatHandler , error ) {
54
+ onClose func (context.Context ),
55
+ ) (_ret * ChatHandlerOBSOLETE , _err error ) {
56
+ logger .Debugf (ctx , "NewChatHandlerOBSOLETE(ctx, client, %d, %p)" , channelID , onClose )
57
+ defer func () {
58
+ logger .Debugf (ctx , "/NewChatHandlerOBSOLETE(ctx, client, %d, %p): %#+v %v" , channelID , onClose , _ret , _err )
59
+ }()
45
60
46
61
ctx , cancelFn := context .WithCancel (ctx )
47
- h := & ChatHandler {
62
+ h := & ChatHandlerOBSOLETE {
48
63
currentCursor : 0 ,
49
64
client : chatClient ,
50
65
channelID : channelID ,
51
66
cancelFunc : cancelFn ,
52
67
messagesOutChan : make (chan streamcontrol.ChatMessage , 100 ),
68
+ onClose : onClose ,
53
69
}
54
70
55
71
observability .Go (ctx , func (ctx context.Context ) {
72
+ if onClose != nil {
73
+ defer func () {
74
+ onClose (ctx )
75
+ }()
76
+ }
56
77
defer func () {
57
78
close (h .messagesOutChan )
58
79
}()
@@ -81,14 +102,14 @@ func NewChatHandler(
81
102
return h , nil
82
103
}
83
104
84
- func (h * ChatHandler ) iterate (ctx context.Context ) error {
105
+ func (h * ChatHandlerOBSOLETE ) iterate (ctx context.Context ) error {
85
106
startTS := time .Now ()
86
- reply , err := h .client .GetChatMessagesV2 (ctx , h .channelID , 0 )
107
+ reply , err := h .client .GetChatMessagesV2 (ctx , uint64 ( h .channelID ) , 0 )
87
108
if err != nil {
88
109
return fmt .Errorf ("unable to get the chat messages of channel with ID %d: %w" , h .channelID , err )
89
110
}
90
111
rtt := time .Since (startTS )
91
- logger .Tracef (ctx , "round trip time == %v" , rtt )
112
+ logger .Tracef (ctx , "round trip time == %v (messages count: %d) " , rtt , len ( reply . Data . Messages ) )
92
113
93
114
// TODO: use the cursor instead of message ID to avoid duplicates
94
115
if reply .Data .Cursor != "" {
@@ -120,7 +141,7 @@ func (h *ChatHandler) iterate(ctx context.Context) error {
120
141
return nil
121
142
}
122
143
123
- func (h * ChatHandler ) sendMessage (
144
+ func (h * ChatHandlerOBSOLETE ) sendMessage (
124
145
msg kickcom.ChatMessageV2 ,
125
146
) {
126
147
h .lastMessageID = msg .ID
@@ -135,6 +156,6 @@ func (h *ChatHandler) sendMessage(
135
156
default :
136
157
}
137
158
}
138
- func (h * ChatHandler ) MessagesChan () <- chan streamcontrol.ChatMessage {
159
+ func (h * ChatHandlerOBSOLETE ) MessagesChan () <- chan streamcontrol.ChatMessage {
139
160
return h .messagesOutChan
140
161
}
0 commit comments