0
votes

Hello Im trying to setup EVERY-n-week events for gmail-snooze thus it would be reocurring events not just one time.

I would like to save everyXweek labeled-threads into "newpage" from the already created Xweek labeled-threads in "page" array. I used page = oldLabel.getThreads(0, 100); to grab the Xweek data.

I tried newpage = everyXweekLabel.page; and newpage = page[everyXweekLabel]; but newpage never gets turned into an array with threads.... so it doesnt work...

How i would like this to work:

  1. threads will have multiple labels.
  2. XweekSnooze is our iteration count down timing label
  3. everyXweekSnooze is our static label to reset the thread once placed in inbox

An example would be thread#1 has 1weekSnooze left (which will be removed on the very next iteration) and it also has an every8weekSnooze label (which will need to apply an 8weekSnooze label once the email is put into inbox)

After the 8weekSnooze label is applied it then iterates (7weekSnooze, 6...) down until it is put into the inbox again where the every8weekSnooze label places another 8weekSnooze label back on to iterate through.

I can make this work for n-weeks but I cannot get the EVERY-n-week part to work since I cannot figure out how to pull everyXweek labels from previously pulled page-thread that contains Xweek labels and other labels as well.

Any help you can extend would be greatly appreciated for sure !

Thanks.

JP

function getLabelNameXWeekSnooze(i) {
    return "Snooze/" + i + "weekSnooze";
}

function getLabelNameEveryXWeekSnooze(i) {
    return "Snooze/" + "every" + i + "weekSnooze";
}

function moveWeekSnoozes() {
    var oldLabel, newLabel, page;
    for (var i = 1; i <= WEEKS_TRACKED; ++i) {
        newLabel = oldLabel;
        oldLabel = GmailApp.getUserLabelByName(getLabelNameXWeekSnooze(i));
        page = null;
        newpage = null;

        // Get threads in "pages" of 100 at a time
        while(!page || page.length == 100) {
            page = oldLabel.getThreads(0, 100);
            if (page.length > 0) {
                if (newLabel) {
                    // Move the threads into "today’s" label
                    newLabel.addToThreads(page);
            } else {
                // Time to Unsnooze
                GmailApp.moveThreadsToInbox(page);
                if (MARK_UNREAD) {
                    GmailApp.markThreadsUnread(page);
                }

                // after returning mail to inbox please create an 
                // XweekSnooze label for every thread in page that has associated everyXweekSnooze label
                // for example every3weekSnooze thread will need 3weekSnooze label to iterate/delay from
                for (var j = 1; j <= WEEKS_TRACKED; ++j) {
                    // format the label data            
                    everyXweekLabel = GmailApp.getUserLabelByName(getLabelNameEveryXWeekSnooze(j));
                    XweekLabel = GmailApp.getUserLabelByName(getLabelNameXWeekSnooze(j));

                    //trying to grab all thread from page that have everyXweek label and save to new page
                    newpage = everyXweekLabel.page;
                    if (newpage.length > 0) {
                        XweekLabel.addToThreads(newpage);
                    }
                }     

                // Move the threads out of "yesterday’s" label
                oldLabel.removeFromThreads(page);
            }
        }
    }
}
1

1 Answers

0
votes

For reference, the original Gmail Snooze algorithm can be found here: Gmail Snooze with Apps Script. The algorithm is based on the idea of percolating a set of Gmail labels on threads, using an Apps Script trigger to handle the timing.

Basically, the problem in your question boils down to the need to find a set of GmailThreads that all have two specified labels (that is, the union of the set of threads that have label A and the set that has label B).

Apps Script doesn't provide a direct way of doing this, so you have to do a bit of work. The approach is straightforward: get the set of threads that have label A, iterate through them to check each for label B, and then record the threads that have both.

In this case, we will use an object to record the threads that have been moved to the inbox but now need to be reset. After adjusting all the other labels, we will have a separate reset step. It's important to do this separately, since if you reset a thread while percolating you will accidentally adjust it one too many times.

function moveSnoozes() {
  var oldLabel, newLabel, page;

  // Set up an empty record of threads that will need
  // to be reset
  var resets = {};
  for (var i = 1; i <= WEEKS_TRACKED; ++i) {
    resets[getLabelNameXWeekSnooze(i)] = [];
  }

  for (var i = 1; i <= WEEKS_TRACKED; ++i) {
    newLabel = oldLabel;
    oldLabel = GmailApp.getUserLabelByName(getLabelNameXWeekSnooze(i));
    page = null;    
    // Get threads in "pages" of 100 at a time
    while(!page || page.length == 100) {
      page = oldLabel.getThreads(0, 100);
      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);
          }

          // Now check for everyXweekSnooze label and 
          // record individual threads that need to be reset
          for(var t=0; t<page.length; t++) {
            var thread = page[t];
            var resetLabel = findResetLabel(thread);
            if(resetLabel) {
              resets[resetLabel.getName()].push(thread);
            }
          }

          if (ADD_UNSNOOZED_LABEL) {
            GmailApp.getUserLabelByName("Unsnoozed")
              .addToThreads(page);
          }          
        }     
        // Move the threads out of "yesterday’s" label
        oldLabel.removeFromThreads(page);
      }  
    }
  }

  // Reset recorded threads
  for (var i = 1; i <= WEEKS_TRACKED; ++i) {
    var resetLabelName = getLabelNameXWeekSnooze(i);
    var resetLabel = GmailApp.getUserLabelByName(resetLabelName);
    resetLabel.addToThreads(resets[resetLabelName]);    
  }
}

// Given a GmailThread, check to see if it has
// any of the everyXweekSnooze labels and return
// the first one found
// @param {Object} thread - GmailThread to examine
// @return {Object} GmailLabel - the first everyXweekSnooze
//    label found, or null if none are found
function findResetLabel(thread) {
  for (var i = WEEKS_TRACKED; i >= 1; i--) {
    var label = getLabelNameEveryXWeekSnooze(i);
    var labels = thread.getLabels();
    for (var j = 0; j < labels.length; j++) {
      if(labels[j].getName() == label) {
        return GmailApp.getUserLabelByName(getLabelNameXWeekSnooze(i));
      }
    }
  }
  return null;
}