forked from eykamp/Folder-Account
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfolderAccount.mjs
111 lines (97 loc) · 3.25 KB
/
folderAccount.mjs
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
export async function checkForMigration() {
const kAlreadyMigrated = "alreadyMigrated";
let results = await browser.storage.local.get(kAlreadyMigrated);
if (kAlreadyMigrated in results) {
return;
}
await browser.storage.local.set({ [kAlreadyMigrated]: true });
const settings = await messenger.FolderAccount.getFolderAccountSettings();
settings.forEach(async (value, key) => {
await browser.storage.local.set({ [key]: value });
});
}
export async function getCustomComposeDetails(details, lastFocusedTabId) {
if (details.type == "draft") {
return {};
}
let folder = (await messenger.mailTabs.get(lastFocusedTabId)).displayedFolder;
if (!folder) {
return {};
}
let [settings] = Object.values(await browser.storage.local.get(folder.id));
// In case of reply, forward and redirect use the folder containing the
// related message, unless it is shown in a saved search folder with
// applicable settings.
if (details.type != "new" && !(folder.isVirtual && settings)) {
const relatedMessage = await messenger.messages
.get(details.relatedMessageId)
.catch((e) => {
console.warn("Could not find related message:", e);
});
if (relatedMessage.folder) {
folder = relatedMessage.folder;
}
}
if (!settings) {
const parentFolders = await messenger.folders.getParentFolders(folder.id);
for (let parentFolder of parentFolders) {
[settings] = Object.values(
await browser.storage.local.get(parentFolder.id)
);
if (settings) {
break;
}
}
}
if (!settings) {
return {};
}
let newDetails = {};
// Do NOT overwrite 'To:' address if the message is new and already has one:
// The user probably selected an address from the address book and wants to
// use that one.
// Only set the 'To:' address on a new message. For forward, or reply-all,
// more likely than not the user will want to use a non-default To: address.
if (details.type == "new" && details.to.length == 0 && settings.to?.length) {
newDetails.to = settings.to;
}
// 'Reply-To:' Set everytime (by Jakob).
if (
(details.type == "new" || settings.replyToOnReplyForward) &&
details.replyTo.length == 0 &&
settings.replyTo?.length
) {
newDetails.replyTo = settings.replyTo;
}
// Set 'CC:' for replies.
if (
details.type == "reply" &&
details.cc.length == 0 &&
settings.to?.length &&
settings.addToCcOnReply
) {
newDetails.cc = settings.to;
}
// 'From:' Make sure we are using the desired identity. No override if this
// is a reply or reply-all and overrideReturnAddress is true.
// Assume the user always wants the default 'From:', for all sorts of
// messages, unless they have specified one.
if (
settings.identityId &&
!(details.type == "reply" && settings.overrideReturnAddress)
) {
newDetails.identityId = settings.identityId;
}
newDetails.isModified = false;
return newDetails;
}
export async function updateSettings(originalFolder, renamedFolder) {
const [settings] = Object.values(
await browser.storage.local.get(originalFolder.id)
);
if (!settings) {
return;
}
await browser.storage.local.set({ [renamedFolder.id]: settings });
await browser.storage.local.remove(originalFolder.id);
}