-
Notifications
You must be signed in to change notification settings - Fork 132
/
notification_email.go
61 lines (50 loc) · 1.46 KB
/
notification_email.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
package ovh
import (
"fmt"
"log"
"sort"
"strings"
)
func notificationEmailSortedIds(meta interface{}) ([]int64, error) {
config := meta.(*Config)
// Create Order
log.Printf("[DEBUG] Will read notification emails ids")
res := []int64{}
endpoint := fmt.Sprintf("/me/notification/email/history")
if err := config.OVHClient.Get(endpoint, &res); err != nil {
return nil, fmt.Errorf("calling Get %s:\n\t %q", endpoint, err)
}
sort.Slice(res, func(i, j int) bool { return res[i] < res[j] })
return res, nil
}
func getNewNotificationEmail(matches []string, oldIds []int64, meta interface{}) (*NotificationEmail, error) {
config := meta.(*Config)
curIds, err := notificationEmailSortedIds(meta)
if err != nil {
return nil, err
}
lastOldId := oldIds[len(oldIds)-1]
for _, id := range curIds {
// matching only new ids (NOTE; a set subtract would be a better impl)
if id > lastOldId {
log.Printf("[DEBUG] Will read notification email %d", id)
email := &NotificationEmail{}
endpoint := fmt.Sprintf("/me/notification/email/history/%d", id)
if err := config.OVHClient.Get(endpoint, email); err != nil {
return nil, fmt.Errorf("calling Get %s:\n\t %q", endpoint, err)
}
match := true
for _, m := range matches {
log.Printf("[DEBUG] test match %v", m)
if !strings.Contains(email.Body, m) {
match = false
}
}
if match {
return email, nil
}
}
}
log.Printf("[DEBUG] no new notification email")
return nil, nil
}