-
Notifications
You must be signed in to change notification settings - Fork 0
/
handles.go
210 lines (182 loc) · 4.99 KB
/
handles.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"bytes"
"context"
"encoding/json"
"io"
"log"
"net/http"
"strings"
"time"
)
func isDiscordWebhook(url string) bool {
prefixCheck := strings.HasPrefix(url, "https://discord.com/api/webhooks/") || strings.HasPrefix(url, "https://discordapp.com/api/webhooks/")
if !prefixCheck {
return false
}
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return false
}
getDiscordHook, err := http.DefaultClient.Do(request)
if err != nil {
return false
}
return getDiscordHook.StatusCode == http.StatusOK
}
func tick[CustomObj any, Feed IFeed, State any](ctx context.Context, tickTime time.Time, state State,
feed Feed,
tickRate time.Duration,
handleTime func(socialFeed Feed, state State, tickTime time.Time, tickRate time.Duration, repo Repository) ([]CustomObj, error),
buildDiscordWebhook func(customObj CustomObj) ([]PreparedHook, error)) error {
var err error
var repo Repository
if err = repo.Init(ctx); err != nil {
return err
}
defer repo.Deinit()
topicSends, err := handleTime(feed, state, tickTime, tickRate, repo)
if err != nil {
return err
}
if topicSends == nil {
return nil
}
callbackReturns := make(chan SendCallbackReturn)
for _, webhook := range topicSends {
go func(topicHooks CustomObj, res chan SendCallbackReturn) {
var preparedHooks []PreparedHook
preparedHooks, err = buildDiscordWebhook(topicHooks)
if err != nil {
log.Println("Error while buildDiscordWebhook in feed ", feed.GetFeedName(), err)
res <- SendCallbackReturn{
Callback: "",
Ok: true,
} // TODO handle more verbose
return
}
for _, preparedHook := range preparedHooks {
go func(preparedHook PreparedHook, channel chan SendCallbackReturn) {
var resp *http.Response
resp, err = http.Post(preparedHook.Callback, "application/json", bytes.NewBuffer([]byte(preparedHook.Body)))
if err != nil {
log.Println("error posting callback ", err)
channel <- SendCallbackReturn{
Callback: preparedHook.Callback,
Ok: false,
}
return
}
defer func(Body io.ReadCloser) {
if err = Body.Close(); err != nil {
log.Println("could not close body io ", err)
}
}(resp.Body)
if resp.StatusCode == http.StatusNotFound {
channel <- SendCallbackReturn{
Callback: preparedHook.Callback,
Ok: false,
}
return
}
if resp.StatusCode != http.StatusNoContent {
log.Println("strange return from discord ", resp.StatusCode)
}
channel <- SendCallbackReturn{
Callback: preparedHook.Callback,
Ok: true,
}
}(preparedHook, callbackReturns)
}
}(webhook, callbackReturns)
}
for range topicSends {
callback := <-callbackReturns
if callback.Ok {
if callback.Callback != "" {
repositoryMutex.Lock()
if err = repo.FireStampWebhook(callback.Callback); err != nil {
log.Println("could not stamp webhook ", callback.Callback, err)
}
repositoryMutex.Unlock()
}
} else {
repositoryMutex.Lock()
if err = repo.DeleteHooksByCallback(callback.Callback); err != nil {
log.Println("error deleting webhook ", err)
}
repositoryMutex.Unlock()
}
}
return nil
}
func Listen[CustomObj any, Feed IFeed, State any](ctx context.Context, tickRate time.Duration,
feed Feed,
state State,
handleTime func(socialFeed Feed, state State, tickTime time.Time, tickRate time.Duration, repo Repository) ([]CustomObj, error),
buildDiscordWebhook func(customObj CustomObj) ([]PreparedHook, error),
) {
ticker := time.NewTicker(tickRate)
for {
select {
case tickTime := <-ticker.C:
if err := tick(ctx, tickTime, state, feed, tickRate, handleTime, buildDiscordWebhook); err != nil {
log.Println("Error in tick ", err)
}
case <-ctx.Done():
ticker.Stop()
return
}
}
}
func SendAndCleanFailed(pack WebhookJobs, deleteByCallbackFunc func(callback string) error) error {
notWorked, err := send(pack)
if err != nil {
return err
}
for _, job := range notWorked {
if err := deleteByCallbackFunc(job); err != nil {
log.Println(err)
continue
}
}
return nil
}
func send(pack WebhookJobs) ([]string, error) {
marshal, err := json.Marshal(pack)
if err != nil {
log.Println(err)
return nil, err
}
var resp *http.Response
resp, err = http.Post(ServerlessSenderUrl, "application/json", bytes.NewBuffer(marshal))
if err != nil {
log.Println(err)
return nil, err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Println(err)
}
}(resp.Body)
if resp.StatusCode != 200 {
log.Println("SocialWebhook worker returned non 200 status code:", resp.StatusCode)
return nil, err
}
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return nil, err
}
if len(string(bodyBytes)) == 0 {
log.Println("SocialWebhook worker returned empty body")
return nil, err
}
var notWorkedUrls []string
err = json.Unmarshal(bodyBytes, ¬WorkedUrls)
if err != nil {
return nil, err
}
return notWorkedUrls, nil
}