-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstreaming.go
78 lines (62 loc) · 1.5 KB
/
streaming.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"log"
"time"
madon "github.com/McKael/madon/v3"
)
func openStream(mc *madon.Client, pEvents *chan madon.StreamEvent, pStopChan *chan bool, pDoneChan *chan bool) error {
// open again
*pEvents = make(chan madon.StreamEvent)
*pStopChan = make(chan bool)
*pDoneChan = make(chan bool)
return mc.StreamListener("user", "", *pEvents, *pStopChan, *pDoneChan)
}
func eventHandler(mc *madon.Client, reactions []Reaction, event madon.StreamEvent) {
if event.Event != "notification" {
return
}
noti, ok := event.Data.(madon.Notification)
if !ok {
return
}
if noti.Account.Bot {
return
}
if noti.Type != "mention" {
return
}
err := reply(mc, noti.Status, reactions)
if err != nil {
log.Println("reply():", err, "|", "noti.status:", noti.Status)
}
}
func run(mc *madon.Client, reactions []Reaction) {
var (
events chan madon.StreamEvent
stopChan chan bool
doneChan chan bool
)
openStream(mc, &events, &stopChan, &doneChan)
for {
select {
case event := <-events:
if event.Error != nil {
log.Println("event.Error", event.Error)
continue
}
go eventHandler(mc, reactions, event)
case <-doneChan: // if close(doneChan) was executed
for {
time.Sleep(time.Millisecond * time.Duration(RESTART_MS)) // RESTART_MS is a global variable
err := openStream(mc, &events, &stopChan, &doneChan)
if err != nil {
close(stopChan)
log.Println("openStream():", err)
continue
}
log.Println("Restart Success!")
break
}
}
}
}