@domicile-desktop/manganese-bar-module-gmail 2.0.1 → 2.1.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/README.md +4 -1
- package/package.json +18 -18
- package/src/addresses.ts +38 -3
- package/src/bar-item.tsx +8 -2
- package/src/consent.ts +9 -3
- package/src/desk-inbox.ts +5 -3
- package/src/gmail-api.ts +87 -3
- package/src/google-token.ts +3 -3
- package/src/held-google.ts +5 -0
- package/src/inbox.ts +9 -2
- package/src/mail-draft.ts +94 -0
- package/src/mail-notices.ts +48 -23
- package/src/open-mail.ts +203 -0
- package/src/read-inbox.ts +35 -12
- package/src/sign-in.ts +5 -1
- package/src/thread-token.ts +31 -0
- package/src/write-mail.ts +94 -0
- package/styled-system/patterns/aspect-ratio.mjs +2 -2
- package/styled-system/patterns/bleed.mjs +1 -2
- package/styled-system/patterns/float.mjs +5 -11
- package/styled-system/patterns/grid-item.mjs +1 -2
- package/styled-system/patterns/grid.mjs +1 -2
- package/styled-system/patterns/link-overlay.mjs +1 -1
- package/styled-system/patterns/spacer.mjs +3 -3
- package/styled-system/patterns/visually-hidden.mjs +1 -1
- package/styled-system/tokens/index.mjs +7 -3
- package/styled-system/tokens/tokens.d.ts +2 -2
- package/styled-system/types/prop-type.d.ts +2 -2
package/README.md
CHANGED
|
@@ -30,12 +30,15 @@ export const Shell = runManganese({
|
|
|
30
30
|
|
|
31
31
|
- `client`: a Google Cloud OAuth client, Desktop app type:
|
|
32
32
|
1. Turn on the Gmail API in a Google Cloud project.
|
|
33
|
-
2. Give the OAuth consent screen the `gmail.metadata
|
|
33
|
+
2. Give the OAuth consent screen the `gmail.metadata`,
|
|
34
|
+
`gmail.settings.basic` and `gmail.compose` scopes, and publish it.
|
|
34
35
|
One in Testing expires sign-ins after 7 days.
|
|
35
36
|
3. Create an OAuth client of the Desktop app type. Google does not treat
|
|
36
37
|
its secret as secret.
|
|
37
38
|
- `email`: the Google account.
|
|
38
39
|
- `nickname`: its name on the bar.
|
|
40
|
+
- `profile`: the browser profile its Gmail window and sign-in open in, such
|
|
41
|
+
as `"work"`. Without one, Gmail's site decides.
|
|
39
42
|
|
|
40
43
|
What it shows, and how it reads the inbox:
|
|
41
44
|
[TOP-BAR.md](../shell-manganese/docs/TOP-BAR.md#gmail).
|
package/package.json
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"dependencies": {
|
|
3
3
|
"@cprussin/option-result": "^2.0.1",
|
|
4
|
-
"@domicile-desktop/component-library": "2.0
|
|
5
|
-
"@domicile-desktop/manganese-host": "2.0.
|
|
6
|
-
"@domicile-desktop/react": "0.
|
|
7
|
-
"@domicile-desktop/sdk": "
|
|
8
|
-
"@domicile-desktop/system-apps": "1.
|
|
9
|
-
"@pandacss/dev": "^1.12.
|
|
4
|
+
"@domicile-desktop/component-library": "2.2.0",
|
|
5
|
+
"@domicile-desktop/manganese-host": "2.0.3",
|
|
6
|
+
"@domicile-desktop/react": "0.2.0",
|
|
7
|
+
"@domicile-desktop/sdk": "3.0.0",
|
|
8
|
+
"@domicile-desktop/system-apps": "1.3.0",
|
|
9
|
+
"@pandacss/dev": "^1.12.2",
|
|
10
10
|
"@phosphor-icons/react": "^2.1.10",
|
|
11
|
-
"react": "^19.
|
|
12
|
-
"zod": "^4.
|
|
11
|
+
"react": "^19.3.1",
|
|
12
|
+
"zod": "^4.6.7"
|
|
13
13
|
},
|
|
14
14
|
"description": "A manganese bar item: one Gmail account's inbox count, read with the Gmail API, and notifications for new mail.",
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"@cprussin/tsconfig": "^5.0.0",
|
|
17
17
|
"@domicile-desktop/test-support": "0.0.2",
|
|
18
|
-
"@pandacss/preset-panda": "^1.12.
|
|
18
|
+
"@pandacss/preset-panda": "^1.12.2",
|
|
19
19
|
"@testing-library/react": "^16.3.3",
|
|
20
|
-
"@testing-library/user-event": "^14.6.
|
|
21
|
-
"@types/bun": "^1.4.
|
|
22
|
-
"@types/react": "^19.
|
|
23
|
-
"@types/react-dom": "^19.
|
|
24
|
-
"postcss": "^8.5.
|
|
20
|
+
"@testing-library/user-event": "^14.6.10",
|
|
21
|
+
"@types/bun": "^1.4.4",
|
|
22
|
+
"@types/react": "^19.3.0",
|
|
23
|
+
"@types/react-dom": "^19.3.0",
|
|
24
|
+
"postcss": "^8.5.32",
|
|
25
25
|
"typescript": "^7.0.2"
|
|
26
26
|
},
|
|
27
27
|
"engines": {
|
|
28
|
-
"bun": ">=1.4.2"
|
|
29
|
-
"node": ">=24"
|
|
28
|
+
"bun": ">=1.4.2"
|
|
30
29
|
},
|
|
31
30
|
"exports": {
|
|
32
31
|
".": "./src/bar-item.tsx",
|
|
33
32
|
"./addresses": "./src/addresses.ts",
|
|
34
33
|
"./consent": "./src/consent.ts",
|
|
35
34
|
"./desk-inbox": "./src/desk-inbox.ts",
|
|
36
|
-
"./inbox": "./src/inbox.ts"
|
|
35
|
+
"./inbox": "./src/inbox.ts",
|
|
36
|
+
"./write-mail": "./src/write-mail.ts"
|
|
37
37
|
},
|
|
38
38
|
"files": [
|
|
39
39
|
"src/**",
|
|
@@ -58,5 +58,5 @@
|
|
|
58
58
|
"test:unit": "bun test"
|
|
59
59
|
},
|
|
60
60
|
"type": "module",
|
|
61
|
-
"version": "2.0
|
|
61
|
+
"version": "2.1.0"
|
|
62
62
|
}
|
package/src/addresses.ts
CHANGED
|
@@ -7,9 +7,44 @@ export const GMAIL_ORIGIN = "https://mail.google.com";
|
|
|
7
7
|
*/
|
|
8
8
|
export const gmailAppId = (email: string): string => `gmail:${email}`;
|
|
9
9
|
|
|
10
|
-
/**
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
/**
|
|
11
|
+
* The account's inbox in a browser window. In a browser profile of its own,
|
|
12
|
+
* the profile's Google sign-in is the account's, so the address names no
|
|
13
|
+
* account; otherwise it picks the account by its address.
|
|
14
|
+
*/
|
|
15
|
+
export const inboxAddress = (email: string, profile?: string): string =>
|
|
16
|
+
profile === undefined
|
|
17
|
+
? `${GMAIL_ORIGIN}/mail/?${asUser(email)}`
|
|
18
|
+
: `${GMAIL_ORIGIN}/mail/`;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The account's Gmail with a new message open over the inbox, filled in from
|
|
22
|
+
* the `mailto:` link `link`: its recipients, subject and body. Gmail's own
|
|
23
|
+
* handler for `mailto:` links, as a browser registers it.
|
|
24
|
+
*/
|
|
25
|
+
export const composeAddress = (
|
|
26
|
+
email: string,
|
|
27
|
+
link: string,
|
|
28
|
+
profile?: string,
|
|
29
|
+
): string =>
|
|
30
|
+
`${GMAIL_ORIGIN}/mail/?${new URLSearchParams([
|
|
31
|
+
...(profile === undefined ? [["authuser", email]] : []),
|
|
32
|
+
["extsrc", "mailto"],
|
|
33
|
+
["url", link],
|
|
34
|
+
]).toString()}`;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The account's Gmail with draft `message` (its message id) open over the
|
|
38
|
+
* drafts list.
|
|
39
|
+
*/
|
|
40
|
+
export const draftAddress = (
|
|
41
|
+
email: string,
|
|
42
|
+
message: string,
|
|
43
|
+
profile?: string,
|
|
44
|
+
): string =>
|
|
45
|
+
`${inboxAddress(email, profile)}#drafts?${new URLSearchParams({
|
|
46
|
+
compose: message,
|
|
47
|
+
}).toString()}`;
|
|
13
48
|
|
|
14
49
|
/**
|
|
15
50
|
* Picks the signed-in account by its address. Gmail's `/mail/u/<n>/` takes an
|
package/src/bar-item.tsx
CHANGED
|
@@ -12,15 +12,20 @@ type Props = {
|
|
|
12
12
|
nickname: string;
|
|
13
13
|
/** The Google Cloud OAuth client (Desktop app) it signs in with. */
|
|
14
14
|
client: GoogleClient;
|
|
15
|
+
/**
|
|
16
|
+
* The id of the browser profile its Gmail window and sign-in open in, such
|
|
17
|
+
* as `work`. By default, the one Gmail's site opens in.
|
|
18
|
+
*/
|
|
19
|
+
profile?: string;
|
|
15
20
|
};
|
|
16
21
|
|
|
17
22
|
/**
|
|
18
23
|
* One Gmail account's inbox count, read with the Gmail API. A press opens
|
|
19
24
|
* the account's Gmail window, or brings it up. Not in the default bar.
|
|
20
25
|
*/
|
|
21
|
-
export const Gmail = ({ client, email, nickname }: Props) => {
|
|
26
|
+
export const Gmail = ({ client, email, nickname, profile }: Props) => {
|
|
22
27
|
const domicile = useDomicile();
|
|
23
|
-
const account = deskInbox(domicile)(email, client, nickname);
|
|
28
|
+
const account = deskInbox(domicile)(email, client, nickname, profile);
|
|
24
29
|
return (
|
|
25
30
|
<Account
|
|
26
31
|
email={email}
|
|
@@ -30,6 +35,7 @@ export const Gmail = ({ client, email, nickname }: Props) => {
|
|
|
30
35
|
domicile.browser.openApp(url, {
|
|
31
36
|
id: gmailAppId(email),
|
|
32
37
|
reuse: AppReuse.Focus,
|
|
38
|
+
...(profile === undefined ? {} : { profile }),
|
|
33
39
|
});
|
|
34
40
|
}}
|
|
35
41
|
onSignIn={account.signIn}
|
package/src/consent.ts
CHANGED
|
@@ -16,9 +16,15 @@ export const REDIRECT = "http://127.0.0.1:47823/";
|
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* Read-only access to labels, which carry the inbox's counts, and to message
|
|
19
|
-
* headers, which name a new message's sender and subject. Never bodies.
|
|
19
|
+
* headers, which name a new message's sender and subject. Never bodies. The
|
|
20
|
+
* basic settings, which list the addresses the account sends from. And
|
|
21
|
+
* drafts, so an email link written from an alias starts as a draft from it.
|
|
20
22
|
*/
|
|
21
|
-
export const
|
|
23
|
+
export const SCOPES = [
|
|
24
|
+
"https://www.googleapis.com/auth/gmail.metadata",
|
|
25
|
+
"https://www.googleapis.com/auth/gmail.settings.basic",
|
|
26
|
+
"https://www.googleapis.com/auth/gmail.compose",
|
|
27
|
+
] as const;
|
|
22
28
|
|
|
23
29
|
/**
|
|
24
30
|
* Google's consent page for `email`. It redirects to {@link REDIRECT} with
|
|
@@ -36,7 +42,7 @@ export const consentAddress = (
|
|
|
36
42
|
prompt: "consent",
|
|
37
43
|
redirect_uri: REDIRECT,
|
|
38
44
|
response_type: "code",
|
|
39
|
-
scope:
|
|
45
|
+
scope: SCOPES.join(" "),
|
|
40
46
|
state,
|
|
41
47
|
}).toString()}`;
|
|
42
48
|
|
package/src/desk-inbox.ts
CHANGED
|
@@ -42,11 +42,12 @@ export const deskInbox = perDesktop((domicile) => {
|
|
|
42
42
|
email: string,
|
|
43
43
|
client: GoogleClient,
|
|
44
44
|
nickname: string,
|
|
45
|
+
profile?: string,
|
|
45
46
|
): DeskAccount => {
|
|
46
47
|
const known = accounts.get(email);
|
|
47
48
|
if (known === undefined) {
|
|
48
|
-
const account = watchedAccount(domicile, email, client, (news) =>
|
|
49
|
-
notify({ email, nickname }, news),
|
|
49
|
+
const account = watchedAccount(domicile, email, client, profile, (news) =>
|
|
50
|
+
notify({ email, nickname, profile }, news),
|
|
50
51
|
);
|
|
51
52
|
accounts.set(email, account);
|
|
52
53
|
return account;
|
|
@@ -60,6 +61,7 @@ const watchedAccount = (
|
|
|
60
61
|
domicile: Domicile,
|
|
61
62
|
email: string,
|
|
62
63
|
client: GoogleClient,
|
|
64
|
+
profile: string | undefined,
|
|
63
65
|
onMail: (news: MailNews) => Promise<void>,
|
|
64
66
|
): DeskAccount => {
|
|
65
67
|
const read = inboxReader(domicile, client, email, onMail);
|
|
@@ -84,7 +86,7 @@ const watchedAccount = (
|
|
|
84
86
|
throw new Error(`no bar shows ${email} to sign in from`);
|
|
85
87
|
} else {
|
|
86
88
|
running.watch.answer(
|
|
87
|
-
signIn(domicile, client, email).then(() => read()),
|
|
89
|
+
signIn(domicile, client, email, profile).then(() => read()),
|
|
88
90
|
);
|
|
89
91
|
}
|
|
90
92
|
},
|
package/src/gmail-api.ts
CHANGED
|
@@ -8,6 +8,13 @@ import { jsonText } from "./json-text";
|
|
|
8
8
|
/** The Gmail API's messages for the signed-in account. */
|
|
9
9
|
const MESSAGES = "https://gmail.googleapis.com/gmail/v1/users/me/messages";
|
|
10
10
|
|
|
11
|
+
/** The Gmail API's drafts of the signed-in account. */
|
|
12
|
+
const DRAFTS = "https://gmail.googleapis.com/gmail/v1/users/me/drafts";
|
|
13
|
+
|
|
14
|
+
/** The Gmail API's addresses the account sends from. */
|
|
15
|
+
const SEND_AS =
|
|
16
|
+
"https://gmail.googleapis.com/gmail/v1/users/me/settings/sendAs";
|
|
17
|
+
|
|
11
18
|
/** The Gmail API's inbox label, which carries its counts. */
|
|
12
19
|
const INBOX = "https://gmail.googleapis.com/gmail/v1/users/me/labels/INBOX";
|
|
13
20
|
|
|
@@ -60,6 +67,29 @@ export const unreadMail = (
|
|
|
60
67
|
): Promise<Result<Unread[], string>> =>
|
|
61
68
|
ask(domicile, accessToken, UNREAD, unreadSchema);
|
|
62
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Asks the Gmail API for the addresses the account sends from: its own,
|
|
72
|
+
* then the aliases it verified, in Gmail's order.
|
|
73
|
+
*/
|
|
74
|
+
export const sendAsAddresses = (
|
|
75
|
+
domicile: Fetch,
|
|
76
|
+
accessToken: string,
|
|
77
|
+
): Promise<Result<string[], string>> =>
|
|
78
|
+
ask(domicile, accessToken, SEND_AS, sendAsSchema);
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Saves the RFC 5322 message `message` as a new draft. Resolves the draft's
|
|
82
|
+
* message id: Gmail's hex number, which its web app opens the draft by.
|
|
83
|
+
*/
|
|
84
|
+
export const createDraft = (
|
|
85
|
+
domicile: Fetch,
|
|
86
|
+
accessToken: string,
|
|
87
|
+
message: string,
|
|
88
|
+
): Promise<Result<string, string>> =>
|
|
89
|
+
ask(domicile, accessToken, DRAFTS, draftSchema, {
|
|
90
|
+
message: { raw: base64Url(new TextEncoder().encode(message)) },
|
|
91
|
+
});
|
|
92
|
+
|
|
63
93
|
/** Asks the Gmail API for message `id`'s sender, subject and arrival. */
|
|
64
94
|
export const mailHeaders = (
|
|
65
95
|
domicile: Fetch,
|
|
@@ -77,15 +107,25 @@ export const mailHeaders = (
|
|
|
77
107
|
headersSchema,
|
|
78
108
|
);
|
|
79
109
|
|
|
80
|
-
|
|
110
|
+
/** GETs `url`, or POSTs `posted` as JSON to it, and reads the answer. */
|
|
111
|
+
const ask = <T extends {}>(
|
|
81
112
|
domicile: Fetch,
|
|
82
113
|
accessToken: string,
|
|
83
114
|
url: string,
|
|
84
115
|
schema: z.ZodType<T, string>,
|
|
116
|
+
posted?: object,
|
|
85
117
|
): Promise<Result<T, string>> =>
|
|
86
118
|
domicile.browser
|
|
87
119
|
.fetch(url, {
|
|
88
|
-
headers: [
|
|
120
|
+
headers: [
|
|
121
|
+
{ name: "Authorization", value: `Bearer ${accessToken}` },
|
|
122
|
+
...(posted === undefined
|
|
123
|
+
? []
|
|
124
|
+
: [{ name: "Content-Type", value: "application/json" }]),
|
|
125
|
+
],
|
|
126
|
+
...(posted === undefined
|
|
127
|
+
? {}
|
|
128
|
+
: { body: JSON.stringify(posted), method: "POST" as const }),
|
|
89
129
|
})
|
|
90
130
|
.then(
|
|
91
131
|
(answer): Result<T, string> => {
|
|
@@ -129,6 +169,50 @@ const unreadSchema = jsonText.pipe(
|
|
|
129
169
|
),
|
|
130
170
|
);
|
|
131
171
|
|
|
172
|
+
const draftSchema = jsonText.pipe(
|
|
173
|
+
z
|
|
174
|
+
.object({ message: z.object({ id: z.string().regex(/^[0-9a-f]+$/) }) })
|
|
175
|
+
.transform(({ message }) => message.id),
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
/** `bytes` in base64url, as the Gmail API takes a raw message. */
|
|
179
|
+
const base64Url = (bytes: Uint8Array): string =>
|
|
180
|
+
btoa(Array.from(bytes, (byte) => String.fromCodePoint(byte)).join(""))
|
|
181
|
+
.replaceAll("+", "-")
|
|
182
|
+
.replaceAll("/", "_");
|
|
183
|
+
|
|
184
|
+
// An alias Gmail has not verified cannot send yet. The primary address has
|
|
185
|
+
// no verification status.
|
|
186
|
+
const sendAsSchema = jsonText.pipe(
|
|
187
|
+
z
|
|
188
|
+
.object({
|
|
189
|
+
sendAs: z.array(
|
|
190
|
+
z.object({
|
|
191
|
+
isPrimary: z.boolean().optional(),
|
|
192
|
+
sendAsEmail: z.string(),
|
|
193
|
+
verificationStatus: z.string().optional(),
|
|
194
|
+
}),
|
|
195
|
+
),
|
|
196
|
+
})
|
|
197
|
+
.transform(({ sendAs }) => [
|
|
198
|
+
...sendAs
|
|
199
|
+
.filter(({ isPrimary }) => isPrimary === true)
|
|
200
|
+
.map(({ sendAsEmail }) => sendAsEmail),
|
|
201
|
+
...sendAs
|
|
202
|
+
.filter(
|
|
203
|
+
({ isPrimary, verificationStatus }) =>
|
|
204
|
+
isPrimary !== true && verificationStatus === "accepted",
|
|
205
|
+
)
|
|
206
|
+
.map(({ sendAsEmail }) => sendAsEmail),
|
|
207
|
+
]),
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* A thread's id: Gmail's hex number, which a press names the thread's row and
|
|
212
|
+
* link by.
|
|
213
|
+
*/
|
|
214
|
+
const threadIdSchema = z.string().regex(/^[0-9a-f]+$/);
|
|
215
|
+
|
|
132
216
|
const headersSchema = jsonText.pipe(
|
|
133
217
|
z
|
|
134
218
|
.object({
|
|
@@ -138,7 +222,7 @@ const headersSchema = jsonText.pipe(
|
|
|
138
222
|
payload: z.object({
|
|
139
223
|
headers: z.array(z.object({ name: z.string(), value: z.string() })),
|
|
140
224
|
}),
|
|
141
|
-
threadId:
|
|
225
|
+
threadId: threadIdSchema,
|
|
142
226
|
})
|
|
143
227
|
.transform(({ id, internalDate, payload, threadId }) => {
|
|
144
228
|
const header = (name: string) =>
|
package/src/google-token.ts
CHANGED
|
@@ -7,7 +7,7 @@ import type {
|
|
|
7
7
|
import { z } from "zod";
|
|
8
8
|
|
|
9
9
|
import type { GoogleClient } from "./consent";
|
|
10
|
-
import { REDIRECT,
|
|
10
|
+
import { REDIRECT, SCOPES } from "./consent";
|
|
11
11
|
import { jsonText } from "./json-text";
|
|
12
12
|
|
|
13
13
|
/** Google's OAuth token endpoint. */
|
|
@@ -50,7 +50,7 @@ export const TokenRefusal = {
|
|
|
50
50
|
}),
|
|
51
51
|
/**
|
|
52
52
|
* Google no longer takes the refresh token, or it was granted without
|
|
53
|
-
* {@link
|
|
53
|
+
* every one of {@link SCOPES}: sign in again.
|
|
54
54
|
*/
|
|
55
55
|
Revoked: () => ({ kind: TokenRefusalKind.Revoked as const }),
|
|
56
56
|
};
|
|
@@ -82,7 +82,7 @@ export const refreshAccess = async (
|
|
|
82
82
|
return parsed(answer, accessSchema)
|
|
83
83
|
.mapErr((why): TokenRefusal => TokenRefusal.Failed(why))
|
|
84
84
|
.andThen((fields) =>
|
|
85
|
-
fields.scope.split(" ").includes(
|
|
85
|
+
SCOPES.every((scope) => fields.scope.split(" ").includes(scope))
|
|
86
86
|
? Ok({
|
|
87
87
|
expiresInMs: fields.expires_in * 1000,
|
|
88
88
|
token: fields.access_token,
|
package/src/held-google.ts
CHANGED
|
@@ -22,6 +22,11 @@ export const INBOX =
|
|
|
22
22
|
export const UNREAD =
|
|
23
23
|
"https://gmail.googleapis.com/gmail/v1/users/me/messages?labelIds=INBOX&labelIds=UNREAD&maxResults=100";
|
|
24
24
|
|
|
25
|
+
export const DRAFTS = "https://gmail.googleapis.com/gmail/v1/users/me/drafts";
|
|
26
|
+
|
|
27
|
+
export const SEND_AS =
|
|
28
|
+
"https://gmail.googleapis.com/gmail/v1/users/me/settings/sendAs";
|
|
29
|
+
|
|
25
30
|
/** The address of message `id`'s sender and subject. */
|
|
26
31
|
export const HEADERS = (id: string) =>
|
|
27
32
|
`https://gmail.googleapis.com/gmail/v1/users/me/messages/${id}?format=metadata&metadataHeaders=From&metadataHeaders=Subject`;
|
package/src/inbox.ts
CHANGED
|
@@ -10,9 +10,16 @@ export enum InboxKind {
|
|
|
10
10
|
export const Inbox = {
|
|
11
11
|
/**
|
|
12
12
|
* Every conversation, and the unread ones, with the newest message of each
|
|
13
|
-
* of the newest unread conversations
|
|
13
|
+
* of the newest unread conversations, and the addresses the account sends
|
|
14
|
+
* from: its own, then its aliases.
|
|
14
15
|
*/
|
|
15
|
-
Counted: (
|
|
16
|
+
Counted: (
|
|
17
|
+
total: number,
|
|
18
|
+
unread: number,
|
|
19
|
+
latest: readonly Mail[] = [],
|
|
20
|
+
addresses: readonly string[] = [],
|
|
21
|
+
) => ({
|
|
22
|
+
addresses,
|
|
16
23
|
kind: InboxKind.Counted as const,
|
|
17
24
|
latest,
|
|
18
25
|
total,
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/** What a `mailto:` link fills a new message in with (RFC 6068). */
|
|
2
|
+
export type MailLink = {
|
|
3
|
+
to: readonly string[];
|
|
4
|
+
cc: readonly string[];
|
|
5
|
+
bcc: readonly string[];
|
|
6
|
+
subject: string;
|
|
7
|
+
body: string;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The recipients, subject and body of the `mailto:` link `link`. A `+` is
|
|
12
|
+
* itself, not a space, as in a link and unlike a form. Fields other than
|
|
13
|
+
* these, and a second subject or body, are left out.
|
|
14
|
+
*/
|
|
15
|
+
export const mailLink = (link: string): MailLink => {
|
|
16
|
+
const [path = "", query = ""] = link
|
|
17
|
+
.replace(/^mailto:/i, "")
|
|
18
|
+
.split(/\?(.*)/s);
|
|
19
|
+
const fields = query
|
|
20
|
+
.split("&")
|
|
21
|
+
.filter((field) => field !== "")
|
|
22
|
+
.map((field) => {
|
|
23
|
+
const [name = "", value = ""] = field.split(/=(.*)/s);
|
|
24
|
+
return [decode(name).toLowerCase(), decode(value)] as const;
|
|
25
|
+
});
|
|
26
|
+
const all = (name: string) =>
|
|
27
|
+
fields
|
|
28
|
+
.filter(([field]) => field === name)
|
|
29
|
+
.flatMap(([, value]) => addresses(value));
|
|
30
|
+
const first = (name: string) =>
|
|
31
|
+
fields.find(([field]) => field === name)?.[1] ?? "";
|
|
32
|
+
return {
|
|
33
|
+
bcc: all("bcc"),
|
|
34
|
+
body: first("body"),
|
|
35
|
+
cc: all("cc"),
|
|
36
|
+
subject: first("subject"),
|
|
37
|
+
to: [...addresses(decode(path)), ...all("to")],
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* An RFC 5322 message from `from`, filled in from `link`: plain text, UTF-8,
|
|
43
|
+
* with lines ending in CRLF.
|
|
44
|
+
*/
|
|
45
|
+
export const draftMessage = (from: string, link: MailLink): string =>
|
|
46
|
+
[
|
|
47
|
+
`From: ${from}`,
|
|
48
|
+
...header("To", link.to.join(", ")),
|
|
49
|
+
...header("Cc", link.cc.join(", ")),
|
|
50
|
+
...header("Bcc", link.bcc.join(", ")),
|
|
51
|
+
...header("Subject", encodedWords(link.subject)),
|
|
52
|
+
"MIME-Version: 1.0",
|
|
53
|
+
'Content-Type: text/plain; charset="UTF-8"',
|
|
54
|
+
"Content-Transfer-Encoding: base64",
|
|
55
|
+
"",
|
|
56
|
+
lines(base64(new TextEncoder().encode(crlf(link.body)))),
|
|
57
|
+
].join("\r\n");
|
|
58
|
+
|
|
59
|
+
/** Header `name`, unless its `value` is empty. */
|
|
60
|
+
const header = (name: string, value: string): string[] =>
|
|
61
|
+
value === "" ? [] : [`${name}: ${value}`];
|
|
62
|
+
|
|
63
|
+
/** A field's addresses, comma-separated. */
|
|
64
|
+
const addresses = (value: string): string[] =>
|
|
65
|
+
value
|
|
66
|
+
.split(",")
|
|
67
|
+
.map((address) => address.trim())
|
|
68
|
+
.filter((address) => address !== "");
|
|
69
|
+
|
|
70
|
+
/** `text` with its percent escapes undone; a broken escape stays as it is. */
|
|
71
|
+
const decode = (text: string): string => {
|
|
72
|
+
try {
|
|
73
|
+
return decodeURIComponent(text);
|
|
74
|
+
} catch {
|
|
75
|
+
return text;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/** `text` as an RFC 2047 encoded word when it isn't plain ASCII. */
|
|
80
|
+
const encodedWords = (text: string): string =>
|
|
81
|
+
/^[\x20-\x7e]*$/.test(text)
|
|
82
|
+
? text
|
|
83
|
+
: `=?UTF-8?B?${base64(new TextEncoder().encode(text))}?=`;
|
|
84
|
+
|
|
85
|
+
/** `text` with every line ending as CRLF. */
|
|
86
|
+
const crlf = (text: string): string => text.replace(/\r?\n/g, "\r\n");
|
|
87
|
+
|
|
88
|
+
/** `bytes` in base64. */
|
|
89
|
+
const base64 = (bytes: Uint8Array): string =>
|
|
90
|
+
btoa(Array.from(bytes, (byte) => String.fromCodePoint(byte)).join(""));
|
|
91
|
+
|
|
92
|
+
/** Base64 `text` in lines of 76, as MIME wants. */
|
|
93
|
+
const lines = (text: string): string =>
|
|
94
|
+
text.match(/.{1,76}/g)?.join("\r\n") ?? "";
|