-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCode.gs
414 lines (377 loc) · 13.8 KB
/
Code.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
var L_PREFIX = 'sharedLabel_';
var MAX_OLD_THREAD_NUM = 50;
var UPDATE_MINUTES = 5;
function propSplit(src, prop, token) {
//splits value on @token, and if empty, returns empty array
var val = src.getProperty(prop);
return (val ? val.split(token) : []);
}
/******************************************************************
* Configuration and Management
******************************************************************/
function setConfig(config) {
// With every member having their own queue of updates, we avoid collisions/overwrites
//
// 1. global[L_PREFIX+'names'] = JSON({name:, label:, unlabel:})
// 2. global[L_PREFIX+name] = ',-member emails'
// 3. user[L_PREFIX+'membership'] = ';-names'
// NOT SET HERE:
// global[L_PREFIX+name+_+memEmail] = ',-commands'
// user[L_PREFIX+'lastRun'] = integer
// user[L_PREFIX+'trigger'] = triggerID
Logger.log('setConfig ' + new Date());
Logger.log(config);
var globalProperties = PropertiesService.getScriptProperties();
var userProperties = PropertiesService.getUserProperties();
var email = Session.getActiveUser().getEmail();
var finalConfig = {};
var memberships = [];
for (var name in config) {
if (!name) {
continue; //blank name, probably an empty 'new row'
}
var lbl = config[name];
name = name.replace(/[^\w ]/g,'');
finalConfig[name] = {name:name,
label:lbl.label.replace(/[^\/\w ]/g,''),
unlabel:lbl.unlabel.replace(/[^\/\w ]/g,'')};
if (finalConfig[name].label == finalConfig[name].unlabel) {
throw Error("label and unlabel must be different");
}
var labelMembershipKey = L_PREFIX + name;
var labelMemberships = propSplit(globalProperties, labelMembershipKey, ',');
var curMemberInd = labelMemberships.indexOf(email);
Logger.log(name + ' __ ' + curMemberInd);
if (lbl.member) {
memberships.push(name);
if (curMemberInd == -1) {
labelMemberships.push(email);
globalProperties.setProperty(labelMembershipKey, labelMemberships.join(',')); //2
}
} else if (curMemberInd >= 0) {
labelMemberships.splice(curMemberInd, 1);
globalProperties.setProperty(labelMembershipKey, labelMemberships.join(',')); //2
}
}
globalProperties.setProperty(L_PREFIX+'names', JSON.stringify(finalConfig)); //1
userProperties.setProperty(L_PREFIX+'membership', memberships.join(';')); //3
Logger.log(finalConfig);
Logger.log(memberships);
return memberships;
}
function getTriggers(userProperties) {
userProperties = userProperties || PropertiesService.getUserProperties();
var triggers = [];
var triggerIds = JSON.parse(userProperties.getProperty(L_PREFIX + 'triggers') || '[]');
ScriptApp.getProjectTriggers().map(function(trigger) {
//OMG! this is ALL the triggers for the script (for all users) -- need to restrict
if (triggerIds.indexOf(trigger.getUniqueId()) != -1) {
triggers.push(trigger);
}
});
return triggers;
}
function isInstalled(triggers) {
triggers = triggers || getTriggers();
return triggers.length != 0;
}
function install(config) {
var userProperties = PropertiesService.getUserProperties();
uninstall();
var triggers = [];
Logger.log('installing triggers ,' + new Date());
triggers.push(ScriptApp.newTrigger('updateUserSharedLabels').timeBased().everyMinutes(UPDATE_MINUTES).create());
if (triggers.length) {
var triggerIds = triggers.map(function(t) {return t.getUniqueId()});
userProperties.setProperty(L_PREFIX + 'triggers', JSON.stringify(triggerIds));
}
Logger.log('installing triggers FINISHED ,' + new Date());
var memberships = setConfig(config);
createOrGetLabels(config, memberships);
return {
triggers: triggers.length
};
}
function uninstall() {
var userProperties = PropertiesService.getUserProperties();
var triggers = getTriggers();
triggers.map(function(userTrigger) {
ScriptApp.deleteTrigger(userTrigger);
//necessary for rate-limiting
Utilities.sleep(1000);
});
userProperties.setProperty(L_PREFIX + 'triggers', '[]');
return {
triggers: 0
};
}
function doGet() {
var t = HtmlService.createTemplateFromFile('ui');
t.installed = isInstalled();
t.synchronization_lag = UPDATE_MINUTES * 2;
var email = Session.getActiveUser().getEmail();
t.logo = '';
if (email) {
var domain = email.split('@')[1];
t.logo = 'https://www.google.com/a/'+domain+'/images/logo.gif?alpha=1&service=google_default';
}
return t.evaluate().setSandboxMode(HtmlService.SandboxMode.NATIVE);
}
function loadPage() {
var globalProperties = PropertiesService.getScriptProperties();
var userProperties = PropertiesService.getUserProperties();
var memberships = propSplit(userProperties, (L_PREFIX + 'membership'), ';');
var sharedLabels = JSON.parse((globalProperties.getProperty(L_PREFIX + 'names')||'{}'));
memberships.map(function(name) {
var lbl = sharedLabels[name];
if (lbl) {
lbl.member = true;
}
});
return {
labels: sharedLabels,
triggers: getTriggers(userProperties).length,
synchronization_lag: UPDATE_MINUTES * 2
};
}
function createOrGetLabels(config, memberships) {
var existingLabels = {};
GmailApp.getUserLabels().map(function(label) {
existingLabels[label.getName()] = label;
});
function getLabel(name) {
return existingLabels[name] || GmailApp.createLabel(name);
};
var labels = {};
memberships.map(function(name) {
labels[name] = {
'label': getLabel(config[name].label),
'unlabel': getLabel(config[name].unlabel)
};
});
return labels;
}
/******************************************************************
* Processing
******************************************************************/
function updateUserSharedLabels() {
var globalProperties = PropertiesService.getScriptProperties();
var userProperties = PropertiesService.getUserProperties();
var lastRun = Number(userProperties.getProperty(L_PREFIX + 'lastRun') || 0);
var userEmail = Session.getActiveUser().getEmail();
Logger.log('running update for ' + userEmail + ' at ' + new Date());
var memberships = propSplit(userProperties, (L_PREFIX + 'membership'), ';');
var config = JSON.parse(globalProperties.getProperty(L_PREFIX + 'names'));
var labels = createOrGetLabels(config, memberships);
memberships.map(function(name) {
updateSharedLabel(name, userEmail, lastRun,
globalProperties, userProperties,
labels[name].label, labels[name].unlabel);
});
// Update timestamp
userProperties.setProperty(L_PREFIX + 'lastRun',
String(Number(new Date())));
Logger.log('finished update for ' + userEmail + ' at ' + new Date());
}
/*
* This is the meat of the whole app, which processes a particular label/unlabel pair
*/
function updateSharedLabel(name, userEmail, lastRun,
globalProperties, userProperties,
label, unlabel) {
// iterate across members' buckets
// parse member's list
// process any list items that are after your last run
// get change list
// reduce updates with last-one winning
// iterate across MY labels (label, unlabel)
// build commands
// find conflicts among unapplied from both
// label command wins over unlabel
var members = propSplit(globalProperties, (L_PREFIX + name), ',');
var msgThreads = {}; //will store <msgId: GmailThread>
var newCommands = [];
var newDecisions = {}; //will store <threadId: [{a||d}, GmailThread]
var userCommands = [];
var pastCommands = [];
var pastDecisions = {};
//BUILD newCommands[], pastCommands[] (and msgThreads)
members.map(function(memEmail) {
var memberCmdList = propSplit(globalProperties, (L_PREFIX + name + '_' + memEmail), ',');
if (memEmail == userEmail) {
//OWN MEMBERSHIP: process differently
userCommands = memberCmdList.slice(); //copy for later
memberCmdList.map(updateProcessor(lastRun, pastCommands, msgThreads));
return;
} else {
//OTHER MEMBERSHIPS: => newDecisions
memberCmdList.map(updateProcessor(lastRun, newCommands, msgThreads, pastCommands));
}
});
//BUILD newDecisions, pastDecisions
var decisionBuilder = function(decisionDict) {
return function(cmd) {
var gmailThread = msgThreads[cmd[2]];
if (gmailThread) {
//by design: might overwrite a previous command with a different result
decisionDict[gmailThread.getId()] = [cmd[1], gmailThread];
}
};
};
newCommands.sort(); //will sort by timestamp, the first elt of the sub-list
newCommands.map(decisionBuilder(newDecisions));
pastCommands.sort();
pastCommands.map(decisionBuilder(pastDecisions));
var myChangedThreads = getChangedThreads(label, unlabel, pastDecisions);
// Who should win? newDecisions or myChangedThreads?
// We are assuming 'shared understanding' rather than contentious labels
// Thus hopefully disagreements may be rare, but if a label
// represents a change in status (i.e. from resolved=>unresolved=>resolved)
// then a conflict may represent an out of sync understanding
// I bias here toward the label, rather than the un-label
// FUTURE:
// Another option could be that we record a conflicted result differently in
// the outputted commands. so there's an (a)dd, (d)elete, (c{ad})onflict
// Then we would also have a 'conflict' label which the script would add to
// conflicts to be resolved by the users.
// Agreement between past and current mean:
// * adding label is unnecessary
// * adding a command is unnecessary
for (var usid in myChangedThreads) {
var us = myChangedThreads[usid];
var them = newDecisions[usid];
if (them) { // in both;
if (them[0] == us[0]) {
//agreement
delete myChangedThreads[usid];
delete newDecisions[usid];
} else {
//disagreement: prefer label
if (them[0] == 'd') {
delete newDecisions[usid];
} else {
delete myChangedThreads[usid];
}
}
}
// we can now clear the unlabel as we log it later
if (us[0] == 'd') {
us[1].removeLabel(unlabel);
us[1].removeLabel(label);
}
}
// Save commands
var timeStamp = Number(new Date());
for (var usid in myChangedThreads) {
var us = myChangedThreads[usid];
var newCmd = getIdsForTracking(us[1]);
newCmd.unshift(timeStamp, us[0]);
userCommands.push(newCmd.join(':'));
}
userCommands = purgeExtraCommands(userCommands);
globalProperties.setProperty(L_PREFIX + name + '_' + userEmail,
userCommands.join(','));
//Possible improvement: if the script fails after this point,
// in theory, if we put our own commands that are after the timestamp
// into newCommands, then we can even rescusitate label updates on
// the next run. I worry a little about circular updating that way, though.
// Update labels
for (var themId in newDecisions) {
var t = newDecisions[themId];
switch (t[0]) {
case 'a':
t[1].addLabel(label);
break;
case 'd':
t[1].removeLabel(label);
break;
}
t[1].removeLabel(unlabel);
}
}
function updateProcessor(lastRun, commandList, msgThreads, fallbackCommandList) {
return function(update) {
var updateComponents = update.split(':');
var timestamp = Number(updateComponents[0]);
if (timestamp > lastRun) {
commandList.push(updateComponents);
} else if (fallbackCommandList) {
fallbackCommandList.push(updateComponents);
}
var thread = false;
// thread Ids are unstable even within a single user's Inbox
// so we map all the message Ids to the same thread
//find a thread
updateComponents.slice(2).map(function(msgId) {
if (msgThreads[msgId]) {
thread = msgThreads[msgId];
} else if (!thread) {
thread = getThreadById(msgId);
}
});
//now setup any new refs
if (thread) {
updateComponents.slice(2).map(function(msgId) {
if (!msgThreads[msgId]) {
msgThreads[msgId] = thread;
}
});
}
};
}
function getChangedThreads(label, unlabel, curStateDecisions) {
var threads = label.getThreads(0, MAX_OLD_THREAD_NUM);
var unthreads = unlabel.getThreads(0, MAX_OLD_THREAD_NUM);
var changedThreads = {};
var ids = {};
threads.map(function(t) {
ids[t.getId()] = t;
});
unthreads.map(function(t) {
//TODO: need to remove from ids or organize into one list
var dtid = t.getId();
delete ids[dtid]; //in case it's there
if (!curStateDecisions[dtid] || curStateDecisions[dtid][0] != 'd') {
changedThreads[dtid] = ['d', t]
}
});
for (var atid in ids) {
if (!curStateDecisions[atid] || curStateDecisions[atid][0] != 'a') {
changedThreads[atid] = ['a', ids[atid]];
}
}
return changedThreads;
}
function getThreadById(msgId) {
//msgId is the subject, because GmailMessage.getId() is not the same across users
var subject = decodeURIComponent(msgId); //
//ASSUME: just search for first one, even if redundant subjects
//IMPROVE: in theory we could make this more rigorous by getting subject+messages[0].id or something
var threads = GmailApp.search('subject:"'+subject+'"', 0, 1);
return threads[0]; //if empty will return undefined
}
function getIdsForTracking(t) {
return [encodeURIComponent(t.getFirstMessageSubject())];
}
function purgeExtraCommands(userCommands) {
var newerCmd = {};
var purgedList = [];
var i = userCommands.length;
while (i--) {
var c = userCommands[i];
var components = c.split(':');
var msgId1 = components[2];
var msgId2 = components[3];
if (newerCmd[msgId2]
|| (msgId2 && newerCmd[msgId2])) {
continue; //newer command already exists
} else {
newerCmd[msgId1] = 1;
if (msgId2) {
newerCmd[msgId2] = 1;
}
purgedList.unshift(c)
}
}
return purgedList.slice(-(MAX_OLD_THREAD_NUM+10))
}