This repository has been archived by the owner on Nov 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.ts
executable file
·379 lines (371 loc) · 8.96 KB
/
config.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
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
import * as _ from 'lodash'
import { normalizeLabelName } from './src/google/gmail/sync'
import LabelFilterSync from './src/sync/label-filter'
import { DBRecord } from './src/sync/root'
import { IConfig, IConfigPublic } from './src/types'
import { isTestEnv } from './src/utils'
import moment = require('moment-timezone')
// TODO move functions to /src/sync/label-filter.ts
const hasLabel = function(r: DBRecord, label: string | RegExp): number {
let ret = checkLabel(r, label)
// TODO read aliases from the config
if (label == '!S/Finished') {
ret = ret || checkLabel(r, '!S/Expired')
} else if (label == '!S/Expired') {
ret = ret || checkLabel(r, '!S/Finished')
}
return ret
}
function checkLabel(r: DBRecord, match: string | RegExp): number {
let matches = 0
for (const [label, data] of Object.entries(r.labels)) {
if (!data.active) continue
if (match instanceof RegExp && match.test(label)) {
matches++
} else if (
!(match instanceof RegExp) &&
match.toLowerCase() == label.toLowerCase()
) {
matches++
}
}
return matches
}
// @ts-ignore TODO
const config: IConfigPublic = {
http_port: 8080,
repl_port: 5002,
google: {
scopes: [
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/tasks',
// tests can delete emails
isTestEnv()
? 'https://mail.google.com/'
: 'https://www.googleapis.com/auth/gmail.modify'
]
},
gmail: {
// TODO implement
max_results: 300,
included_labels: [
/^!S\/[\w\s-]+$/,
/^!S$/,
/^!T\/[\w\s-/]+$/,
/^M\/[\w\s-/]+$/,
/^P\/[\w\s-]+$/,
/^R\/[\w\s-]+$/,
/^L\/[\w\s-]+$/,
/^INBOX$/,
/^UNREAD$/
],
domain: 'mail.google.com',
orphans_freq_min: 5,
// per user
request_quota_100: 2_000_000,
request_quota_100_user: 25_000,
// global
request_quota_day: 1_000_000_000
},
// TODO group exception settings
exception_delay: 5,
exception_flood_delay: 10 * 60,
gtasks: {
// per user
request_quota_100_user: 500,
// when to delay based on user quota
request_quota_100_user_cap: 0.25,
// global
request_quota_day: 50_000,
quota_exceeded_delay: 50,
// seconds
sync_frequency: 10 * 60
},
// seconds
sync_frequency: 1,
labels: [
// statuses
{
symbol: '!',
shortcut: 'na',
name: 'Next Action',
prefix: '!S/',
colors: {
bg: '#fb4c2f',
fg: '#ffffff'
}
},
{
symbol: '!',
shortcut: 'a',
name: 'Action',
prefix: '!S/',
colors: {
bg: '#ffad47',
fg: '#ffffff'
}
},
{
symbol: '!',
shortcut: 'p',
name: 'Pending',
prefix: '!S/',
colors: {
bg: '#efa093',
fg: '#000000'
}
},
{
symbol: '!',
shortcut: 'sd',
name: 'Someday',
prefix: '!S/',
colors: {
bg: '#c9daf8',
fg: '#000000'
}
},
// {
// symbol: '!',
// shortcut: 'e',
// name: 'Expired',
// alias: ['!S/Finished'],
// prefix: '!S/',
// colors: {
// bg: '#cccccc',
// // TODO black
// fg: '#ffffff'
// }
// },
{
symbol: '!',
shortcut: 'f',
name: 'Finished',
// alias: ['!S/Expired'],
prefix: '!S/',
// TODO
colors: {
bg: '#e07798',
fg: '#ffffff'
}
},
// global gtd status
{
prefix: '',
name: '!S',
// TODO default gray
// colors: {
// },
hide_list: true
},
// hashtags
{
symbol: '#R-',
prefix: 'R/',
colors: {
bg: '#ffd6a2',
fg: '#000000'
}
},
{
symbol: '#L-',
prefix: 'L/',
// TODO
colors: {
bg: '#b9e4d0',
fg: '#000000'
}
},
{
symbol: '#',
prefix: 'P/',
colors: {
bg: '#a4c2f4',
fg: '#000000'
}
},
// tasks & msgs
{
prefix: '!T/',
name: 'Sync GTasks',
colors: {
bg: '#b9e4d0',
fg: '#000000'
},
hide_menu: true
},
{
prefix: 'M/',
name: 'GTasks/Quota Exceeded',
colors: {
bg: '#b9e4d0',
fg: '#000000'
},
hide_menu: true
}
],
// TODO move those to /src/label-filters na ref the names here
label_filters: [
{
name: 'newest status removes other ones',
// match all records with at least 2 status labels
db_query: r => hasLabel(r, /!S\/.+/) > 1,
modify(this: LabelFilterSync, r: DBRecord) {
let newest: { label: string; date: number } = null
const remove = []
for (const [label, data] of Object.entries(r.labels)) {
if (!data.active || !/!S\/.+/.test(label)) continue
remove.push(label)
if (!newest || newest.date < data.updated) {
newest = {
label,
date: data.updated
}
}
}
return {
remove: _.without(remove, newest.label)
}
}
},
{
name: 'task labels',
db_query: r => Boolean(hasLabel(r, /!T\/.+/)),
modify(this: LabelFilterSync, r: DBRecord) {
const remove = []
const add = new Set<string>()
for (const [label, data] of Object.entries(r.labels)) {
if (!data.active || !/!T\/.+/.test(label)) continue
// remove every task label, even the unmatched ones
remove.push(label)
let match
this.log_verbose(`Matching task label '${label}'`)
// sync gtasks
match = label.match(/\/Sync GTasks$/)
if (match) {
// const name = match[1]
// ? (match[1] as string).replace('/', '').toLowerCase()
// : null
// this.log(`Executing task label '${label}' (${name})`)
this.log(`Executing task label '${label}'`)
// TODO expose as a callable method, merge with App.handleGTasksSync
for (const list of this.root.subs.google.subs.tasks.subs.lists) {
// force refresh
if (list.state.is('QuotaExceeded')) {
add.add('M/GTasks Quota Exceeded')
} else {
list.state.add('Dirty')
}
}
}
}
return {
add: [...add],
remove
}
}
}
],
lists: [
// all the `!T/Task` labels (anywhere, gmail only)
(config: IConfig) => {
const query = config.labels
// @ts-ignore `!T/sync-tasks`
.filter(l => l.prefix == '!T/')
// @ts-ignore `P/name` or `name`
.map(l => `label:${normalizeLabelName((l.prefix || '') + l.name)}`)
.join(' ')
return {
name: 'task-labels',
gmail_query: query,
db_query: r => Boolean(hasLabel(r, /^!T\//)),
enter: {},
exit: {},
writers: ['gmail']
}
},
// query unread self emails in the inbox (gmail only)
(config: IConfig) => ({
name: 'inbox-labels',
gmail_query: `label:inbox label:unread from:${
config.google.username
} to:${config.google.username}`,
db_query: r => {
const ret =
hasLabel(r, 'INBOX') && hasLabel(r, 'UNREAD') && !hasLabel(r, '!S')
if (!ret) return false
// TODO this shouldn't happen here
r.labels['UNREAD'].active = false
r.labels['UNREAD'].updated = parseInt(moment().format('x'), 10)
return ret
},
enter: {},
exit: {
remove: ['UNREAD']
},
writers: ['gmail']
}),
{
name: '!Next',
gmail_query: 'label:!s-next-action',
db_query: r => Boolean(hasLabel(r, '!S/Next Action')),
enter: {
add: ['!S/Next Action'],
remove: ['!S/Finished']
},
exit: {
add: ['!S/Finished'],
remove: ['!S/Next Action']
},
sync_frequency: {
gtasks_multi: 0.5
}
},
{
name: '!Pending',
gmail_query: 'label:!s-pending',
db_query: r => Boolean(hasLabel(r, '!S/Pending')),
enter: {
add: ['!S/Pending'],
remove: ['!S/Finished']
},
exit: {
add: ['!S/Finished'],
remove: ['!S/Pending']
},
sync_frequency: {
gtasks_multi: 1.5
}
},
{
name: '!Actions',
gmail_query: 'label:!s-action',
db_query: r => Boolean(hasLabel(r, '!S/Action')),
enter: {
add: ['!S/Action'],
remove: ['!S/Finished']
},
exit: {
add: ['!S/Finished'],
remove: ['!S/Action']
}
},
{
name: '!Someday',
gmail_query: 'label:!s-some-day',
db_query: r => Boolean(hasLabel(r, '!S/Some day')),
enter: {
add: ['!S/Some day'],
remove: ['!S/Finished']
},
exit: {
add: ['!S/Finished'],
remove: ['!S/Some day']
},
sync_frequency: {
gtasks_multi: 2
}
}
]
}
export default config