Skip to content

Commit

Permalink
feat: Added a notification for each operation
Browse files Browse the repository at this point in the history
  • Loading branch information
micz committed Feb 13, 2025
1 parent ea74239 commit 2021279
Show file tree
Hide file tree
Showing 7 changed files with 511 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,24 @@
},
"partialMatchFullPath": {
"message": "Search for keywords within the entire folder path"
},
"operation_action_move": {
"message": "Moved"
},
"operation_action_copy": {
"message": "Copied"
},
"operation_action_tag": {
"message": "Tagged"
},
"operation_message_pluralforms": {
"message": "message;messages",
"description": "List all plural forms for the word 'message', separated by ';', following your language's plural rules. The order must match the language's pluralization pattern: singular first, then all plural variations. Example: 'apple;apples' (English), 'pomme;pommes' (French), 'jabłko;jabłka;jabłek' (Polish), 'تفاح;تفاحة;تفاحتان;تفاحات;تفاحة;تفاح' (Arabic)."
},
"notificationActive": {
"message": "Show a notification for each operation"
},
"Folder": {
"message": "Folder"
}
}
24 changes: 21 additions & 3 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* Portions Copyright (C) Philipp Kewisch */

import { createNotificationText } from "../common/util.js";
import { DEFAULT_PREFERENCES } from "./common/util.js";

const DEFAULT_ACTION_URL = "/popup/popup.html?action=move&allowed=move,copy,goto,tag";

// Manifest v3: this needs to go into state memory or be queried for
Expand Down Expand Up @@ -42,7 +45,7 @@ async function spinWith(func, ...args) {
}

async function processSelectedMessages(folder, operation="move", goToFolder=false) {
let { markAsRead } = await browser.storage.local.get({ markAsRead: true });
let { markAsRead, notificationActive, op_counters } = await browser.storage.local.get({ markAsRead: true, notificationActive: DEFAULT_PREFERENCES.notificationActive, op_counters: DEFAULT_PREFERENCES.op_counters });

let ops = [];

Expand All @@ -53,6 +56,7 @@ async function processSelectedMessages(folder, operation="move", goToFolder=fals

let folderId = folder.id;
let messagePages;
let numMessages = 0;
if (tab.type == "messageDisplay") {
messagePages = [browser.messageDisplay.getDisplayedMessages(tab.id)];
} else if (tab.type == "mail") {
Expand All @@ -67,6 +71,8 @@ async function processSelectedMessages(folder, operation="move", goToFolder=fals

for await (let messages of messagePages) {
let ids = messages.map(message => message.id);
numMessages += messages.length;
op_counters[operation] += messages.length;
let op = Promise.resolve();
if (markAsRead) {
op = op.then(() => Promise.all(ids.map(id => browser.messages.update(id, { read: true }))));
Expand All @@ -81,6 +87,7 @@ async function processSelectedMessages(folder, operation="move", goToFolder=fals
ops.push(op);
}

await browser.storage.local.set({ op_counters }); console.log(">>>>>>>>>> ", JSON.stringify(op_counters));
await Promise.all(ops);

if (majorVersion < 137) {
Expand All @@ -91,14 +98,20 @@ async function processSelectedMessages(folder, operation="move", goToFolder=fals
if (goToFolder) {
await browser.mailTabs.update(tab.id, { displayedFolder: folderId }).catch(() => {});
}

if (operation != "goto" && notificationActive) {
createNotificationText(operation, numMessages, folderId);
}
}
async function applyTags(tag) {
let { markAsRead } = await browser.storage.local.get({ markAsRead: true });

let { markAsRead, notificationActive, op_counters } = await browser.storage.local.get({ markAsRead: true, notificationActive: DEFAULT_PREFERENCES.notificationActive, op_counters: DEFAULT_PREFERENCES.op_counters });
let ops = [];
let numMessages = 0;

for await (let messages of selectedMessagePages()) {
let ids = messages.map(message => message.id);
numMessages += messages.length;
op_counters.tag += messages.length;
ops.push(Promise.all(ids.map(async (id) => {
let msg = await browser.messages.get(id);
let tagset = new Set(msg.tags);
Expand All @@ -114,10 +127,15 @@ async function applyTags(tag) {
if (markAsRead) {
data.read = true;
}
if (notificationActive) {
createNotificationText("tag", numMessages, tag);
}

return browser.messages.update(id, data);
})));
}

await browser.storage.local.set({ op_counters });
await Promise.all(ops);
}

Expand Down
Loading

0 comments on commit 2021279

Please sign in to comment.