-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.ts
163 lines (144 loc) · 5.74 KB
/
index.ts
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
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Polyfills
import Condition from './Condition';
import {Config} from './Config';
import {JestExpect, JestIt} from './JestSheets';
import {Processor} from './Processor';
import {Rule} from './Rule';
import {Stats} from './Stats';
import ThreadAction from './ThreadAction';
import Utils from './utils';
// String.startsWith polyfill
if (!String.prototype.startsWith) {
Object.defineProperty(String.prototype, 'startsWith', {
value: function (search: String, rawPos: number) {
var pos = rawPos > 0 ? rawPos | 0 : 0;
return this.substring(pos, pos + search.length) === search;
}
});
}
// String.endsWith polyfill
if (!String.prototype.endsWith) {
String.prototype.endsWith = function (search, this_len) {
if (this_len === undefined || this_len > this.length) {
this_len = this.length;
}
return this.substring(this_len - search.length, this_len) === search;
};
}
// Object.assign polyfill
if (typeof Object.assign !== 'function') {
// Must be writable: true, enumerable: false, configurable: true
Object.defineProperty(Object, "assign", {
value: function assign(target: Object, _source1: Object, ..._sources: Array<Object>) { // .length of function is 2
if (target === null || target === undefined) {
throw new TypeError('Cannot convert undefined or null to object');
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource !== null && nextSource !== undefined) {
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
},
writable: true,
configurable: true
});
}
// Top level functions
function ensurePermissionsEstablished() {
// Some Gmail configurations do not initiate asking for Gmail
// permissions when Gmail APIs are used behind withTimer(). See Issue#63.
Session.getActiveUser();
}
// Triggered when Spreadsheet is opened
// noinspection JSUnusedGlobalSymbols
function onOpen(e: { authMode: GoogleAppsScript.Script.AuthMode }) {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('Gmail Automata');
if (e && e.authMode == ScriptApp.AuthMode.NONE) {
menu.addItem('Configure this spreadsheet', 'configureSpreadsheets');
} else {
menu
.addItem('Process now', 'processEmails')
.addSeparator()
.addItem('Start auto processing', 'setupTriggers')
.addItem('Stop auto processing', 'cancelTriggers')
.addSeparator()
.addSubMenu(
ui.createMenu('DEBUG')
.addItem('Run tests', 'testAll'));
}
menu.addToUi();
}
// Triggered when time-driven trigger or click via Spreadsheet menu
function processEmails() {
ensurePermissionsEstablished();
Utils.withFailureEmailed("processEmails", () => Processor.processAllUnprocessedThreads());
}
function sanityChecking() {
Utils.withFailureEmailed("sanityChecking", () => {
Stats.collapseStatRecords();
});
}
function setupTriggers() {
ensurePermissionsEstablished();
cancelTriggers();
Utils.withFailureEmailed("setupTriggers", () => {
const config = Utils.withTimer("getConfigs", () => Config.getConfig());
Utils.withTimer("addingTriggers", () => {
let trigger = ScriptApp.newTrigger('processEmails')
.timeBased()
.everyMinutes(config.processing_frequency_in_minutes)
.create();
Logger.log(`Created trigger ${trigger.getHandlerFunction()}: ${trigger.getUniqueId()}`);
trigger = ScriptApp.newTrigger('sanityChecking')
.timeBased()
.atHour(config.hour_of_day_to_run_sanity_checking)
.everyDays(1)
.create();
Logger.log(`Created trigger ${trigger.getHandlerFunction()}: ${trigger.getUniqueId()}`);
Utils.assert(ScriptApp.getProjectTriggers().length === 2,
`Unexpected trigger lists: ${ScriptApp.getProjectTriggers()
.map(trigger => trigger.getHandlerFunction())}`);
});
});
}
function cancelTriggers() {
Utils.withFailureEmailed("cancelTriggers", () => {
ScriptApp.getProjectTriggers().forEach(trigger => {
Logger.log(`Deleting trigger ${trigger.getHandlerFunction()}...`);
ScriptApp.deleteTrigger(trigger);
});
});
}
function testAll() {
const jestExpect = new JestExpect();
const jestIt = new JestIt();
Condition.testRegex(jestIt.it, jestExpect.expect);
Condition.testConditionParsing(jestIt.it, jestExpect.expect);
Rule.testRules(jestIt.it, jestExpect.expect);
ThreadAction.testThreadActions(jestIt.it, jestExpect.expect);
Processor.testProcessing(jestIt.it, jestExpect.expect);
}