Repository navigation
Expand file tree
/
Copy pathThreadData.js
More file actions
360 lines (316 loc) · 9.61 KB
/
Copy pathThreadData.js
File metadata and controls
360 lines (316 loc) · 9.61 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
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
/**
* Thread Data Module
*
* Provides type-safe, validated thread data extraction from GmailApp API.
* Distinct from GmailApp's native GmailThread class - this module extracts and structures data.
*
* Benefits:
* - Centralized thread data extraction
* - Safe access to all thread properties
* - Type documentation via JSDoc
* - Consistent object structure across codebase
* - Bulk thread operations with validation
*
* @module ThreadData
*/
/**
* @typedef {Object} ThreadDataObject
* @property {string} threadId - Unique thread identifier
* @property {string} permalink - Permanent link to thread
*
* @property {string} firstMessageSubject - Subject of first message in thread
* @property {Date} firstMessageDate - Date of first message
* @property {Date} lastMessageDate - Date of most recent message
*
* @property {number} messageCount - Number of messages in thread
* @property {Array<string>} labels - Array of label names
* @property {string} labelsSummary - Comma-separated label list
*
* @property {boolean} isUnread - Has any unread messages
* @property {boolean} isStarred - Has any starred messages
* @property {boolean} isInInbox - In inbox (not archived)
* @property {boolean} isInTrash - In trash
* @property {boolean} isInSpam - In spam folder
* @property {boolean} isImportant - Marked as important
* @property {boolean} hasStarredMessages - Contains starred messages
*/
/**
* Create a validated, type-safe thread data object from GmailApp API
*
* @param {GmailThread|string} threadOrId - GmailThread object or thread ID string
* @return {ThreadDataObject|null} Validated thread data object, or null on error
*
* @example
* var threadData = createThreadData(thread);
* if (threadData) {
* safeLog("Thread", threadData.firstMessageSubject + " (" + threadData.messageCount + " msgs)");
* }
*/
function createThreadData(threadOrId) {
var fetchStart = new Date().getTime();
try {
// Handle both thread object and thread ID
var thread;
if (typeof threadOrId === 'string') {
thread = GmailApp.getThreadById(threadOrId);
if (!thread) {
logErrorWithContext("createThreadData", "Thread not found: " + threadOrId);
return null;
}
} else if (threadOrId && typeof threadOrId === 'object') {
thread = threadOrId;
} else {
logErrorWithContext("createThreadData", "Invalid thread parameter");
return null;
}
// === EXTRACT ALL DATA ===
var threadData = {
// IDs
threadId: safeGetThreadIdFromThread(thread),
permalink: safeGetPermalink(thread),
// Subject & Dates
firstMessageSubject: safeGetFirstMessageSubject(thread),
firstMessageDate: safeGetFirstMessageDate(thread),
lastMessageDate: safeGetLastMessageDate(thread),
// Counts & Labels
messageCount: safeGetMessageCount(thread),
labels: safeGetLabels(thread),
labelsSummary: safeGetLabelsSummary(thread),
// Status flags
isUnread: safeIsThreadUnread(thread),
isStarred: safeHasStarredMessages(thread),
isInInbox: safeIsThreadInInbox(thread),
isInTrash: safeIsThreadInTrash(thread),
isInSpam: safeIsInSpam(thread),
isImportant: safeIsImportant(thread),
hasStarredMessages: safeHasStarredMessages(thread)
};
var duration = new Date().getTime() - fetchStart;
logDataFetch("Fetch Thread Data", threadData.threadId, true, duration);
return threadData;
} catch (error) {
logDataFetch("Fetch Thread Data", "unknown", false, new Date().getTime() - fetchStart);
logErrorWithContext("createThreadData", error);
return null;
}
}
/**
* Create thread data objects for multiple threads
* @param {Array<GmailThread>} threads - Array of GmailThread objects
* @return {Array<ThreadDataObject>} Array of thread data objects (nulls filtered out)
*/
function createMultipleThreadData(threads) {
if (!threads || !Array.isArray(threads)) {
logErrorWithContext("createMultipleThreadData", "Invalid threads array");
return [];
}
return threads.map(function(thread) {
return createThreadData(thread);
}).filter(function(data) {
return data !== null;
});
}
// ===== SAFE EXTRACTION HELPERS =====
function safeGetThreadIdFromThread(thread) {
try {
return thread.getId() || "unknown";
} catch (e) {
return "unknown";
}
}
function safeGetPermalink(thread) {
try {
return thread.getPermalink() || "";
} catch (e) {
return "";
}
}
function safeGetFirstMessageSubject(thread) {
try {
return thread.getFirstMessageSubject() || "(No Subject)";
} catch (e) {
return "(No Subject)";
}
}
function safeGetFirstMessageDate(thread) {
try {
var messages = thread.getMessages();
if (messages && messages.length > 0) {
return messages[0].getDate() || new Date();
}
return new Date();
} catch (e) {
return new Date();
}
}
function safeGetLastMessageDate(thread) {
try {
return thread.getLastMessageDate() || new Date();
} catch (e) {
return new Date();
}
}
function safeGetMessageCount(thread) {
try {
return thread.getMessageCount() || 0;
} catch (e) {
return 0;
}
}
function safeGetLabels(thread) {
try {
var labelObjects = thread.getLabels() || [];
return labelObjects.map(function(label) {
try {
return label.getName() || "unnamed";
} catch (e) {
return "error";
}
});
} catch (e) {
return [];
}
}
function safeGetLabelsSummary(thread) {
try {
var labels = safeGetLabels(thread);
return labels.length > 0 ? labels.join(", ") : "none";
} catch (e) {
return "error";
}
}
function safeIsThreadUnread(thread) {
try {
return thread.isUnread() === true;
} catch (e) {
return false;
}
}
function safeHasStarredMessages(thread) {
try {
return thread.hasStarredMessages() === true;
} catch (e) {
return false;
}
}
function safeIsThreadInInbox(thread) {
try {
return thread.isInInbox() === true;
} catch (e) {
return false;
}
}
function safeIsThreadInTrash(thread) {
try {
return thread.isInTrash() === true;
} catch (e) {
return false;
}
}
function safeIsInSpam(thread) {
try {
return thread.isInSpam() === true;
} catch (e) {
return false;
}
}
function safeIsImportant(thread) {
try {
return thread.isImportant() === true;
} catch (e) {
return false;
}
}
// ===== BULK OPERATIONS =====
/**
* Apply label to multiple threads safely
* @param {Array<GmailThread>} threads - Threads to label
* @param {GmailLabel} label - Label to apply
* @return {Object} {success: number, failed: number}
*/
function applyLabelToThreads(threads, label) {
var results = { success: 0, failed: 0 };
if (!threads || !Array.isArray(threads) || !label) {
logErrorWithContext("applyLabelToThreads", "Invalid parameters");
return results;
}
threads.forEach(function(thread) {
try {
thread.addLabel(label);
results.success++;
} catch (e) {
results.failed++;
logErrorWithContext("applyLabelToThreads", "Failed for thread: " + safeGetThreadIdFromThread(thread));
}
});
logWithContext("BulkOperation", "Applied label to " + results.success + " threads, " + results.failed + " failed");
return results;
}
/**
* Archive multiple threads safely
* @param {Array<GmailThread>} threads - Threads to archive
* @return {Object} {success: number, failed: number}
*/
function archiveThreads(threads) {
var results = { success: 0, failed: 0 };
if (!threads || !Array.isArray(threads)) {
logErrorWithContext("archiveThreads", "Invalid threads parameter");
return results;
}
threads.forEach(function(thread) {
try {
thread.moveToArchive();
results.success++;
} catch (e) {
results.failed++;
logErrorWithContext("archiveThreads", "Failed for thread: " + safeGetThreadIdFromThread(thread));
}
});
logWithContext("BulkOperation", "Archived " + results.success + " threads, " + results.failed + " failed");
return results;
}
/**
* Mark multiple threads as read safely
* @param {Array<GmailThread>} threads - Threads to mark as read
* @return {Object} {success: number, failed: number}
*/
function markThreadsRead(threads) {
var results = { success: 0, failed: 0 };
if (!threads || !Array.isArray(threads)) {
logErrorWithContext("markThreadsRead", "Invalid threads parameter");
return results;
}
threads.forEach(function(thread) {
try {
thread.markRead();
results.success++;
} catch (e) {
results.failed++;
logErrorWithContext("markThreadsRead", "Failed for thread: " + safeGetThreadIdFromThread(thread));
}
});
logWithContext("BulkOperation", "Marked " + results.success + " threads as read, " + results.failed + " failed");
return results;
}
// ===== FORMATTING HELPERS =====
/**
* Format thread data for logging
* @param {ThreadDataObject} threadData - Thread data object
* @return {string} Formatted log string
*/
function formatThreadDataLog(threadData) {
if (!threadData) return "Error: No thread data";
return "Thread: " + threadData.threadId +
" | Subject: " + threadData.firstMessageSubject +
" | Messages: " + threadData.messageCount +
" | First: " + threadData.firstMessageDate +
" | Last: " + threadData.lastMessageDate +
" | Labels: " + threadData.labelsSummary +
" | Unread: " + threadData.isUnread +
" | Starred: " + threadData.isStarred +
" | InInbox: " + threadData.isInInbox +
" | InTrash: " + threadData.isInTrash +
" | InSpam: " + threadData.isInSpam +
" | Important: " + threadData.isImportant +
" | Link: " + threadData.permalink;
}