-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtriggers.gs
174 lines (155 loc) · 5.66 KB
/
triggers.gs
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
/*
Functions:
updateCalendars: updates out of office calendars
notify: Sends the email digest and webhook event (if applicable)
updateGroups: Builds the flattened array of nested groups
*/
function updateCalendars() {
var runStartTime = new Date();
var groups = init();
var count = groups.count;
var backward = 0; // update the calendar starting this many days ago (default: 0)
var calUpdate = 56; // update the calendar for this many days, starting from today or the past as determined by `backward`.
var static = setStatic(groups, calUpdate, backward);
for (var i = 0; i < groups.count; i++) {
try {
var active = setActive(groups, i);
if (active.skip != true) {
active.targetGroups = containedGroups(active.group);
}
updateSharedCalendars(static, active);
for (var sc = 0; sc < active.share.length; sc++) {
var calId = CalendarApp.getCalendarsByName(active.sCal)[0].getId();
shareCalendar(calId, active.share[sc], "reader");
}
} catch (e) {
MailApp.sendEmail(static.maintainer, "Error: Out of office failed on updateCalendars for " + active.group, e.message);
}
}
var runEndTime = new Date();
var runLength = (runEndTime - runStartTime) / 1000;
var logMessage = "start: " + runStartTime + "\nend: " + runEndTime + "\nduration: " + runLength + " seconds.";
}
function notify() {
var groups = init();
var backward = 0;
var calUpdate = 14;
var static = setStatic(groups, calUpdate, backward);
for (var i = 0; i < groups.count; i++) {
try {
var active = setActive(groups, i);
active.window = (active.window <= calUpdate) ? active.window : calUpdate;
if (active.skip != true) {
active.targetGroups = containedGroups(active.group);
}
sendEmails(static, active);
if (active.webhook.length > 0) {
sendWebhooks(static, active);
}
} catch (e) {
MailApp.sendEmail(static.maintainer, "Error: Out of office failed on notify for " + active.group, e.message);
}
}
}
function updateGroups() {
try {
/**
* Builds a flattened array of a target group and all the groups and members it contains.
* Necessary because the Admin Directory API only returns membership of a specific group,
* not any groups contained by the target group.
*
* @param {string} group The target group (email address)
* @param {object} s Static script properties
*
* @returns {aclResource} See https://developers.google.com/google-apps/calendar/v3/reference/acl#resource
*/
function generateGroupTree(group, s) {
// Write the group membership data to script properties as a cache
function putMemberTree(memberTree) {
var scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperties(memberTree);
}
function getGroupMembers(group, s) {
var groupKey = group;
var queryRows = [];
var memberTree = {};
var pageToken, page;
do {
page = AdminDirectory.Members.list(groupKey,
{
domainName: s.domainName,
maxResults: 500,
pageToken: pageToken,
});
var members = [];
members = page.members;
if (members)
{
for (var t = 0; t < members.length; t++)
{
var member = members[t];
if (member.type === "GROUP") {
var row = [];
row = [member.email];
queryRows.push(row);
} else {
// For each member, create a "[member]__[group] = true" key/value to be used as a cache
// This is used by events.gs to check whether an event creator is a member of the active group
memberTree[member.email+"__"+groupKey] = {};
memberTree[member.email+"__"+groupKey] = true;
}
}
}
pageToken = page.nextPageToken;
} while (pageToken);
putMemberTree(memberTree);
return queryRows;
}
var parent = group;
var containedGroups = {};
var toVisit = [group];
while (toVisit.length > 0) {
group = toVisit.pop();
containedGroups[group] = true;
groupMembers = getGroupMembers(group, s);
for (w = 0; w < groupMembers.length; w++) {
email = groupMembers[w];
toVisit.splice(toVisit.length, 0, email);
}
}
var groups = [];
for (var k in containedGroups) {
if (containedGroups.hasOwnProperty(k)) {
groups.splice(groups.length, 0, k);
}
}
return groups;
}
var groups = init();
var static = setStatic(groups);
var memberCache = PropertiesService.getScriptProperties();
memberCache.deleteAllProperties();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetData = ss.getSheetByName("Flattened Groups")
var header = ['Parent Group', 'Contained Groups'];
var rows = [];
sheetData.clear();
sheetData.appendRow(header).setFrozenRows(1);
for (var i = 0; i < groups.count; i++) {
var active = setActive(groups, i);
if (active.skip !== true) {
rows = generateGroupTree(active.group, static);
for (j = 0; j < rows.length; j++) {
rows[j] = [active.group, rows[j]];
}
if (rows.length > 0) {
var lastRow = sheetData.getLastRow();
var startRow = lastRow + 1;
sheetData.getRange(startRow, 1, rows.length, header.length).setValues(rows);
}
}
}
} catch (e) {
MailApp.sendEmail(static.maintainer, "Error: Out of office failed on updateGroups", e.message);
}
}