@wirecat/mail-cli 0.16.3 → 0.18.0
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/CHANGELOG.md +100 -0
- package/README.md +7 -4
- package/dist/bin/mail.js +2 -2
- package/dist/bundle/{all-profiles-TUDREUUW.js → all-profiles-4CVOXA5Y.js} +10 -10
- package/dist/bundle/{anthropic-KQOVSP6C.js → anthropic-5VIKYINO.js} +6 -6
- package/dist/bundle/{app-server-QK7PKVT7.js → app-server-LTSQBDPF.js} +53 -50
- package/dist/bundle/{bun-2NMWL5YB.js → bun-C7DBRUW2.js} +2 -2
- package/dist/bundle/{bun-sqlite-Y4FP6SDX.js → bun-sqlite-Y5JKT62B.js} +5 -5
- package/dist/bundle/{chunk-MLWN27HF.js → chunk-23HWKHPZ.js} +326 -288
- package/dist/bundle/{chunk-4IEDXIUN.js → chunk-2NWOYXPJ.js} +240 -64
- package/dist/bundle/{chunk-YPF6OS37.js → chunk-3DQYO7FB.js} +1 -1
- package/dist/bundle/{chunk-I3K6JG4I.js → chunk-3W5H4TM7.js} +182 -60
- package/dist/bundle/{chunk-CGH6UH3T.js → chunk-3YRE6DNM.js} +8 -8
- package/dist/bundle/{chunk-LMN7SYX4.js → chunk-43I5XD7S.js} +1 -1
- package/dist/bundle/{chunk-KMZRXGTF.js → chunk-5S6Q3SWU.js} +4 -2
- package/dist/bundle/{chunk-XEIGKPYU.js → chunk-6RO5RDNR.js} +50 -3
- package/dist/bundle/{chunk-OYB3FUYQ.js → chunk-7P4ICYJT.js} +1 -1
- package/dist/bundle/{chunk-XBIPSSKI.js → chunk-7SWWVDEZ.js} +4 -4
- package/dist/bundle/{chunk-4IIH5PXR.js → chunk-A5OMARL4.js} +28 -28
- package/dist/bundle/{chunk-ZNW4E3SG.js → chunk-ACLILULA.js} +3 -3
- package/dist/bundle/{chunk-EZIJPXDZ.js → chunk-AOMSP4EI.js} +6 -6
- package/dist/bundle/{chunk-G6RG2AZV.js → chunk-BHNBWFT5.js} +55 -8
- package/dist/bundle/{chunk-FHY664YS.js → chunk-BMFPCW6Q.js} +4 -4
- package/dist/bundle/{chunk-PWTEWT4M.js → chunk-BMGVKULS.js} +4 -4
- package/dist/bundle/{chunk-QCNK3YUZ.js → chunk-BMV56JCL.js} +19 -19
- package/dist/bundle/{chunk-B2NDDA2W.js → chunk-C4RUKMDT.js} +16 -10
- package/dist/bundle/{chunk-XAANQN4R.js → chunk-C5T5K7HW.js} +208 -83
- package/dist/bundle/{chunk-7LMTWOTP.js → chunk-CPEN6BZZ.js} +2 -2
- package/dist/bundle/{chunk-EB2IULWB.js → chunk-DALBYUPC.js} +2 -2
- package/dist/bundle/{chunk-6DVPPLAN.js → chunk-DPSH46NO.js} +5 -5
- package/dist/bundle/{chunk-VTJ5TMGN.js → chunk-EFP5NN7I.js} +3 -3
- package/dist/bundle/{chunk-KJV3BI7S.js → chunk-ERBEBORA.js} +23 -21
- package/dist/bundle/{chunk-RPEW7E3S.js → chunk-FLOOPXOT.js} +12 -12
- package/dist/bundle/{chunk-R6SBRHVG.js → chunk-GLLLYACZ.js} +6 -6
- package/dist/bundle/{chunk-WKHHCCJE.js → chunk-GRP67IQ4.js} +56 -47
- package/dist/bundle/{chunk-5KPPP3QN.js → chunk-HFUS33UT.js} +14 -2
- package/dist/bundle/{chunk-TTOKDZQX.js → chunk-HTFBXGJF.js} +2 -2
- package/dist/bundle/{chunk-N7DLDEFU.js → chunk-IANOK5SE.js} +2 -2
- package/dist/bundle/{chunk-3CVDHJER.js → chunk-ITUC5WAV.js} +1 -1
- package/dist/bundle/{chunk-POS35G6D.js → chunk-JO3BIMYK.js} +3 -3
- package/dist/bundle/{chunk-EBEYU7RW.js → chunk-KVSC657S.js} +44 -143
- package/dist/bundle/{chunk-DDQXDX4U.js → chunk-L3YX7A5M.js} +7 -7
- package/dist/bundle/{chunk-VP5FL5JQ.js → chunk-M7C4IGJ6.js} +8 -8
- package/dist/bundle/{chunk-ZS6YKNYS.js → chunk-MCR43SWG.js} +4 -4
- package/dist/bundle/{chunk-UBLFUQ7O.js → chunk-N5VEJ3E7.js} +97 -12
- package/dist/bundle/{chunk-IJYYD2JU.js → chunk-NDJZQCF3.js} +467 -231
- package/dist/bundle/{chunk-HN3EDDVX.js → chunk-OLJDZTHB.js} +13 -13
- package/dist/bundle/{chunk-3ZJE5TIG.js → chunk-OMKPALMX.js} +2 -2
- package/dist/bundle/{chunk-X7EBOXBY.js → chunk-OUGGAAXI.js} +13235 -1228
- package/dist/bundle/{chunk-YGJO5VNR.js → chunk-P6WCG6X2.js} +36 -30
- package/dist/bundle/{chunk-K2OQCQ2N.js → chunk-QPF4LTK5.js} +6 -6
- package/dist/bundle/{chunk-ARDUJR5Z.js → chunk-RIO3TD3I.js} +1 -1
- package/dist/bundle/{chunk-5FPQGUJA.js → chunk-RQSF7X22.js} +1 -1
- package/dist/bundle/{chunk-GAYW2JRW.js → chunk-SIYT2BBI.js} +10 -10
- package/dist/bundle/chunk-SMS5YNPB.js +64 -0
- package/dist/bundle/{chunk-PYX6K527.js → chunk-SW2KERUB.js} +648 -130
- package/dist/bundle/{chunk-Q4T2HBZP.js → chunk-TVZ77ZOV.js} +2 -2
- package/dist/bundle/{chunk-DKIAHQRN.js → chunk-U43HUE35.js} +9 -9
- package/dist/bundle/{chunk-CT2MIWXY.js → chunk-UEGABYXI.js} +3 -3
- package/dist/bundle/{chunk-KV4267S4.js → chunk-UHC5I3JV.js} +1 -1
- package/dist/bundle/{chunk-EDJQDR5X.js → chunk-UWL2IOII.js} +4 -4
- package/dist/bundle/{chunk-M6O4DH5F.js → chunk-UXD5JMS5.js} +1 -1
- package/dist/bundle/{chunk-ANFIIXBL.js → chunk-WA3DQSAZ.js} +2 -2
- package/dist/bundle/{chunk-FROWEHK7.js → chunk-XGTW3GRX.js} +2 -2
- package/dist/bundle/{chunk-VD6GZ3W2.js → chunk-YA2JPMYX.js} +2 -2
- package/dist/bundle/{chunk-CRYWPLJP.js → chunk-ZLH465EU.js} +1 -1
- package/dist/bundle/{chunk-HVAXTTOT.js → chunk-ZT2Y267T.js} +3 -3
- package/dist/bundle/command-contract-Y3GIKCPB.js +34 -0
- package/dist/bundle/{decode-WCS4KOHV.js → decode-WVIDLK4E.js} +5 -5
- package/dist/bundle/{dispatch-3ILCYZ4Q.js → dispatch-R5OF2FVZ.js} +8 -8
- package/dist/bundle/{dist-FL422DTA.js → dist-KGQWS3XL.js} +41 -23
- package/dist/bundle/{echarts-45D6USAX.js → echarts-VMKACZBF.js} +2 -2
- package/dist/bundle/{embed-4B4ATLCN.js → embed-EQINOOST.js} +6 -6
- package/dist/bundle/{embedder-ASWGJXNV.js → embedder-LM52TDRN.js} +9 -9
- package/dist/bundle/{failure-UDL5QZRZ.js → failure-WYKVF6RH.js} +2 -2
- package/dist/bundle/{first-run-5K3UD4BU.js → first-run-H4VXZAAF.js} +11 -11
- package/dist/bundle/first-screen-ZP74TJLK.js +32 -0
- package/dist/bundle/{instructions-6EFSPC7C.js → instructions-NLSME663.js} +1 -1
- package/dist/bundle/mail.js +12 -12
- package/dist/bundle/{mcp-PPVOM4SE.js → mcp-CFVG2OUS.js} +2 -2
- package/dist/bundle/{node-OXC26QXO.js → node-FGMSXLTA.js} +2 -2
- package/dist/bundle/{node-sqlite-PXHZAG7Y.js → node-sqlite-K3T3ZRBY.js} +6 -6
- package/dist/bundle/{oauth-YK2QLC7U.js → oauth-OJKJ6JLH.js} +7 -7
- package/dist/bundle/{open-SABDXZ4B.js → open-DCYIXHXS.js} +5 -5
- package/dist/bundle/{openai-DPURPXEU.js → openai-C4H4W57J.js} +14 -14
- package/dist/bundle/{process-YWPAOI6G.js → process-MN4NSYGA.js} +1 -1
- package/dist/bundle/{program-4ILOQHLO.js → program-ZZZOKWCX.js} +4026 -12667
- package/dist/bundle/{prompt-GKVNFGFE.js → prompt-ELEJUDAU.js} +5 -5
- package/dist/bundle/{recognize-344YDSLX.js → recognize-G2FVMI5M.js} +4 -4
- package/dist/bundle/{remote-53UZUJJU.js → remote-F4YVSEYM.js} +7 -7
- package/dist/bundle/{rerank-PAIRND6R.js → rerank-7VM5R5N6.js} +7 -7
- package/dist/bundle/{serve-DZSO7LJK.js → serve-A7NRFPRN.js} +9 -9
- package/dist/bundle/{server-EGG3V67I.js → server-7R2BEIRY.js} +39 -39
- package/dist/bundle/server-DFJA2HBO.js +64 -0
- package/dist/bundle/{sqlite-runtime-ZQ6F3VYZ.js → sqlite-runtime-4SKUB4EN.js} +6 -6
- package/dist/bundle/{state-LXPOWLZ5.js → state-S2FKK2XB.js} +6 -6
- package/dist/bundle/{store-PHT75I6S.js → store-4HAARQVR.js} +34 -26
- package/dist/bundle/{store-4ZMSK5R5.js → store-FLHO3UMA.js} +17 -15
- package/dist/bundle/store-maintenance-command-BINJIQ6F.js +54 -0
- package/dist/commands/account.d.ts.map +1 -1
- package/dist/commands/account.js +56 -64
- package/dist/commands/account.js.map +1 -1
- package/dist/commands/attachments.d.ts.map +1 -1
- package/dist/commands/attachments.js +15 -12
- package/dist/commands/attachments.js.map +1 -1
- package/dist/commands/context.d.ts +4 -0
- package/dist/commands/context.d.ts.map +1 -1
- package/dist/commands/context.js +12 -1
- package/dist/commands/context.js.map +1 -1
- package/dist/commands/doctor.d.ts.map +1 -0
- package/dist/{mail → commands}/doctor.js +1 -1
- package/dist/commands/doctor.js.map +1 -0
- package/dist/commands/drafts-send.d.ts.map +1 -1
- package/dist/commands/drafts-send.js +12 -10
- package/dist/commands/drafts-send.js.map +1 -1
- package/dist/commands/drafts.d.ts.map +1 -1
- package/dist/commands/drafts.js +28 -14
- package/dist/commands/drafts.js.map +1 -1
- package/dist/commands/export.d.ts.map +1 -1
- package/dist/commands/export.js +194 -195
- package/dist/commands/export.js.map +1 -1
- package/dist/commands/folders.d.ts.map +1 -1
- package/dist/commands/folders.js +19 -32
- package/dist/commands/folders.js.map +1 -1
- package/dist/commands/import.d.ts.map +1 -1
- package/dist/commands/import.js +10 -5
- package/dist/commands/import.js.map +1 -1
- package/dist/commands/messages.d.ts +0 -21
- package/dist/commands/messages.d.ts.map +1 -1
- package/dist/commands/messages.js +10 -278
- package/dist/commands/messages.js.map +1 -1
- package/dist/commands/organising.d.ts +98 -0
- package/dist/commands/organising.d.ts.map +1 -0
- package/dist/commands/organising.js +231 -0
- package/dist/commands/organising.js.map +1 -0
- package/dist/commands/restore.d.ts +3 -0
- package/dist/commands/restore.d.ts.map +1 -0
- package/dist/commands/restore.js +93 -0
- package/dist/commands/restore.js.map +1 -0
- package/dist/commands/rules.d.ts.map +1 -1
- package/dist/commands/rules.js +3 -2
- package/dist/commands/rules.js.map +1 -1
- package/dist/commands/send.d.ts +14 -3
- package/dist/commands/send.d.ts.map +1 -1
- package/dist/commands/send.js +18 -11
- package/dist/commands/send.js.map +1 -1
- package/dist/commands/session-start.d.ts.map +1 -1
- package/dist/commands/session-start.js +14 -4
- package/dist/commands/session-start.js.map +1 -1
- package/dist/commands/status.js +2 -2
- package/dist/commands/status.js.map +1 -1
- package/dist/commands/sync.d.ts.map +1 -1
- package/dist/commands/sync.js +136 -141
- package/dist/commands/sync.js.map +1 -1
- package/dist/commands/tags.d.ts.map +1 -1
- package/dist/commands/tags.js +11 -7
- package/dist/commands/tags.js.map +1 -1
- package/dist/commands/words.d.ts +8 -0
- package/dist/commands/words.d.ts.map +1 -0
- package/dist/commands/words.js +52 -0
- package/dist/commands/words.js.map +1 -0
- package/dist/discovery.d.ts.map +1 -1
- package/dist/discovery.js +2 -1
- package/dist/discovery.js.map +1 -1
- package/dist/export/journal.d.ts +8 -6
- package/dist/export/journal.d.ts.map +1 -1
- package/dist/export/journal.js +39 -14
- package/dist/export/journal.js.map +1 -1
- package/dist/mail/changes.d.ts +9 -2
- package/dist/mail/changes.d.ts.map +1 -1
- package/dist/mail/changes.js +45 -17
- package/dist/mail/changes.js.map +1 -1
- package/dist/mail/cleanup.d.ts.map +1 -1
- package/dist/mail/cleanup.js +5 -1
- package/dist/mail/cleanup.js.map +1 -1
- package/dist/mail/domain-age.d.ts.map +1 -1
- package/dist/mail/domain-age.js +24 -6
- package/dist/mail/domain-age.js.map +1 -1
- package/dist/mail/drafts.d.ts.map +1 -1
- package/dist/mail/drafts.js +9 -7
- package/dist/mail/drafts.js.map +1 -1
- package/dist/mail/imap/connection.d.ts.map +1 -1
- package/dist/mail/imap/connection.js +8 -9
- package/dist/mail/imap/connection.js.map +1 -1
- package/dist/mail/imap/errors.d.ts.map +1 -1
- package/dist/mail/imap/errors.js +2 -2
- package/dist/mail/imap/errors.js.map +1 -1
- package/dist/mail/imap/read.d.ts.map +1 -1
- package/dist/mail/imap/read.js +8 -5
- package/dist/mail/imap/read.js.map +1 -1
- package/dist/mail/imap/write.d.ts.map +1 -1
- package/dist/mail/imap/write.js +2 -1
- package/dist/mail/imap/write.js.map +1 -1
- package/dist/mail/locate.d.ts +32 -0
- package/dist/mail/locate.d.ts.map +1 -0
- package/dist/mail/locate.js +99 -0
- package/dist/mail/locate.js.map +1 -0
- package/dist/mail/mbox.d.ts.map +1 -1
- package/dist/mail/mbox.js +7 -5
- package/dist/mail/mbox.js.map +1 -1
- package/dist/mail/organise.d.ts +35 -50
- package/dist/mail/organise.d.ts.map +1 -1
- package/dist/mail/organise.js +44 -279
- package/dist/mail/organise.js.map +1 -1
- package/dist/mail/restore.d.ts +22 -0
- package/dist/mail/restore.d.ts.map +1 -0
- package/dist/mail/restore.js +193 -0
- package/dist/mail/restore.js.map +1 -0
- package/dist/mail/rules/apply.d.ts.map +1 -1
- package/dist/mail/rules/apply.js +5 -4
- package/dist/mail/rules/apply.js.map +1 -1
- package/dist/mail/rules/file.d.ts.map +1 -1
- package/dist/mail/rules/file.js +37 -8
- package/dist/mail/rules/file.js.map +1 -1
- package/dist/mail/rules/filters.d.ts.map +1 -1
- package/dist/mail/rules/filters.js +13 -7
- package/dist/mail/rules/filters.js.map +1 -1
- package/dist/mail/rules/query.d.ts.map +1 -1
- package/dist/mail/rules/query.js +7 -7
- package/dist/mail/rules/query.js.map +1 -1
- package/dist/mail/save.js +1 -1
- package/dist/mail/save.js.map +1 -1
- package/dist/mail/send.d.ts +4 -3
- package/dist/mail/send.d.ts.map +1 -1
- package/dist/mail/send.js +8 -8
- package/dist/mail/send.js.map +1 -1
- package/dist/mail/smtp.js +1 -1
- package/dist/mail/smtp.js.map +1 -1
- package/dist/oauth.d.ts +3 -2
- package/dist/oauth.d.ts.map +1 -1
- package/dist/oauth.js +16 -7
- package/dist/oauth.js.map +1 -1
- package/dist/profile.d.ts.map +1 -1
- package/dist/profile.js +3 -2
- package/dist/profile.js.map +1 -1
- package/dist/program.js +1 -1
- package/dist/program.js.map +1 -1
- package/dist/providers.js +3 -3
- package/dist/providers.js.map +1 -1
- package/dist/reasons.d.ts.map +1 -1
- package/dist/reasons.js +12 -0
- package/dist/reasons.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +5 -4
- package/skills/mail-cli/SKILL.md +3 -3
- package/dist/bundle/chunk-GIKNAOER.js +0 -132
- package/dist/bundle/command-contract-GVNLRXWS.js +0 -34
- package/dist/bundle/first-screen-NNX54MYC.js +0 -32
- package/dist/bundle/server-SGGMRV2K.js +0 -64
- package/dist/bundle/store-maintenance-command-C6ONZNRX.js +0 -54
- package/dist/index.d.ts +0 -3
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -3
- package/dist/index.js.map +0 -1
- package/dist/mail/doctor.d.ts.map +0 -1
- package/dist/mail/doctor.js.map +0 -1
- /package/dist/bundle/{chunk-LU3WKZBA.js → chunk-QZLK2ISN.js} +0 -0
- /package/dist/{mail → commands}/doctor.d.ts +0 -0
package/dist/mail/organise.d.ts
CHANGED
|
@@ -3,7 +3,8 @@ import { type SendGuard } from "@wirecat/cli-core/sends";
|
|
|
3
3
|
import type { AccountKey, MessageStore } from "@wirecat/cli-core/store";
|
|
4
4
|
import type { Organise } from "../providers.js";
|
|
5
5
|
import type { FolderInfo, MailWrites, ServerState } from "./backend.js";
|
|
6
|
-
import
|
|
6
|
+
import type { ChangeLog, StepLine } from "./changes.js";
|
|
7
|
+
import type { Located } from "./locate.js";
|
|
7
8
|
/** One mailbox as the write commands see it: its stored mail, its server and where its provider differs. */
|
|
8
9
|
export interface Mailbox {
|
|
9
10
|
app: AppIdentity;
|
|
@@ -21,36 +22,10 @@ export declare const MAX_LIMIT = 5000;
|
|
|
21
22
|
export declare const BULK = 20;
|
|
22
23
|
/** A flag as the owner types it, `read` or `$Junk`, in IMAP's spelling; `\Deleted` is refused, it is how mail is expunged. */
|
|
23
24
|
export declare const flagOf: (given: string) => string;
|
|
24
|
-
/** Stored mail by reference or by the shared query, at most `limit`; `more` says the query matched more. */
|
|
25
|
-
export declare const selectIds: (mailbox: Mailbox, { refs, filter, limit }: {
|
|
26
|
-
refs?: string[];
|
|
27
|
-
filter?: string;
|
|
28
|
-
limit?: number;
|
|
29
|
-
}) => Promise<{
|
|
30
|
-
ids: string[];
|
|
31
|
-
more: boolean;
|
|
32
|
-
}>;
|
|
33
|
-
interface Located {
|
|
34
|
-
folder: string;
|
|
35
|
-
validity: string;
|
|
36
|
-
items: ChangedItem[];
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Where each email is on the server now, folder by folder. `everywhere` keeps every place an email is in (a flag is
|
|
40
|
-
* set on each copy); otherwise only its first, so one email is moved once. Trash counts only when `trash` is given.
|
|
41
|
-
*/
|
|
42
|
-
export declare const locate: (mailbox: Mailbox, ids: readonly string[], { everywhere, trash }?: {
|
|
43
|
-
everywhere?: boolean;
|
|
44
|
-
trash?: string;
|
|
45
|
-
}) => Promise<{
|
|
46
|
-
located: Located[];
|
|
47
|
-
missing: string[];
|
|
48
|
-
deleted: string[];
|
|
49
|
-
inTrash: string[];
|
|
50
|
-
}>;
|
|
51
25
|
/** The folder a name or a role names: its path, a role (`archive`, `trash`), then a name in any case. */
|
|
52
26
|
export declare const folderNamed: (folders: readonly FolderInfo[], given: string) => FolderInfo | undefined;
|
|
53
27
|
export declare const trashOf: (mailbox: Mailbox) => Promise<string>;
|
|
28
|
+
export declare const allMailOf: (mailbox: Mailbox) => Promise<string>;
|
|
54
29
|
/** On Gmail a folder is a label: what a move or copy to `given` adds and takes away, or a real folder to move to. */
|
|
55
30
|
type Target = {
|
|
56
31
|
kind: "folder";
|
|
@@ -62,19 +37,47 @@ type Target = {
|
|
|
62
37
|
name: string;
|
|
63
38
|
};
|
|
64
39
|
export declare const targetOf: (mailbox: Mailbox, given: string, copy: boolean) => Promise<Target>;
|
|
40
|
+
export declare const storeBox: (folder: string) => {
|
|
41
|
+
externalId: string;
|
|
42
|
+
name: string;
|
|
43
|
+
kind: null;
|
|
44
|
+
};
|
|
45
|
+
/** One write a change made, as its answer names it (WC-OUT-10). */
|
|
46
|
+
export interface Write {
|
|
47
|
+
operation_id: string;
|
|
48
|
+
op: StepLine["op"];
|
|
49
|
+
folder: string;
|
|
50
|
+
to?: string;
|
|
51
|
+
messages: number;
|
|
52
|
+
undoes?: string;
|
|
53
|
+
}
|
|
65
54
|
/** Runs one step against the server under the guard, recording it before it goes and once it is done. */
|
|
66
55
|
export declare class Changer {
|
|
67
56
|
readonly mailbox: Mailbox;
|
|
68
57
|
readonly guard: SendGuard;
|
|
69
|
-
|
|
58
|
+
/** A preview passes a log that records nothing. */
|
|
59
|
+
readonly log: Pick<ChangeLog, "begin" | "done">;
|
|
70
60
|
readonly key: string;
|
|
71
61
|
readonly action: "message-flag" | "message-move" | "message-copy" | "message-delete" | "message-restore";
|
|
72
|
-
|
|
73
|
-
|
|
62
|
+
readonly writes: Write[];
|
|
63
|
+
constructor(mailbox: Mailbox, guard: SendGuard,
|
|
64
|
+
/** A preview passes a log that records nothing. */
|
|
65
|
+
log: Pick<ChangeLog, "begin" | "done">, key: string, action: "message-flag" | "message-move" | "message-copy" | "message-delete" | "message-restore");
|
|
66
|
+
step<T>(line: Omit<StepLine, "type" | "step" | "operation_id">, act: () => Promise<T>, moved?: (done: T) => {
|
|
74
67
|
uids: Map<string, number>;
|
|
75
68
|
validity?: string;
|
|
76
69
|
}): Promise<T>;
|
|
77
70
|
}
|
|
71
|
+
export declare const groupBy: <T>(items: T[], keyOf: (item: T) => string) => Map<string, [T, ...T[]]>;
|
|
72
|
+
/** The values of `keys` that `map` holds, in order; a key it lacks is left out. */
|
|
73
|
+
export declare const valuesOf: <K, V>(map: ReadonlyMap<K, V>, keys: readonly K[]) => V[];
|
|
74
|
+
export declare const flagChanges: (change: {
|
|
75
|
+
add: string[];
|
|
76
|
+
remove: string[];
|
|
77
|
+
}) => {
|
|
78
|
+
read?: boolean | undefined;
|
|
79
|
+
flagged?: boolean | undefined;
|
|
80
|
+
};
|
|
78
81
|
/**
|
|
79
82
|
* Adds and removes flags, or with `replace` makes `add` each message's whole set. On Gmail the flags of All Mail are
|
|
80
83
|
* the message's own. The store takes read and flagged once the server said yes.
|
|
@@ -84,6 +87,7 @@ export declare const changeFlags: (changer: Changer, located: Located[], change:
|
|
|
84
87
|
remove: string[];
|
|
85
88
|
replace?: boolean;
|
|
86
89
|
}) => Promise<string[]>;
|
|
90
|
+
export declare const relabel: (mailbox: Mailbox, ids: string[], add: string[], remove: string[]) => Promise<void>;
|
|
87
91
|
export declare const moveMessages: (changer: Changer, located: Located[], target: Target, op: "move" | "copy") => Promise<string[]>;
|
|
88
92
|
/**
|
|
89
93
|
* Moves the messages to Trash, which the provider empties in its own time; until then `messages restore` brings them
|
|
@@ -93,24 +97,5 @@ export declare const trashMessages: (changer: Changer, located: Located[], trash
|
|
|
93
97
|
changed: string[];
|
|
94
98
|
kept: Record<string, number>;
|
|
95
99
|
}>;
|
|
96
|
-
export interface RestorePlan {
|
|
97
|
-
run: string;
|
|
98
|
-
command: string;
|
|
99
|
-
started_at: string;
|
|
100
|
-
restorable_until: string | null;
|
|
101
|
-
expired: boolean;
|
|
102
|
-
/** Messages the run changed. */
|
|
103
|
-
logged: number;
|
|
104
|
-
/** Messages a restore puts back: still where the run left them. */
|
|
105
|
-
restorable: number;
|
|
106
|
-
/** Changed by the run, and no longer where it left them: restored already, moved by hand, or gone for good. */
|
|
107
|
-
missing: number;
|
|
108
|
-
restored: number;
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Undoes a run, last step first, for the messages still where it left them. Without `apply` it only counts. Trash
|
|
112
|
-
* brings mail back to where it was (on Gmail: to the Inbox if it was there, else All Mail, labels put back).
|
|
113
|
-
*/
|
|
114
|
-
export declare const restoreRun: (changer: Changer, { run, steps, restored: before }: ChangeRun, path: string, apply: boolean) => Promise<RestorePlan>;
|
|
115
100
|
export {};
|
|
116
101
|
//# sourceMappingURL=organise.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"organise.d.ts","sourceRoot":"","sources":["../../src/mail/organise.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"organise.d.ts","sourceRoot":"","sources":["../../src/mail/organise.ts"],"names":[],"mappings":"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"}
|
package/dist/mail/organise.js
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
|
-
import {
|
|
3
|
-
import { decodedLabelName, ENVELOPE, searchMail } from "@wirecat/cli-core/mail";
|
|
2
|
+
import { decodedLabelName } from "@wirecat/cli-core/mail";
|
|
4
3
|
import { guardedWrite, newOperationId } from "@wirecat/cli-core/sends";
|
|
5
|
-
import { appendRestore } from "./changes.js";
|
|
6
4
|
export const FILTER_LIMIT = 500;
|
|
7
5
|
export const MAX_LIMIT = 5_000;
|
|
8
6
|
/** More messages than this at once is a bulk change: previewed first and checked as `messages.bulk` too. */
|
|
9
7
|
export const BULK = 20;
|
|
10
|
-
const PAGE = 100;
|
|
11
8
|
const SYSTEM_FLAGS = {
|
|
12
9
|
read: "\\Seen",
|
|
13
10
|
seen: "\\Seen",
|
|
@@ -24,103 +21,14 @@ export const flagOf = (given) => {
|
|
|
24
21
|
if (system)
|
|
25
22
|
return system;
|
|
26
23
|
if (/^\\deleted$/i.test(word) || word.toLowerCase() === "deleted")
|
|
27
|
-
throw new CliError("validation_error", "\\Deleted is how a server expunges mail
|
|
24
|
+
throw new CliError("validation_error", "\\Deleted is how a server expunges mail", {
|
|
28
25
|
param: "flag",
|
|
26
|
+
reason: "mail.flag-deleted",
|
|
29
27
|
});
|
|
30
28
|
if (!KEYWORD.test(word))
|
|
31
29
|
throw new CliError("validation_error", `"${given}" is no flag: give read, flagged, answered, draft, or a keyword of letters, digits and $._-`, { param: "flag" });
|
|
32
30
|
return word;
|
|
33
31
|
};
|
|
34
|
-
/** Stored mail by reference or by the shared query, at most `limit`; `more` says the query matched more. */
|
|
35
|
-
export const selectIds = async (mailbox, { refs = [], filter, limit = FILTER_LIMIT }) => {
|
|
36
|
-
if (refs.length === 0 && filter === undefined)
|
|
37
|
-
throw new CliError("validation_error", "name the messages: their msg:email/… references, or --filter <query>");
|
|
38
|
-
if (refs.length > 0 && filter !== undefined)
|
|
39
|
-
throw new CliError("validation_error", "give message references or --filter, not both", { param: "filter" });
|
|
40
|
-
const ids = new Set();
|
|
41
|
-
for (const ref of refs) {
|
|
42
|
-
const place = parseReference(ref);
|
|
43
|
-
if (place.type !== "message")
|
|
44
|
-
throw new CliError("validation_error", `${ref} names no email — give the msg:email/… reference search shows`);
|
|
45
|
-
if (place.provider !== mailbox.key.provider || place.account !== mailbox.key.account)
|
|
46
|
-
throw new CliError("not_found", `${ref} is not mail of this profile's mailbox ${mailbox.key.account}`);
|
|
47
|
-
ids.add(place.message);
|
|
48
|
-
}
|
|
49
|
-
if (filter === undefined)
|
|
50
|
-
return { ids: [...ids], more: false };
|
|
51
|
-
let more = false;
|
|
52
|
-
for (let offset = 0; ids.size < limit; offset += PAGE) {
|
|
53
|
-
const found = await searchMail(mailbox.store, mailbox.key, { text: filter, limit: PAGE, offset, mailAccount: mailbox.key.account, newest: true }, { app: mailbox.app });
|
|
54
|
-
for (const hit of found.items) {
|
|
55
|
-
const place = parseReference(hit.locator);
|
|
56
|
-
if (place.type === "message")
|
|
57
|
-
ids.add(place.message);
|
|
58
|
-
}
|
|
59
|
-
if (!found.hasMore)
|
|
60
|
-
break;
|
|
61
|
-
more = ids.size >= limit;
|
|
62
|
-
}
|
|
63
|
-
return { ids: [...ids].slice(0, limit), more: more || ids.size > limit };
|
|
64
|
-
};
|
|
65
|
-
/** Gmail searches by its own id; the hash of a Message-ID is found through the stored UIDs. */
|
|
66
|
-
const serverId = (id) => !id.startsWith("imap:");
|
|
67
|
-
/**
|
|
68
|
-
* Where each email is on the server now, folder by folder. `everywhere` keeps every place an email is in (a flag is
|
|
69
|
-
* set on each copy); otherwise only its first, so one email is moved once. Trash counts only when `trash` is given.
|
|
70
|
-
*/
|
|
71
|
-
export const locate = async (mailbox, ids, { everywhere = false, trash } = {}) => {
|
|
72
|
-
const byFolder = new Map();
|
|
73
|
-
const missing = [];
|
|
74
|
-
const deleted = [];
|
|
75
|
-
const inTrash = [];
|
|
76
|
-
for (const id of ids) {
|
|
77
|
-
const email = await mailbox.store.mail.email(mailbox.accountId, id);
|
|
78
|
-
if (email === null) {
|
|
79
|
-
missing.push(id);
|
|
80
|
-
continue;
|
|
81
|
-
}
|
|
82
|
-
if (email.deletedAt !== null) {
|
|
83
|
-
deleted.push(id);
|
|
84
|
-
continue;
|
|
85
|
-
}
|
|
86
|
-
const folders = email.mailboxes.filter(({ kind }) => kind === null).map(({ externalId }) => externalId);
|
|
87
|
-
const usable = folders.filter((folder) => folder !== trash);
|
|
88
|
-
if (usable.length === 0 && folders.length > 0) {
|
|
89
|
-
inTrash.push(id);
|
|
90
|
-
continue;
|
|
91
|
-
}
|
|
92
|
-
if (usable.length === 0) {
|
|
93
|
-
missing.push(id);
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
for (const folder of everywhere ? usable : usable.slice(0, 1))
|
|
97
|
-
byFolder.set(folder, [...(byFolder.get(folder) ?? []), id]);
|
|
98
|
-
}
|
|
99
|
-
const located = [];
|
|
100
|
-
const placed = new Set();
|
|
101
|
-
for (const [folder, wanted] of byFolder) {
|
|
102
|
-
const { validity, uids } = await mailbox.writes.locate(folder, { emailIds: wanted.filter(serverId) });
|
|
103
|
-
const stored = wanted.some((id) => !serverId(id))
|
|
104
|
-
? new Map((await mailbox.store.mail.folderEmails(mailbox.accountId, folder, validity)).map((row) => [
|
|
105
|
-
row.externalId,
|
|
106
|
-
row.uid,
|
|
107
|
-
]))
|
|
108
|
-
: new Map();
|
|
109
|
-
const items = wanted.flatMap((id) => {
|
|
110
|
-
const uid = uids.get(id) ?? stored.get(id);
|
|
111
|
-
return uid === undefined ? [] : [{ id, uid }];
|
|
112
|
-
});
|
|
113
|
-
for (const { id } of items)
|
|
114
|
-
placed.add(id);
|
|
115
|
-
if (items.length)
|
|
116
|
-
located.push({ folder, validity, items });
|
|
117
|
-
}
|
|
118
|
-
for (const list of byFolder.values())
|
|
119
|
-
for (const id of list)
|
|
120
|
-
if (!placed.has(id) && !missing.includes(id))
|
|
121
|
-
missing.push(id);
|
|
122
|
-
return { located, missing, deleted, inTrash };
|
|
123
|
-
};
|
|
124
32
|
/** The folder a name or a role names: its path, a role (`archive`, `trash`), then a name in any case. */
|
|
125
33
|
export const folderNamed = (folders, given) => {
|
|
126
34
|
const word = given.trim();
|
|
@@ -135,7 +43,7 @@ export const trashOf = async (mailbox) => {
|
|
|
135
43
|
throw new CliError("provider_unavailable", "the mailbox has no Trash folder this tool can find, so nothing is deleted; folders list shows the folders", { reason: "mail.no-trash" });
|
|
136
44
|
return found.path;
|
|
137
45
|
};
|
|
138
|
-
const allMailOf = async (mailbox) => {
|
|
46
|
+
export const allMailOf = async (mailbox) => {
|
|
139
47
|
const found = (await mailbox.folders()).find(({ role }) => role === "all");
|
|
140
48
|
if (!found)
|
|
141
49
|
throw new CliError("provider_unavailable", "Gmail's All Mail folder is not listed", { reason: "mail.no-all-mail" });
|
|
@@ -145,8 +53,9 @@ export const targetOf = async (mailbox, given, copy) => {
|
|
|
145
53
|
const folders = await mailbox.folders();
|
|
146
54
|
const found = folderNamed(folders, given);
|
|
147
55
|
if (found?.role === "trash")
|
|
148
|
-
throw new CliError("validation_error", "moving to Trash is a deletion
|
|
56
|
+
throw new CliError("validation_error", "moving to Trash is a deletion", {
|
|
149
57
|
param: "folder",
|
|
58
|
+
reason: "mail.move-to-trash",
|
|
150
59
|
});
|
|
151
60
|
if (!mailbox.organise.labels) {
|
|
152
61
|
if (!found)
|
|
@@ -169,7 +78,7 @@ export const targetOf = async (mailbox, given, copy) => {
|
|
|
169
78
|
const name = found?.path ?? given.trim();
|
|
170
79
|
return { kind: "labels", add: [name], remove: copy ? [] : ["\\Inbox"], name };
|
|
171
80
|
};
|
|
172
|
-
const storeBox = (folder) => ({ externalId: folder, name: folder, kind: null });
|
|
81
|
+
export const storeBox = (folder) => ({ externalId: folder, name: folder, kind: null });
|
|
173
82
|
const labelBox = (raw) => {
|
|
174
83
|
const name = decodedLabelName(raw);
|
|
175
84
|
return { externalId: `label:${name}`, name, kind: "label" };
|
|
@@ -181,7 +90,10 @@ export class Changer {
|
|
|
181
90
|
log;
|
|
182
91
|
key;
|
|
183
92
|
action;
|
|
184
|
-
|
|
93
|
+
writes = [];
|
|
94
|
+
constructor(mailbox, guard,
|
|
95
|
+
/** A preview passes a log that records nothing. */
|
|
96
|
+
log, key, action) {
|
|
185
97
|
this.mailbox = mailbox;
|
|
186
98
|
this.guard = guard;
|
|
187
99
|
this.log = log;
|
|
@@ -197,20 +109,43 @@ export class Changer {
|
|
|
197
109
|
count: line.items.length,
|
|
198
110
|
operationId: newOperationId(),
|
|
199
111
|
};
|
|
200
|
-
const step = this.log.begin(line);
|
|
112
|
+
const step = this.log.begin({ ...line, operation_id: request.operationId });
|
|
201
113
|
const done = await guardedWrite(this.guard, request, act);
|
|
202
114
|
this.log.done(step, line.items.map(({ id }) => id), moved?.(done));
|
|
115
|
+
this.writes.push({
|
|
116
|
+
operation_id: request.operationId,
|
|
117
|
+
op: line.op,
|
|
118
|
+
folder: line.folder,
|
|
119
|
+
...(line.to ? { to: line.to } : {}),
|
|
120
|
+
messages: line.items.length,
|
|
121
|
+
...(line.undoes ? { undoes: line.undoes } : {}),
|
|
122
|
+
});
|
|
203
123
|
return done;
|
|
204
124
|
}
|
|
205
125
|
}
|
|
206
126
|
const statesOf = async (mailbox, { folder, validity, items }) => mailbox.writes.state(folder, validity, items.map(({ uid }) => uid));
|
|
207
|
-
const groupBy = (items, keyOf) => {
|
|
127
|
+
export const groupBy = (items, keyOf) => {
|
|
208
128
|
const groups = new Map();
|
|
209
|
-
for (const item of items)
|
|
210
|
-
|
|
129
|
+
for (const item of items) {
|
|
130
|
+
const group = groups.get(keyOf(item));
|
|
131
|
+
if (group)
|
|
132
|
+
group.push(item);
|
|
133
|
+
else
|
|
134
|
+
groups.set(keyOf(item), [item]);
|
|
135
|
+
}
|
|
211
136
|
return groups;
|
|
212
137
|
};
|
|
213
|
-
|
|
138
|
+
/** The values of `keys` that `map` holds, in order; a key it lacks is left out. */
|
|
139
|
+
export const valuesOf = (map, keys) => keys.flatMap((key) => {
|
|
140
|
+
const value = map.get(key);
|
|
141
|
+
return value === undefined ? [] : [value];
|
|
142
|
+
});
|
|
143
|
+
/** A server's answer keyed by UID, keyed by message id instead; a UID with no id is left out. */
|
|
144
|
+
const byId = (uids, idOf) => new Map([...uids].flatMap(([uid, value]) => {
|
|
145
|
+
const id = idOf.get(uid);
|
|
146
|
+
return id === undefined ? [] : [[id, value]];
|
|
147
|
+
}));
|
|
148
|
+
export const flagChanges = (change) => ({
|
|
214
149
|
...(change.add.includes("\\Seen") ? { read: true } : change.remove.includes("\\Seen") ? { read: false } : {}),
|
|
215
150
|
...(change.add.includes("\\Flagged")
|
|
216
151
|
? { flagged: true }
|
|
@@ -242,7 +177,7 @@ export const changeFlags = async (changer, located, change) => {
|
|
|
242
177
|
: [];
|
|
243
178
|
});
|
|
244
179
|
for (const group of groupBy(items, ({ add, remove }) => JSON.stringify([add, remove])).values()) {
|
|
245
|
-
const { add, remove } = group
|
|
180
|
+
const [{ add, remove }] = group;
|
|
246
181
|
const line = {
|
|
247
182
|
op: "flag",
|
|
248
183
|
folder: place.folder,
|
|
@@ -279,7 +214,7 @@ const changeLabels = async (changer, place, add, remove) => {
|
|
|
279
214
|
await relabel(mailbox, items.map(({ id }) => id), add, remove);
|
|
280
215
|
return items.map(({ id }) => id);
|
|
281
216
|
};
|
|
282
|
-
const relabel = async (mailbox, ids, add, remove) => {
|
|
217
|
+
export const relabel = async (mailbox, ids, add, remove) => {
|
|
283
218
|
const scanned = [...add, ...remove].map(labelBox);
|
|
284
219
|
const kept = add.map(labelBox).map(({ externalId }) => externalId);
|
|
285
220
|
if (scanned.length === 0)
|
|
@@ -299,7 +234,7 @@ const moveFolder = async (changer, place, to, op) => {
|
|
|
299
234
|
const uids = items.map(({ uid }) => uid);
|
|
300
235
|
const idOf = new Map(items.map(({ id, uid }) => [uid, id]));
|
|
301
236
|
const moved = await changer.step({ op, folder: place.folder, validity: place.validity, to, items }, () => (op === "move" ? mailbox.writes.move : mailbox.writes.copy)(place.folder, place.validity, uids, to), (done) => ({
|
|
302
|
-
uids:
|
|
237
|
+
uids: byId(done.uids, idOf),
|
|
303
238
|
...(done.validity ? { validity: done.validity } : {}),
|
|
304
239
|
}));
|
|
305
240
|
const ids = items.map(({ id }) => id);
|
|
@@ -308,7 +243,7 @@ const moveFolder = async (changer, place, to, op) => {
|
|
|
308
243
|
if (op === "move")
|
|
309
244
|
await mailbox.store.mail.forgetLocations(mailbox.accountId, place.folder, place.validity, uids);
|
|
310
245
|
if (moved.validity && moved.uids.size)
|
|
311
|
-
await mailbox.store.mail.setLocations(mailbox.accountId, to, moved.validity, new Map([...moved.uids].
|
|
246
|
+
await mailbox.store.mail.setLocations(mailbox.accountId, to, moved.validity, new Map([...byId(moved.uids, idOf)].map(([id, uid]) => [id, [uid]])));
|
|
312
247
|
return ids;
|
|
313
248
|
};
|
|
314
249
|
export const moveMessages = async (changer, located, target, op) => {
|
|
@@ -353,7 +288,7 @@ export const trashMessages = async (changer, located, trash, keep = () => undefi
|
|
|
353
288
|
continue;
|
|
354
289
|
const idOf = new Map(items.map(({ id, uid }) => [uid, id]));
|
|
355
290
|
await changer.step({ op: "trash", folder: place.folder, validity: place.validity, to: trash, items }, () => mailbox.writes.move(place.folder, place.validity, items.map(({ uid }) => uid), trash), (done) => ({
|
|
356
|
-
uids:
|
|
291
|
+
uids: byId(done.uids, idOf),
|
|
357
292
|
...(done.validity ? { validity: done.validity } : {}),
|
|
358
293
|
}));
|
|
359
294
|
const ids = items.map(({ id }) => id);
|
|
@@ -363,174 +298,4 @@ export const trashMessages = async (changer, located, trash, keep = () => undefi
|
|
|
363
298
|
}
|
|
364
299
|
return { changed, kept };
|
|
365
300
|
};
|
|
366
|
-
const SYSTEM = new Set(["\\Inbox", "\\Important", "\\Starred", "\\Sent", "\\Drafts", "\\Spam", "\\Trash"]);
|
|
367
|
-
const inverse = (change = {}, item) => {
|
|
368
|
-
const had = new Set(item.had ?? []);
|
|
369
|
-
return {
|
|
370
|
-
add: (change.remove ?? []).filter((one) => had.has(one)),
|
|
371
|
-
remove: (change.add ?? []).filter((one) => !had.has(one)),
|
|
372
|
-
};
|
|
373
|
-
};
|
|
374
|
-
/** Where a step's messages are now: the UIDs it left them under, else found again by id or Message-ID. */
|
|
375
|
-
const whereNow = async (mailbox, folder, items, given) => {
|
|
376
|
-
const look = await mailbox.writes.locate(folder, {
|
|
377
|
-
emailIds: items.map(({ id }) => id).filter(serverId),
|
|
378
|
-
messageIds: items.flatMap(({ id, mid }) => (serverId(id) || mid === undefined ? [] : [mid])),
|
|
379
|
-
});
|
|
380
|
-
const found = new Map();
|
|
381
|
-
const same = given.validity === undefined || given.validity === look.validity;
|
|
382
|
-
for (const item of items) {
|
|
383
|
-
const uid = look.uids.get(item.id) ?? (item.mid ? look.uids.get(item.mid) : undefined);
|
|
384
|
-
if (uid !== undefined)
|
|
385
|
-
found.set(item.id, uid);
|
|
386
|
-
else if (same && given.uids?.[item.id] !== undefined) {
|
|
387
|
-
const left = given.uids[item.id];
|
|
388
|
-
const present = await mailbox.writes.state(folder, look.validity, [left]);
|
|
389
|
-
if (present.has(left))
|
|
390
|
-
found.set(item.id, left);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
return { validity: look.validity, found };
|
|
394
|
-
};
|
|
395
|
-
/**
|
|
396
|
-
* The copies a copy step made, by the UIDs the server gave them (COPYUID) alone. The original and a copy share the
|
|
397
|
-
* Message-ID, and on a server with OBJECTID the email id too, so finding them again by either could pick the
|
|
398
|
-
* original, which undoing a copy then expunges for good.
|
|
399
|
-
*/
|
|
400
|
-
const copiesNow = async (mailbox, folder, items, done) => {
|
|
401
|
-
const { uids, to_validity: validity } = done ?? {};
|
|
402
|
-
if (!uids || !validity)
|
|
403
|
-
throw new CliError("provider_unavailable", "the server gave no UIDs for the copies, so they cannot be told from other mail and are left in place", { reason: "mail.copy-not-undoable" });
|
|
404
|
-
const left = items.flatMap(({ id }) => (uids[id] === undefined ? [] : [[id, uids[id]]]));
|
|
405
|
-
const present = await mailbox.writes.state(folder, validity, left.map(([, uid]) => uid));
|
|
406
|
-
return { validity, found: new Map(left.filter(([, uid]) => present.has(uid))) };
|
|
407
|
-
};
|
|
408
|
-
/**
|
|
409
|
-
* Undoes a run, last step first, for the messages still where it left them. Without `apply` it only counts. Trash
|
|
410
|
-
* brings mail back to where it was (on Gmail: to the Inbox if it was there, else All Mail, labels put back).
|
|
411
|
-
*/
|
|
412
|
-
export const restoreRun = async (changer, { run, steps, restored: before }, path, apply) => {
|
|
413
|
-
const { mailbox } = changer;
|
|
414
|
-
const now = mailbox.now();
|
|
415
|
-
const until = run.restorable_until ?? null;
|
|
416
|
-
const plan = {
|
|
417
|
-
run: run.id,
|
|
418
|
-
command: run.command,
|
|
419
|
-
started_at: run.started_at,
|
|
420
|
-
restorable_until: until,
|
|
421
|
-
expired: until !== null && Date.parse(until) < now,
|
|
422
|
-
logged: 0,
|
|
423
|
-
restorable: 0,
|
|
424
|
-
missing: 0,
|
|
425
|
-
restored: 0,
|
|
426
|
-
};
|
|
427
|
-
const restored = [];
|
|
428
|
-
// A step can fail after earlier ones changed the mailbox; what came back is recorded, so the next restore skips it.
|
|
429
|
-
try {
|
|
430
|
-
for (const { step, done } of [...steps].reverse()) {
|
|
431
|
-
const items = step.items.filter(({ id }) => !before.has(id));
|
|
432
|
-
plan.logged += step.items.length;
|
|
433
|
-
if (items.length === 0)
|
|
434
|
-
continue;
|
|
435
|
-
const folder = step.folder || (step.op === "trash" ? await allMailOf(mailbox) : step.folder);
|
|
436
|
-
if (step.op === "flag" || step.op === "label") {
|
|
437
|
-
// A flag or a label does not move a message, so the UID it had is still its UID.
|
|
438
|
-
const { validity, found } = await whereNow(mailbox, folder, items, {
|
|
439
|
-
uids: Object.fromEntries(items.map(({ id, uid }) => [id, uid])),
|
|
440
|
-
validity: step.validity,
|
|
441
|
-
});
|
|
442
|
-
const here = items.filter(({ id }) => found.has(id));
|
|
443
|
-
plan.restorable += here.length;
|
|
444
|
-
plan.missing += items.length - here.length;
|
|
445
|
-
if (!apply)
|
|
446
|
-
continue;
|
|
447
|
-
for (const group of groupBy(here, (item) => JSON.stringify(inverse(step.change, item))).values()) {
|
|
448
|
-
const back = inverse(step.change, group[0]);
|
|
449
|
-
if (back.add.length === 0 && back.remove.length === 0)
|
|
450
|
-
continue;
|
|
451
|
-
const uids = group.map(({ id }) => found.get(id));
|
|
452
|
-
const ids = group.map(({ id }) => id);
|
|
453
|
-
await changer.step({
|
|
454
|
-
op: step.op,
|
|
455
|
-
folder,
|
|
456
|
-
validity,
|
|
457
|
-
change: back,
|
|
458
|
-
items: group.map((item) => ({ ...item, uid: found.get(item.id) })),
|
|
459
|
-
}, () => (step.op === "flag" ? mailbox.writes.flag : mailbox.writes.label)(folder, validity, uids, back));
|
|
460
|
-
if (step.op === "flag") {
|
|
461
|
-
const flags = flagChanges(back);
|
|
462
|
-
if (Object.keys(flags).length)
|
|
463
|
-
await mailbox.store.mail.setFlags(mailbox.accountId, new Map(ids.map((id) => [id, flags])), mailbox.now());
|
|
464
|
-
}
|
|
465
|
-
else
|
|
466
|
-
await relabel(mailbox, ids, back.add, back.remove);
|
|
467
|
-
for (const id of ids)
|
|
468
|
-
restored.push(id);
|
|
469
|
-
}
|
|
470
|
-
continue;
|
|
471
|
-
}
|
|
472
|
-
const to = step.to || (await trashOf(mailbox));
|
|
473
|
-
const { validity, found } = step.op === "copy"
|
|
474
|
-
? await copiesNow(mailbox, to, items, done)
|
|
475
|
-
: await whereNow(mailbox, to, items, {
|
|
476
|
-
...(done?.uids ? { uids: done.uids } : {}),
|
|
477
|
-
...(done?.to_validity ? { validity: done.to_validity } : {}),
|
|
478
|
-
});
|
|
479
|
-
const here = items.filter(({ id }) => found.has(id));
|
|
480
|
-
plan.restorable += here.length;
|
|
481
|
-
plan.missing += items.length - here.length;
|
|
482
|
-
if (!apply || here.length === 0)
|
|
483
|
-
continue;
|
|
484
|
-
if (step.op === "copy") {
|
|
485
|
-
const uids = here.map(({ id }) => found.get(id));
|
|
486
|
-
await changer.step({ op: "copy", folder: to, validity, items: here }, () => mailbox.writes.expunge(to, validity, uids));
|
|
487
|
-
await mailbox.store.mail.setMailboxes(mailbox.accountId, [storeBox(to)], new Map(here.map(({ id }) => [id, []])), mailbox.now());
|
|
488
|
-
for (const { id } of here)
|
|
489
|
-
restored.push(id);
|
|
490
|
-
continue;
|
|
491
|
-
}
|
|
492
|
-
const gmailTrash = step.op === "trash" && mailbox.organise.labels === true;
|
|
493
|
-
const groups = gmailTrash
|
|
494
|
-
? groupBy(here, (item) => ((item.labels ?? []).includes("\\Inbox") ? "INBOX" : folder))
|
|
495
|
-
: new Map([[folder, here]]);
|
|
496
|
-
for (const [back, group] of groups) {
|
|
497
|
-
const uids = group.map(({ id }) => found.get(id));
|
|
498
|
-
await changer.step({ op: "move", folder: to, validity, to: back, items: group }, () => mailbox.writes.move(to, validity, uids, back));
|
|
499
|
-
const ids = group.map(({ id }) => id);
|
|
500
|
-
if (step.op === "move")
|
|
501
|
-
await mailbox.store.mail.setMailboxes(mailbox.accountId, [storeBox(to), storeBox(back)], new Map(ids.map((id) => [id, [back]])), mailbox.now());
|
|
502
|
-
for (const id of ids)
|
|
503
|
-
restored.push(id);
|
|
504
|
-
}
|
|
505
|
-
if (gmailTrash) {
|
|
506
|
-
const all = await allMailOf(mailbox);
|
|
507
|
-
const labelled = here.filter((item) => (item.labels ?? []).some((label) => label !== "\\Inbox"));
|
|
508
|
-
if (labelled.length) {
|
|
509
|
-
const { validity: allValidity, found: inAll } = await whereNow(mailbox, all, labelled, {});
|
|
510
|
-
for (const group of groupBy(labelled, (item) => JSON.stringify((item.labels ?? []).filter((l) => l !== "\\Inbox").sort())).values()) {
|
|
511
|
-
const labels = (group[0].labels ?? []).filter((label) => label !== "\\Inbox");
|
|
512
|
-
const placed = group.filter(({ id }) => inAll.has(id));
|
|
513
|
-
if (placed.length === 0)
|
|
514
|
-
continue;
|
|
515
|
-
await mailbox.writes.label(all, allValidity, placed.map(({ id }) => inAll.get(id)), { add: labels.filter((label) => !SYSTEM.has(label) || label === "\\Starred" || label === "\\Important") });
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
if (step.op === "trash") {
|
|
520
|
-
// Only an envelope comes back whole at once: it has no text to lose. Full mail is saved again by the next sync.
|
|
521
|
-
const envelopes = (await mailbox.store.mail.emailRefs({ accountId: mailbox.accountId, includeDeleted: true }))
|
|
522
|
-
.filter(({ externalId, mailboxes }) => found.has(externalId) && mailboxes.some(({ kind }) => kind === ENVELOPE))
|
|
523
|
-
.map(({ externalId }) => externalId);
|
|
524
|
-
if (envelopes.length)
|
|
525
|
-
await mailbox.store.mail.markRestored(mailbox.accountId, envelopes, mailbox.now());
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
finally {
|
|
530
|
-
if (restored.length)
|
|
531
|
-
appendRestore(path, restored, mailbox.now());
|
|
532
|
-
}
|
|
533
|
-
plan.restored = restored.length;
|
|
534
|
-
return plan;
|
|
535
|
-
};
|
|
536
301
|
//# sourceMappingURL=organise.js.map
|