-
Notifications
You must be signed in to change notification settings - Fork 0
/
arkiv.ts
180 lines (160 loc) · 5 KB
/
arkiv.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// https://github.com/mericsson/arkiv
// 2022-11-27
const labelNames = {
archive: 'arkiv.',
allow: 'arkiv.allow',
removeAllow: 'arkiv.removeAllow',
}
// eslint-disable-next-line no-useless-escape
const EMAIL_REG_EXP = /[0-9a-zA-Z\.\+\=\-]+@[0-9a-zA-Z\.\+\=\-]+/g
/**
* This is the main entrypoint for the Google Apps Script.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function cleanInbox(): void {
// User properties data is shared only to current user.
// https://developers.google.com/apps-script/guides/properties
const props = PropertiesService.getUserProperties()
// Index if needed.
const toStart = parseInt(props.getProperty('..start..') || '0')
if (toStart <= 200) {
const indexCount = util.indexTo(props, toStart)
props.setProperty('..start..', `${toStart + indexCount}`)
return
} else {
// Index most recent sent emails.
util.indexTo(props, 0)
}
let archiveLabel = GmailApp.getUserLabelByName(labelNames.archive)
if (!archiveLabel) {
archiveLabel = GmailApp.createLabel(labelNames.archive)
}
// First look at any explict allow-listed emails.
util.processAllowed(props, archiveLabel)
// Then look at attempts to remove from allow-list.
util.processRemoveAllowed(props, archiveLabel)
// Then organize inbox.
// Assumption is inbox has less than 100 threads. Since this is meant
// to run every 5 minutes we need to chunk the work to "roughly 5 min"
// without doing time checking. This means that if it is the first
// time running it could take a while to organize the inbox.
const threads = GmailApp.getInboxThreads(0, 100)
for (const thread of threads) {
if (!util.shouldKeep(props, thread)) {
thread.addLabel(archiveLabel)
thread.moveToArchive()
}
}
}
const util = {
processAllowed: function (
props: GoogleAppsScript.Properties.Properties,
archiveLabel: GoogleAppsScript.Gmail.GmailLabel
) {
let label = GmailApp.getUserLabelByName(labelNames.allow)
if (!label) {
label = GmailApp.createLabel(labelNames.allow)
}
for (const thread of label.getThreads()) {
for (const msg of thread.getMessages()) {
for (const email of util.getEmails(msg.getFrom())) {
util.allowlist(props, email)
}
}
thread.moveToInbox()
thread.removeLabel(archiveLabel)
thread.removeLabel(label)
}
},
processRemoveAllowed: function (
props: GoogleAppsScript.Properties.Properties,
archiveLabel: GoogleAppsScript.Gmail.GmailLabel
) {
let label = GmailApp.getUserLabelByName(labelNames.removeAllow)
if (!label) {
label = GmailApp.createLabel(labelNames.removeAllow)
}
for (const thread of label.getThreads()) {
for (const msg of thread.getMessages()) {
for (const email of util.getEmails(msg.getFrom())) {
util.removeAllow(props, email)
}
}
if (thread.isInInbox()) {
thread.moveToArchive()
}
thread.addLabel(archiveLabel)
thread.removeLabel(label)
}
},
getEmails: function (str: string): string[] {
return str.match(EMAIL_REG_EXP) || []
},
isAllowlisted: function (
props: GoogleAppsScript.Properties.Properties,
email: string
): boolean {
return !!props.getProperty(email.toLowerCase())
},
allowlist: function (
props: GoogleAppsScript.Properties.Properties,
email: string
): void {
props.setProperty(email.toLowerCase(), '1')
},
removeAllow: function (
props: GoogleAppsScript.Properties.Properties,
email: string
): void {
props.deleteProperty(email.toLowerCase())
},
indexTo: function (
props: GoogleAppsScript.Properties.Properties,
start: number
): number {
const indexCount = 10
const threads = GmailApp.search('in:sent', start, indexCount)
// Get set of emails sent `to`.
const toSet = new Set<string>()
for (const thread of threads) {
for (const msg of thread.getMessages()) {
for (const email of util.getEmails(msg.getTo())) {
toSet.add(email)
}
}
}
// Persist emails.
toSet.forEach((to) => {
if (!util.isAllowlisted(props, to)) {
util.allowlist(props, to)
}
})
return indexCount
},
shouldKeep: function (
props: GoogleAppsScript.Properties.Properties,
thread: GoogleAppsScript.Gmail.GmailThread
): boolean {
for (const msg of thread.getMessages()) {
for (const email of util.getEmails(msg.getFrom())) {
if (util.isAllowlisted(props, email)) {
return true
}
}
}
return false
},
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function _viewProperties(): void {
const props = PropertiesService.getUserProperties()
const keys = props.getKeys()
console.log('keys.length', keys.length)
for (const key of keys) {
console.log(key, props.getProperty(key))
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function _deleteProperties(): void {
PropertiesService.getUserProperties().deleteAllProperties()
}