-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCode.gs
More file actions
139 lines (125 loc) · 3.77 KB
/
Copy pathCode.gs
File metadata and controls
139 lines (125 loc) · 3.77 KB
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
/**
* Label recent unlabeled Gmail threads by whether the sender is in
* Google Contacts (People API). Contacts in the "Ignore" group are
* treated as not-in-contacts.
*
* Trigger: time-driven on addLabelIfContactExists.
*/
function addLabelIfContactExists() {
var lock = LockService.getScriptLock();
if (!lock.tryLock(30000)) {
Logger.log('Script is already running. Exiting to prevent overlap.');
return;
}
try {
processEmails();
} finally {
lock.releaseLock();
}
}
function processEmails() {
var threads = GmailApp.search('newer_than:10d has:nouserlabels');
var contactLabel = getOrCreateLabel_('Contact Exists');
var notInContactsLabel = getOrCreateLabel_('Not in Contacts');
var index = buildContactIndex_();
Logger.log(
'Threads=' + threads.length +
' contacts=' + index.emailCount +
' ignored=' + index.ignoredCount
);
for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
var messages = thread.getMessages();
var known = false;
for (var j = 0; j < messages.length; j++) {
var email = extractEmail(messages[j].getFrom()).toLowerCase();
if (!email) {
continue;
}
if (index.ignored[email]) {
continue;
}
if (index.emails[email]) {
known = true;
break;
}
}
thread.addLabel(known ? contactLabel : notInContactsLabel);
}
}
function getOrCreateLabel_(name) {
return GmailApp.getUserLabelByName(name) || GmailApp.createLabel(name);
}
/**
* One pass over Connections + ContactGroups so each run does not
* re-scan the whole address book per message.
*/
function buildContactIndex_() {
var emails = {};
var ignored = {};
var ignoreGroupResourceNames = {};
var groupPageToken = null;
do {
var groupOpts = { pageSize: 100 };
if (groupPageToken) {
groupOpts.pageToken = groupPageToken;
}
var groupResp = People.ContactGroups.list(groupOpts);
var groups = (groupResp && groupResp.contactGroups) || [];
for (var g = 0; g < groups.length; g++) {
var groupName = String(groups[g].name || '').toLowerCase();
if (groupName === 'ignore') {
ignoreGroupResourceNames[groups[g].resourceName] = true;
}
}
groupPageToken = groupResp && groupResp.nextPageToken;
} while (groupPageToken);
var pageToken = null;
do {
var opts = {
personFields: 'emailAddresses,memberships',
pageSize: 1000
};
if (pageToken) {
opts.pageToken = pageToken;
}
var page = People.People.Connections.list('people/me', opts);
var connections = (page && page.connections) || [];
for (var i = 0; i < connections.length; i++) {
var person = connections[i];
var isIgnored = false;
var memberships = person.memberships || [];
for (var m = 0; m < memberships.length; m++) {
var membership = memberships[m].contactGroupMembership;
var resourceName = membership && membership.contactGroupResourceName;
if (resourceName && ignoreGroupResourceNames[resourceName]) {
isIgnored = true;
break;
}
}
var addrs = person.emailAddresses || [];
for (var a = 0; a < addrs.length; a++) {
var value = String(addrs[a].value || '').toLowerCase().trim();
if (!value) {
continue;
}
if (isIgnored) {
ignored[value] = true;
} else {
emails[value] = true;
}
}
}
pageToken = page && page.nextPageToken;
} while (pageToken);
return {
emails: emails,
ignored: ignored,
emailCount: Object.keys(emails).length,
ignoredCount: Object.keys(ignored).length
};
}
function extractEmail(fromField) {
var match = String(fromField || '').match(/<([^>]+)>/);
return (match ? match[1] : String(fromField || '')).trim();
}