-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdont-forget-to-retry.js
More file actions
90 lines (72 loc) · 2.27 KB
/
dont-forget-to-retry.js
File metadata and controls
90 lines (72 loc) · 2.27 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
function main(){
/*
* You can configure this block
*/
var gs_request = "GS-Request";
var gs_request_ok = "GS-RequestOK";
var gs_request_pending = "GS-RequestPending";
var gs_request_forget = "GS-RequestForget";
/*
* Don't touch this
*/
var label = GmailApp.getUserLabelByName(gs_request);
var labelOK = GmailApp.getUserLabelByName(gs_request_ok);
if(!labelOK)
labelOK = GmailApp.createLabel(gs_request_ok);
var labelPending = GmailApp.getUserLabelByName(gs_request_pending);
if(!labelPending)
labelPending = GmailApp.createLabel(gs_request_pending);
var labelForget = GmailApp.getUserLabelByName(gs_request_forget);
if(!labelForget)
labelForget = GmailApp.createLabel(gs_request_forget);
var threads = label.getThreads();
var thread = null;
for (var i = 0; i < threads.length; i++) {
thread = threads[i];
//Was this thread already processed? i.e: labeled as 'OK'
var isOK = false;
var threadLabels = thread.getLabels();
for ( var l = 0 ; l < threadLabels.length ; l ++ ){
if ( threadLabels[l].getName() == labelOK.getName() )
isOK = true;
}
if(isOK)
continue;
var messages = thread.getMessages();
//Was I the original sender?
var firstMessage = messages[0];
if ( !amITheSender(firstMessage.getFrom()) ) {
//I'm not the original sender, so I'm not interested in tracking this thread.
continue;
}
//Did anyone else replied?
var replied = false;
for ( var j = 0 ; j < messages.length ; j ++)
{
if ( !amITheSender(messages[j].getFrom()) ){
thread.addLabel(labelOK);
thread.removeLabel(labelPending);
thread.removeLabel(labelForget);
replied = true;
break;
}
}
if (replied)
continue;
//Did I try 3 times yet?
if(messages.length >= 3)
{
//We don't want to be a spammer...
thread.addLabel(labelForget);
thread.removeLabel(labelPending);
continue;
}
//Are you still here? So they haven't replied you yet
thread.addLabel(labelPending);
}
}
function amITheSender(from)
{
var fromAddress = from.substring(from.indexOf("<"));
return ( fromAddress == '<' + Session.getActiveUser() + '>' );
}