forked from webdigi/GmailScheduler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTriggers.gs
151 lines (118 loc) · 4.71 KB
/
Triggers.gs
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
//Refer to troubleshooting #1 to see how to remove any error messages
function createTriggers() {
deleteTriggers();
var timerTrigger = ScriptApp.newTrigger("processTimer")
.timeBased()
.everyMinutes(1)
.create();
var queueTrigger = ScriptApp.newTrigger("processQueue")
.timeBased()
.everyMinutes(1)
.create();
}
function deleteTriggers() {
var allTriggers = ScriptApp.getProjectTriggers();
// Loop over all triggers
for (var i = 0; i < allTriggers.length; i++) {
ScriptApp.deleteTrigger(allTriggers[i]);
}
}
function processTimer() {
debug('processTimer Activated '+new Date().toString());
var queueLabel = SCHEDULER_LABEL+'/'+SCHEDULER_QUEUE_LABEL;
var queueLabelObject = serviceGetUserLabelByName(queueLabel);
var timerChildLabels = getUserChildLabels(SCHEDULER_LABEL+'/'+SCHEDULER_TIMER_LABEL);
for(var i=0; i<timerChildLabels.length; i++){
var timerChildLabelObject, queueLabelObject, page;
var date = parseDate(timerChildLabels[i]);
if (date == null) {
continue;
}
var queueChildLabel = SCHEDULER_LABEL+'/'+SCHEDULER_QUEUE_LABEL+'/'+date.full();
timerChildLabelObject = serviceGetUserLabelByName(SCHEDULER_LABEL+'/'+SCHEDULER_TIMER_LABEL+'/'+timerChildLabels[i]);
page = null;
// Get threads in "pages" of 100 at a time
while(!page || page.length == 100) {
Utilities.sleep(1000);
page = timerChildLabelObject.getThreads(0, 100);
if (page.length > 0) {
createLabel(queueChildLabel);
queueChildLabelObject = serviceGetUserLabelByName(queueChildLabel);
if (queueChildLabelObject) {
queueLabelObject.addToThreads(page);
// Move the threads into queueChildLabel
queueChildLabelObject.addToThreads(page);
}
// Move the threads out of timerLabel
timerChildLabelObject.removeFromThreads(page);
}
}
}
}
function processQueue(){
debug('processQueue Activated '+new Date().toString());
var userPrefs = getUserPrefs(false);
var queueLabel = SCHEDULER_LABEL+'/'+SCHEDULER_QUEUE_LABEL;
var queueLabelObject = serviceGetUserLabelByName(queueLabel);
var queueChildLabels = getUserChildLabels(SCHEDULER_LABEL+'/'+SCHEDULER_QUEUE_LABEL);
for(var i=0; i<queueChildLabels.length; i++){
var currentDate = convertToUserDate(new Date());
var queueChildDate = parseDate(queueChildLabels[i]);
//skip if queuedatetime is not ready to process
if (currentDate.getTime() < queueChildDate.getTime()) {
debug('process later');
continue;
}
var queueChildLabel = SCHEDULER_LABEL+'/'+SCHEDULER_QUEUE_LABEL+'/'+queueChildLabels[i];
var queueChildLabelObject = serviceGetUserLabelByName(queueChildLabel);
var threads = queueChildLabelObject.getThreads();
var message;
var draftId;
//Remove queue child label if nothing to process
if(threads.length==0){
deleteLabel(queueChildLabel);
}
for (var x in threads) {
var thread = threads[x];
var y=0;
for (y=0; y<thread.getMessageCount(); y++)
{
if( GmailApp.getMessageById(threads[x].getMessages()[y].getId()).isDraft() ){
message = GmailApp.getMessageById(threads[x].getMessages()[y].getId());
//Logger.log(y+"-------------------\n");
//Logger.log("MessageIsDraft:"+message.getId());
//Logger.log(y+"\n-------------------.");
}
}
try{
message.isDraft();
}catch (e){
message = GmailApp.getMessageById(threads[x].getMessages()[0].getId());
}
var draftsList = GmailApp.getDrafts();
for (var i = 0; i < draftsList.length; i++) {
if(message.getId() == draftsList[i].getMessageId())
{
draftId=draftsList[i].getId();
}
}
//Logger.log("MessageIsDraft:"+message.isDraft()+"\nMessageBody:"+message.getPlainBody());
//Logger.log("\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
// return;
if(message.isDraft()){
forwardDraft(draftId);
//Logger.log("\nUSED forwardDraftFunction");
//Logger.log("\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
thread.removeLabel(queueLabelObject);
thread.removeLabel(queueChildLabelObject);
}else{
thread.removeLabel(queueLabelObject);
thread.removeLabel(queueChildLabelObject);
if(userPrefs['mark_sent_messages_inbox_unread']){
GmailApp.markMessageUnread(message);
//Logger.log("IF mark sent messages inbox unread");
}
}
}
}
}