@open-workspace/create 0.4.2 → 0.5.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 +9 -5
- package/dist/backup-schema.ts +110 -0
- package/dist/workspace.ts +162 -0
- package/import/backup-snapshot.ts +254 -0
- package/import/backup-source.ts +270 -0
- package/import/cli.ts +134 -90
- package/import/concurrency.ts +34 -37
- package/import/csv.ts +1 -1
- package/import/directory/command.ts +293 -72
- package/import/directory/google.ts +158 -120
- package/import/directory/invitation-file.ts +108 -0
- package/import/directory/plan.ts +314 -38
- package/import/directory/read.ts +55 -0
- package/import/gmail.ts +9 -12
- package/import/headers.ts +1 -1
- package/import/idp/apps.ts +244 -0
- package/import/idp/command.ts +161 -0
- package/import/idp/csv.ts +70 -0
- package/import/idp/entra.ts +297 -0
- package/import/idp/keycloak.ts +250 -0
- package/import/idp/model.ts +108 -0
- package/import/idp/okta.ts +159 -0
- package/import/idp/pages.ts +65 -0
- package/import/imap-command.ts +42 -18
- package/import/imap-source.ts +245 -220
- package/import/invariant.ts +1 -1
- package/import/loopback.ts +136 -0
- package/import/mbox-source.ts +106 -55
- package/import/mbox.ts +1 -1
- package/import/oauth.ts +234 -138
- package/import/preview.ts +48 -0
- package/import/uploader.ts +169 -115
- package/import/workspace.ts +78 -15
- package/index.ts +3 -0
- package/module-templates.ts +9 -12
- package/module.ts +1 -5
- package/package.json +13 -2
- package/setup/alchemy.ts +2 -2
- package/setup/args.ts +16 -17
- package/setup/cloudflare-api.ts +75 -19
- package/setup/domains.ts +18 -6
- package/setup/flow.ts +217 -58
- package/setup/migration-guide.ts +255 -0
- package/setup/mx-recovery.ts +115 -0
- package/setup/ui.ts +4 -1
- package/templates.ts +26 -18
package/import/directory/plan.ts
CHANGED
|
@@ -17,6 +17,11 @@ const directoryUserSchema = z.object({
|
|
|
17
17
|
export type DirectoryUser = z.infer<typeof directoryUserSchema>;
|
|
18
18
|
|
|
19
19
|
const directoryGroupSchema = z.object({
|
|
20
|
+
// Groups without mail use their stable source key for membership and retries.
|
|
21
|
+
key: z.string().optional(),
|
|
22
|
+
sourceId: z.string().optional(),
|
|
23
|
+
mailboxSkipReason: z.string().optional(),
|
|
24
|
+
mailboxReadError: z.string().optional(),
|
|
20
25
|
email: z.string(),
|
|
21
26
|
name: z.string(),
|
|
22
27
|
description: z.string().nullable(),
|
|
@@ -25,6 +30,8 @@ const directoryGroupSchema = z.object({
|
|
|
25
30
|
members: z.array(z.string()),
|
|
26
31
|
/** A Google collaborative inbox: a shared inbox rather than a mailing list. */
|
|
27
32
|
sharedInbox: z.boolean(),
|
|
33
|
+
/** Whether members may send as the group; null when settings were not requested. */
|
|
34
|
+
membersCanSend: z.boolean().nullable(),
|
|
28
35
|
/** Who may write to it, when known. */
|
|
29
36
|
posting: z.enum(["anyone", "workspace", "members"]).nullable(),
|
|
30
37
|
});
|
|
@@ -60,7 +67,14 @@ export const workspaceStateSchema = z.object({
|
|
|
60
67
|
})
|
|
61
68
|
),
|
|
62
69
|
teams: z.array(
|
|
63
|
-
z.object({
|
|
70
|
+
z.object({
|
|
71
|
+
id: z.string(),
|
|
72
|
+
slug: z.string(),
|
|
73
|
+
name: z.string(),
|
|
74
|
+
/** A team for everyone keeps no members. */
|
|
75
|
+
everyone: z.boolean(),
|
|
76
|
+
members: z.array(z.object({ userId: z.string(), canSend: z.boolean() })),
|
|
77
|
+
})
|
|
64
78
|
),
|
|
65
79
|
});
|
|
66
80
|
|
|
@@ -74,17 +88,37 @@ export type PlanOptions = {
|
|
|
74
88
|
|
|
75
89
|
export type Action =
|
|
76
90
|
| { kind: "invite"; name: string; email: string }
|
|
77
|
-
| {
|
|
91
|
+
| {
|
|
92
|
+
kind: "alias";
|
|
93
|
+
target: string;
|
|
94
|
+
address: string;
|
|
95
|
+
displayName: string;
|
|
96
|
+
teamId: string | null;
|
|
97
|
+
}
|
|
78
98
|
| {
|
|
79
99
|
kind: "team";
|
|
100
|
+
teamId: string;
|
|
80
101
|
slug: string;
|
|
81
102
|
name: string;
|
|
82
103
|
description: string | null;
|
|
83
104
|
everyone: boolean;
|
|
84
105
|
memberEmails: string[];
|
|
106
|
+
canSend: boolean;
|
|
107
|
+
}
|
|
108
|
+
| {
|
|
109
|
+
/**
|
|
110
|
+
* People of a group missing from the team an earlier, interrupted run
|
|
111
|
+
* made (one of its requests failed); active or invited, never disabled.
|
|
112
|
+
*/
|
|
113
|
+
kind: "team-members";
|
|
114
|
+
teamId: string;
|
|
115
|
+
slug: string;
|
|
116
|
+
memberEmails: string[];
|
|
117
|
+
canSend: boolean;
|
|
85
118
|
}
|
|
86
119
|
| {
|
|
87
120
|
kind: "team-mailbox";
|
|
121
|
+
teamId: string;
|
|
88
122
|
teamSlug: string;
|
|
89
123
|
address: string;
|
|
90
124
|
displayName: string;
|
|
@@ -97,25 +131,94 @@ export type Plan = {
|
|
|
97
131
|
/** Already in the workspace, left as they are. */
|
|
98
132
|
existing: string[];
|
|
99
133
|
skipped: { what: string; reason: string }[];
|
|
134
|
+
sourceErrors: { what: string; reason: string }[];
|
|
100
135
|
/** Directory administrators, to make administrators by hand (that needs admin.access). */
|
|
101
136
|
administrators: string[];
|
|
102
137
|
};
|
|
103
138
|
|
|
104
139
|
const normalize = (address: string) => address.trim().toLowerCase();
|
|
140
|
+
|
|
141
|
+
/** Workspace statuses of people a team may take: not disabled. */
|
|
142
|
+
const joinableStatuses = new Set(["active", "invited"]);
|
|
105
143
|
const domainOf = (address: string) => normalize(address).split("@")[1] ?? "";
|
|
106
144
|
const localPart = (address: string) => normalize(address).split("@")[0] ?? "";
|
|
107
145
|
|
|
108
|
-
/** A team handle from
|
|
109
|
-
|
|
110
|
-
const slug =
|
|
146
|
+
/** A team handle from text: lowercase letters, digits and single dashes, at most `max` long. */
|
|
147
|
+
function slugify(text: string, max = 64) {
|
|
148
|
+
const slug = text
|
|
111
149
|
.replaceAll(/[^a-z0-9]+/gu, "-")
|
|
112
150
|
.replaceAll(/^-+|-+$/gu, "")
|
|
113
|
-
.slice(0,
|
|
151
|
+
.slice(0, max)
|
|
114
152
|
.replace(/-+$/u, "");
|
|
115
153
|
|
|
116
154
|
return slug.length >= 2 ? slug : `team-${slug || "x"}`;
|
|
117
155
|
}
|
|
118
156
|
|
|
157
|
+
/** Keep imported teams' saved handles and allocate new ones within this directory. */
|
|
158
|
+
function planTeams(
|
|
159
|
+
groups: DirectoryGroup[],
|
|
160
|
+
existingTeams: WorkspaceState["teams"]
|
|
161
|
+
) {
|
|
162
|
+
const byId = new Map(existingTeams.map((team) => [team.id, team]));
|
|
163
|
+
const emailBases = new Set<string>();
|
|
164
|
+
const sharedEmailBases = new Set<string>();
|
|
165
|
+
const used = new Set<string>();
|
|
166
|
+
|
|
167
|
+
const entries = groups.map((group) => {
|
|
168
|
+
const key = normalize(group.key ?? group.email);
|
|
169
|
+
|
|
170
|
+
const hash = new Bun.CryptoHasher("sha256")
|
|
171
|
+
.update(group.sourceId ?? key)
|
|
172
|
+
.digest("hex");
|
|
173
|
+
|
|
174
|
+
const teamId = `team_import_${hash}`;
|
|
175
|
+
const existing = byId.get(teamId);
|
|
176
|
+
|
|
177
|
+
if (existing) used.add(existing.slug);
|
|
178
|
+
|
|
179
|
+
const email = normalize(group.email);
|
|
180
|
+
const name = normalize(group.name);
|
|
181
|
+
|
|
182
|
+
const base = email
|
|
183
|
+
? slugify(localPart(email))
|
|
184
|
+
: slugify(/[a-z0-9]/u.test(name) ? name : key);
|
|
185
|
+
|
|
186
|
+
if (email) {
|
|
187
|
+
if (emailBases.has(base)) sharedEmailBases.add(base);
|
|
188
|
+
emailBases.add(base);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return { group, teamId, existing, email, base, slug: existing?.slug ?? "" };
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// Groups with mail get their address-based handles first. Names from other
|
|
195
|
+
// providers can then use the same suffix rule without displacing a mail group.
|
|
196
|
+
for (const entry of [
|
|
197
|
+
...entries.filter((e) => e.email),
|
|
198
|
+
...entries.filter((e) => !e.email),
|
|
199
|
+
]) {
|
|
200
|
+
if (entry.existing) continue;
|
|
201
|
+
const { email, base } = entry;
|
|
202
|
+
|
|
203
|
+
const wanted =
|
|
204
|
+
email && sharedEmailBases.has(base)
|
|
205
|
+
? slugify(`${localPart(email)}-${domainOf(email)}`)
|
|
206
|
+
: base;
|
|
207
|
+
|
|
208
|
+
let slug = wanted;
|
|
209
|
+
|
|
210
|
+
for (let n = 2; used.has(slug); n += 1) {
|
|
211
|
+
const suffix = `-${n}`;
|
|
212
|
+
slug = slugify(wanted, 64 - suffix.length) + suffix;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
used.add(slug);
|
|
216
|
+
entry.slug = slug;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return entries;
|
|
220
|
+
}
|
|
221
|
+
|
|
119
222
|
/** Names must be 2–80 characters. */
|
|
120
223
|
function displayName(name: string, fallback: string) {
|
|
121
224
|
const trimmed = name.trim().slice(0, 80);
|
|
@@ -128,50 +231,118 @@ function displayName(name: string, fallback: string) {
|
|
|
128
231
|
/** A group's members with nested groups flattened: everyone in the organization, or these people. */
|
|
129
232
|
type GroupMembers = { everyone: boolean; people: Set<string> };
|
|
130
233
|
|
|
234
|
+
function mailboxSkipReason(
|
|
235
|
+
group: DirectoryGroup,
|
|
236
|
+
delivery: PlanOptions["groupDelivery"],
|
|
237
|
+
existing: WorkspaceState["teams"][number] | undefined,
|
|
238
|
+
everyone: boolean
|
|
239
|
+
) {
|
|
240
|
+
if (group.mailboxSkipReason || delivery !== "inbox") {
|
|
241
|
+
return group.mailboxSkipReason;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (group.posting && group.posting !== "anyone") {
|
|
245
|
+
return "a shared inbox cannot enforce this posting restriction";
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (group.membersCanSend === false) {
|
|
249
|
+
// Everyone teams always grant send access. A resumed import also keeps
|
|
250
|
+
// the rights of members already on the team.
|
|
251
|
+
if (existing?.everyone ?? everyone) {
|
|
252
|
+
return "an Everyone team cannot have a read-only shared inbox";
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (existing?.members.some((member) => member.canSend)) {
|
|
256
|
+
return "existing team members have send access; review their rights before importing this read-only inbox";
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return undefined;
|
|
261
|
+
}
|
|
262
|
+
|
|
131
263
|
export function planDirectory(
|
|
132
264
|
directory: Directory,
|
|
133
265
|
state: WorkspaceState,
|
|
134
266
|
options: PlanOptions
|
|
135
|
-
)
|
|
267
|
+
) {
|
|
136
268
|
const domains = new Set(state.mailDomains.map((domain) => normalize(domain)));
|
|
137
269
|
|
|
138
|
-
const
|
|
139
|
-
state.
|
|
270
|
+
const owners = new Map(
|
|
271
|
+
state.users.map((user) => [
|
|
272
|
+
user.id,
|
|
273
|
+
normalize(user.personalEmail ?? user.id),
|
|
274
|
+
])
|
|
140
275
|
);
|
|
141
276
|
|
|
277
|
+
const teamNames = new Map(state.teams.map((team) => [team.id, team.name]));
|
|
278
|
+
|
|
279
|
+
const addressOwners = new Map(
|
|
280
|
+
state.mailboxes.map((mailbox) => [
|
|
281
|
+
normalize(mailbox.address),
|
|
282
|
+
mailbox.ownerUserId
|
|
283
|
+
? (owners.get(mailbox.ownerUserId) ?? mailbox.ownerUserId)
|
|
284
|
+
: (mailbox.teamId ?? mailbox.id),
|
|
285
|
+
])
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
function ownerLabel(owner: string) {
|
|
289
|
+
const team = teamNames.get(owner);
|
|
290
|
+
|
|
291
|
+
return team ? `team ${team}` : owner;
|
|
292
|
+
}
|
|
293
|
+
|
|
142
294
|
const knownPeople = new Set(
|
|
143
295
|
state.users.flatMap((user) =>
|
|
144
296
|
user.personalEmail ? [normalize(user.personalEmail)] : []
|
|
145
297
|
)
|
|
146
298
|
);
|
|
147
299
|
|
|
148
|
-
|
|
300
|
+
/** People here a team may take: not a disabled account. */
|
|
301
|
+
const joinable = new Set(
|
|
302
|
+
state.users.flatMap((user) =>
|
|
303
|
+
user.personalEmail && joinableStatuses.has(user.status)
|
|
304
|
+
? [normalize(user.personalEmail)]
|
|
305
|
+
: []
|
|
306
|
+
)
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
const existingSlugs = new Set(state.teams.map((team) => team.slug));
|
|
310
|
+
|
|
311
|
+
const userIdOf = new Map(
|
|
312
|
+
state.users.flatMap((user) =>
|
|
313
|
+
user.personalEmail ? [[normalize(user.personalEmail), user.id]] : []
|
|
314
|
+
)
|
|
315
|
+
);
|
|
316
|
+
|
|
317
|
+
const aliases: Extract<Action, { kind: "alias" }>[] = [];
|
|
149
318
|
|
|
150
319
|
const plan: Plan = {
|
|
151
320
|
actions: [],
|
|
152
321
|
existing: [],
|
|
153
322
|
skipped: [],
|
|
323
|
+
sourceErrors: [],
|
|
154
324
|
administrators: [],
|
|
155
325
|
};
|
|
156
326
|
|
|
157
327
|
const inWorkspace = (address: string) => domains.has(domainOf(address));
|
|
158
328
|
|
|
159
|
-
const claim = (address: string) => {
|
|
329
|
+
const claim = (address: string, owner: string) => {
|
|
160
330
|
const key = normalize(address);
|
|
161
331
|
|
|
162
|
-
if (
|
|
163
|
-
|
|
332
|
+
if (addressOwners.has(key)) return false;
|
|
333
|
+
addressOwners.set(key, owner);
|
|
164
334
|
|
|
165
335
|
return true;
|
|
166
336
|
};
|
|
167
337
|
|
|
168
338
|
// People first: teams name them as members.
|
|
169
|
-
const people = new Set<string>(
|
|
339
|
+
const people = new Set<string>(joinable);
|
|
170
340
|
|
|
171
341
|
for (const user of directory.users) {
|
|
172
342
|
const email = normalize(user.email);
|
|
173
343
|
|
|
174
344
|
if (user.suspended && !options.includeSuspended) {
|
|
345
|
+
people.delete(email);
|
|
175
346
|
plan.skipped.push({ what: email, reason: "suspended" });
|
|
176
347
|
continue;
|
|
177
348
|
}
|
|
@@ -184,9 +355,17 @@ export function planDirectory(
|
|
|
184
355
|
continue;
|
|
185
356
|
}
|
|
186
357
|
|
|
358
|
+
if (knownPeople.has(email) && !joinable.has(email)) {
|
|
359
|
+
plan.skipped.push({
|
|
360
|
+
what: email,
|
|
361
|
+
reason: "disabled here: left off teams",
|
|
362
|
+
});
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
|
|
187
366
|
if (knownPeople.has(email)) {
|
|
188
367
|
plan.existing.push(email);
|
|
189
|
-
} else if (claim(email)) {
|
|
368
|
+
} else if (claim(email, email)) {
|
|
190
369
|
plan.actions.push({
|
|
191
370
|
kind: "invite",
|
|
192
371
|
name: displayName(user.name, email),
|
|
@@ -205,35 +384,44 @@ export function planDirectory(
|
|
|
205
384
|
if (user.isAdmin) plan.administrators.push(email);
|
|
206
385
|
|
|
207
386
|
for (const alias of user.aliases.map(normalize)) {
|
|
387
|
+
const owner = addressOwners.get(alias);
|
|
388
|
+
|
|
208
389
|
if (!inWorkspace(alias)) {
|
|
209
390
|
plan.skipped.push({
|
|
210
391
|
what: alias,
|
|
211
392
|
reason: `alias of ${email} in another domain`,
|
|
212
393
|
});
|
|
213
|
-
} else if (
|
|
214
|
-
|
|
394
|
+
} else if (owner === undefined) {
|
|
395
|
+
claim(alias, email);
|
|
396
|
+
aliases.push({
|
|
215
397
|
kind: "alias",
|
|
216
|
-
|
|
398
|
+
target: email,
|
|
399
|
+
teamId: null,
|
|
217
400
|
address: alias,
|
|
218
401
|
displayName: displayName(user.name, email),
|
|
219
402
|
});
|
|
220
|
-
} else {
|
|
403
|
+
} else if (owner === email) {
|
|
221
404
|
plan.existing.push(alias);
|
|
405
|
+
} else {
|
|
406
|
+
plan.skipped.push({
|
|
407
|
+
what: alias,
|
|
408
|
+
reason: `alias of ${email} already belongs to ${ownerLabel(owner)}`,
|
|
409
|
+
});
|
|
222
410
|
}
|
|
223
411
|
}
|
|
224
412
|
}
|
|
225
413
|
|
|
226
414
|
// Group members, nested groups flattened.
|
|
227
415
|
const groupsByEmail = new Map(
|
|
228
|
-
directory.groups.map((group) => [
|
|
416
|
+
directory.groups.map((group) => [
|
|
417
|
+
normalize(group.key ?? group.email),
|
|
418
|
+
group,
|
|
419
|
+
])
|
|
229
420
|
);
|
|
230
421
|
|
|
231
|
-
const membersOf = (
|
|
232
|
-
group: DirectoryGroup,
|
|
233
|
-
seen = new Set<string>()
|
|
234
|
-
): GroupMembers => {
|
|
422
|
+
const membersOf = (group: DirectoryGroup, seen = new Set<string>()) => {
|
|
235
423
|
const result: GroupMembers = { everyone: false, people: new Set<string>() };
|
|
236
|
-
seen.add(normalize(group.email));
|
|
424
|
+
seen.add(normalize(group.key ?? group.email));
|
|
237
425
|
|
|
238
426
|
for (const member of group.members.map(normalize)) {
|
|
239
427
|
if (member === "*") result.everyone = true;
|
|
@@ -252,12 +440,36 @@ export function planDirectory(
|
|
|
252
440
|
return result;
|
|
253
441
|
};
|
|
254
442
|
|
|
255
|
-
for (const group of
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
443
|
+
for (const { group, teamId, email, existing, slug } of planTeams(
|
|
444
|
+
directory.groups,
|
|
445
|
+
state.teams
|
|
446
|
+
)) {
|
|
447
|
+
const addresses = [email, ...group.aliases.map(normalize)].filter(Boolean);
|
|
259
448
|
const { everyone, people: members } = membersOf(group);
|
|
260
449
|
|
|
450
|
+
if (!existing && existingSlugs.has(slug)) {
|
|
451
|
+
plan.skipped.push({
|
|
452
|
+
what: group.email || group.name,
|
|
453
|
+
reason: `team handle ${slug} already belongs to another team`,
|
|
454
|
+
});
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const taken = addresses.find(
|
|
459
|
+
(address) =>
|
|
460
|
+
addressOwners.has(address) && addressOwners.get(address) !== teamId
|
|
461
|
+
);
|
|
462
|
+
|
|
463
|
+
if (taken) {
|
|
464
|
+
plan.skipped.push({
|
|
465
|
+
what: group.email || group.name,
|
|
466
|
+
reason: `${taken} for team ${group.name} already belongs to ${ownerLabel(addressOwners.get(taken) ?? taken)}`,
|
|
467
|
+
});
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
teamNames.set(teamId, group.name);
|
|
472
|
+
|
|
261
473
|
const outside = group.members
|
|
262
474
|
.map(normalize)
|
|
263
475
|
.filter(
|
|
@@ -267,48 +479,105 @@ export function planDirectory(
|
|
|
267
479
|
|
|
268
480
|
if (outside.length) {
|
|
269
481
|
plan.skipped.push({
|
|
270
|
-
what: `${email} members`,
|
|
482
|
+
what: `${email || group.name} members`,
|
|
271
483
|
reason: `not people here: ${outside.join(", ")}`,
|
|
272
484
|
});
|
|
273
485
|
}
|
|
274
486
|
|
|
275
|
-
|
|
487
|
+
// If Google could not return the policy, defer explicit memberships until
|
|
488
|
+
// the next run can assign their actual send permission.
|
|
489
|
+
const memberEmails = group.mailboxReadError ? [] : [...members];
|
|
490
|
+
const canSend = group.membersCanSend ?? true;
|
|
491
|
+
|
|
492
|
+
if (existing) {
|
|
276
493
|
plan.existing.push(`team ${slug}`);
|
|
494
|
+
const on = new Set(existing.members.map((member) => member.userId));
|
|
495
|
+
|
|
496
|
+
// People invited by this plan, or not added to the team before.
|
|
497
|
+
const missing = existing.everyone
|
|
498
|
+
? []
|
|
499
|
+
: memberEmails.filter((person) => {
|
|
500
|
+
const userId = userIdOf.get(person);
|
|
501
|
+
|
|
502
|
+
return !userId || !on.has(userId);
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
if (missing.length) {
|
|
506
|
+
plan.actions.push({
|
|
507
|
+
kind: "team-members",
|
|
508
|
+
teamId,
|
|
509
|
+
slug,
|
|
510
|
+
memberEmails: missing,
|
|
511
|
+
canSend,
|
|
512
|
+
});
|
|
513
|
+
}
|
|
277
514
|
} else {
|
|
278
|
-
takenSlugs.add(slug);
|
|
279
515
|
plan.actions.push({
|
|
280
516
|
kind: "team",
|
|
517
|
+
teamId,
|
|
281
518
|
slug,
|
|
282
519
|
name: displayName(group.name, email),
|
|
283
520
|
description: group.description?.slice(0, 240) || null,
|
|
284
521
|
everyone,
|
|
285
|
-
memberEmails: everyone ? [] :
|
|
522
|
+
memberEmails: everyone ? [] : memberEmails,
|
|
523
|
+
canSend: everyone || canSend,
|
|
286
524
|
});
|
|
287
525
|
}
|
|
288
526
|
|
|
527
|
+
if (group.mailboxReadError) {
|
|
528
|
+
plan.sourceErrors.push({
|
|
529
|
+
what: addresses.join(", "),
|
|
530
|
+
reason: group.mailboxReadError,
|
|
531
|
+
});
|
|
532
|
+
continue;
|
|
533
|
+
}
|
|
534
|
+
|
|
289
535
|
const delivery = group.sharedInbox ? "inbox" : options.groupDelivery;
|
|
290
536
|
|
|
537
|
+
const reason = mailboxSkipReason(group, delivery, existing, everyone);
|
|
538
|
+
|
|
539
|
+
if (reason) {
|
|
540
|
+
plan.skipped.push({ what: addresses.join(", "), reason });
|
|
541
|
+
continue;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
const target = addresses.find(inWorkspace);
|
|
545
|
+
|
|
291
546
|
for (const address of addresses) {
|
|
292
547
|
if (!inWorkspace(address)) {
|
|
293
548
|
plan.skipped.push({
|
|
294
549
|
what: address,
|
|
295
550
|
reason: `${domainOf(address)} is not one of the workspace's mail domains`,
|
|
296
551
|
});
|
|
297
|
-
} else if (
|
|
552
|
+
} else if (addressOwners.has(address)) {
|
|
553
|
+
plan.existing.push(address);
|
|
554
|
+
} else if (target && address !== target) {
|
|
555
|
+
claim(address, teamId);
|
|
556
|
+
aliases.push({
|
|
557
|
+
kind: "alias",
|
|
558
|
+
target,
|
|
559
|
+
teamId,
|
|
560
|
+
address,
|
|
561
|
+
displayName: displayName(group.name, email),
|
|
562
|
+
});
|
|
563
|
+
} else {
|
|
564
|
+
claim(address, teamId);
|
|
298
565
|
plan.actions.push({
|
|
299
566
|
kind: "team-mailbox",
|
|
567
|
+
teamId,
|
|
300
568
|
teamSlug: slug,
|
|
301
569
|
address,
|
|
302
570
|
displayName: displayName(group.name, email),
|
|
303
571
|
delivery,
|
|
304
572
|
listPosting: group.posting ?? "workspace",
|
|
305
573
|
});
|
|
306
|
-
} else {
|
|
307
|
-
plan.existing.push(address);
|
|
308
574
|
}
|
|
309
575
|
}
|
|
310
576
|
}
|
|
311
577
|
|
|
578
|
+
// All alias targets must exist before the executor resolves their mailbox IDs.
|
|
579
|
+
plan.actions.push(...aliases);
|
|
580
|
+
|
|
312
581
|
return plan;
|
|
313
582
|
}
|
|
314
583
|
|
|
@@ -326,13 +595,20 @@ export function describePlan(plan: Plan) {
|
|
|
326
595
|
}
|
|
327
596
|
|
|
328
597
|
case "alias": {
|
|
329
|
-
lines.push(`
|
|
598
|
+
lines.push(`alias ${action.address} → ${action.target}`);
|
|
330
599
|
break;
|
|
331
600
|
}
|
|
332
601
|
|
|
333
602
|
case "team": {
|
|
334
603
|
lines.push(
|
|
335
|
-
`team ${action.name} (${action.slug}): ${action.everyone ? "everyone" : `${action.memberEmails.length} members`}`
|
|
604
|
+
`team ${action.name} (${action.slug}): ${action.everyone ? "everyone" : `${action.memberEmails.length} members`} (team inboxes: ${action.canSend ? "read and send" : "read only"})`
|
|
605
|
+
);
|
|
606
|
+
break;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
case "team-members": {
|
|
610
|
+
lines.push(
|
|
611
|
+
`members team ${action.slug} (team inboxes: ${action.canSend ? "read and send" : "read only"}): add ${action.memberEmails.join(", ")}`
|
|
336
612
|
);
|
|
337
613
|
break;
|
|
338
614
|
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
2
|
+
|
|
3
|
+
import type { Fetch } from "../oauth.ts";
|
|
4
|
+
|
|
5
|
+
function retryDelay(headers: Headers, attempt: number) {
|
|
6
|
+
const after = headers.get("retry-after")?.trim();
|
|
7
|
+
|
|
8
|
+
if (after) {
|
|
9
|
+
const ms = /^\d+$/u.test(after)
|
|
10
|
+
? Number(after) * 1000
|
|
11
|
+
: Date.parse(after) - Date.now();
|
|
12
|
+
|
|
13
|
+
if (!Number.isNaN(ms)) return Math.max(0, ms);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Okta gives the end of its quota window in Unix seconds.
|
|
17
|
+
const reset = headers.get("x-rate-limit-reset")?.trim();
|
|
18
|
+
|
|
19
|
+
if (reset && /^\d+$/u.test(reset)) {
|
|
20
|
+
return Math.max(0, Number(reset) * 1000 - Date.now());
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return 500 * 2 ** attempt;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Repeat just the throttled GET, keeping the provider's page token and headers. */
|
|
27
|
+
export async function readDirectoryPage(
|
|
28
|
+
fetchImpl: Fetch,
|
|
29
|
+
url: string,
|
|
30
|
+
init: RequestInit,
|
|
31
|
+
retryForbidden?: (response: Response) => Promise<boolean>
|
|
32
|
+
) {
|
|
33
|
+
for (let attempt = 0; ; attempt++) {
|
|
34
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- wait for this attempt before deciding to retry
|
|
35
|
+
const response = await fetchImpl(url, init);
|
|
36
|
+
|
|
37
|
+
if (attempt === 3) return response;
|
|
38
|
+
|
|
39
|
+
if (response.status !== 429) {
|
|
40
|
+
if (response.status !== 403 || !retryForbidden) return response;
|
|
41
|
+
|
|
42
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- the provider distinguishes quotas from denied access in the body
|
|
43
|
+
if (!(await retryForbidden(response))) return response;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const waitMs = retryDelay(response.headers, attempt);
|
|
47
|
+
|
|
48
|
+
// Leave longer limits to the caller instead of retrying before the provider allows it.
|
|
49
|
+
if (waitMs > 60_000) return response;
|
|
50
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- release this response before the next GET
|
|
51
|
+
await response.body?.cancel();
|
|
52
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- respect the provider's retry window
|
|
53
|
+
await delay(waitMs);
|
|
54
|
+
}
|
|
55
|
+
}
|
package/import/gmail.ts
CHANGED
|
@@ -102,14 +102,11 @@ export type GmailOptions = {
|
|
|
102
102
|
};
|
|
103
103
|
|
|
104
104
|
/**
|
|
105
|
-
* A message's place from its labels: Trash, then Spam, then the Inbox, then
|
|
106
|
-
*
|
|
107
|
-
*
|
|
105
|
+
* A message's place from its labels: Trash, then Spam, then the Inbox, then Sent.
|
|
106
|
+
* A sent message can contribute a thread label, but only received mail
|
|
107
|
+
* appears in an Inbox folder.
|
|
108
108
|
*/
|
|
109
|
-
export function gmailMessage(
|
|
110
|
-
labels: string[],
|
|
111
|
-
options: GmailOptions
|
|
112
|
-
): GmailMessage {
|
|
109
|
+
export function gmailMessage(labels: string[], options: GmailOptions) {
|
|
113
110
|
const names = new Set(labels.map(systemName));
|
|
114
111
|
|
|
115
112
|
const userLabels = [
|
|
@@ -133,10 +130,10 @@ export function gmailMessage(
|
|
|
133
130
|
? "spam"
|
|
134
131
|
: names.has("inbox")
|
|
135
132
|
? "inbox"
|
|
136
|
-
:
|
|
137
|
-
? "
|
|
138
|
-
:
|
|
139
|
-
? "
|
|
133
|
+
: sent
|
|
134
|
+
? "sent"
|
|
135
|
+
: userLabels.length && options.archivedLabels === "folder"
|
|
136
|
+
? "inbox"
|
|
140
137
|
: "archive";
|
|
141
138
|
|
|
142
139
|
const seen = names.has("unread") ? false : names.has("opened") ? true : null;
|
|
@@ -170,7 +167,7 @@ export class ThreadFolders {
|
|
|
170
167
|
* The thread's folder: the first of `priority` it carries, else the label
|
|
171
168
|
* on most of its messages, else (ties) the first alphabetically.
|
|
172
169
|
*/
|
|
173
|
-
folder(thread: string, fallback: string[] = [])
|
|
170
|
+
folder(thread: string, fallback: string[] = []) {
|
|
174
171
|
const tally =
|
|
175
172
|
this.counts.get(thread) ?? new Map(fallback.map((label) => [label, 1]));
|
|
176
173
|
|
package/import/headers.ts
CHANGED
|
@@ -32,7 +32,7 @@ function decodeHeader(bytes: Uint8Array) {
|
|
|
32
32
|
export type HeaderFields = Map<string, string[]>;
|
|
33
33
|
|
|
34
34
|
/** Unfolded header fields by lowercased name, in order. */
|
|
35
|
-
export function parseHeaders(text: string)
|
|
35
|
+
export function parseHeaders(text: string) {
|
|
36
36
|
const fields: HeaderFields = new Map();
|
|
37
37
|
|
|
38
38
|
for (const line of text.replaceAll(/\r?\n[\t ]+/gu, " ").split(/\r?\n/u)) {
|