@remit/imap-worker 0.0.43 → 0.0.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/sent-mailbox.ts +4 -44
package/package.json
CHANGED
package/src/sent-mailbox.ts
CHANGED
|
@@ -1,38 +1,12 @@
|
|
|
1
1
|
import type { MailboxItem } from "@remit/data-ports";
|
|
2
|
+
import { resolveMailboxByLeafName } from "@remit/data-ports/mailbox-name";
|
|
2
3
|
|
|
3
4
|
export type SentMailboxCandidate = Pick<
|
|
4
5
|
MailboxItem,
|
|
5
6
|
"mailboxId" | "fullPath" | "hierarchyDelimiter"
|
|
6
7
|
>;
|
|
7
8
|
|
|
8
|
-
const SENT_FOLDER_NAMES = [
|
|
9
|
-
"sent",
|
|
10
|
-
"sent items",
|
|
11
|
-
"sent messages",
|
|
12
|
-
"sent mail",
|
|
13
|
-
] as const;
|
|
14
|
-
|
|
15
|
-
// A flat mailbox namespace reports no delimiter at all — ImapFlow gives `""`
|
|
16
|
-
// for a NIL LIST delimiter — and splitting on "" returns single characters, so
|
|
17
|
-
// the path is its own leaf.
|
|
18
|
-
const segments = (mailbox: SentMailboxCandidate): string[] =>
|
|
19
|
-
mailbox.hierarchyDelimiter.length === 0
|
|
20
|
-
? [mailbox.fullPath]
|
|
21
|
-
: mailbox.fullPath.split(mailbox.hierarchyDelimiter);
|
|
22
|
-
|
|
23
|
-
const rank = (mailbox: SentMailboxCandidate): number | null => {
|
|
24
|
-
const parts = segments(mailbox);
|
|
25
|
-
const leaf = parts[parts.length - 1] ?? mailbox.fullPath;
|
|
26
|
-
const nameIndex = SENT_FOLDER_NAMES.indexOf(
|
|
27
|
-
leaf.toLowerCase() as (typeof SENT_FOLDER_NAMES)[number],
|
|
28
|
-
);
|
|
29
|
-
if (nameIndex < 0) {
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
// Depth outranks the name: a "Sent" buried under Trash or an archive must
|
|
33
|
-
// never beat the account's real "Sent Items" one level up.
|
|
34
|
-
return parts.length * SENT_FOLDER_NAMES.length + nameIndex;
|
|
35
|
-
};
|
|
9
|
+
const SENT_FOLDER_NAMES = ["sent", "sent items", "sent messages", "sent mail"];
|
|
36
10
|
|
|
37
11
|
/**
|
|
38
12
|
* The Sent folder by conventional name, for servers that advertise no `\Sent`
|
|
@@ -42,19 +16,5 @@ const rank = (mailbox: SentMailboxCandidate): number | null => {
|
|
|
42
16
|
*/
|
|
43
17
|
export const resolveSentMailboxByName = (
|
|
44
18
|
mailboxes: SentMailboxCandidate[],
|
|
45
|
-
): SentMailboxCandidate | null =>
|
|
46
|
-
|
|
47
|
-
for (const mailbox of mailboxes) {
|
|
48
|
-
const candidate = rank(mailbox);
|
|
49
|
-
if (candidate === null) continue;
|
|
50
|
-
if (
|
|
51
|
-
!best ||
|
|
52
|
-
candidate < best.rank ||
|
|
53
|
-
(candidate === best.rank &&
|
|
54
|
-
mailbox.fullPath.localeCompare(best.mailbox.fullPath) < 0)
|
|
55
|
-
) {
|
|
56
|
-
best = { mailbox, rank: candidate };
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
return best?.mailbox ?? null;
|
|
60
|
-
};
|
|
19
|
+
): SentMailboxCandidate | null =>
|
|
20
|
+
resolveMailboxByLeafName(mailboxes, SENT_FOLDER_NAMES);
|