Skip to content

Commit

Permalink
Use expunge instead of compact for IMAP folders
Browse files Browse the repository at this point in the history
When the drafts folder is an IMAP folder, expunge the IMAP folder
instead of compacting the local folder, because the expunge is
actually all we need to work around all the problems that led us to
introducing the compact in the first place.
  • Loading branch information
jikamens committed Sep 20, 2024
1 parent bc79585 commit d39bf0d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
58 changes: 27 additions & 31 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,8 @@ const SendLater = {
// to display the old content for a message even though the server has the
// new content; in this case it appears that even the server doesn't show
// the new content, as evidenced by looking at the draft on mail.google.com.
// Compacting the Drafts folder after saving the message seems to solve
// this.
SendLater.addToDraftsToCompact(msg.folder, true);
// Cleaning the Drafts folder after saving the message seems to solve this.
SendLater.addToDraftsToClean(msg.folder, true);

let targetFolder = await SLTools.getTargetSubfolder(preferences, msg);
if (targetFolder) {
Expand All @@ -409,10 +408,10 @@ const SendLater = {
// error.
msg.id = null;
msg.folder = targetFolder;
SendLater.addToDraftsToCompact(msg.folder, true);
SendLater.addToDraftsToClean(msg.folder, true);
}

SendLater.compactDrafts();
SendLater.cleanDrafts();

if (preferences.ignoredAccounts && preferences.ignoredAccounts.length) {
let identity = await messenger.identities.get(composeDetails.identityId);
Expand Down Expand Up @@ -481,42 +480,39 @@ const SendLater = {
return true;
},

draftsToCompact: [],
draftsToClean: [],

addToDraftsToCompact(folder, force) {
addToDraftsToClean(folder, force) {
if (
!SendLater.draftsToCompact.some(
!SendLater.draftsToClean.some(
(f) => f.accountId == folder.accountId && f.path == folder.path,
)
) {
SLStatic.debug("Adding folder to draftsToCompact:", folder);
SendLater.draftsToCompact.push(folder);
SLStatic.debug("Adding folder to draftsToClean:", folder);
SendLater.draftsToClean.push(folder);
} else {
SLStatic.debug("Compact is already queued for:", folder);
SLStatic.debug("Clean is already queued for:", folder);
}
if (force) SendLater.draftsToCompact.slforce = true;
if (force) SendLater.draftsToClean.slforce = true;
},

async compactDrafts() {
if (!SendLater.draftsToCompact.length) return;
if (
SendLater.draftsToCompact.slforce ||
SendLater.prefCache.compactDrafts
) {
await messenger.SL3U.waitUntilIdle(SendLater.draftsToCompact);
for (let folder of SendLater.draftsToCompact) {
SLStatic.debug("Compacting folder:", folder);
await messenger.SL3U.compactFolder(folder);
async cleanDrafts() {
if (!SendLater.draftsToClean.length) return;
if (SendLater.draftsToClean.slforce || SendLater.prefCache.compactDrafts) {
await messenger.SL3U.waitUntilIdle(SendLater.draftsToClean);
for (let folder of SendLater.draftsToClean) {
SLStatic.debug("Cleaning folder:", folder);
await messenger.SL3U.expungeOrCompactFolder(folder);
}
SendLater.draftsToCompact.slforce = false;
SendLater.draftsToClean.slforce = false;
} else {
SLStatic.debug("Not compacting folders, preference is disabled");
SLStatic.debug("Not cleaning folders, preference is disabled");
}
SendLater.draftsToCompact.length = 0;
SendLater.draftsToClean.length = 0;
},

async deleteMessage(hdr) {
SendLater.addToDraftsToCompact(hdr.folder);
SendLater.addToDraftsToClean(hdr.folder);
let account = await messenger.accounts.get(hdr.folder.accountId, false);
let accountType = account.type;
let succeeded;
Expand Down Expand Up @@ -2788,12 +2784,12 @@ async function mainLoop() {
}

try {
// We do this compact at both the beginning and end of the main loop
// because at the beginning there may be compacting needed as a result of
// We do this clean at both the beginning and end of the main loop
// because at the beginning there may be cleaning needed as a result of
// messages that were edited or scheduled in the interim, and at the end
// there may be compacting needed as a result of messages sent and/or
// there may be cleaning needed as a result of messages sent and/or
// rescheduled during the main loop.
await SendLater.compactDrafts();
await SendLater.cleanDrafts();

let preferences = await SLTools.getPrefs();
let interval = preferences.checkTimePref || 0;
Expand Down Expand Up @@ -2849,7 +2845,7 @@ async function mainLoop() {
nActive,
);

await SendLater.compactDrafts();
await SendLater.cleanDrafts();
setDeferred("mainLoop", 60000 * interval, mainLoop);
SLStatic.debug(
`Next main loop iteration in ${60 * interval} seconds.`,
Expand Down
13 changes: 10 additions & 3 deletions experiments/sl3u.js
Original file line number Diff line number Diff line change
Expand Up @@ -835,14 +835,21 @@ var SL3U = class extends ExtensionCommon.ExtensionAPI {
if (waited) SendLaterFunctions.debug("Finished waiting until idle");
},

// Compact a folder
async compactFolder({ accountId, path }) {
// Expunge or compact a folder
async expungeOrCompactFolder({ accountId, path }) {
let uri = SendLaterFunctions.folderPathToURI(accountId, path);
let folder = MailServices.folderLookup.getFolderForURL(uri);
let msgWindow = Cc["@mozilla.org/messenger/msgwindow;1"]
.createInstance()
.QueryInterface(Ci.nsIMsgWindow);
folder.compact(null, msgWindow);
try {
folder
.QueryInterface(Ci.nsIMsgImapMailFolder)
.expunge(null, msgWindow);
} catch {
SendLaterFunctions.debug("expunge failed, calling compact");
folder.compact(null, msgWindow);
}
},

// Saves raw message content in specified folder.
Expand Down
4 changes: 2 additions & 2 deletions experiments/sl3u.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@
]
},
{
"name": "compactFolder",
"name": "expungeOrCompactFolder",
"type": "function",
"async": true,
"description": "Compact a folder",
"description": "Expunge (IMAP) or compact a folder",
"parameters": [
{
"name": "folder",
Expand Down

0 comments on commit d39bf0d

Please sign in to comment.