-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailsnooze.js
58 lines (51 loc) · 1.56 KB
/
mailsnooze.js
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
var MARK_UNREAD = false
, SNOOZE_DAYS = 30
, ADD_UNSNOOZED_LABEL = true
, UNSNOOZED_LABEL = 'Snooze/Unsnoozed'
;
function setup() {
// Create the labels we’ll need for snoozing
GmailApp.createLabel("Snooze");
for (var i = 1; i <= SNOOZE_DAYS; ++i) {
GmailApp.createLabel(getLabelName(i));
}
if ( ADD_UNSNOOZED_LABEL ) {
GmailApp.createLabel( UNSNOOZED_LABEL );
}
}
function getLabelName(i) {
return "Snooze/" + i + " days";
}
function moveSnoozes() {
var oldLabel
, newLabel
, page, pagesize = 25
;
/**
* Get threads in "pages" of {pagesize} at a time
* loop over ever 'snooze day' page, to re-process
* and move mails up accordingly
*/
for (var i = 1; i <= SNOOZE_DAYS; ++i) {
var newLabel = oldLabel
, oldLabel = GmailApp.getUserLabelByName(getLabelName(i))
;
while( ! page || page.length == pagesize ) {
page = oldLabel.getThreads(0, pagesize);
if ( page.length > 0 ) {
if ( newLabel ) {
// Move the threads into "today’s" label
newLabel . addToThreads( page );
}
else {
// Unless it’s time to unsnooze it
GmailApp . moveThreadsToInbox( page );
if ( MARK_UNREAD ) GmailApp . markThreadsUnread( page );
if ( ADD_UNSNOOZED_LABEL ) GmailApp . getUserLabelByName( UNSNOOZED_LABEL ) . addToThreads( page );
}
// Move / remove out of "yesterday’s" label
oldLabel.removeFromThreads( page );
}
}
}
}