-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.js
531 lines (472 loc) · 14.6 KB
/
Code.js
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
let labelNotFoundErrors = 0;
/**
* If a thread has this label, the thread's other labels should be applied to
* future messages from this sender's address.
*/
const LabelLearnAddress = Label("📛/Learn");
/**
* If a thread has this label, the thread's other labels should be applied to
* future messages from this sender's domain.
*/
const LabelLearnDomain = Label("📛/Learn (Domain)");
/**
* If a thread has this label, we should process it in a rate-limited way.
*/
const LabelTodo = Label("📛/Todo");
// /**
// * If a thread has this label, we should process it as soon as possible.
// *
// * @deprecated Not implemented yet.
// */
// const LabelTodoManual = Label("📛/Todo (Manual)");
/**
* If a thread has this label, we should archive it if it is read.
*/
const LabelTodoArchiveWhenRead = Label("📛/Archive If Read");
/**
* The screener will mark processed threads with this label.
*/
const LabelDone = Label("📛/Done");
/**
* Each thread should have at least one "main label", which is a label
* containing this emoji. If a thread doesn't have a main label by the time we
* finish processing it, we'll add the Uncategorized label.
*/
const LabelIconAutoMutex = "🏡";
/**
* Labels with this emoji are non-main labels, but that still should be
* auto-applied.
*/
const LabelIconAuto = "🏷";
/**
* The label that we use to indicate that a thread doesn't have a main label.
*/
const LabelUncategorized = Label("‼️ Uncategorized");
/**
* The label we use to indicate that a thread is from an unknown sender.
*/
const LabelScreenedOut = Label("‼️ Screened Out");
/**
* When applying a label with this emoji, mark the message as read.
*
* Useful for emails where we don't care about the read status.
*/
const LabelIconAutoRead = "📖";
/**
* When applying a label with this emoji, remove it from the inbox.
*
* Useful for emails that we don't want to see by default.
*/
const LabelIconAutoArchive = "📂";
/**
* When applying a label with this emoji, keep it in the inbox, but remove it
* from the Inbox as soon as it is read.
*
* Useful for emails where we don't want to separately track "read" and
* "archived" statuses.
*/
const LabelIconAutoArchiveWhenRead = "📁";
if (labelNotFoundErrors > 0) {
throw new Error("Create these labels manually and try again.");
}
function ScreenEmails() {
ArchiveWhenRead();
for (let thread of GetTodoThreads()) {
log("---------");
/** @type {ThreadScreenState} */
let state = {
thread,
};
getThreadContactAddress(state);
if (getReferenceThread(state)) {
getReferenceLabels(state);
} else {
getToContactThreads(state);
}
updateLabelsAndActionsToApply(state);
// getThreadIsStarred(state);
getThreadIsRead(state);
log(
"thread",
thread.getLastMessageDate(),
state.threadContactAddress,
thread.getFirstMessageSubject()
);
// log(
// "referenceThread",
// state.referenceThread?.getLastMessageDate(),
// state.referenceThread?.getFirstMessageSubject()
// );
// log(
// "referenceLabels",
// state.referenceLabels?.map((x) => x.getName())
// );
log(
"labelsToApply",
state.labelsToApply.mutex.map((x) => x.getName()),
state.labelsToApply.other.map((x) => x.getName())
);
// log("unmergedActions", state.unmergedActions);
log("actionsToApply", state.actionsToApply);
// if (1 === 1) continue;
thread.removeLabel(LabelScreenedOut.object);
thread.removeLabel(LabelUncategorized.object);
applyLabels(state);
applyActions(state);
thread.removeLabel(LabelTodo.object);
thread.addLabel(LabelDone.object);
}
}
/**
* @param {ReturnType<typeof Label>} label
*/
function TallyLabel(label = LabelScreenedOut) {
let addresses = [];
let threads = GmailApp.search(label.query);
for (let thread of threads) {
let sender = thread.getMessages()[0].getFrom();
let senderAddress = StringHelpers.extractEmail(sender);
addresses.push(senderAddress);
}
CollectionHelpers.sortUsingMap(addresses, StringHelpers.reverseDomainEmail);
Logger.log(addresses.join("\n"));
}
function ArchiveWhenRead() {
let threads = GmailApp.search(
`is:read ${LabelTodoArchiveWhenRead.query} -${LabelTodo.query}`
);
log("Archiving read threads:");
for (let thread of threads) {
log("thread", thread.getLastMessageDate(), thread.getFirstMessageSubject());
thread.moveToArchive();
thread.removeLabel(LabelTodoArchiveWhenRead.object);
}
log("---");
}
/**
* @return {GmailThread[]}
*/
function GetTodoThreads() {
return LabelTodo.object.getThreads();
}
/**
* @param {ThreadScreenState} state
* @return {asserts state is S<"threadContactAddress">}
*/
function getThreadContactAddress(state) {
// The reason we use the first message instead of the last message is because
// the last message could just be a forwarded-ish message to keep someone else
// in the loop. It's not representative of who the overall conversation was
// with.
let firstMessage = (state.threadFirstMessage = state.thread.getMessages()[0]);
state.threadFirstMessageIsSent = GmailHelpers.isMessageFromMe(firstMessage);
state.threadContactAddress = GmailHelpers.isMessageFromMe(firstMessage)
? StringHelpers.extractEmail(firstMessage.getTo().split(",")[0])
: StringHelpers.extractEmail(firstMessage.getFrom());
}
/**
* @param {S} state
* @return {asserts state is S<"threadLabels">}
*/
function getThreadLabels(state) {
state.threadLabels = state.thread.getLabels();
}
/**
* @param {S<"threadContactAddress">} state
* @return {state is S<"referenceThread">}
*/
function getReferenceThread(state) {
let query = `${LabelLearnAddress.query} to:${state.threadContactAddress} OR from:${state.threadContactAddress}`;
console.log("query", query);
state.referenceThreads = GmailApp.search(query, 0, 2);
state.referenceThread = state.referenceThreads[0];
if (state.referenceThread == null) {
let domain = state.threadContactAddress
.replace(/^.*@/, "")
// Emails from Credit Karma always come from a different domain every
// time. I can't tell if they've set it up this way to make their emails
// intentionally hard to filter. Some examples:
// - [email protected]
// - [email protected]
// - [email protected]
// - [email protected]
// - [email protected]
//
// The following filter will turn these domains into just
// "creditkarma.com". I originally considering being generic and targeting
// all domains of the form aaaaa11.*.*, but that could result in false
// positives, like area120.google.com.
.replace(/[a-z]+[0-9]+\.(creditkarma\.com)$/i, "$1");
// The naive way to write the query would be to follow the same format as
// the one for exact matching. However, a problem arises when the incoming
// email is from `gmail.com` or `nyu.edu` or other domains of mine. When
// this happens, a lot of the "Learn"-labeled emails will match the query.
// To avoid false positives, we apply a stricter filter.
query = `${LabelLearnDomain.query} -is:sent from:${domain}`;
console.log("query", query);
state.referenceThreads = GmailApp.search(query, 0, 2);
state.referenceThread = state.referenceThreads[0];
// If the current thread is from google.com, we want to look for a reference
// thread from specifically google.com, and not txt.voice.google.com or any
// other subdomain. However, there is no way to construct such a query.
// Instead, we'll just check whether the reference thread matches the domain
// exactly, and send it to Uncategorized/Screened Out if it doesn't.
//
// The user can work around this issue by making sure the reference thread
// for google.com is newer than the reference thread for
// txt.voice.google.com.
if (state.referenceThread) {
let from = state.referenceThread.getMessages()[0].getFrom();
let fromDomain = StringHelpers.extractEmail(from).replace(/^.*@/, "");
if (fromDomain !== domain) {
state.referenceThread = undefined;
}
}
}
return state.referenceThread != null;
}
/**
* @param {S<"referenceThread">} state
* @return {asserts state is S<"referenceLabels">}
*/
function getReferenceLabels(state) {
state.referenceLabels = state.referenceThread.getLabels();
}
/**
* @param {S} state
* @return {asserts state is S<"toContactThreads">}
*/
function getToContactThreads(state) {
let query = `to:${state.threadContactAddress}`;
state.toContactThreads = GmailApp.search(query, 0, 2);
}
/**
* @param {S<"referenceLabels"> | S<"toContactThreads">} state
* @return {asserts state is S<"labelsToApply" | "actionsToApply">}
*/
function updateLabelsAndActionsToApply(state) {
(state.labelsToApply = {
mutex: [],
other: [],
}),
(state.unmergedActions = []);
if (state.referenceLabels != null) {
for (let label of state.referenceLabels) {
let name = label.getName(); //.replace(/.*\//, "");
if (name.indexOf(LabelIconAutoMutex) !== -1) {
state.labelsToApply.mutex.push(label);
state.unmergedActions.push({
read: name.indexOf(LabelIconAutoRead) !== -1 ? "Immediately" : false,
archive:
name.indexOf(LabelIconAutoArchive) !== -1
? "Immediately"
: name.indexOf(LabelIconAutoArchiveWhenRead) !== -1
? "WhenRead"
: false,
});
} else if (name.indexOf(LabelIconAuto) !== -1) {
state.labelsToApply.other.push(label);
}
}
}
if (state.labelsToApply.mutex.length === 0) {
getThreadLabels(state);
let alreadyHasMutex = false;
for (let label of state.threadLabels) {
let name = label.getName(); //.replace(/.*\//, "");
if (name.indexOf(LabelIconAutoMutex) !== -1) {
alreadyHasMutex = true;
break;
}
}
if (!alreadyHasMutex) {
if (
state.toContactThreads != null &&
state.toContactThreads.length === 0
) {
state.labelsToApply.mutex.push(LabelScreenedOut.object);
state.unmergedActions.push({
read: false,
archive: "Immediately",
});
} else {
state.labelsToApply.mutex.push(LabelUncategorized.object);
state.unmergedActions.push({
read: false,
archive: false,
});
}
}
}
state.actionsToApply = {
read: CollectionHelpers.maxInList(
state.unmergedActions.map((actions) => actions.read),
["Immediately", false],
false
),
archive: CollectionHelpers.maxInList(
state.unmergedActions.map((actions) => actions.archive),
["Immediately", "WhenRead", false],
false
),
};
}
/**
* @param {S<"labelsToApply">} state
* @return {asserts state is S}
*/
function applyLabels(state) {
let labels = [...state.labelsToApply.mutex, ...state.labelsToApply.other];
for (let label of labels) {
state.thread.addLabel(label);
}
}
/**
* @param {S} state
* @return {asserts state is S<"threadIsRead">}
*/
function getThreadIsRead(state) {
state.threadIsRead = !state.thread.isUnread();
}
/**
* @param {S} state
* @return {asserts state is S<"threadIsStarred">}
*/
function getThreadIsStarred(state) {
state.threadIsStarred = state.thread.hasStarredMessages();
}
/**
* @param {S<"threadIsStarred" | "threadIsRead" | "labelsToApply" | "actionsToApply">} state
* @return {asserts state is S<never>}
*/
function applyActions(state) {
if (!state.threadIsStarred) {
if (state.actionsToApply.read === "Immediately") {
state.thread.markRead();
}
if (state.actionsToApply.archive === "Immediately") {
state.thread.moveToArchive();
}
if (state.actionsToApply.archive === "WhenRead") {
if (state.threadIsRead) {
state.thread.moveToArchive();
} else {
state.thread.addLabel(LabelTodoArchiveWhenRead.object);
}
}
}
}
/*
* Utils
*/
const log = (/** @type {any[]} */ ...objs) => {
return Logger.log(objs.map((x) => JSON.stringify(x)).join(" "));
};
const StringHelpers = {
/**
* @param {string} email
*/
extractEmail(email) {
let match = email.match(/[^\s<>]+@[^\s<>]+/i);
if (match) {
return match[0];
} else {
return email.split(",")[0];
}
},
/**
* @param {string} email
*/
reverseDomainEmail(email) {
email = email.replace("@", "..");
let parts = email.split(".");
parts.reverse();
return parts.join(".");
},
};
const CollectionHelpers = {
/**
* Example:
*
* let values = ["med", "low"];
* let possibleValuesInOrder = ["none", "low", "med", "hi"];
* maxInList(values, possibleValuesInOrder) => "med"
*
* @template T
* @param {T[]} values
* @param {T[]} possibleValuesInOrder
* @param {T} defaultValue
*/
maxInList(
values,
possibleValuesInOrder,
defaultValue = possibleValuesInOrder[0]
) {
let max = defaultValue;
let maxIndex = -1;
for (let value of values) {
let index = possibleValuesInOrder.indexOf(value);
if (index !== -1 && index > maxIndex) {
max = value;
maxIndex = index;
}
}
return max;
},
/**
* @template T
* @param {T[]} collection
* @param {(item: T) => any} mapper
*/
sortUsingMap(collection, mapper) {
let sorted = collection.sort((a, b) => {
let aValue = mapper(a);
let bValue = mapper(b);
if (aValue < bValue) {
return -1;
} else if (aValue > bValue) {
return 1;
} else {
return 0;
}
});
return sorted;
},
};
function Label(/** @type {string} */ name) {
const object = GmailApp.getUserLabelByName(name);
const query = `label:${name.replace(/ +/g, "-")}`;
if (object == null) {
console.error("Can't find label:", name);
labelNotFoundErrors++;
}
return { name, object, query };
}
const GmailHelpers = {
/**
* @param {string} messageId
*/
getMessageLabelIds(messageId) {
return Gmail.Users?.Messages?.get("me", messageId).labelIds;
},
/**
* Note: This will return false for scheduled messages. Use `isMessageFromMe`
* to check whether the message will eventually be in "Sent".
*
* @param {GmailMessage} message
*/
isMessageInSent(message) {
let messageLabelIds = this.getMessageLabelIds(message.getId());
return new Set(messageLabelIds).has("SENT");
},
/**
* @param {GmailMessage} message
*/
isMessageFromMe(message) {
let id = message.getHeader("message-id");
let results = GmailApp.search(
`from:me -from:*.me rfc822msgid:${JSON.stringify(id)}`
);
return results.length > 0;
},
};