@anthusai/papyrus 5.135.0-next.7 → 5.136.0-next.1
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/amplify/plugins/newsletter/config.js +3 -10
- package/amplify/plugins/newsletter/config.ts +3 -10
- package/amplify/plugins/newsletter/papyrus.plugin.js +0 -1
- package/amplify/plugins/newsletter/papyrus.plugin.ts +0 -1
- package/amplify/plugins/notifications/function/handler.py +2 -2
- package/amplify/plugins/notifications/papyrus.plugin.js +55 -2
- package/amplify/plugins/notifications/papyrus.plugin.ts +58 -2
- package/lib/notifications-api.ts +43 -5
- package/package.json +1 -1
- package/routes.manifest.json +1 -1
|
@@ -21,7 +21,7 @@ const THEME_KEYS = [
|
|
|
21
21
|
"footerWordmarkWidth",
|
|
22
22
|
];
|
|
23
23
|
const KNOWN_KEYS = new Set([
|
|
24
|
-
"name", "siteId", "siteOrigin", "basePath", "computeRoleArn", "sendingDomain", "fromAddress", "fromName", "
|
|
24
|
+
"name", "siteId", "siteOrigin", "basePath", "computeRoleArn", "sendingDomain", "fromAddress", "fromName", "theme", "lists",
|
|
25
25
|
"configurationSet", "eventTopicName", "itemUrlStyle",
|
|
26
26
|
]);
|
|
27
27
|
const LIST_KEYS = {
|
|
@@ -169,7 +169,7 @@ export function resolveRule(raw, label) {
|
|
|
169
169
|
...(rule.types === undefined ? {} : { types: stringList(rule.types, `${label}.types`) }),
|
|
170
170
|
};
|
|
171
171
|
}
|
|
172
|
-
function resolveList(raw, index
|
|
172
|
+
function resolveList(raw, index) {
|
|
173
173
|
const label = `lists[${index}]`;
|
|
174
174
|
if (typeof raw !== "object" || raw === null || Array.isArray(raw))
|
|
175
175
|
fail(`${label} must be an object`);
|
|
@@ -198,9 +198,6 @@ function resolveList(raw, index, instantAlerts) {
|
|
|
198
198
|
const approval = list.approval === undefined ? "admin" : list.approval;
|
|
199
199
|
if (approval !== "admin" && approval !== "none")
|
|
200
200
|
fail(`${label}.approval must be "admin" or "none"`);
|
|
201
|
-
if (approval === "none" && !instantAlerts) {
|
|
202
|
-
fail(`${label}.approval "none" sends alerts without an admin approving them: set instantAlerts: true on the plugin first`);
|
|
203
|
-
}
|
|
204
201
|
return { ...base, kind, rule: resolveRule(list.rule, `${label}.rule`), approval };
|
|
205
202
|
}
|
|
206
203
|
export function resolveNewsletterConfig(request) {
|
|
@@ -216,12 +213,9 @@ export function resolveNewsletterConfig(request) {
|
|
|
216
213
|
if (extra.length > 0 || !local || local.length > 64 || !LOCAL_PART.test(local) || domain !== sendingDomain) {
|
|
217
214
|
fail(`fromAddress must be an address on ${sendingDomain} (lowercase, such as newsletter@${sendingDomain})`);
|
|
218
215
|
}
|
|
219
|
-
if (raw.instantAlerts !== undefined && typeof raw.instantAlerts !== "boolean")
|
|
220
|
-
fail("instantAlerts must be a boolean");
|
|
221
|
-
const instantAlerts = raw.instantAlerts === true;
|
|
222
216
|
if (!Array.isArray(raw.lists) || raw.lists.length === 0 || raw.lists.length > 20)
|
|
223
217
|
fail("lists must be a list of 1 to 20 lists");
|
|
224
|
-
const lists = raw.lists.map((entry, index) => resolveList(entry, index
|
|
218
|
+
const lists = raw.lists.map((entry, index) => resolveList(entry, index));
|
|
225
219
|
const duplicates = lists.map((list) => list.id).filter((id, index, ids) => ids.indexOf(id) !== index);
|
|
226
220
|
if (duplicates.length > 0)
|
|
227
221
|
fail(`lists has duplicate ids: ${[...new Set(duplicates)].join(", ")}`);
|
|
@@ -236,7 +230,6 @@ export function resolveNewsletterConfig(request) {
|
|
|
236
230
|
sendingDomain,
|
|
237
231
|
fromAddress,
|
|
238
232
|
fromName: text(raw.fromName, "fromName", 80),
|
|
239
|
-
instantAlerts,
|
|
240
233
|
theme: resolveTheme(raw.theme),
|
|
241
234
|
lists,
|
|
242
235
|
itemUrlStyle: itemUrlStyle(raw.itemUrlStyle),
|
|
@@ -85,7 +85,6 @@ export type NewsletterConfig = {
|
|
|
85
85
|
fromAddress: string;
|
|
86
86
|
fromName: string;
|
|
87
87
|
/** A site must set this to true before any list may send without approval. */
|
|
88
|
-
instantAlerts: boolean;
|
|
89
88
|
theme: NewsletterTheme;
|
|
90
89
|
lists: NewsletterList[];
|
|
91
90
|
/** How the site addresses its item pages (`urls.style` in projection.json): roundup drafts link to published items with it. */
|
|
@@ -97,7 +96,7 @@ export type NewsletterConfig = {
|
|
|
97
96
|
};
|
|
98
97
|
|
|
99
98
|
const KNOWN_KEYS = new Set([
|
|
100
|
-
"name", "siteId", "siteOrigin", "basePath", "computeRoleArn", "sendingDomain", "fromAddress", "fromName", "
|
|
99
|
+
"name", "siteId", "siteOrigin", "basePath", "computeRoleArn", "sendingDomain", "fromAddress", "fromName", "theme", "lists",
|
|
101
100
|
"configurationSet", "eventTopicName", "itemUrlStyle",
|
|
102
101
|
]);
|
|
103
102
|
const LIST_KEYS = {
|
|
@@ -235,7 +234,7 @@ export function resolveRule(raw: unknown, label: string): NewsletterRule {
|
|
|
235
234
|
};
|
|
236
235
|
}
|
|
237
236
|
|
|
238
|
-
function resolveList(raw: unknown, index: number
|
|
237
|
+
function resolveList(raw: unknown, index: number): NewsletterList {
|
|
239
238
|
const label = `lists[${index}]`;
|
|
240
239
|
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) fail(`${label} must be an object`);
|
|
241
240
|
const kind = (raw as Record<string, unknown>).kind;
|
|
@@ -261,9 +260,6 @@ function resolveList(raw: unknown, index: number, instantAlerts: boolean): Newsl
|
|
|
261
260
|
}
|
|
262
261
|
const approval = list.approval === undefined ? "admin" : list.approval;
|
|
263
262
|
if (approval !== "admin" && approval !== "none") fail(`${label}.approval must be "admin" or "none"`);
|
|
264
|
-
if (approval === "none" && !instantAlerts) {
|
|
265
|
-
fail(`${label}.approval "none" sends alerts without an admin approving them: set instantAlerts: true on the plugin first`);
|
|
266
|
-
}
|
|
267
263
|
return { ...base, kind, rule: resolveRule(list.rule, `${label}.rule`), approval };
|
|
268
264
|
}
|
|
269
265
|
|
|
@@ -280,10 +276,8 @@ export function resolveNewsletterConfig(request: PluginRequest): NewsletterConfi
|
|
|
280
276
|
if (extra.length > 0 || !local || local.length > 64 || !LOCAL_PART.test(local) || domain !== sendingDomain) {
|
|
281
277
|
fail(`fromAddress must be an address on ${sendingDomain} (lowercase, such as newsletter@${sendingDomain})`);
|
|
282
278
|
}
|
|
283
|
-
if (raw.instantAlerts !== undefined && typeof raw.instantAlerts !== "boolean") fail("instantAlerts must be a boolean");
|
|
284
|
-
const instantAlerts = raw.instantAlerts === true;
|
|
285
279
|
if (!Array.isArray(raw.lists) || raw.lists.length === 0 || raw.lists.length > 20) fail("lists must be a list of 1 to 20 lists");
|
|
286
|
-
const lists = raw.lists.map((entry, index) => resolveList(entry, index
|
|
280
|
+
const lists = raw.lists.map((entry, index) => resolveList(entry, index));
|
|
287
281
|
const duplicates = lists.map((list) => list.id).filter((id, index, ids) => ids.indexOf(id) !== index);
|
|
288
282
|
if (duplicates.length > 0) fail(`lists has duplicate ids: ${[...new Set(duplicates)].join(", ")}`);
|
|
289
283
|
const roundups = lists.filter((list) => list.kind === "roundup");
|
|
@@ -296,7 +290,6 @@ export function resolveNewsletterConfig(request: PluginRequest): NewsletterConfi
|
|
|
296
290
|
sendingDomain,
|
|
297
291
|
fromAddress,
|
|
298
292
|
fromName: text(raw.fromName, "fromName", 80),
|
|
299
|
-
instantAlerts,
|
|
300
293
|
theme: resolveTheme(raw.theme),
|
|
301
294
|
lists,
|
|
302
295
|
itemUrlStyle: itemUrlStyle(raw.itemUrlStyle),
|
|
@@ -71,7 +71,6 @@ export function newsletterEnvironment(config, tableName, keyParameter) {
|
|
|
71
71
|
FROM_NAME: config.fromName,
|
|
72
72
|
CONFIGURATION_SET: config.configurationSet,
|
|
73
73
|
ITEM_URL_STYLE: config.itemUrlStyle,
|
|
74
|
-
INSTANT_ALERTS: config.instantAlerts ? "true" : "false",
|
|
75
74
|
LISTS_JSON: JSON.stringify(config.lists.map((list) => ({
|
|
76
75
|
id: list.id,
|
|
77
76
|
name: list.name,
|
|
@@ -84,7 +84,6 @@ export function newsletterEnvironment(config: NewsletterConfig, tableName: strin
|
|
|
84
84
|
FROM_NAME: config.fromName,
|
|
85
85
|
CONFIGURATION_SET: config.configurationSet,
|
|
86
86
|
ITEM_URL_STYLE: config.itemUrlStyle,
|
|
87
|
-
INSTANT_ALERTS: config.instantAlerts ? "true" : "false",
|
|
88
87
|
LISTS_JSON: JSON.stringify(
|
|
89
88
|
config.lists.map((list) => ({
|
|
90
89
|
id: list.id,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"""Bundle entry kept for the shared Python bundler, which copies ``handler.py`` to the asset root.
|
|
2
2
|
|
|
3
|
-
The Lambdas' handlers are ``papyrus_content.notifications.handler.{router,email,flush,ses_events}``
|
|
3
|
+
The Lambdas' handlers are ``papyrus_content.notifications.handler.{router,email,x,flush,ses_events}``
|
|
4
4
|
(src/papyrus_content/notifications/handler.py).
|
|
5
5
|
"""
|
|
6
6
|
|
|
7
|
-
from papyrus_content.notifications.handler import email, flush, router, ses_events # noqa: F401
|
|
7
|
+
from papyrus_content.notifications.handler import email, flush, router, ses_events, x # noqa: F401
|
|
@@ -17,11 +17,14 @@ import { ALARM_MINIMUM_SENDS, BOUNCE_RATE_THRESHOLD, COMPLAINT_RATE_THRESHOLD }
|
|
|
17
17
|
import { postalAddressParameterArns } from "../newsletter/sending.js";
|
|
18
18
|
import { resolveNotificationsConfig } from "./config.js";
|
|
19
19
|
import { siteApiEnvironment } from "../../site-api-access.js";
|
|
20
|
+
import { X_ENDPOINTS, announcementsParameterPrefix } from "../../functions/announcements/wire.js";
|
|
21
|
+
import { ANNOUNCEMENTS_TIMEOUT_SECONDS } from "../../functions/announcements/resource.js";
|
|
20
22
|
export { resolveNotificationsConfig };
|
|
21
23
|
export const NOTIFICATIONS_OUTPUT_KEY = "notifications";
|
|
22
24
|
export const HANDLERS = {
|
|
23
25
|
router: "papyrus_content.notifications.handler.router",
|
|
24
26
|
email: "papyrus_content.notifications.handler.email",
|
|
27
|
+
x: "papyrus_content.notifications.handler.x",
|
|
25
28
|
flush: "papyrus_content.notifications.handler.flush",
|
|
26
29
|
sesEvents: "papyrus_content.notifications.handler.ses_events",
|
|
27
30
|
pause: "papyrus_content.notifications.handler.pause",
|
|
@@ -33,6 +36,9 @@ export const NOTIFICATIONS_REQUIREMENTS = [CADUCUS_REQUIREMENT, "PyYAML>=6.0.2,<
|
|
|
33
36
|
const FUNCTION_DIRECTORY = "../plugins/notifications/function";
|
|
34
37
|
export const EMAIL_TIMEOUT_SECONDS = 120;
|
|
35
38
|
export const EMAIL_MAX_RECEIVE_COUNT = 3;
|
|
39
|
+
/** One post can wait out X's rate limit inside the consumer (Caducus sleeps until `x-rate-limit-reset`), and X's windows are fifteen minutes: the Lambda maximum. */
|
|
40
|
+
export const X_TIMEOUT_SECONDS = 900;
|
|
41
|
+
export const X_MAX_RECEIVE_COUNT = 3;
|
|
36
42
|
export const notificationsManifest = {
|
|
37
43
|
name: "notifications",
|
|
38
44
|
contract: 1,
|
|
@@ -50,6 +56,7 @@ export function overrideNotificationsCodeForTests(code) {
|
|
|
50
56
|
}
|
|
51
57
|
export const notificationsFunctionName = (siteId) => `papyrus-notifications-${siteId}`;
|
|
52
58
|
export const notificationsEmailName = (siteId) => `papyrus-notifications-email-${siteId}`;
|
|
59
|
+
export const notificationsXName = (siteId) => `papyrus-notifications-x-${siteId}`;
|
|
53
60
|
export const notificationsFlushName = (siteId) => `papyrus-notifications-flush-${siteId}`;
|
|
54
61
|
export const notificationsSesEventsName = (siteId) => `papyrus-notifications-ses-${siteId}`;
|
|
55
62
|
export const notificationsPauseName = (siteId) => `papyrus-notifications-pause-${siteId}`;
|
|
@@ -62,6 +69,7 @@ export const notificationsKeyParameterName = (siteId) => `/papyrus/${siteId}/not
|
|
|
62
69
|
export const legacyNewsletterKeyParameterName = (siteId) => `/papyrus/${siteId}/newsletter-key`;
|
|
63
70
|
export const notificationsAlarmNames = (siteId) => ({
|
|
64
71
|
emailDeadLetters: `papyrus-notifications-${siteId}-email-dlq`,
|
|
72
|
+
xDeadLetters: `papyrus-notifications-${siteId}-x-dlq`,
|
|
65
73
|
routerDeadLetters: `papyrus-notifications-${siteId}-router-dlq`,
|
|
66
74
|
});
|
|
67
75
|
/** The pause alarms, created only where the newsletter plugin is absent (python: `pause.alarm_names`). */
|
|
@@ -182,6 +190,22 @@ export const notificationsPlugin = {
|
|
|
182
190
|
enforceSSL: true,
|
|
183
191
|
deadLetterQueue: { queue: emailDeadLetters, maxReceiveCount: EMAIL_MAX_RECEIVE_COUNT },
|
|
184
192
|
});
|
|
193
|
+
// X: its own queue, so one consumer at a time posts for the site's one account (the budget is charged one post after another), and its dead letter queue.
|
|
194
|
+
const xDeadLetters = new Queue(stack, "XDeadLetterQueue", {
|
|
195
|
+
queueName: `papyrus-notifications-${config.siteId}-x-dlq`,
|
|
196
|
+
retentionPeriod: Duration.days(14),
|
|
197
|
+
encryption: QueueEncryption.SQS_MANAGED,
|
|
198
|
+
enforceSSL: true,
|
|
199
|
+
});
|
|
200
|
+
const xQueue = new Queue(stack, "XQueue", {
|
|
201
|
+
queueName: `papyrus-notifications-${config.siteId}-x`,
|
|
202
|
+
visibilityTimeout: Duration.seconds(X_TIMEOUT_SECONDS * 6),
|
|
203
|
+
encryption: QueueEncryption.SQS_MANAGED,
|
|
204
|
+
enforceSSL: true,
|
|
205
|
+
deadLetterQueue: { queue: xDeadLetters, maxReceiveCount: X_MAX_RECEIVE_COUNT },
|
|
206
|
+
});
|
|
207
|
+
// The announcements function's model tables: the settings (pause, budget, prices), the account connection and the spend counter. The consumer has no signed-in person, so it reads and writes them with its own role.
|
|
208
|
+
const announcementTables = backend.data.resources.tables;
|
|
185
209
|
const routerDeadLetters = new Queue(stack, "RouterDeadLetterQueue", {
|
|
186
210
|
queueName: `papyrus-notifications-${config.siteId}-router-dlq`,
|
|
187
211
|
retentionPeriod: Duration.days(14),
|
|
@@ -191,6 +215,12 @@ export const notificationsPlugin = {
|
|
|
191
215
|
const environment = {
|
|
192
216
|
...notificationsEnvironment(config, { table: table.tableName, operators: operatorsParameter, key: keyParameter.parameterName }),
|
|
193
217
|
EMAIL_QUEUE_URL: emailQueue.queueUrl,
|
|
218
|
+
X_QUEUE_URL: xQueue.queueUrl,
|
|
219
|
+
...X_ENDPOINTS,
|
|
220
|
+
ANNOUNCEMENT_SETTINGS_TABLE: announcementTables.AnnouncementSettings.tableName,
|
|
221
|
+
X_CONNECTION_TABLE: announcementTables.XConnection.tableName,
|
|
222
|
+
ANNOUNCEMENT_SPEND_TABLE: announcementTables.AnnouncementSpend.tableName,
|
|
223
|
+
X_REFRESH_LEASE_SECONDS: String(ANNOUNCEMENTS_TIMEOUT_SECONDS),
|
|
194
224
|
...siteApiEnvironment(context.graphqlEndpoint),
|
|
195
225
|
};
|
|
196
226
|
const common = { runtime: Runtime.PYTHON_3_12, architecture: Architecture.ARM_64, code, environment };
|
|
@@ -215,6 +245,17 @@ export const notificationsPlugin = {
|
|
|
215
245
|
description: `Mails queued notifications for ${config.siteId}, paced under the SES send rate`,
|
|
216
246
|
});
|
|
217
247
|
email.addEventSource(new SqsEventSource(emailQueue, { batchSize: 10, reportBatchItemFailures: true }));
|
|
248
|
+
const x = new LambdaFunction(stack, "XFunction", {
|
|
249
|
+
...common,
|
|
250
|
+
functionName: notificationsXName(config.siteId),
|
|
251
|
+
handler: HANDLERS.x,
|
|
252
|
+
timeout: Duration.seconds(X_TIMEOUT_SECONDS),
|
|
253
|
+
memorySize: 512,
|
|
254
|
+
// One post at a time: the budget is charged one post after another, and a post whose outcome is unknown is checked before it is repeated.
|
|
255
|
+
reservedConcurrentExecutions: 1,
|
|
256
|
+
description: `Posts queued announcements for ${config.siteId} to its connected X account`,
|
|
257
|
+
});
|
|
258
|
+
x.addEventSource(new SqsEventSource(xQueue, { batchSize: 1, reportBatchItemFailures: true }));
|
|
218
259
|
const flush = new LambdaFunction(stack, "FlushFunction", {
|
|
219
260
|
...common,
|
|
220
261
|
functionName: notificationsFlushName(config.siteId),
|
|
@@ -255,13 +296,24 @@ export const notificationsPlugin = {
|
|
|
255
296
|
parameterArns.push(`arn:${stack.partition}:ssm:${stack.region}:${stack.account}:parameter${legacyNewsletterKeyParameterName(config.siteId)}`);
|
|
256
297
|
}
|
|
257
298
|
// Every function builds the same runtime (the audience needs the recipient key), and the SES events suppress readers.
|
|
258
|
-
for (const fn of [router, email, flush, sesEvents]) {
|
|
299
|
+
for (const fn of [router, email, x, flush, sesEvents]) {
|
|
259
300
|
table.grantReadWriteData(fn); // includes the table key's encrypt and decrypt
|
|
260
301
|
fn.addToRolePolicy(new PolicyStatement({ effect: Effect.ALLOW, actions: ["ssm:GetParameter"], resources: parameterArns }));
|
|
261
302
|
}
|
|
262
303
|
// A reader who lifts a bounce or complaint by opting in again is cleared from the SES account suppression list too.
|
|
263
304
|
// The action has no resource-level form (as for the newsletter function).
|
|
264
305
|
router.addToRolePolicy(new PolicyStatement({ effect: Effect.ALLOW, actions: ["ses:DeleteSuppressedDestination"], resources: ["*"] }));
|
|
306
|
+
// The X consumer reads the account's connection, its token parameters (a refresh saves the next one), the settings and the spend counter, and nothing else of the site's.
|
|
307
|
+
announcementTables.AnnouncementSettings.grantReadData(x);
|
|
308
|
+
announcementTables.XConnection.grantReadWriteData(x); // a refresh takes the connection's lease and records its result
|
|
309
|
+
announcementTables.AnnouncementSettings.grantReadWriteData(x); // a refresh X refuses pauses announcements
|
|
310
|
+
announcementTables.AnnouncementSpend.grantReadWriteData(x);
|
|
311
|
+
x.addToRolePolicy(new PolicyStatement({
|
|
312
|
+
effect: Effect.ALLOW,
|
|
313
|
+
actions: ["ssm:GetParameter", "ssm:PutParameter"],
|
|
314
|
+
resources: [`arn:${stack.partition}:ssm:${stack.region}:${stack.account}:parameter${announcementsParameterPrefix(config.siteId)}/*`],
|
|
315
|
+
}));
|
|
316
|
+
xQueue.grantSendMessages(router);
|
|
265
317
|
emailQueue.grantSendMessages(router);
|
|
266
318
|
emailQueue.grantSendMessages(flush);
|
|
267
319
|
emailQueue.grantSendMessages(email); // a fan-out page queues its deliveries and the next page
|
|
@@ -339,6 +391,7 @@ export const notificationsPlugin = {
|
|
|
339
391
|
const alarmNames = notificationsAlarmNames(config.siteId);
|
|
340
392
|
for (const [id, alarmName, queue] of [
|
|
341
393
|
["EmailDeadLetterAlarm", alarmNames.emailDeadLetters, emailDeadLetters],
|
|
394
|
+
["XDeadLetterAlarm", alarmNames.xDeadLetters, xDeadLetters],
|
|
342
395
|
["RouterDeadLetterAlarm", alarmNames.routerDeadLetters, routerDeadLetters],
|
|
343
396
|
]) {
|
|
344
397
|
new Alarm(stack, id, {
|
|
@@ -359,7 +412,7 @@ export const notificationsPlugin = {
|
|
|
359
412
|
}
|
|
360
413
|
// The CMS routes are the only caller: a publish tells this function nothing (the announce agent is woken by the Item change feed).
|
|
361
414
|
router.addPermission("InvokeFromCmsCompute", { principal: new ArnPrincipal(config.computeRoleArn), action: "lambda:InvokeFunction" });
|
|
362
|
-
backend.addOutput({ custom: { [NOTIFICATIONS_OUTPUT_KEY]: { actionsFunction: router.functionName } } });
|
|
415
|
+
backend.addOutput({ custom: { [NOTIFICATIONS_OUTPUT_KEY]: { actionsFunction: router.functionName, xQueueUrl: xQueue.queueUrl } } });
|
|
363
416
|
},
|
|
364
417
|
};
|
|
365
418
|
function installRequirements(requirementsPath, outputDir) {
|
|
@@ -7,6 +7,7 @@ import { ArnPrincipal, Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
|
|
|
7
7
|
import { Key } from "aws-cdk-lib/aws-kms";
|
|
8
8
|
import { Architecture, type Code, Function as LambdaFunction, Runtime } from "aws-cdk-lib/aws-lambda";
|
|
9
9
|
import { SqsEventSource } from "aws-cdk-lib/aws-lambda-event-sources";
|
|
10
|
+
import type { ITable } from "aws-cdk-lib/aws-dynamodb";
|
|
10
11
|
import { Queue, QueueEncryption } from "aws-cdk-lib/aws-sqs";
|
|
11
12
|
import { execFileSync } from "node:child_process";
|
|
12
13
|
import path from "node:path";
|
|
@@ -18,6 +19,8 @@ import { postalAddressParameterArns } from "../newsletter/sending";
|
|
|
18
19
|
import type { PapyrusPlugin, PluginManifest, PluginWiringContext } from "../types";
|
|
19
20
|
import { resolveNotificationsConfig, type NotificationsConfig } from "./config";
|
|
20
21
|
import { siteApiEnvironment } from "../../site-api-access";
|
|
22
|
+
import { X_ENDPOINTS, announcementsParameterPrefix } from "../../functions/announcements/wire";
|
|
23
|
+
import { ANNOUNCEMENTS_TIMEOUT_SECONDS } from "../../functions/announcements/resource";
|
|
21
24
|
|
|
22
25
|
export { resolveNotificationsConfig };
|
|
23
26
|
export type { NotificationsConfig };
|
|
@@ -26,6 +29,7 @@ export const NOTIFICATIONS_OUTPUT_KEY = "notifications";
|
|
|
26
29
|
export const HANDLERS = {
|
|
27
30
|
router: "papyrus_content.notifications.handler.router",
|
|
28
31
|
email: "papyrus_content.notifications.handler.email",
|
|
32
|
+
x: "papyrus_content.notifications.handler.x",
|
|
29
33
|
flush: "papyrus_content.notifications.handler.flush",
|
|
30
34
|
sesEvents: "papyrus_content.notifications.handler.ses_events",
|
|
31
35
|
pause: "papyrus_content.notifications.handler.pause",
|
|
@@ -37,6 +41,9 @@ export const NOTIFICATIONS_REQUIREMENTS = [CADUCUS_REQUIREMENT, "PyYAML>=6.0.2,<
|
|
|
37
41
|
const FUNCTION_DIRECTORY = "../plugins/notifications/function";
|
|
38
42
|
export const EMAIL_TIMEOUT_SECONDS = 120;
|
|
39
43
|
export const EMAIL_MAX_RECEIVE_COUNT = 3;
|
|
44
|
+
/** One post can wait out X's rate limit inside the consumer (Caducus sleeps until `x-rate-limit-reset`), and X's windows are fifteen minutes: the Lambda maximum. */
|
|
45
|
+
export const X_TIMEOUT_SECONDS = 900;
|
|
46
|
+
export const X_MAX_RECEIVE_COUNT = 3;
|
|
40
47
|
|
|
41
48
|
export const notificationsManifest: PluginManifest = {
|
|
42
49
|
name: "notifications",
|
|
@@ -58,6 +65,7 @@ export function overrideNotificationsCodeForTests(code?: Code): void {
|
|
|
58
65
|
|
|
59
66
|
export const notificationsFunctionName = (siteId: string) => `papyrus-notifications-${siteId}`;
|
|
60
67
|
export const notificationsEmailName = (siteId: string) => `papyrus-notifications-email-${siteId}`;
|
|
68
|
+
export const notificationsXName = (siteId: string) => `papyrus-notifications-x-${siteId}`;
|
|
61
69
|
export const notificationsFlushName = (siteId: string) => `papyrus-notifications-flush-${siteId}`;
|
|
62
70
|
export const notificationsSesEventsName = (siteId: string) => `papyrus-notifications-ses-${siteId}`;
|
|
63
71
|
export const notificationsPauseName = (siteId: string) => `papyrus-notifications-pause-${siteId}`;
|
|
@@ -70,6 +78,7 @@ export const notificationsKeyParameterName = (siteId: string) => `/papyrus/${sit
|
|
|
70
78
|
export const legacyNewsletterKeyParameterName = (siteId: string) => `/papyrus/${siteId}/newsletter-key`;
|
|
71
79
|
export const notificationsAlarmNames = (siteId: string) => ({
|
|
72
80
|
emailDeadLetters: `papyrus-notifications-${siteId}-email-dlq`,
|
|
81
|
+
xDeadLetters: `papyrus-notifications-${siteId}-x-dlq`,
|
|
73
82
|
routerDeadLetters: `papyrus-notifications-${siteId}-router-dlq`,
|
|
74
83
|
});
|
|
75
84
|
|
|
@@ -201,6 +210,22 @@ export const notificationsPlugin: PapyrusPlugin<NotificationsConfig, Record<stri
|
|
|
201
210
|
enforceSSL: true,
|
|
202
211
|
deadLetterQueue: { queue: emailDeadLetters, maxReceiveCount: EMAIL_MAX_RECEIVE_COUNT },
|
|
203
212
|
});
|
|
213
|
+
// X: its own queue, so one consumer at a time posts for the site's one account (the budget is charged one post after another), and its dead letter queue.
|
|
214
|
+
const xDeadLetters = new Queue(stack, "XDeadLetterQueue", {
|
|
215
|
+
queueName: `papyrus-notifications-${config.siteId}-x-dlq`,
|
|
216
|
+
retentionPeriod: Duration.days(14),
|
|
217
|
+
encryption: QueueEncryption.SQS_MANAGED,
|
|
218
|
+
enforceSSL: true,
|
|
219
|
+
});
|
|
220
|
+
const xQueue = new Queue(stack, "XQueue", {
|
|
221
|
+
queueName: `papyrus-notifications-${config.siteId}-x`,
|
|
222
|
+
visibilityTimeout: Duration.seconds(X_TIMEOUT_SECONDS * 6),
|
|
223
|
+
encryption: QueueEncryption.SQS_MANAGED,
|
|
224
|
+
enforceSSL: true,
|
|
225
|
+
deadLetterQueue: { queue: xDeadLetters, maxReceiveCount: X_MAX_RECEIVE_COUNT },
|
|
226
|
+
});
|
|
227
|
+
// The announcements function's model tables: the settings (pause, budget, prices), the account connection and the spend counter. The consumer has no signed-in person, so it reads and writes them with its own role.
|
|
228
|
+
const announcementTables = backend.data.resources.tables as Record<string, ITable>;
|
|
204
229
|
const routerDeadLetters = new Queue(stack, "RouterDeadLetterQueue", {
|
|
205
230
|
queueName: `papyrus-notifications-${config.siteId}-router-dlq`,
|
|
206
231
|
retentionPeriod: Duration.days(14),
|
|
@@ -211,6 +236,12 @@ export const notificationsPlugin: PapyrusPlugin<NotificationsConfig, Record<stri
|
|
|
211
236
|
const environment = {
|
|
212
237
|
...notificationsEnvironment(config, { table: table.tableName, operators: operatorsParameter, key: keyParameter.parameterName }),
|
|
213
238
|
EMAIL_QUEUE_URL: emailQueue.queueUrl,
|
|
239
|
+
X_QUEUE_URL: xQueue.queueUrl,
|
|
240
|
+
...X_ENDPOINTS,
|
|
241
|
+
ANNOUNCEMENT_SETTINGS_TABLE: announcementTables.AnnouncementSettings.tableName,
|
|
242
|
+
X_CONNECTION_TABLE: announcementTables.XConnection.tableName,
|
|
243
|
+
ANNOUNCEMENT_SPEND_TABLE: announcementTables.AnnouncementSpend.tableName,
|
|
244
|
+
X_REFRESH_LEASE_SECONDS: String(ANNOUNCEMENTS_TIMEOUT_SECONDS),
|
|
214
245
|
...siteApiEnvironment(context.graphqlEndpoint),
|
|
215
246
|
};
|
|
216
247
|
const common = { runtime: Runtime.PYTHON_3_12, architecture: Architecture.ARM_64, code, environment };
|
|
@@ -235,6 +266,17 @@ export const notificationsPlugin: PapyrusPlugin<NotificationsConfig, Record<stri
|
|
|
235
266
|
description: `Mails queued notifications for ${config.siteId}, paced under the SES send rate`,
|
|
236
267
|
});
|
|
237
268
|
email.addEventSource(new SqsEventSource(emailQueue, { batchSize: 10, reportBatchItemFailures: true }));
|
|
269
|
+
const x = new LambdaFunction(stack, "XFunction", {
|
|
270
|
+
...common,
|
|
271
|
+
functionName: notificationsXName(config.siteId),
|
|
272
|
+
handler: HANDLERS.x,
|
|
273
|
+
timeout: Duration.seconds(X_TIMEOUT_SECONDS),
|
|
274
|
+
memorySize: 512,
|
|
275
|
+
// One post at a time: the budget is charged one post after another, and a post whose outcome is unknown is checked before it is repeated.
|
|
276
|
+
reservedConcurrentExecutions: 1,
|
|
277
|
+
description: `Posts queued announcements for ${config.siteId} to its connected X account`,
|
|
278
|
+
});
|
|
279
|
+
x.addEventSource(new SqsEventSource(xQueue, { batchSize: 1, reportBatchItemFailures: true }));
|
|
238
280
|
const flush = new LambdaFunction(stack, "FlushFunction", {
|
|
239
281
|
...common,
|
|
240
282
|
functionName: notificationsFlushName(config.siteId),
|
|
@@ -276,13 +318,26 @@ export const notificationsPlugin: PapyrusPlugin<NotificationsConfig, Record<stri
|
|
|
276
318
|
parameterArns.push(`arn:${stack.partition}:ssm:${stack.region}:${stack.account}:parameter${legacyNewsletterKeyParameterName(config.siteId)}`);
|
|
277
319
|
}
|
|
278
320
|
// Every function builds the same runtime (the audience needs the recipient key), and the SES events suppress readers.
|
|
279
|
-
for (const fn of [router, email, flush, sesEvents]) {
|
|
321
|
+
for (const fn of [router, email, x, flush, sesEvents]) {
|
|
280
322
|
table.grantReadWriteData(fn); // includes the table key's encrypt and decrypt
|
|
281
323
|
fn.addToRolePolicy(new PolicyStatement({ effect: Effect.ALLOW, actions: ["ssm:GetParameter"], resources: parameterArns }));
|
|
282
324
|
}
|
|
283
325
|
// A reader who lifts a bounce or complaint by opting in again is cleared from the SES account suppression list too.
|
|
284
326
|
// The action has no resource-level form (as for the newsletter function).
|
|
285
327
|
router.addToRolePolicy(new PolicyStatement({ effect: Effect.ALLOW, actions: ["ses:DeleteSuppressedDestination"], resources: ["*"] }));
|
|
328
|
+
// The X consumer reads the account's connection, its token parameters (a refresh saves the next one), the settings and the spend counter, and nothing else of the site's.
|
|
329
|
+
announcementTables.AnnouncementSettings.grantReadData(x);
|
|
330
|
+
announcementTables.XConnection.grantReadWriteData(x); // a refresh takes the connection's lease and records its result
|
|
331
|
+
announcementTables.AnnouncementSettings.grantReadWriteData(x); // a refresh X refuses pauses announcements
|
|
332
|
+
announcementTables.AnnouncementSpend.grantReadWriteData(x);
|
|
333
|
+
x.addToRolePolicy(
|
|
334
|
+
new PolicyStatement({
|
|
335
|
+
effect: Effect.ALLOW,
|
|
336
|
+
actions: ["ssm:GetParameter", "ssm:PutParameter"],
|
|
337
|
+
resources: [`arn:${stack.partition}:ssm:${stack.region}:${stack.account}:parameter${announcementsParameterPrefix(config.siteId)}/*`],
|
|
338
|
+
}),
|
|
339
|
+
);
|
|
340
|
+
xQueue.grantSendMessages(router);
|
|
286
341
|
emailQueue.grantSendMessages(router);
|
|
287
342
|
emailQueue.grantSendMessages(flush);
|
|
288
343
|
emailQueue.grantSendMessages(email); // a fan-out page queues its deliveries and the next page
|
|
@@ -369,6 +424,7 @@ export const notificationsPlugin: PapyrusPlugin<NotificationsConfig, Record<stri
|
|
|
369
424
|
const alarmNames = notificationsAlarmNames(config.siteId);
|
|
370
425
|
for (const [id, alarmName, queue] of [
|
|
371
426
|
["EmailDeadLetterAlarm", alarmNames.emailDeadLetters, emailDeadLetters],
|
|
427
|
+
["XDeadLetterAlarm", alarmNames.xDeadLetters, xDeadLetters],
|
|
372
428
|
["RouterDeadLetterAlarm", alarmNames.routerDeadLetters, routerDeadLetters],
|
|
373
429
|
] as const) {
|
|
374
430
|
new Alarm(stack, id, {
|
|
@@ -391,7 +447,7 @@ export const notificationsPlugin: PapyrusPlugin<NotificationsConfig, Record<stri
|
|
|
391
447
|
// The CMS routes are the only caller: a publish tells this function nothing (the announce agent is woken by the Item change feed).
|
|
392
448
|
router.addPermission("InvokeFromCmsCompute", { principal: new ArnPrincipal(config.computeRoleArn), action: "lambda:InvokeFunction" });
|
|
393
449
|
|
|
394
|
-
backend.addOutput({ custom: { [NOTIFICATIONS_OUTPUT_KEY]: { actionsFunction: router.functionName } } });
|
|
450
|
+
backend.addOutput({ custom: { [NOTIFICATIONS_OUTPUT_KEY]: { actionsFunction: router.functionName, xQueueUrl: xQueue.queueUrl } } });
|
|
395
451
|
},
|
|
396
452
|
};
|
|
397
453
|
|
package/lib/notifications-api.ts
CHANGED
|
@@ -213,7 +213,7 @@ export async function handleNotificationsUnsubscribe(request: NextRequest, optio
|
|
|
213
213
|
}
|
|
214
214
|
}
|
|
215
215
|
|
|
216
|
-
const PREFERENCE_MESSAGES = new Set(["saved", "unsubscribed"]);
|
|
216
|
+
const PREFERENCE_MESSAGES = new Set(["saved", "unsubscribed", "check_email"]);
|
|
217
217
|
|
|
218
218
|
/** /api/notifications/preferences. POST (same-origin form) saves, unsubscribes from everything, or deletes after a confirm step; GET redirects. */
|
|
219
219
|
export async function handleNotificationsPreferences(request: NextRequest, options: NotificationsApiOptions = {}): Promise<NextResponse> {
|
|
@@ -233,9 +233,21 @@ export async function handleNotificationsPreferences(request: NextRequest, optio
|
|
|
233
233
|
if (!token || !plugin.functionName) return to.go("expired");
|
|
234
234
|
const action = form.get("action");
|
|
235
235
|
if (action === "delete" && form.get("confirm") !== "1") return to.go("preferences", { token, step: "delete" });
|
|
236
|
+
// Joining a family the reader does not hold needs their consent: the page shows the site's wording and posts it back with the form, the page and the network as
|
|
237
|
+
// evidence, as the subscribe form does. The function uses them only for a new family; a cadence change inside a family the reader holds ignores them.
|
|
236
238
|
const operation =
|
|
237
239
|
action === "save"
|
|
238
|
-
? {
|
|
240
|
+
? {
|
|
241
|
+
action: "update_preferences",
|
|
242
|
+
token,
|
|
243
|
+
lists: form.getAll("l").slice(0, MAXIMUM_LISTS).filter((id) => LIST_ID.test(id)),
|
|
244
|
+
consent: limited(form.get("consent"), 500),
|
|
245
|
+
formId: "notifications-preferences",
|
|
246
|
+
page: `${to.origin}${basePathOf(plugin)}/notifications/preferences/`,
|
|
247
|
+
origin: to.origin,
|
|
248
|
+
ip: clientAddress(request.headers),
|
|
249
|
+
userAgent: limited(request.headers.get("user-agent"), NEWSLETTER_USER_AGENT_CHARACTERS),
|
|
250
|
+
}
|
|
239
251
|
: action === "unsubscribe_all"
|
|
240
252
|
? { action: "unsubscribe", token }
|
|
241
253
|
: action === "delete"
|
|
@@ -243,10 +255,11 @@ export async function handleNotificationsPreferences(request: NextRequest, optio
|
|
|
243
255
|
: null;
|
|
244
256
|
if (!operation) return failure(400, "invalid_action", "Choose save, unsubscribe from everything, or delete.");
|
|
245
257
|
try {
|
|
246
|
-
const
|
|
258
|
+
const { body: answer } = await call(options, plugin.functionName, operation);
|
|
259
|
+
const status = answer.status;
|
|
247
260
|
if (status === "deleted") return to.go("preferences", { msg: "deleted" });
|
|
248
261
|
if (typeof status !== "string" || !PREFERENCE_MESSAGES.has(status)) return to.go("expired");
|
|
249
|
-
return to.go("preferences", { token, msg: status });
|
|
262
|
+
return to.go("preferences", { token, msg: status, ...(status === "check_email" ? { confirming: (Array.isArray(answer.confirming) ? answer.confirming : []).filter((id): id is string => typeof id === "string" && LIST_ID.test(id)).join(",") } : {}) });
|
|
250
263
|
} catch (error) {
|
|
251
264
|
console.error(`Notifications preferences save failed (${error instanceof Error ? error.name : "error"}).`);
|
|
252
265
|
return to.go("expired");
|
|
@@ -261,6 +274,17 @@ function configuredLists(plugin: Enabled, ids: string[]): Array<{ id: string; na
|
|
|
261
274
|
});
|
|
262
275
|
}
|
|
263
276
|
|
|
277
|
+
/** What the preferences page needs to group lists into families: the site's public settings, nothing about any reader. A list that names no family is its own. */
|
|
278
|
+
function familiesOf(plugin: Enabled): Record<string, { family: string; cadence: string }> {
|
|
279
|
+
const known = Array.isArray(plugin.settings.lists) ? (plugin.settings.lists as Array<{ id?: unknown; family?: unknown; cadence?: unknown }>) : [];
|
|
280
|
+
const result: Record<string, { family: string; cadence: string }> = {};
|
|
281
|
+
for (const entry of known) {
|
|
282
|
+
if (typeof entry.id !== "string") continue;
|
|
283
|
+
result[entry.id] = { family: typeof entry.family === "string" ? entry.family : entry.id, cadence: typeof entry.cadence === "string" ? entry.cadence : "" };
|
|
284
|
+
}
|
|
285
|
+
return result;
|
|
286
|
+
}
|
|
287
|
+
|
|
264
288
|
function offeredLists(plugin: Enabled): Array<{ id: string; name: string; description: string }> {
|
|
265
289
|
const known = Array.isArray(plugin.settings.lists) ? (plugin.settings.lists as Array<{ id?: unknown; name?: unknown; description?: unknown }>) : [];
|
|
266
290
|
return known.flatMap((entry) =>
|
|
@@ -294,10 +318,24 @@ export async function handleNotificationsState(request: NextRequest, options: No
|
|
|
294
318
|
if (result.body.status !== "ready") return json(400, { status: "invalid" });
|
|
295
319
|
const lists = Array.isArray(result.body.lists) ? (result.body.lists as Array<Record<string, unknown>>) : [];
|
|
296
320
|
if (kind === "confirm") return json(200, { status: "ready", lists: lists.flatMap((entry) => (typeof entry.name === "string" ? [{ name: entry.name }] : [])) });
|
|
321
|
+
const known = familiesOf(plugin);
|
|
322
|
+
// The wording the page shows, and posts back, when the reader chooses a family they do not hold: the same sentence as the subscribe form.
|
|
323
|
+
const consent = typeof plugin.settings.consent === "string" ? plugin.settings.consent : "";
|
|
297
324
|
return json(200, {
|
|
298
325
|
status: "ready",
|
|
299
326
|
email: typeof result.body.email === "string" ? result.body.email : "",
|
|
300
|
-
|
|
327
|
+
consent,
|
|
328
|
+
lists: lists.map((entry) => {
|
|
329
|
+
const own = typeof entry.id === "string" ? known[entry.id] : undefined;
|
|
330
|
+
return {
|
|
331
|
+
id: entry.id,
|
|
332
|
+
name: entry.name,
|
|
333
|
+
description: entry.description ?? "",
|
|
334
|
+
subscribed: entry.subscribed === true,
|
|
335
|
+
family: own?.family ?? entry.id,
|
|
336
|
+
cadence: own?.cadence ?? "",
|
|
337
|
+
};
|
|
338
|
+
}),
|
|
301
339
|
});
|
|
302
340
|
} catch (error) {
|
|
303
341
|
console.error(`Notifications state failed (${error instanceof Error ? error.name : "error"}).`);
|
package/package.json
CHANGED
package/routes.manifest.json
CHANGED