-
Notifications
You must be signed in to change notification settings - Fork 12
/
api.go
134 lines (100 loc) · 4.04 KB
/
api.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
)
type RecurseAPI struct {
rcAPIURL string
}
type RecurserProfile struct {
Email string
}
func (ra *RecurseAPI) userIsCurrentlyAtRC(accessToken string, email string) bool {
emailsOfPeopleAtRC := ra.getCurrentlyActiveEmails(accessToken)
return contains(emailsOfPeopleAtRC, email)
}
//The profile API endpoint is updated at midnight on the last day (Friday) of a batch.
func (ra *RecurseAPI) getCurrentlyActiveEmails(accessToken string) []string {
var emailsOfPeopleAtRC []string
offset := 0
limit := 50
apiHasMoreResults := true
for apiHasMoreResults {
emailsStartingFromOffset := ra.getCurrentlyActiveEmailsWithOffset(accessToken, offset, limit)
emailsOfPeopleAtRC = append(emailsOfPeopleAtRC, emailsStartingFromOffset...)
log.Printf("The API returned %v profiles from the offset of %v", len(emailsStartingFromOffset), offset)
//The API limits respones to 50 total profiles. Keep querying the API until there are no more Recurser Profiles remaining
if len(emailsStartingFromOffset) == limit {
apiHasMoreResults = true
offset += limit
log.Println("We reached the limit of results from the Profiles API and need to make another query")
} else {
apiHasMoreResults = false
}
}
log.Println("The API returned this many TOTAL profiles", len(emailsOfPeopleAtRC))
log.Println("Here are the emails of people currently at RC", emailsOfPeopleAtRC)
return emailsOfPeopleAtRC
}
/*
The RC API limits queries to the profiles endpoint to 50 results. However, there may be more than 50 people currently at RC.
The RC API takes in an "offset" query param that allows us to grab records beyond that limit of 50 results by performing multiple api calls.
*/
func (ra *RecurseAPI) getCurrentlyActiveEmailsWithOffset(accessToken string, offset int, limit int) []string {
var emailsOfPeopleAtRC []string
endpointString := fmt.Sprintf("/profiles?scope=current&offset=%v&limit=%v&role=recurser&access_token=%v", offset, limit, accessToken)
resp, err := http.Get(ra.rcAPIURL + endpointString)
if err != nil {
log.Printf("Got the following error while getting the RC batches from the RC API: %s\n", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Unable to get the emails of people currently at RC due to the following error: %s", err)
return emailsOfPeopleAtRC
}
//Parse the json response from the API
recursers := []RecurserProfile{}
json.Unmarshal([]byte(body), &recursers)
for i := range recursers {
email := recursers[i].Email
emailsOfPeopleAtRC = append(emailsOfPeopleAtRC, email)
}
return emailsOfPeopleAtRC
}
func (ra *RecurseAPI) isSecondWeekOfBatch(accessToken string) bool {
resp, err := http.Get(ra.rcAPIURL + "/batches?access_token=" + accessToken)
if err != nil {
log.Printf("Got the following error while getting the RC batches from the RC API: %s\n", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
//Parse the json response from the API
var batches []map[string]interface{}
json.Unmarshal([]byte(body), &batches)
var batchStart string
//Loop through the batches until we find the first non-mini batch. Mini batches are only 1 week long, so it doesn't make sense to send a message
//1 week after a mini batch has started :joy:
for i := range batches {
if !strings.HasPrefix(batches[i]["name"].(string), "Mini") {
batchStart = batches[i]["start_date"].(string)
break
}
}
//Convert strings of the form "YYYY-MM-DD" into time objects that Go can perform mathematical operations with
const shortForm = "2006-01-02"
batchStartDate, _ := time.Parse(shortForm, batchStart)
if err != nil {
log.Printf("Unable to parse the batch start date of: %s. Exiting out of the welcome cron job.", batchStart)
return false
}
todayDate := time.Now()
hoursSinceStartOfBatch := todayDate.Sub(batchStartDate).Hours()
log.Printf("Hours since start of the batch: %f", hoursSinceStartOfBatch)
return (hoursSinceStartOfBatch > 168) && (hoursSinceStartOfBatch < 336)
}