-
Notifications
You must be signed in to change notification settings - Fork 0
/
submissions.go
89 lines (81 loc) · 2.91 KB
/
submissions.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
package main
import (
"encoding/json"
"fmt"
"github.com/gregdel/pushover"
"github.com/kr/pretty"
"github.com/pkg/errors"
"io/ioutil"
"log"
"net/http"
)
// checks if there is any new submission on pretalx
// return the suppressed error in order to avoid stopping the cron-job
func checkSubmissions(knownSubmissions map[string]submission, onlyNew bool, pretalxURL, pretalxAuthToken string,
pushOverApp *pushover.Pushover, pushoverUserToken string) (suppressedError error) {
var messages []string
log.Print("checking for new submissions...")
submissions, err := getSubmissions(pretalxURL, pretalxAuthToken)
if err != nil {
log.Fatal(err)
return
}
for _, submission := range submissions.Results {
if knownSubmission, found := knownSubmissions[submission.Code]; !found {
log.Print("found a new submission, adding notification...")
messages = append(messages, fmt.Sprintf("A new %s with the title '%s' has been submitted.", getSubmissionTypeString(submission.SubmissionType), submission.Title))
knownSubmissions[submission.Code] = submission
} else if !onlyNew {
if diff := pretty.Diff(knownSubmission, submission); diff != nil {
log.Print("found a changed submission, adding notification...")
messages = append(messages, fmt.Sprintf("The %s with the title '%s' has been changed: %s", getSubmissionTypeString(submission.SubmissionType), submission.Title, diff))
knownSubmissions[submission.Code] = submission
}
}
}
if len(messages) > 0 {
response, err := notify(pushOverApp, pushoverUserToken, pretalxURL, messages)
if err != nil {
log.Fatal("sending notifications failed", err)
} else {
log.Print("notifications have been sent successfully:", response)
}
}
log.Print("submission check completed")
return
}
// reads the submissions from pretalx
func getSubmissions(pretalxURL string, pretalxAuthToken string) (submissions, error) {
var submissions submissions
// TODO implement paging instead of using a limit of 1k
apiURL := pretalxURL + "/submissions?limit=1000"
log.Print("requesting submissions from " + apiURL)
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
return submissions, errors.Wrap(err, "request could not be created")
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "Token "+pretalxAuthToken)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return submissions, errors.Wrap(err, "request to PreTalx failed")
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return submissions, errors.Wrap(err, "reading response body failed")
}
err = json.Unmarshal(body, &submissions)
if err != nil {
return submissions, errors.Wrap(err, "un-marshalling response failed")
}
return submissions, nil
}
// get the name of the submission type (prefer english, use german as fallback)
func getSubmissionTypeString(st submissionType) string {
if st.En != "" {
return st.En
} else {
return st.De
}
}