-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathSettingsConfig.ts
339 lines (329 loc) · 9.96 KB
/
SettingsConfig.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
import { Expose, Type, plainToInstance } from "class-transformer"
import "reflect-metadata"
import { essentialObject } from "../utils/ConfigUtils"
import { RequiredDeep } from "../utils/UtilityTypes"
// TODO: Use these constants in SettingsConfig below, when typescript-json-schema bug is resolved.
// See https://github.com/YousefED/typescript-json-schema/issues/336#issuecomment-1528969616
// PR: https://github.com/YousefED/typescript-json-schema/pull/600
export const DEFAULT_SETTING_MAX_BATCH_SIZE = 10
export const DEFAULT_SETTING_MAX_RUNTIME = 280
export const DEFAULT_SETTING_SLEEP_TIME_THREADS = 100
export const LOG_MESSAGE_NAME = "log.message"
export const LOG_LEVEL_NAME = "log.level"
export const LOG_LOCATION_NAME = "log.location"
/** Levels of log messages used for marking and filtering. */
export enum LogLevel {
/** Log level for execution tracing */
TRACE = "trace",
/** Log level for debugging messages. */
DEBUG = "debug",
/** Log level for info messages. */
INFO = "info",
/** Log level for warning messages. */
WARN = "warn",
/** Log level for error messages. */
ERROR = "error",
}
/**
* The method to mark processed threads/messages/attachments.
*/
export enum MarkProcessedMethod {
/**
* Adds the label defined in the setting `markProcessedLabel` to each processed thread.
*
* **NOTE:**
* - Automatically appends the action `thread.addLabel` to the list of global thread actions
* - Automatically appends `-label:<markProcessedLabel>` to the global thread match query config
*
* **Limitations:**
* - It cannot handle multiple messages per thread properly.
*/
ADD_THREAD_LABEL = "add-label",
/**
* Doesn't do anything to mark threads, messages or attachments as processed and leaves this task to the user.
*
* **NOTE:**
* - Use actions on the desired level (threads, messages or attachments) to mark them as processed.
* - Take care to exclude them from queries in the thread match config, to prevent re-processing over and over again.
*
* **Limitations:**
* - Is more complex since you have to take care to
*/
CUSTOM = "custom",
/**
* Marks processed messages as read, which is more flexible than adding a thread label.
* But it comes at the cost of marking messages as read, which may not be expected.
*
* **NOTE:**
* - Automatically appends the action `message.markRead` to the list of global message actions
* - Automatically appends `-is:read` to the global thread match query config
* - Automatically adds `is: ["unread"]` to the global message match config
*
* **Limitations:**
* - Since it marks messages as read it may not be applicable in all cases.
*/
MARK_MESSAGE_READ = "mark-read",
// /**
// * (Experimental) Adds labels to threads that track processed threads, messages or attachments.
// * This is the most flexible method, since it can track each state in a label.
// */
// ADD_LABEL_METADATA = "add-label-metadata",
}
/**
* Represents the context-dependant value mapping.
*/
export class LogFieldContextConfig {
/** The value to be used for attachment context. Supports placeholder substitution. */
attachment?: string
/** The value to be used for environment context. Supports placeholder substitution. */
env?: string
/** The value to be used for message context. Supports placeholder substitution. */
message?: string
/** The value to be used for processing context. Supports placeholder substitution. */
proc?: string
/** The value to be used for thread context. Supports placeholder substitution. */
thread?: string
}
/**
* Represents a log field configuration.
*/
export class LogFieldConfig {
/** The name of the log field that can be referenced from the list of log fields. */
name: string = ""
/** The title of the log field that is used as the headline of the log sheet. */
title: string = ""
/** The value of the log field. Supports placeholder substitution. */
value?: string = undefined
/** The context-dependent values. It allows different values depending on the context type. */
ctxValues?: LogFieldContextConfig = {}
}
/**
* Specifies how sensitive data should be redacted for logging.
*/
export enum LogRedactionMode {
/** Do not redact sensitive data at all. */
NONE = "none",
/** Automatically detect sensitive data to be redacted */
AUTO = "auto",
/** Redact all possibly sensitive data */
ALL = "all",
}
/**
* Represents a settings config that affect the way GmailProcessor works.
*/
export class SettingsConfig {
/**
* Default format string for timestamp formatting.
* See [date-fns format strings](https://date-fns.org/docs/format).
*/
defaultTimestampFormat? = "yyyy-MM-dd HH:mm:ss"
/**
* Default separator to be used when joining arrays in string substitution.
*/
defaultArrayJoinSeparator? = ","
/**
* Location of the spreadsheet log file. Enables logging to a spreadsheet if not empty.
* Example: `GmailProcessor/logsheet-{{date.now|formatDate('yyyy-MM')}}`
*/
@Expose()
logSheetLocation? = ""
/**
* The list of field names to be used for log sheet logging.
* All context placeholder names can be referenced as well as special fields defined for logging.
*/
@Expose()
logFields?: string[] = [
"log.timestamp",
"log.level",
"log.location",
"log.message",
"object.id",
"object.date",
"object.subject",
"object.from",
"object.url",
"attachment.name",
"attachment.size",
"attachment.contentType",
"stored.location",
"stored.url",
"stored.downloadUrl",
]
/**
* Defines additional fields that can be used in addition to the built-in ones for log sheet logging.
*/
@Expose()
@Type(() => LogFieldConfig)
logConfig?: LogFieldConfig[] = [
{
name: "log.timestamp",
title: "Timestamp",
value: "{{date.now|formatDate('yyyy-MM-dd HH:mm:ss.SSS')}}",
},
{
name: "log.level",
title: "Log Level",
},
{
name: "log.message",
title: "Log Message",
},
{
name: "context.type",
title: "Context Type",
},
{
name: "object.id",
title: "ID",
ctxValues: {
attachment: "{{attachment.hash}}",
message: "{{message.id}}",
thread: "{{thread.id}}",
},
},
{
name: "object.url",
title: "GMail URL",
ctxValues: {
attachment: "{{message.url}}",
message: "{{message.url}}",
thread: "{{thread.url}}",
},
},
{
name: "object.date",
title: "Message Date",
ctxValues: {
attachment: "{{message.date}}",
message: "{{message.date}}",
thread: "{{thread.lastMessageDate}}",
},
},
{
name: "object.subject",
title: "Subject",
ctxValues: {
attachment: "{{message.subject}}",
message: "{{message.subject}}",
thread: "{{thread.firstMessageSubject}}",
},
},
{
name: "object.from",
title: "From",
ctxValues: {
attachment: "{{message.from}}",
message: "{{message.from}}",
},
},
{
name: "attachment.name",
title: "Attachment Name",
},
{
name: "attachment.contentType",
title: "Content Type",
},
{
name: "attachment.size",
title: "Attachment Size",
},
{
name: "stored.location",
title: "Stored Location",
ctxValues: {
attachment: "{{attachment.stored.location}}",
},
},
{
name: "stored.url",
title: "Stored URL",
ctxValues: {
attachment: "{{attachment.stored.url}}",
},
},
{
name: "stored.downloadUrl",
title: "Download URL",
ctxValues: {
attachment: "{{attachment.stored.downloadUrl}}",
},
},
]
/**
* Filter logs to given level or higher.
*/
logLevel? = LogLevel.INFO
/**
* Specifies how sensitive information should be redacted for logging.
*/
logSensitiveRedactionMode? = LogRedactionMode.AUTO
/**
* Enables trace logging into the logsheet.
* This automatically loggs useful information for debugging without placing `global.sheetLog`
*/
logSheetTracing?: boolean = false
/**
* The maximum batch size of threads to process in a single run to respect Google processing limits
*/
@Expose()
maxBatchSize? = 10
/**
* The maximum runtime in seconds for a single run to respect Google processing limits
*/
@Expose()
maxRuntime? = 280
/**
* The label to be added to processed GMail threads (only for markProcessedMode="label").
*/
@Expose()
markProcessedLabel? = ""
/**
* The method to mark processed threads/messages.
*/
@Expose()
markProcessedMethod?: MarkProcessedMethod =
MarkProcessedMethod.MARK_MESSAGE_READ
/**
* The sleep time in milliseconds between processing each thread
*/
@Expose()
sleepTimeThreads? = 100
/**
* The sleep time in milliseconds between processing each message
*/
@Expose()
sleepTimeMessages? = 0
/**
* The sleep time in milliseconds between processing each attachment
*/
@Expose()
sleepTimeAttachments? = 0
/**
* The timezone to be used for date/time operations.
* Value `default` uses the <a href="https://developers.google.com/apps-script/reference/base/session#getscripttimezone">script timezone</a>.
* @deprecated Timezone should be set in project settings or `appscript.json` of Google Apps Script instead. Will be removed in the future.
*/
@Expose()
timezone?: string = "default"
}
type RequiredSettingsConfig = RequiredDeep<SettingsConfig>
export function newSettingsConfig(
json: SettingsConfig = {},
): RequiredSettingsConfig {
return plainToInstance(SettingsConfig, json, {
exposeDefaultValues: true,
exposeUnsetFields: false,
}) as RequiredSettingsConfig
}
export function essentialSettingsConfig(
config: SettingsConfig,
): SettingsConfig {
config = essentialObject(
config,
newSettingsConfig(),
{},
["markProcessedMethod"], // TODO: Extract required properties from class definition
)
return config
}