@anthusai/papyrus 5.19.1 → 5.21.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.
Files changed (81) hide show
  1. package/amplify/data/resource.js +1 -0
  2. package/amplify/data/resource.ts +1 -0
  3. package/amplify/data/schema.js +1 -0
  4. package/amplify/data/schema.ts +1 -0
  5. package/amplify/functions/assignment-action/data-access.js +2 -0
  6. package/amplify/functions/assignment-action/data-access.ts +2 -0
  7. package/amplify/functions/assignment-action/handler.ts +1 -1
  8. package/amplify/functions/content-actions/handler.py +20 -2
  9. package/amplify/functions/shared/editorial-context.ts +63 -4
  10. package/amplify/plugins/newsletter/config.js +195 -0
  11. package/amplify/plugins/newsletter/config.ts +240 -0
  12. package/amplify/plugins/newsletter/function/handler.py +6 -0
  13. package/amplify/plugins/newsletter/function/requirements.txt +5 -0
  14. package/amplify/plugins/newsletter/ops/handler.py +12 -0
  15. package/amplify/plugins/newsletter/ops/resource.js +27 -0
  16. package/amplify/plugins/newsletter/ops/resource.ts +37 -0
  17. package/amplify/plugins/newsletter/papyrus.plugin.js +260 -0
  18. package/amplify/plugins/newsletter/papyrus.plugin.ts +308 -0
  19. package/amplify/plugins/newsletter/schema.js +56 -0
  20. package/amplify/plugins/newsletter/schema.ts +61 -0
  21. package/amplify/plugins/newsletter/secure-string-parameter.js +107 -0
  22. package/amplify/plugins/newsletter/secure-string-parameter.ts +120 -0
  23. package/amplify/plugins/newsletter/sending.js +140 -0
  24. package/amplify/plugins/newsletter/sending.ts +186 -0
  25. package/amplify/plugins/reader-steering/config.js +162 -0
  26. package/amplify/plugins/reader-steering/config.ts +248 -0
  27. package/amplify/plugins/reader-steering/function/ai_moderation.py +230 -0
  28. package/amplify/plugins/reader-steering/function/handler.py +1363 -0
  29. package/amplify/plugins/reader-steering/function/moderation-prompt.md +27 -0
  30. package/amplify/plugins/reader-steering/function/moderation.py +316 -0
  31. package/amplify/plugins/reader-steering/function/resource.js +27 -0
  32. package/amplify/plugins/reader-steering/function/resource.ts +35 -0
  33. package/amplify/plugins/reader-steering/papyrus.plugin.js +175 -0
  34. package/amplify/plugins/reader-steering/papyrus.plugin.ts +201 -0
  35. package/amplify/plugins/reader-steering/schema.js +140 -0
  36. package/amplify/plugins/reader-steering/schema.ts +142 -0
  37. package/amplify/plugins/registry.js +4 -0
  38. package/amplify/plugins/registry.ts +4 -0
  39. package/amplify/plugins/types.ts +5 -0
  40. package/amplify/plugins/wire.js +1 -0
  41. package/amplify/plugins/wire.ts +1 -0
  42. package/app/api/newsletter/confirm/route.ts +13 -0
  43. package/app/api/newsletter/preferences/route.ts +13 -0
  44. package/app/api/newsletter/subscribe/route.ts +9 -0
  45. package/app/api/newsletter/unsubscribe/route.ts +13 -0
  46. package/app/api/reader/comment/delete/route.ts +9 -0
  47. package/app/api/reader/comment/edit/route.ts +9 -0
  48. package/app/api/reader/comment/route.ts +9 -0
  49. package/app/api/reader/handle/route.ts +9 -0
  50. package/app/api/reader/me/route.ts +9 -0
  51. package/app/api/reader/rating/route.ts +9 -0
  52. package/app/api/reader/report/route.ts +9 -0
  53. package/app/api/reader/retired/route.ts +9 -0
  54. package/app/api/reader/submit/route.ts +9 -0
  55. package/app/api/reader/vote/route.ts +9 -0
  56. package/components/moderation-panel.tsx +248 -0
  57. package/components/newsletter-panel.tsx +474 -0
  58. package/components/newsletter-sections.tsx +163 -0
  59. package/components/newsroom-organizer.css +14 -0
  60. package/components/reference-cyclotron-priming.tsx +54 -15
  61. package/components/topic-steering-workspace.tsx +42 -5
  62. package/infra/amplify-app-shell.js +15 -1
  63. package/infra/amplify-app-shell.ts +17 -1
  64. package/infra/site-config.js +12 -2
  65. package/infra/site-config.ts +19 -2
  66. package/lib/editorial-checks.ts +6 -2
  67. package/lib/newsletter-api.ts +391 -0
  68. package/lib/newsletter-client.ts +46 -0
  69. package/lib/newsletter-fixture.ts +184 -0
  70. package/lib/newsletter-model.ts +202 -0
  71. package/lib/newsletter-pages.ts +264 -0
  72. package/lib/prospectus-session.ts +1 -0
  73. package/lib/prospectus-sign-in-routes.ts +2 -2
  74. package/lib/prospectus-site-identity.ts +24 -1
  75. package/lib/reader-api.ts +145 -0
  76. package/lib/reader-moderation.ts +220 -0
  77. package/lib/reader-retirement-route.ts +89 -0
  78. package/lib/reader-subject.md +29 -0
  79. package/lib/reader-subject.ts +53 -0
  80. package/package.json +27 -1
  81. package/routes.manifest.json +158 -1
@@ -0,0 +1,1363 @@
1
+ """reader-actions: the one function behind the reader API of the reader-steering plugin.
2
+
3
+ The CMS app's reader routes invoke it with ``{publication, siteSubject, isStaff, action, payload}`` after they
4
+ have checked the session, the Origin and the body size. It validates every field, rate limits per reader,
5
+ and writes the ReaderProfile, ReaderHandle and ReaderVote tables directly with its own role. It answers
6
+ ``{statusCode, body}``; the body never contains the siteSubject.
7
+
8
+ The siteSubject is the per-site pairwise id that Prospectus returns. The global person id never reaches this
9
+ function. Only ``ss1_`` subjects are accepted, plus ``ssdev_`` ones when the site was synthesized for
10
+ development or test (docs/reader-steering.md).
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ import hashlib
16
+ import json
17
+ import os
18
+ import re
19
+ import sys
20
+ import time
21
+ import uuid
22
+ from dataclasses import dataclass
23
+ from datetime import datetime, timezone
24
+ from decimal import Decimal
25
+ from typing import Any, Callable, Protocol
26
+ from urllib.parse import urlsplit, urlunsplit
27
+
28
+ _FUNCTION_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
29
+ if _FUNCTION_DIRECTORY not in sys.path: # a Lambda already has its code directory on the path; a test loading this file by path does not
30
+ sys.path.insert(0, _FUNCTION_DIRECTORY)
31
+ import ai_moderation # noqa: E402
32
+ import moderation as moderation_rules # noqa: E402
33
+
34
+ SITE_SUBJECT = re.compile(r"ss1_[A-Za-z0-9_-]{43}")
35
+ INTERIM_SUBJECT = re.compile(r"ssdev_[A-Za-z0-9_-]{43}")
36
+ SUBJECT_KEY = re.compile(r"[A-Za-z0-9._:-]{1,200}")
37
+ HANDLE = re.compile(r"[a-z0-9_-]{3,24}")
38
+ TOGGLE_DIMENSIONS = ("up", "used-it")
39
+ PUBLISHED_ID_PREFIX = "published-"
40
+ MAXIMUM_SUBJECTS = 50
41
+ MINIMUM_RATING = 1
42
+ MAXIMUM_RATING = 5
43
+ HOUR_SECONDS = 3600
44
+ HANDLE_CHANGES_PER_HOUR = 10
45
+ STATE_ACTIVE = "active"
46
+ STATE_LIMITED = "limited"
47
+ STATE_BANNED = "banned"
48
+ ROLE_READER = "reader"
49
+ ROLE_EDITOR = "editor"
50
+ ACTION_ME = "me"
51
+ ACTION_HANDLE = "handle"
52
+ ACTION_VOTE = "vote"
53
+ ACTION_RATING = "rating"
54
+ ACTION_SUBMIT = "submit"
55
+ ACTION_COMMENT = "comment"
56
+ ACTION_REPORT = "report"
57
+ RATE_BUCKET_REPORT = "report"
58
+ REPORT_ID_PREFIX = "rep-"
59
+ MAXIMUM_REPORT_REASON = 500
60
+ MODERATION_MODES = ("off", "human", "ai")
61
+ AI_WORKER = "ai-review"
62
+ AI_TASK = {"worker": AI_WORKER}
63
+ AI_DEADLINE_MARGIN_SECONDS = 12
64
+ ACTION_COMMENT_EDIT = "comment-edit"
65
+ ACTION_COMMENT_DELETE = "comment-delete"
66
+ RATE_BUCKET_COMMENT = "comment"
67
+ COMMENT_ID_PREFIX = "cmt-"
68
+ COMMENT_ID = re.compile(r"cmt-[0-9a-f]{32}")
69
+ COMMENT_VISIBLE = "visible"
70
+ COMMENT_HIDDEN = "hidden"
71
+ COMMENT_DELETED = "deleted"
72
+ COMMENT_PAYLOAD_KEYS = frozenset({"subject", "body", "parent", "ratingContext"})
73
+ COMMENT_EDIT_KEYS = frozenset({"id", "body"})
74
+ COMMENT_DELETE_KEYS = frozenset({"id"})
75
+ LINK = re.compile(r"(?:https?://|www\.)\S+", re.IGNORECASE)
76
+ TEXT_CONTROL_CHARACTERS = re.compile(r"[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]")
77
+ RATE_BUCKET_REACTIONS = "react"
78
+ RATE_BUCKET_HANDLE = "handle"
79
+ RATE_BUCKET_SUBMIT = "submit"
80
+ DAY_SECONDS = 24 * HOUR_SECONDS
81
+ SUBMISSION_ID_PREFIX = "sub-"
82
+ SUBMISSION_ID = re.compile(r"sub-[0-9a-f]{24}")
83
+ SUBMISSION_DOMAIN_INDEX = "submissionsByDomain"
84
+ PROFILE_READER_INDEX = "profilesByReader"
85
+ REPORT_TARGET_INDEX = "reportsByTarget"
86
+ SUBMISSION_PENDING = "pending"
87
+ SUBMISSION_PROMOTED = "promoted"
88
+ SUBMISSION_HIDDEN = "hidden"
89
+ VISIBLE_SUBMISSION_STATES = (SUBMISSION_PENDING, SUBMISSION_PROMOTED)
90
+ EXTRACTION_PENDING = "pending"
91
+ SUBMISSION_PAYLOAD_KEYS = frozenset({"url", "title", "kind", "note"})
92
+ MAXIMUM_URL_LENGTH = 2048
93
+ MAXIMUM_TITLE_LENGTH = 200
94
+ MAXIMUM_NOTE_LENGTH = 500
95
+ MAXIMUM_SIMILAR = 5
96
+ EMAIL_ADDRESS = re.compile(r"[A-Za-z0-9._%+-]+@(?:[A-Za-z0-9-]*[A-Za-z][A-Za-z0-9-]*\.)+[A-Za-z]{2,}")
97
+ CONTROL_CHARACTERS = re.compile(r"[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]")
98
+ SNAPSHOT_TASK = {"projectionTask": {"task": "reader-snapshot"}}
99
+ SNAPSHOT_SCHEDULE_PREFIX = "papyrus-reader-snapshot-"
100
+ RESERVED_HANDLES = frozenset(
101
+ "admin administrator root system sysadmin moderator mod mods staff editor editors support help security "
102
+ "official anonymous anon guest null undefined none me you everyone nobody deleted reader readers "
103
+ "papyrus anthus prospectus api www mail abuse postmaster webmaster".split()
104
+ )
105
+
106
+
107
+ @dataclass(frozen=True)
108
+ class ReaderSettings:
109
+ dimensions: tuple[str, ...] = ()
110
+ actions_per_hour: int = 120
111
+ new_account_hours: int = 24
112
+ new_account_divisor: int = 3
113
+ reserved_handles: frozenset[str] = frozenset()
114
+ allow_interim_subjects: bool = False
115
+ snapshot_debounce_seconds: int = 60
116
+ submissions_per_day: int = 10
117
+ similar_hours: int = 48
118
+ submission_kinds: tuple[str, ...] = ("link",)
119
+ comments_per_hour: int = 30
120
+ comment_edit_minutes: int = 10
121
+ comment_maximum_length: int = 2000
122
+ comment_maximum_links: int = 3
123
+ moderation_mode: str = "human"
124
+ pre_moderation: bool = False
125
+ reports_per_day: int = 20
126
+ promote_item_type: str = "brief"
127
+ promote_section: str = "unsectioned"
128
+ ai_model: str = ai_moderation.DEFAULT_MODEL
129
+ ai_key_parameter: str = ""
130
+
131
+
132
+ def settings_from_environment(environment: dict[str, str] | None = None) -> ReaderSettings:
133
+ environment = os.environ if environment is None else environment
134
+ raw = json.loads(environment.get("PAPYRUS_READER_SETTINGS") or "{}")
135
+ limits = raw.get("limits") or {}
136
+ defaults = ReaderSettings()
137
+ return ReaderSettings(
138
+ dimensions=tuple(raw.get("dimensions") or ()),
139
+ actions_per_hour=int(limits.get("actionsPerHour", defaults.actions_per_hour)),
140
+ new_account_hours=int(limits.get("newAccountHours", defaults.new_account_hours)),
141
+ new_account_divisor=int(limits.get("newAccountDivisor", defaults.new_account_divisor)),
142
+ reserved_handles=frozenset(raw.get("reservedHandles") or ()),
143
+ allow_interim_subjects=bool(raw.get("allowInterimSubjects", False)),
144
+ snapshot_debounce_seconds=int(raw.get("snapshotDebounceSeconds", defaults.snapshot_debounce_seconds)),
145
+ submissions_per_day=int((raw.get("submissions") or {}).get("perDay", defaults.submissions_per_day)),
146
+ similar_hours=int((raw.get("submissions") or {}).get("similarHours", defaults.similar_hours)),
147
+ submission_kinds=tuple((raw.get("submissions") or {}).get("kinds") or defaults.submission_kinds),
148
+ comments_per_hour=int((raw.get("comments") or {}).get("perHour", defaults.comments_per_hour)),
149
+ comment_edit_minutes=int((raw.get("comments") or {}).get("editWindowMinutes", defaults.comment_edit_minutes)),
150
+ comment_maximum_length=int((raw.get("comments") or {}).get("maximumLength", defaults.comment_maximum_length)),
151
+ comment_maximum_links=int((raw.get("comments") or {}).get("maximumLinks", defaults.comment_maximum_links)),
152
+ moderation_mode=str((raw.get("moderation") or {}).get("mode", defaults.moderation_mode)),
153
+ pre_moderation=bool((raw.get("moderation") or {}).get("preModeration", defaults.pre_moderation)),
154
+ reports_per_day=int((raw.get("moderation") or {}).get("reportsPerDay", defaults.reports_per_day)),
155
+ ai_model=str(((raw.get("moderation") or {}).get("ai") or {}).get("model", defaults.ai_model)),
156
+ ai_key_parameter=str(((raw.get("moderation") or {}).get("ai") or {}).get("apiKeyParameter", defaults.ai_key_parameter)),
157
+ promote_item_type=str((raw.get("submissions") or {}).get("itemType", defaults.promote_item_type)),
158
+ promote_section=str((raw.get("submissions") or {}).get("section", defaults.promote_section)),
159
+ )
160
+
161
+
162
+ class ReaderStore(Protocol):
163
+ def get_profile(self, site_subject: str) -> dict[str, Any] | None: ...
164
+
165
+ def create_profile(self, profile: dict[str, Any]) -> bool: ...
166
+
167
+ def claim_handle(self, site_subject: str, reader_id: str, handle: str, now_iso: str) -> str: ...
168
+
169
+ def get_vote_values(self, vote_ids: list[str]) -> dict[str, int]: ...
170
+
171
+ def put_vote(self, vote: dict[str, Any], now_iso: str) -> None: ...
172
+
173
+ def delete_vote(self, vote_id: str) -> None: ...
174
+
175
+ def is_published(self, subject_key: str) -> bool: ...
176
+
177
+ def get_submission(self, submission_id: str) -> dict[str, Any] | None: ...
178
+
179
+ def create_submission(self, submission: dict[str, Any]) -> bool: ...
180
+
181
+ def recent_submissions_by_domain(self, domain: str, since_iso: str) -> list[dict[str, Any]]: ...
182
+
183
+ def get_comment(self, comment_id: str) -> dict[str, Any] | None: ...
184
+
185
+ def create_comment(self, comment: dict[str, Any]) -> None: ...
186
+
187
+ def update_comment(self, comment_id: str, changes: dict[str, Any], now_iso: str, *, expected_state: str) -> bool: ...
188
+
189
+ def put_report(self, report: dict[str, Any], target_key: str, target_table: str, now_iso: str) -> bool: ...
190
+
191
+ def get_profile_by_reader(self, reader_id: str) -> dict[str, Any] | None: ...
192
+
193
+ def get_action(self, action_id: str) -> dict[str, Any] | None: ...
194
+
195
+ def transact_moderation(self, action: dict[str, Any], target_type: str, target_key: str, plan: Any, *, undoes: str | None = None) -> str: ...
196
+
197
+ def close_reports(self, target: str, now_iso: str) -> int: ...
198
+
199
+ def new_content(self) -> list[tuple[str, dict[str, Any]]]: ...
200
+
201
+ def human_acted_on(self) -> set[str]: ...
202
+
203
+ def count_action(self, reader_id: str, bucket: str, window_start: int, limit: int, expires_at: int) -> bool: ...
204
+
205
+ def claim_snapshot_slot(self, now_seconds: float, debounce_seconds: int) -> int | None: ...
206
+
207
+ def release_snapshot_slot(self, locked_until: int) -> None: ...
208
+
209
+
210
+ def reply(status: int, body: dict[str, Any]) -> dict[str, Any]:
211
+ return {"statusCode": status, "body": body}
212
+
213
+
214
+ def refusal(status: int, error: str, message: str, **extra: Any) -> dict[str, Any]:
215
+ return reply(status, {"error": error, "message": message, **extra})
216
+
217
+
218
+ def iso(now_seconds: float) -> str:
219
+ """UTC with milliseconds, so rows written in the same second still sort in the order they were written."""
220
+ moment = datetime.fromtimestamp(now_seconds, tz=timezone.utc)
221
+ return moment.strftime("%Y-%m-%dT%H:%M:%S.") + f"{moment.microsecond // 1000:03d}Z"
222
+
223
+
224
+ def seconds_of(iso_text: str) -> float:
225
+ return datetime.strptime(iso_text, "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=timezone.utc).timestamp()
226
+
227
+
228
+ def vote_id(subject_key: str, reader_id: str, dimension: str) -> str:
229
+ return f"{subject_key}#{reader_id}#{dimension}"
230
+
231
+
232
+ def valid_site_subject(value: Any, settings: ReaderSettings) -> bool:
233
+ if not isinstance(value, str):
234
+ return False
235
+ if SITE_SUBJECT.fullmatch(value):
236
+ return True
237
+ return settings.allow_interim_subjects and INTERIM_SUBJECT.fullmatch(value) is not None
238
+
239
+
240
+ def action_limit(profile: dict[str, Any], settings: ReaderSettings, now_seconds: float) -> int:
241
+ age_hours = (now_seconds - seconds_of(profile["createdAt"])) / HOUR_SECONDS
242
+ if age_hours < settings.new_account_hours:
243
+ return max(1, settings.actions_per_hour // settings.new_account_divisor)
244
+ return settings.actions_per_hour
245
+
246
+
247
+ def within_rate_limit(
248
+ store: ReaderStore, profile: dict[str, Any], bucket: str, limit: int, now_seconds: float, window_seconds: int = HOUR_SECONDS
249
+ ) -> dict[str, Any] | None:
250
+ window_start = int(now_seconds // window_seconds) * window_seconds
251
+ window_end = window_start + window_seconds
252
+ if store.count_action(profile["readerId"], bucket, window_start, limit, window_end + HOUR_SECONDS):
253
+ return None
254
+ return refusal(429, "rate_limited", "Too many actions. Try again later.", retryAfter=max(1, int(window_end - now_seconds)))
255
+
256
+
257
+ def new_account_limit(profile: dict[str, Any], settings: ReaderSettings, now_seconds: float, full_limit: int) -> int:
258
+ """The limit for an action: a third (``new_account_divisor``) of it while the profile is younger than ``new_account_hours``."""
259
+ age_hours = (now_seconds - seconds_of(profile["createdAt"])) / HOUR_SECONDS
260
+ return max(1, full_limit // settings.new_account_divisor) if age_hours < settings.new_account_hours else full_limit
261
+
262
+
263
+ class InvalidUrl(ValueError):
264
+ pass
265
+
266
+
267
+ def canonical_source_url(value: Any) -> str:
268
+ """The canonical form of a source URL. Identical to ``papyrus_content.reference_intake.canonical_source_url`` (a test pins them together)."""
269
+ if not isinstance(value, str) or len(value) > 4096 or any(ord(c) <= 32 for c in value):
270
+ raise InvalidUrl("source_uri must be an HTTP(S) URL without whitespace.")
271
+ try:
272
+ parts = urlsplit(value)
273
+ if parts.scheme.lower() not in {"https", "http"} or not parts.hostname or parts.username or parts.password:
274
+ raise ValueError()
275
+ host = parts.hostname.encode("idna").decode("ascii").lower()
276
+ if ":" in host:
277
+ host = f"[{host}]"
278
+ port = parts.port
279
+ if port is not None and port != {"http": 80, "https": 443}[parts.scheme.lower()]:
280
+ host += f":{port}"
281
+ return urlunsplit((parts.scheme.lower(), host, parts.path or "/", parts.query, ""))
282
+ except (ValueError, UnicodeError) as error:
283
+ raise InvalidUrl("source_uri must be a valid HTTP(S) URL without credentials.") from error
284
+
285
+
286
+ def submission_id_for(canonical_url: str) -> str:
287
+ return SUBMISSION_ID_PREFIX + hashlib.sha256(canonical_url.encode("utf-8")).hexdigest()[:24]
288
+
289
+
290
+ def domain_of(canonical_url: str) -> str:
291
+ return (urlsplit(canonical_url).hostname or "").lower()
292
+
293
+
294
+ def ensure_profile(store: ReaderStore, site_subject: str, now_seconds: float) -> dict[str, Any]:
295
+ profile = store.get_profile(site_subject)
296
+ if profile is not None:
297
+ return profile
298
+ created = {
299
+ "id": site_subject,
300
+ "siteSubject": site_subject,
301
+ "readerId": str(uuid.uuid4()),
302
+ "state": STATE_ACTIVE,
303
+ "karma": 0,
304
+ "createdAt": iso(now_seconds),
305
+ }
306
+ if store.create_profile(created):
307
+ return created
308
+ existing = store.get_profile(site_subject)
309
+ if existing is None:
310
+ raise RuntimeError("The reader profile could not be created.")
311
+ return existing
312
+
313
+
314
+ def public_profile(profile: dict[str, Any]) -> dict[str, Any]:
315
+ return {
316
+ "readerId": profile["readerId"],
317
+ "handle": profile.get("handle"),
318
+ "karma": int(profile.get("karma") or 0),
319
+ "state": profile.get("state", STATE_ACTIVE),
320
+ }
321
+
322
+
323
+ def my_state(store: ReaderStore, profile: dict[str, Any], payload: dict[str, Any], settings: ReaderSettings) -> dict[str, Any]:
324
+ subjects = payload.get("subjects", [])
325
+ if (
326
+ not isinstance(subjects, list)
327
+ or len(subjects) > MAXIMUM_SUBJECTS
328
+ or not all(isinstance(subject, str) and SUBJECT_KEY.fullmatch(subject) for subject in subjects)
329
+ ):
330
+ return refusal(422, "invalid_subjects", f"Send at most {MAXIMUM_SUBJECTS} item keys.")
331
+ dimensions = [*TOGGLE_DIMENSIONS, *settings.dimensions]
332
+ wanted = {vote_id(subject, profile["readerId"], dimension): (subject, dimension) for subject in subjects for dimension in dimensions}
333
+ found = store.get_vote_values(list(wanted))
334
+ votes: dict[str, dict[str, int]] = {}
335
+ for stored_id, value in found.items():
336
+ subject, dimension = wanted[stored_id]
337
+ votes.setdefault(subject, {})[dimension] = value
338
+ return reply(200, {**public_profile(profile), "votes": votes})
339
+
340
+
341
+ def set_handle(
342
+ store: ReaderStore, profile: dict[str, Any], payload: dict[str, Any], settings: ReaderSettings, now_seconds: float
343
+ ) -> dict[str, Any]:
344
+ requested = payload.get("handle")
345
+ if not isinstance(requested, str):
346
+ return refusal(422, "invalid_handle", "Send a handle.")
347
+ handle = requested.lower()
348
+ if not HANDLE.fullmatch(handle):
349
+ return refusal(422, "invalid_handle", "A handle has 3 to 24 letters, digits, hyphens or underscores.")
350
+ if handle in RESERVED_HANDLES or handle in settings.reserved_handles:
351
+ return refusal(422, "reserved_handle", "That handle is reserved.")
352
+ if profile.get("handle"):
353
+ return refusal(409, "handle_already_set", "The handle is set once and cannot be changed.")
354
+ limited = within_rate_limit(store, profile, RATE_BUCKET_HANDLE, HANDLE_CHANGES_PER_HOUR, now_seconds)
355
+ if limited:
356
+ return limited
357
+ outcome = store.claim_handle(profile["siteSubject"], profile["readerId"], handle, iso(now_seconds))
358
+ if outcome == "taken":
359
+ return refusal(409, "handle_taken", "That handle is taken.")
360
+ if outcome == "already_set":
361
+ return refusal(409, "handle_already_set", "The handle is set once and cannot be changed.")
362
+ return reply(200, {**public_profile({**profile, "handle": handle})})
363
+
364
+
365
+ def react(
366
+ store: ReaderStore,
367
+ profile: dict[str, Any],
368
+ *,
369
+ subject: Any,
370
+ dimension: Any,
371
+ allowed_dimensions: tuple[str, ...],
372
+ value: int | None,
373
+ is_staff: bool,
374
+ settings: ReaderSettings,
375
+ now_seconds: float,
376
+ schedule_snapshot: Callable[[], None],
377
+ ) -> dict[str, Any]:
378
+ if not isinstance(subject, str) or not SUBJECT_KEY.fullmatch(subject):
379
+ return refusal(422, "invalid_subject", "Send the key of a published item.")
380
+ if not isinstance(dimension, str) or dimension not in allowed_dimensions:
381
+ return refusal(422, "invalid_dimension", "That dimension is not available.")
382
+ if profile.get("state") == STATE_BANNED:
383
+ return refusal(403, "reader_banned", "This reader cannot take part.")
384
+ if profile.get("state") == STATE_LIMITED:
385
+ return refusal(403, "reader_limited", "This reader cannot vote or rate right now.")
386
+ limited = within_rate_limit(store, profile, RATE_BUCKET_REACTIONS, action_limit(profile, settings, now_seconds), now_seconds)
387
+ if limited:
388
+ return limited
389
+ if not subject_is_open(store, subject):
390
+ return refusal(404, "unknown_subject", "That item is not published.")
391
+ stored_id = vote_id(subject, profile["readerId"], dimension)
392
+ if value is None:
393
+ store.delete_vote(stored_id)
394
+ else:
395
+ store.put_vote(
396
+ {
397
+ "id": stored_id,
398
+ "subjectKey": subject,
399
+ "readerId": profile["readerId"],
400
+ "dimension": dimension,
401
+ "value": value,
402
+ "voterRole": ROLE_EDITOR if is_staff else ROLE_READER,
403
+ },
404
+ iso(now_seconds),
405
+ )
406
+ schedule_snapshot()
407
+ return reply(200, {"subject": subject, "dimension": dimension, "value": value})
408
+
409
+
410
+ def subject_is_open(store: ReaderStore, subject: str) -> bool:
411
+ """A published item, or a submission that is visible (pending or promoted); hidden submissions cannot be voted on."""
412
+ if SUBMISSION_ID.fullmatch(subject):
413
+ submission = store.get_submission(subject)
414
+ return submission is not None and submission.get("state") in VISIBLE_SUBMISSION_STATES
415
+ return store.is_published(subject)
416
+
417
+
418
+ def clean_text(value: Any, label: str, maximum: int, *, required: bool) -> tuple[str | None, dict[str, Any] | None]:
419
+ if value is None or value == "":
420
+ if required:
421
+ return None, refusal(422, f"invalid_{label}", f"Send a {label}.")
422
+ return None, None
423
+ if not isinstance(value, str) or CONTROL_CHARACTERS.search(value):
424
+ return None, refusal(422, f"invalid_{label}", f"The {label} must be plain text.")
425
+ if EMAIL_ADDRESS.search(value):
426
+ return None, refusal(422, f"invalid_{label}", f"Public text cannot contain an email address. Remove it from the {label}.")
427
+ text = " ".join(value.split()) if label == "title" else value.strip()
428
+ if not text and required:
429
+ return None, refusal(422, f"invalid_{label}", f"Send a {label}.")
430
+ if len(text) > maximum:
431
+ return None, refusal(422, f"invalid_{label}", f"The {label} is at most {maximum} characters.")
432
+ return text or None, None
433
+
434
+
435
+ def public_submission(submission: dict[str, Any]) -> dict[str, Any]:
436
+ promoted = submission.get("state") == SUBMISSION_PROMOTED and submission.get("itemId")
437
+ return {"subject": submission["itemId"] if promoted else submission["id"], "url": submission["canonicalUrl"], "title": submission["title"], "state": submission["state"]}
438
+
439
+
440
+ def duplicate(existing: dict[str, Any]) -> dict[str, Any]:
441
+ if existing.get("state") not in VISIBLE_SUBMISSION_STATES:
442
+ return refusal(409, "duplicate_submission", "That link was already submitted.")
443
+ return refusal(409, "duplicate_submission", "That link was already submitted.", existing=public_submission(existing))
444
+
445
+
446
+ def new_content_fields(settings: ReaderSettings, visible: str) -> dict[str, Any]:
447
+ """``state`` and ``moderation`` for a new submission or comment.
448
+
449
+ With moderation off nothing waits in a queue. With pre-moderation the item starts hidden ("held") until a moderator approves it.
450
+ """
451
+ if settings.moderation_mode == "off":
452
+ return {"state": visible, "moderation": moderation_rules.MODERATION_APPROVED}
453
+ if settings.pre_moderation:
454
+ return {"state": "hidden", "moderation": moderation_rules.MODERATION_NEW}
455
+ return {"state": visible, "moderation": moderation_rules.MODERATION_NEW}
456
+
457
+
458
+ def submit_action(
459
+ store: ReaderStore,
460
+ profile: dict[str, Any],
461
+ payload: dict[str, Any],
462
+ settings: ReaderSettings,
463
+ now_seconds: float,
464
+ schedule_snapshot: Callable[[], None],
465
+ ) -> dict[str, Any]:
466
+ if profile.get("state") == STATE_LIMITED:
467
+ return refusal(403, "reader_limited", "This reader cannot submit right now.")
468
+ if not profile.get("handle"):
469
+ return refusal(422, "handle_required", "Choose a handle before submitting a link.")
470
+ if set(payload) - SUBMISSION_PAYLOAD_KEYS:
471
+ return refusal(422, "invalid_submission", "Send only url, title, kind and note.")
472
+ raw_url = payload.get("url")
473
+ if not isinstance(raw_url, str) or not raw_url.strip() or len(raw_url) > MAXIMUM_URL_LENGTH:
474
+ return refusal(422, "invalid_url", "Send the link as an http or https address.")
475
+ try:
476
+ canonical = canonical_source_url(raw_url.strip())
477
+ except InvalidUrl:
478
+ return refusal(422, "invalid_url", "Send the link as an http or https address.")
479
+ if len(canonical) > MAXIMUM_URL_LENGTH or EMAIL_ADDRESS.search(canonical):
480
+ return refusal(422, "invalid_url", "Send the link as an http or https address without an email address in it.")
481
+ title, problem = clean_text(payload.get("title"), "title", MAXIMUM_TITLE_LENGTH, required=True)
482
+ if problem:
483
+ return problem
484
+ note, problem = clean_text(payload.get("note"), "note", MAXIMUM_NOTE_LENGTH, required=False)
485
+ if problem:
486
+ return problem
487
+ kind = payload.get("kind", settings.submission_kinds[0])
488
+ if not isinstance(kind, str) or kind not in settings.submission_kinds:
489
+ return refusal(422, "invalid_kind", "That kind is not available.")
490
+ limit = new_account_limit(profile, settings, now_seconds, settings.submissions_per_day)
491
+ limited = within_rate_limit(store, profile, RATE_BUCKET_SUBMIT, limit, now_seconds, DAY_SECONDS)
492
+ if limited:
493
+ return limited
494
+ submission_id = submission_id_for(canonical)
495
+ existing = store.get_submission(submission_id)
496
+ if existing is not None:
497
+ return duplicate(existing)
498
+ domain = domain_of(canonical)
499
+ since = iso(now_seconds - settings.similar_hours * HOUR_SECONDS)
500
+ similar_rows = [
501
+ row
502
+ for row in store.recent_submissions_by_domain(domain, since)
503
+ if row.get("state") in VISIBLE_SUBMISSION_STATES and row["id"] != submission_id
504
+ ]
505
+ similar_rows.sort(key=lambda row: row["createdAt"], reverse=True)
506
+ similar_rows = similar_rows[:MAXIMUM_SIMILAR]
507
+ submission = {
508
+ "id": submission_id,
509
+ "canonicalUrl": canonical,
510
+ "domain": domain,
511
+ "title": title,
512
+ "kind": kind,
513
+ "readerId": profile["readerId"],
514
+ **new_content_fields(settings, SUBMISSION_PENDING),
515
+ "similar": [row["id"] for row in similar_rows],
516
+ "extractionState": EXTRACTION_PENDING,
517
+ "createdAt": iso(now_seconds),
518
+ }
519
+ if note:
520
+ submission["note"] = note
521
+ if not store.create_submission(submission):
522
+ return duplicate(store.get_submission(submission_id) or submission)
523
+ schedule_snapshot()
524
+ return reply(
525
+ 201,
526
+ {
527
+ "subject": submission_id,
528
+ "url": canonical,
529
+ "title": title,
530
+ "kind": kind,
531
+ "state": "held" if submission["state"] == SUBMISSION_HIDDEN else SUBMISSION_PENDING,
532
+ "extraction": EXTRACTION_PENDING,
533
+ "similar": [{"subject": row["id"], "url": row["canonicalUrl"], "title": row["title"]} for row in similar_rows],
534
+ },
535
+ )
536
+
537
+
538
+ def clean_comment_body(value: Any, settings: ReaderSettings) -> tuple[str | None, dict[str, Any] | None]:
539
+ """Plain text, trimmed, at most ``comment_maximum_length`` characters, ``comment_maximum_links`` links, no email address."""
540
+ if not isinstance(value, str):
541
+ return None, refusal(422, "invalid_body", "Send the comment as text.")
542
+ text = value.replace("\r\n", "\n").replace("\r", "\n").strip()
543
+ if not text:
544
+ return None, refusal(422, "invalid_body", "Write something first.")
545
+ if TEXT_CONTROL_CHARACTERS.search(text):
546
+ return None, refusal(422, "invalid_body", "A comment is plain text.")
547
+ if len(text) > settings.comment_maximum_length:
548
+ return None, refusal(422, "body_too_long", f"A comment is at most {settings.comment_maximum_length} characters.")
549
+ if len(LINK.findall(text)) > settings.comment_maximum_links:
550
+ return None, refusal(422, "too_many_links", f"A comment has at most {settings.comment_maximum_links} links.")
551
+ if EMAIL_ADDRESS.search(text):
552
+ return None, refusal(422, "invalid_body", "Public text cannot contain an email address. Remove it from the comment.")
553
+ return text, None
554
+
555
+
556
+ def writes_comments(profile: dict[str, Any]) -> dict[str, Any] | None:
557
+ if profile.get("state") == STATE_LIMITED:
558
+ return refusal(403, "reader_limited", "This reader cannot comment right now.")
559
+ if not profile.get("handle"):
560
+ return refusal(422, "handle_required", "Choose a handle before commenting.")
561
+ return None
562
+
563
+
564
+ def rating_context_for(store: ReaderStore, profile: dict[str, Any], subject: str, raw: Any, settings: ReaderSettings) -> tuple[dict[str, Any] | None, dict[str, Any] | None]:
565
+ """The rating a comment explains must be the reader's own current vote, so a comment cannot claim a rating nobody gave."""
566
+ if raw is None:
567
+ return None, None
568
+ if not isinstance(raw, dict) or set(raw) != {"dimension", "value"}:
569
+ return None, refusal(422, "invalid_rating_context", "Send ratingContext as {dimension, value}.")
570
+ dimension, value = raw["dimension"], raw["value"]
571
+ if not isinstance(dimension, str) or dimension not in (*TOGGLE_DIMENSIONS, *settings.dimensions) or isinstance(value, bool) or not isinstance(value, int):
572
+ return None, refusal(422, "invalid_rating_context", "That rating context is not available.")
573
+ stored = store.get_vote_values([vote_id(subject, profile["readerId"], dimension)])
574
+ if stored.get(vote_id(subject, profile["readerId"], dimension)) != value:
575
+ return None, refusal(422, "rating_context_mismatch", "Rate or vote first; a comment can only explain your own current rating.")
576
+ return {"dimension": dimension, "value": value}, None
577
+
578
+
579
+ def comment_action(
580
+ store: ReaderStore,
581
+ profile: dict[str, Any],
582
+ payload: dict[str, Any],
583
+ settings: ReaderSettings,
584
+ now_seconds: float,
585
+ schedule_snapshot: Callable[[], None],
586
+ ) -> dict[str, Any]:
587
+ problem = writes_comments(profile)
588
+ if problem:
589
+ return problem
590
+ if set(payload) - COMMENT_PAYLOAD_KEYS:
591
+ return refusal(422, "invalid_comment", "Send only subject, body, parent and ratingContext.")
592
+ subject = payload.get("subject")
593
+ if not isinstance(subject, str) or not SUBJECT_KEY.fullmatch(subject):
594
+ return refusal(422, "invalid_subject", "Send the key of a published item.")
595
+ body, problem = clean_comment_body(payload.get("body"), settings)
596
+ if problem:
597
+ return problem
598
+ parent_id = payload.get("parent")
599
+ if parent_id is not None and (not isinstance(parent_id, str) or not COMMENT_ID.fullmatch(parent_id)):
600
+ return refusal(422, "invalid_parent", "That comment cannot be replied to.")
601
+ context, problem = rating_context_for(store, profile, subject, payload.get("ratingContext"), settings)
602
+ if problem:
603
+ return problem
604
+ if context is not None and parent_id is not None:
605
+ return refusal(422, "invalid_rating_context", "A reply does not explain a rating.")
606
+ limited = within_rate_limit(store, profile, RATE_BUCKET_COMMENT, new_account_limit(profile, settings, now_seconds, settings.comments_per_hour), now_seconds)
607
+ if limited:
608
+ return limited
609
+ if not subject_is_open(store, subject):
610
+ return refusal(404, "unknown_subject", "That item is not published.")
611
+ if parent_id is not None:
612
+ parent = store.get_comment(parent_id)
613
+ if parent is None or parent.get("subjectKey") != subject or parent.get("parentId") or parent.get("state") != COMMENT_VISIBLE:
614
+ return refusal(422, "invalid_parent", "That comment cannot be replied to.")
615
+ comment_id = COMMENT_ID_PREFIX + uuid.uuid4().hex
616
+ created = iso(now_seconds)
617
+ comment = {
618
+ "id": comment_id,
619
+ "subjectKey": subject,
620
+ "readerId": profile["readerId"],
621
+ "body": body,
622
+ **new_content_fields(settings, COMMENT_VISIBLE),
623
+ "createdAt": created,
624
+ }
625
+ if parent_id is not None:
626
+ comment["parentId"] = parent_id
627
+ if context is not None:
628
+ comment["ratingContext"] = json.dumps(context, separators=(",", ":"))
629
+ store.create_comment(comment)
630
+ schedule_snapshot()
631
+ return reply(
632
+ 201,
633
+ {
634
+ "id": comment_id, "subject": subject, "parent": parent_id, "body": body, "createdAt": created, "ratingContext": context,
635
+ "state": "held" if comment["state"] == COMMENT_HIDDEN else COMMENT_VISIBLE,
636
+ },
637
+ )
638
+
639
+
640
+ def report_action(
641
+ store: ReaderStore,
642
+ profile: dict[str, Any],
643
+ payload: dict[str, Any],
644
+ settings: ReaderSettings,
645
+ now_seconds: float,
646
+ schedule_snapshot: Callable[[], None],
647
+ ) -> dict[str, Any]:
648
+ """A reader reports a visible submission or comment; it goes back to the moderation queue. One report per reader and target."""
649
+ if set(payload) - {"target", "reason"}:
650
+ return refusal(422, "invalid_report", "Send only target and reason.")
651
+ target = payload.get("target")
652
+ target_type = moderation_rules.target_type_of(target) if isinstance(target, str) else None
653
+ if target_type not in (moderation_rules.TARGET_SUBMISSION, moderation_rules.TARGET_COMMENT):
654
+ return refusal(422, "invalid_target", "Report a submission or a comment.")
655
+ reason = payload.get("reason")
656
+ if not isinstance(reason, str) or not reason.strip() or len(reason.strip()) > MAXIMUM_REPORT_REASON or CONTROL_CHARACTERS.search(reason):
657
+ return refusal(422, "invalid_reason", f"Give a reason in plain text of at most {MAXIMUM_REPORT_REASON} characters.")
658
+ limited = within_rate_limit(
659
+ store, profile, RATE_BUCKET_REPORT, new_account_limit(profile, settings, now_seconds, settings.reports_per_day), now_seconds, DAY_SECONDS
660
+ )
661
+ if limited:
662
+ return limited
663
+ row = store.get_submission(target) if target_type == moderation_rules.TARGET_SUBMISSION else store.get_comment(target)
664
+ visible = VISIBLE_SUBMISSION_STATES if target_type == moderation_rules.TARGET_SUBMISSION else (COMMENT_VISIBLE,)
665
+ if row is None or row.get("state") not in visible:
666
+ return refusal(404, "unknown_target", "That cannot be reported.")
667
+ report = {
668
+ "id": REPORT_ID_PREFIX + hashlib.sha256(f"{profile['readerId']}#{target}".encode("utf-8")).hexdigest()[:24],
669
+ "targetType": target_type,
670
+ "target": target,
671
+ "reporterReaderId": profile["readerId"],
672
+ "reason": reason.strip(),
673
+ "state": "open",
674
+ "createdAt": iso(now_seconds),
675
+ }
676
+ store.put_report(report, target, "submission" if target_type == moderation_rules.TARGET_SUBMISSION else "comment", iso(now_seconds))
677
+ return reply(200, {"reported": True})
678
+
679
+
680
+ def own_comment(store: ReaderStore, profile: dict[str, Any], payload: dict[str, Any], allowed: frozenset[str]) -> tuple[dict[str, Any] | None, dict[str, Any] | None]:
681
+ if set(payload) - allowed:
682
+ return None, refusal(422, "invalid_comment", "Send only " + ", ".join(sorted(allowed)) + ".")
683
+ comment_id = payload.get("id")
684
+ if not isinstance(comment_id, str) or not COMMENT_ID.fullmatch(comment_id):
685
+ return None, refusal(404, "unknown_comment", "That comment does not exist.")
686
+ comment = store.get_comment(comment_id)
687
+ if comment is None or comment.get("readerId") != profile["readerId"] or comment.get("state") == COMMENT_HIDDEN:
688
+ return None, refusal(404, "unknown_comment", "That comment does not exist.")
689
+ return comment, None
690
+
691
+
692
+ def edit_comment_action(
693
+ store: ReaderStore,
694
+ profile: dict[str, Any],
695
+ payload: dict[str, Any],
696
+ settings: ReaderSettings,
697
+ now_seconds: float,
698
+ schedule_snapshot: Callable[[], None],
699
+ ) -> dict[str, Any]:
700
+ problem = writes_comments(profile)
701
+ if problem:
702
+ return problem
703
+ comment, problem = own_comment(store, profile, payload, COMMENT_EDIT_KEYS)
704
+ if problem:
705
+ return problem
706
+ if comment.get("state") != COMMENT_VISIBLE:
707
+ return refusal(409, "comment_deleted", "A deleted comment cannot be edited.")
708
+ if now_seconds - seconds_of(comment["createdAt"]) > settings.comment_edit_minutes * 60:
709
+ return refusal(403, "edit_window_closed", f"A comment can be edited for {settings.comment_edit_minutes} minutes after it is posted.")
710
+ body, problem = clean_comment_body(payload.get("body"), settings)
711
+ if problem:
712
+ return problem
713
+ limited = within_rate_limit(store, profile, RATE_BUCKET_COMMENT, new_account_limit(profile, settings, now_seconds, settings.comments_per_hour), now_seconds)
714
+ if limited:
715
+ return limited
716
+ edited = iso(now_seconds)
717
+ if not store.update_comment(comment["id"], {"body": body, "editedAt": edited, "reviewedAt": None, "signals": None}, edited, expected_state=COMMENT_VISIBLE):
718
+ return refusal(409, "comment_changed", "That comment changed. Reload and try again.")
719
+ schedule_snapshot()
720
+ return reply(200, {"id": comment["id"], "body": body, "editedAt": edited})
721
+
722
+
723
+ def delete_comment_action(
724
+ store: ReaderStore,
725
+ profile: dict[str, Any],
726
+ payload: dict[str, Any],
727
+ settings: ReaderSettings,
728
+ now_seconds: float,
729
+ schedule_snapshot: Callable[[], None],
730
+ ) -> dict[str, Any]:
731
+ comment, problem = own_comment(store, profile, payload, COMMENT_DELETE_KEYS)
732
+ if problem:
733
+ return problem
734
+ if comment.get("state") == COMMENT_DELETED:
735
+ return reply(200, {"id": comment["id"], "state": COMMENT_DELETED})
736
+ deleted = iso(now_seconds)
737
+ if not store.update_comment(comment["id"], {"body": "", "deletedAt": deleted, "state": COMMENT_DELETED, "ratingContext": None}, deleted, expected_state=COMMENT_VISIBLE):
738
+ return refusal(409, "comment_changed", "That comment changed. Reload and try again.")
739
+ schedule_snapshot()
740
+ return reply(200, {"id": comment["id"], "state": COMMENT_DELETED})
741
+
742
+
743
+ def vote_action(store, profile, payload, settings, is_staff, now_seconds, schedule_snapshot):
744
+ on = payload.get("on")
745
+ if not isinstance(on, bool):
746
+ return refusal(422, "invalid_vote", "Send on as true or false.")
747
+ return react(
748
+ store, profile, subject=payload.get("subject"), dimension=payload.get("dimension"), allowed_dimensions=TOGGLE_DIMENSIONS,
749
+ value=1 if on else None, is_staff=is_staff, settings=settings, now_seconds=now_seconds, schedule_snapshot=schedule_snapshot,
750
+ )
751
+
752
+
753
+ def rating_action(store, profile, payload, settings, is_staff, now_seconds, schedule_snapshot):
754
+ value = payload.get("value")
755
+ if value is not None and (isinstance(value, bool) or not isinstance(value, int) or not MINIMUM_RATING <= value <= MAXIMUM_RATING):
756
+ return refusal(422, "invalid_rating", f"Send a whole number from {MINIMUM_RATING} to {MAXIMUM_RATING}, or null.")
757
+ return react(
758
+ store, profile, subject=payload.get("subject"), dimension=payload.get("dimension"), allowed_dimensions=settings.dimensions,
759
+ value=value, is_staff=is_staff, settings=settings, now_seconds=now_seconds, schedule_snapshot=schedule_snapshot,
760
+ )
761
+
762
+
763
+ def handle_event(
764
+ event: dict[str, Any],
765
+ *,
766
+ store: ReaderStore,
767
+ settings: ReaderSettings,
768
+ now_seconds: float,
769
+ schedule_snapshot: Callable[[], None],
770
+ ) -> dict[str, Any]:
771
+ site_subject, action, payload = event.get("siteSubject"), event.get("action"), event.get("payload")
772
+ if not valid_site_subject(site_subject, settings):
773
+ return refusal(403, "reader_identity_invalid", "The reader identity is not valid for this site.")
774
+ if not isinstance(payload, dict) or not isinstance(event.get("isStaff"), bool):
775
+ return refusal(400, "malformed_request", "The request is malformed.")
776
+ profile = ensure_profile(store, site_subject, now_seconds)
777
+ if action == ACTION_ME:
778
+ return my_state(store, profile, payload, settings)
779
+ if profile.get("state") == STATE_BANNED:
780
+ return refusal(403, "reader_banned", "This reader cannot take part.")
781
+ if action == ACTION_HANDLE:
782
+ return set_handle(store, profile, payload, settings, now_seconds)
783
+ if action == ACTION_VOTE:
784
+ return vote_action(store, profile, payload, settings, event["isStaff"], now_seconds, schedule_snapshot)
785
+ if action == ACTION_RATING:
786
+ return rating_action(store, profile, payload, settings, event["isStaff"], now_seconds, schedule_snapshot)
787
+ if action == ACTION_SUBMIT:
788
+ return submit_action(store, profile, payload, settings, now_seconds, schedule_snapshot)
789
+ if action == ACTION_COMMENT:
790
+ return comment_action(store, profile, payload, settings, now_seconds, schedule_snapshot)
791
+ if action == ACTION_COMMENT_EDIT:
792
+ return edit_comment_action(store, profile, payload, settings, now_seconds, schedule_snapshot)
793
+ if action == ACTION_COMMENT_DELETE:
794
+ return delete_comment_action(store, profile, payload, settings, now_seconds, schedule_snapshot)
795
+ if action == ACTION_REPORT:
796
+ return report_action(store, profile, payload, settings, now_seconds, schedule_snapshot)
797
+ return refusal(400, "unknown_action", "Unknown action.")
798
+
799
+
800
+ def plain(value: Any) -> Any:
801
+ if isinstance(value, Decimal):
802
+ return int(value) if value == value.to_integral_value() else float(value)
803
+ if isinstance(value, dict):
804
+ return {key: plain(item) for key, item in value.items()}
805
+ if isinstance(value, list):
806
+ return [plain(item) for item in value]
807
+ return value
808
+
809
+
810
+ def _without_none(item: dict[str, Any]) -> dict[str, Any]:
811
+ return {key: value for key, value in item.items() if value is not None}
812
+
813
+
814
+ def is_conditional_failure(error: Exception) -> bool:
815
+ return str(getattr(error, "response", {}).get("Error", {}).get("Code", "")) == "ConditionalCheckFailedException"
816
+
817
+
818
+ class DynamoReaderStore:
819
+ """The Amplify model tables written directly: items carry ``__typename``, ``createdAt`` and ``updatedAt`` so staff can list them."""
820
+
821
+ def __init__(
822
+ self,
823
+ *,
824
+ resource: Any,
825
+ profile_table: str,
826
+ handle_table: str,
827
+ vote_table: str,
828
+ published_item_table: str,
829
+ rate_table: str,
830
+ submission_table: str = "",
831
+ comment_table: str = "",
832
+ report_table: str = "",
833
+ action_table: str = "",
834
+ item_table: str = "",
835
+ ) -> None:
836
+ self._resource = resource
837
+ self._names = {
838
+ "profile": profile_table,
839
+ "handle": handle_table,
840
+ "vote": vote_table,
841
+ "published": published_item_table,
842
+ "rate": rate_table,
843
+ "submission": submission_table,
844
+ "comment": comment_table,
845
+ "report": report_table,
846
+ "action": action_table,
847
+ "item": item_table,
848
+ }
849
+
850
+ def _table(self, key: str) -> Any:
851
+ return self._resource.Table(self._names[key])
852
+
853
+ def get_profile(self, site_subject: str) -> dict[str, Any] | None:
854
+ item = self._table("profile").get_item(Key={"id": site_subject}, ConsistentRead=True).get("Item")
855
+ return plain(item) if item else None
856
+
857
+ def create_profile(self, profile: dict[str, Any]) -> bool:
858
+ item = {**profile, "__typename": "ReaderProfile", "updatedAt": profile["createdAt"]}
859
+ try:
860
+ self._table("profile").put_item(Item=item, ConditionExpression="attribute_not_exists(id)")
861
+ except Exception as error:
862
+ if is_conditional_failure(error):
863
+ return False
864
+ raise
865
+ return True
866
+
867
+ def claim_handle(self, site_subject: str, reader_id: str, handle: str, now_iso: str) -> str:
868
+ client = self._resource.meta.client
869
+ try:
870
+ client.transact_write_items(
871
+ TransactItems=[
872
+ {
873
+ "Put": {
874
+ "TableName": self._names["handle"],
875
+ "Item": {"id": handle, "__typename": "ReaderHandle", "readerId": reader_id, "createdAt": now_iso, "updatedAt": now_iso},
876
+ "ConditionExpression": "attribute_not_exists(id)",
877
+ }
878
+ },
879
+ {
880
+ "Update": {
881
+ "TableName": self._names["profile"],
882
+ "Key": {"id": site_subject},
883
+ "UpdateExpression": "SET #h = :h, updatedAt = :now",
884
+ "ConditionExpression": "attribute_exists(id) AND attribute_not_exists(#h)",
885
+ "ExpressionAttributeNames": {"#h": "handle"},
886
+ "ExpressionAttributeValues": {":h": handle, ":now": now_iso},
887
+ }
888
+ },
889
+ ]
890
+ )
891
+ except Exception as error:
892
+ reasons = getattr(error, "response", {}).get("CancellationReasons") or []
893
+ codes = [str(reason.get("Code", "")) for reason in reasons]
894
+ if codes[:1] == ["ConditionalCheckFailed"]:
895
+ return "taken"
896
+ if codes[1:2] == ["ConditionalCheckFailed"]:
897
+ return "already_set"
898
+ raise
899
+ return "claimed"
900
+
901
+ def get_vote_values(self, vote_ids: list[str]) -> dict[str, int]:
902
+ found: dict[str, int] = {}
903
+ table_name = self._names["vote"]
904
+ for start in range(0, len(vote_ids), 100):
905
+ pending = {table_name: {"Keys": [{"id": stored_id} for stored_id in vote_ids[start : start + 100]], "ProjectionExpression": "id, #v", "ExpressionAttributeNames": {"#v": "value"}}}
906
+ while pending:
907
+ response = self._resource.batch_get_item(RequestItems=pending)
908
+ for item in response.get("Responses", {}).get(table_name, []):
909
+ found[item["id"]] = int(item["value"])
910
+ pending = response.get("UnprocessedKeys") or {}
911
+ return found
912
+
913
+ def put_vote(self, vote: dict[str, Any], now_iso: str) -> None:
914
+ self._table("vote").update_item(
915
+ Key={"id": vote["id"]},
916
+ UpdateExpression=(
917
+ "SET #t = :t, subjectKey = :s, readerId = :r, #d = :d, #v = :v, voterRole = :role, "
918
+ "createdAt = if_not_exists(createdAt, :now), updatedAt = :now"
919
+ ),
920
+ ExpressionAttributeNames={"#t": "__typename", "#d": "dimension", "#v": "value"},
921
+ ExpressionAttributeValues={
922
+ ":t": "ReaderVote",
923
+ ":s": vote["subjectKey"],
924
+ ":r": vote["readerId"],
925
+ ":d": vote["dimension"],
926
+ ":v": vote["value"],
927
+ ":role": vote["voterRole"],
928
+ ":now": now_iso,
929
+ },
930
+ )
931
+
932
+ def delete_vote(self, vote_id_: str) -> None:
933
+ self._table("vote").delete_item(Key={"id": vote_id_})
934
+
935
+ def is_published(self, subject_key: str) -> bool:
936
+ item = self._table("published").get_item(Key={"id": PUBLISHED_ID_PREFIX + subject_key}, ProjectionExpression="#s", ExpressionAttributeNames={"#s": "status"}).get("Item")
937
+ return bool(item) and item.get("status") == "published"
938
+
939
+ def get_submission(self, submission_id: str) -> dict[str, Any] | None:
940
+ item = self._table("submission").get_item(Key={"id": submission_id}, ConsistentRead=True).get("Item")
941
+ return plain(item) if item else None
942
+
943
+ def create_submission(self, submission: dict[str, Any]) -> bool:
944
+ item = {**submission, "__typename": "ReaderSubmission", "updatedAt": submission["createdAt"]}
945
+ try:
946
+ self._table("submission").put_item(Item=item, ConditionExpression="attribute_not_exists(id)")
947
+ except Exception as error:
948
+ if is_conditional_failure(error):
949
+ return False
950
+ raise
951
+ return True
952
+
953
+ def recent_submissions_by_domain(self, domain: str, since_iso: str) -> list[dict[str, Any]]:
954
+ from boto3.dynamodb.conditions import Key
955
+
956
+ rows: list[dict[str, Any]] = []
957
+ kwargs: dict[str, Any] = {
958
+ "IndexName": SUBMISSION_DOMAIN_INDEX,
959
+ "KeyConditionExpression": Key("domain").eq(domain) & Key("createdAt").gte(since_iso),
960
+ }
961
+ while True:
962
+ page = self._table("submission").query(**kwargs)
963
+ rows.extend(plain(item) for item in page.get("Items", []))
964
+ if "LastEvaluatedKey" not in page:
965
+ return rows
966
+ kwargs["ExclusiveStartKey"] = page["LastEvaluatedKey"]
967
+
968
+ def get_comment(self, comment_id: str) -> dict[str, Any] | None:
969
+ item = self._table("comment").get_item(Key={"id": comment_id}, ConsistentRead=True).get("Item")
970
+ return plain(item) if item else None
971
+
972
+ def create_comment(self, comment: dict[str, Any]) -> None:
973
+ self._table("comment").put_item(
974
+ Item={**comment, "__typename": "ReaderComment", "updatedAt": comment["createdAt"]}, ConditionExpression="attribute_not_exists(id)"
975
+ )
976
+
977
+ def update_comment(self, comment_id: str, changes: dict[str, Any], now_iso: str, *, expected_state: str) -> bool:
978
+ """Sets the changed fields (``None`` removes one) while the comment is still in ``expected_state``."""
979
+ names = {"#state": "state"}
980
+ values: dict[str, Any] = {":expected": expected_state, ":now": now_iso}
981
+ sets, removes = ["updatedAt = :now"], []
982
+ for number, (field, value) in enumerate(changes.items()):
983
+ names[f"#f{number}"] = field
984
+ if value is None:
985
+ removes.append(f"#f{number}")
986
+ else:
987
+ values[f":v{number}"] = value
988
+ sets.append(f"#f{number} = :v{number}")
989
+ expression = "SET " + ", ".join(sets) + (" REMOVE " + ", ".join(removes) if removes else "")
990
+ try:
991
+ self._table("comment").update_item(
992
+ Key={"id": comment_id},
993
+ UpdateExpression=expression,
994
+ ConditionExpression="attribute_exists(id) AND #state = :expected",
995
+ ExpressionAttributeNames=names,
996
+ ExpressionAttributeValues=values,
997
+ )
998
+ except Exception as error:
999
+ if is_conditional_failure(error):
1000
+ return False
1001
+ raise
1002
+ return True
1003
+
1004
+ def put_report(self, report: dict[str, Any], target_key: str, target_table: str, now_iso: str) -> bool:
1005
+ """Stores the report and puts the target back in the queue in one transaction. A second report by the same reader changes nothing."""
1006
+ item = {**report, "__typename": "ReaderReport", "updatedAt": report["createdAt"]}
1007
+ try:
1008
+ self._resource.meta.client.transact_write_items(
1009
+ TransactItems=[
1010
+ {"Put": {"TableName": self._names["report"], "Item": _without_none(item), "ConditionExpression": "attribute_not_exists(id)"}},
1011
+ {
1012
+ "Update": {
1013
+ "TableName": self._names[target_table],
1014
+ "Key": {"id": target_key},
1015
+ "UpdateExpression": "SET moderation = :new, updatedAt = :now ADD reportCount :one",
1016
+ "ConditionExpression": "attribute_exists(id)",
1017
+ "ExpressionAttributeValues": {":new": moderation_rules.MODERATION_NEW, ":now": now_iso, ":one": 1},
1018
+ }
1019
+ },
1020
+ ]
1021
+ )
1022
+ except Exception as error:
1023
+ reasons = getattr(error, "response", {}).get("CancellationReasons") or []
1024
+ if [str(reason.get("Code", "")) for reason in reasons][:1] == ["ConditionalCheckFailed"]:
1025
+ return False
1026
+ raise
1027
+ return True
1028
+
1029
+ def get_profile_by_reader(self, reader_id: str) -> dict[str, Any] | None:
1030
+ from boto3.dynamodb.conditions import Key
1031
+
1032
+ page = self._table("profile").query(IndexName=PROFILE_READER_INDEX, KeyConditionExpression=Key("readerId").eq(reader_id))
1033
+ rows = page.get("Items", [])
1034
+ return plain(rows[0]) if rows else None
1035
+
1036
+ def get_action(self, action_id: str) -> dict[str, Any] | None:
1037
+ item = self._table("action").get_item(Key={"id": action_id}, ConsistentRead=True).get("Item")
1038
+ return plain(item) if item else None
1039
+
1040
+ def transact_moderation(self, action: dict[str, Any], target_type: str, target_key: str, plan: Any, *, undoes: str | None = None) -> str:
1041
+ """Writes the log row, the target change (only if the target still has the expected state) and, for an undo, the pointer on the original."""
1042
+ table_for = {moderation_rules.TARGET_SUBMISSION: "submission", moderation_rules.TARGET_COMMENT: "comment", moderation_rules.TARGET_READER: "profile"}
1043
+ names: dict[str, str] = {}
1044
+ values: dict[str, Any] = {":now": action["at"]}
1045
+ sets = ["updatedAt = :now"]
1046
+ conditions = ["attribute_exists(id)"]
1047
+ for number, (field, value) in enumerate(plan.changes.items()):
1048
+ names[f"#c{number}"] = field
1049
+ values[f":c{number}"] = value
1050
+ sets.append(f"#c{number} = :c{number}")
1051
+ for number, (field, value) in enumerate(plan.expected.items()):
1052
+ names[f"#e{number}"] = field
1053
+ values[f":e{number}"] = value
1054
+ conditions.append(f"#e{number} = :e{number}")
1055
+ items: list[dict[str, Any]] = [
1056
+ {"Put": {"TableName": self._names["action"], "Item": _without_none({**action, "__typename": "ModerationAction", "updatedAt": action["at"]}), "ConditionExpression": "attribute_not_exists(id)"}},
1057
+ {
1058
+ "Update": {
1059
+ "TableName": self._names[table_for[target_type]],
1060
+ "Key": {"id": target_key},
1061
+ "UpdateExpression": "SET " + ", ".join(sets),
1062
+ "ConditionExpression": " AND ".join(conditions),
1063
+ "ExpressionAttributeNames": names,
1064
+ "ExpressionAttributeValues": values,
1065
+ }
1066
+ },
1067
+ ]
1068
+ if undoes:
1069
+ items.append(
1070
+ {
1071
+ "Update": {
1072
+ "TableName": self._names["action"],
1073
+ "Key": {"id": undoes},
1074
+ "UpdateExpression": "SET undoneBy = :by, updatedAt = :now",
1075
+ "ConditionExpression": "attribute_exists(id) AND attribute_not_exists(undoneBy)",
1076
+ "ExpressionAttributeValues": {":by": action["id"], ":now": action["at"]},
1077
+ }
1078
+ }
1079
+ )
1080
+ if plan.item is not None:
1081
+ items.append({"Put": {"TableName": self._names["item"], "Item": _without_none(plan.item), "ConditionExpression": "attribute_not_exists(id)"}})
1082
+ try:
1083
+ self._resource.meta.client.transact_write_items(TransactItems=items)
1084
+ except Exception as error:
1085
+ reasons = getattr(error, "response", {}).get("CancellationReasons") or []
1086
+ codes = [str(reason.get("Code", "")) for reason in reasons]
1087
+ if codes[1:2] == ["ConditionalCheckFailed"]:
1088
+ return "target_changed"
1089
+ if codes[2:3] == ["ConditionalCheckFailed"] and undoes:
1090
+ return "already_undone"
1091
+ raise
1092
+ return "ok"
1093
+
1094
+ def close_reports(self, target: str, now_iso: str) -> int:
1095
+ from boto3.dynamodb.conditions import Key
1096
+
1097
+ table = self._table("report")
1098
+ closed = 0
1099
+ kwargs: dict[str, Any] = {"IndexName": REPORT_TARGET_INDEX, "KeyConditionExpression": Key("target").eq(target)}
1100
+ while True:
1101
+ page = table.query(**kwargs)
1102
+ for row in page.get("Items", []):
1103
+ if row.get("state") == "open":
1104
+ table.update_item(
1105
+ Key={"id": row["id"]},
1106
+ UpdateExpression="SET #s = :closed, closedAt = :now, updatedAt = :now",
1107
+ ExpressionAttributeNames={"#s": "state"},
1108
+ ExpressionAttributeValues={":closed": "closed", ":now": now_iso},
1109
+ )
1110
+ closed += 1
1111
+ if "LastEvaluatedKey" not in page:
1112
+ return closed
1113
+ kwargs["ExclusiveStartKey"] = page["LastEvaluatedKey"]
1114
+
1115
+ def _scan(self, table_key: str, **arguments: Any) -> list[dict[str, Any]]:
1116
+ rows: list[dict[str, Any]] = []
1117
+ while True:
1118
+ page = self._table(table_key).scan(**arguments)
1119
+ rows.extend(plain(item) for item in page.get("Items", []))
1120
+ if "LastEvaluatedKey" not in page:
1121
+ return rows
1122
+ arguments["ExclusiveStartKey"] = page["LastEvaluatedKey"]
1123
+
1124
+ def new_content(self) -> list[tuple[str, dict[str, Any]]]:
1125
+ """Submissions and comments waiting for a decision (``moderation: new``). A scan: the queue of a pilot is small."""
1126
+ from boto3.dynamodb.conditions import Attr
1127
+
1128
+ waiting = Attr("moderation").eq(moderation_rules.MODERATION_NEW)
1129
+ return [
1130
+ *((moderation_rules.TARGET_SUBMISSION, row) for row in self._scan("submission", FilterExpression=waiting)),
1131
+ *((moderation_rules.TARGET_COMMENT, row) for row in self._scan("comment", FilterExpression=waiting)),
1132
+ ]
1133
+
1134
+ def human_acted_on(self) -> set[str]:
1135
+ """Targets a person has decided on; the model never judges those again."""
1136
+ from boto3.dynamodb.conditions import Attr
1137
+
1138
+ rows = self._scan("action", FilterExpression=Attr("by").eq(moderation_rules.BY_HUMAN))
1139
+ return {row["target"] for row in rows}
1140
+
1141
+ def count_action(self, reader_id: str, bucket: str, window_start: int, limit: int, expires_at: int) -> bool:
1142
+ try:
1143
+ self._table("rate").update_item(
1144
+ Key={"pk": f"{reader_id}#{bucket}#{window_start}"},
1145
+ UpdateExpression="ADD hits :one SET expiresAt = :expires",
1146
+ ConditionExpression="attribute_not_exists(hits) OR hits < :limit",
1147
+ ExpressionAttributeValues={":one": 1, ":expires": expires_at, ":limit": limit},
1148
+ )
1149
+ except Exception as error:
1150
+ if is_conditional_failure(error):
1151
+ return False
1152
+ raise
1153
+ return True
1154
+
1155
+ def claim_snapshot_slot(self, now_seconds: float, debounce_seconds: int) -> int | None:
1156
+ """Opens a debounce window and returns when it closes, or ``None`` when a window is already open."""
1157
+ locked_until = int(now_seconds) + debounce_seconds
1158
+ try:
1159
+ self._table("rate").put_item(
1160
+ Item={"pk": "snapshot-debounce", "lockedUntil": locked_until, "expiresAt": locked_until + debounce_seconds + HOUR_SECONDS},
1161
+ ConditionExpression="attribute_not_exists(pk) OR lockedUntil <= :now",
1162
+ ExpressionAttributeValues={":now": int(now_seconds)},
1163
+ )
1164
+ except Exception as error:
1165
+ if is_conditional_failure(error):
1166
+ return None
1167
+ raise
1168
+ return locked_until
1169
+
1170
+ def release_snapshot_slot(self, locked_until: int) -> None:
1171
+ """Reopens the window this call opened, so the next write can schedule the run that failed to schedule."""
1172
+ try:
1173
+ self._table("rate").delete_item(
1174
+ Key={"pk": "snapshot-debounce"},
1175
+ ConditionExpression="lockedUntil = :until",
1176
+ ExpressionAttributeValues={":until": locked_until},
1177
+ )
1178
+ except Exception as error:
1179
+ if not is_conditional_failure(error):
1180
+ raise
1181
+
1182
+
1183
+ def snapshot_schedule_name(locked_until: int) -> str:
1184
+ return f"{SNAPSHOT_SCHEDULE_PREFIX}{locked_until}"
1185
+
1186
+
1187
+ def snapshot_scheduler(
1188
+ store: ReaderStore, settings: ReaderSettings, now_seconds: float, target: "SnapshotTarget | None", scheduler_client: Any
1189
+ ) -> Callable[[], None]:
1190
+ """Trailing-edge debounce: the first write in a window schedules one run at the end of the window.
1191
+
1192
+ Later writes in the window schedule nothing, because the run at the end of the window reads them all. A failure never
1193
+ fails the vote; it reopens the window so the next write tries again.
1194
+ """
1195
+
1196
+ def schedule() -> None:
1197
+ if target is None:
1198
+ return
1199
+ locked_until = None
1200
+ try:
1201
+ locked_until = store.claim_snapshot_slot(now_seconds, settings.snapshot_debounce_seconds)
1202
+ if locked_until is None:
1203
+ return
1204
+ scheduler_client.create_schedule(
1205
+ Name=snapshot_schedule_name(locked_until),
1206
+ ScheduleExpression="at(" + time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(locked_until)) + ")",
1207
+ ScheduleExpressionTimezone="UTC",
1208
+ FlexibleTimeWindow={"Mode": "OFF"},
1209
+ ActionAfterCompletion="DELETE",
1210
+ Target={
1211
+ "Arn": target.function_arn,
1212
+ "RoleArn": target.role_arn,
1213
+ "Input": target.task_json,
1214
+ "RetryPolicy": {"MaximumRetryAttempts": 0},
1215
+ },
1216
+ )
1217
+ except Exception as error:
1218
+ print(json.dumps({"readerSnapshotScheduleFailed": type(error).__name__}))
1219
+ if locked_until is not None and type(error).__name__ != "ConflictException":
1220
+ try:
1221
+ store.release_snapshot_slot(locked_until)
1222
+ except Exception as release_error:
1223
+ print(json.dumps({"readerSnapshotReleaseFailed": type(release_error).__name__}))
1224
+
1225
+ return schedule
1226
+
1227
+
1228
+ @dataclass(frozen=True)
1229
+ class SnapshotTarget:
1230
+ function_arn: str
1231
+ role_arn: str
1232
+ task_json: str = json.dumps(SNAPSHOT_TASK)
1233
+
1234
+ def for_ai_review(self, own_function_arn: str) -> "SnapshotTarget":
1235
+ """In AI mode the debounced run is the AI worker (this function), which asks for the snapshot when it has decided."""
1236
+ return SnapshotTarget(own_function_arn, self.role_arn, json.dumps(AI_TASK))
1237
+
1238
+
1239
+ def snapshot_target_from_environment(environment: dict[str, str] | None = None) -> SnapshotTarget | None:
1240
+ environment = os.environ if environment is None else environment
1241
+ function_arn = environment.get("PAPYRUS_READER_SNAPSHOT_FUNCTION_ARN", "")
1242
+ role_arn = environment.get("PAPYRUS_READER_SCHEDULER_ROLE_ARN", "")
1243
+ return SnapshotTarget(function_arn, role_arn) if function_arn and role_arn else None
1244
+
1245
+
1246
+ def store_from_environment(environment: dict[str, str] | None = None) -> DynamoReaderStore:
1247
+ import boto3
1248
+
1249
+ environment = os.environ if environment is None else environment
1250
+ return DynamoReaderStore(
1251
+ resource=boto3.resource("dynamodb"),
1252
+ profile_table=environment["PAPYRUS_READER_PROFILE_TABLE"],
1253
+ handle_table=environment["PAPYRUS_READER_HANDLE_TABLE"],
1254
+ vote_table=environment["PAPYRUS_READER_VOTE_TABLE"],
1255
+ published_item_table=environment["PAPYRUS_READER_PUBLISHED_ITEM_TABLE"],
1256
+ rate_table=environment["PAPYRUS_READER_RATE_TABLE"],
1257
+ submission_table=environment.get("PAPYRUS_READER_SUBMISSION_TABLE", ""),
1258
+ comment_table=environment.get("PAPYRUS_READER_COMMENT_TABLE", ""),
1259
+ report_table=environment.get("PAPYRUS_READER_REPORT_TABLE", ""),
1260
+ action_table=environment.get("PAPYRUS_READER_ACTION_TABLE", ""),
1261
+ item_table=environment.get("PAPYRUS_READER_ITEM_TABLE", ""),
1262
+ )
1263
+
1264
+
1265
+ def read_api_key(settings: ReaderSettings, ssm_client: Any) -> str:
1266
+ """The model's API key from its SSM SecureString (never Secrets Manager). The caller keeps it out of logs and results."""
1267
+ name = settings.ai_key_parameter
1268
+ if not ai_moderation.PARAMETER_NAME.fullmatch(name):
1269
+ raise ValueError("moderation.ai.apiKeyParameter is not a /papyrus/<site>/openai-api-key name")
1270
+ value = ssm_client.get_parameter(Name=name, WithDecryption=True)["Parameter"]["Value"].strip()
1271
+ if not value:
1272
+ raise ValueError("the API key parameter is empty")
1273
+ return value
1274
+
1275
+
1276
+ def run_ai_worker(
1277
+ *,
1278
+ store: Any,
1279
+ settings: ReaderSettings,
1280
+ now: Callable[[], float],
1281
+ deadline: float,
1282
+ read_key: Callable[[], str],
1283
+ model_factory: Callable[[str, str], Any],
1284
+ invoke_snapshot: Callable[[], None],
1285
+ ) -> dict[str, Any]:
1286
+ """The scheduled AI review: judge new items, then ask for a snapshot so decisions reach the public files.
1287
+
1288
+ Anything that goes wrong is reported by error class name. The snapshot is requested whatever happens, so votes still show.
1289
+ """
1290
+ if settings.moderation_mode != "ai":
1291
+ return {"ok": False, "error": "ai_moderation_off"}
1292
+ result: dict[str, Any]
1293
+ try:
1294
+ model = model_factory(read_key(), settings.ai_model)
1295
+ report = ai_moderation.run_ai_moderation(
1296
+ store,
1297
+ model,
1298
+ now=now,
1299
+ settings=moderation_rules.ModerationSettings(item_type=settings.promote_item_type, section=settings.promote_section),
1300
+ deadline=deadline,
1301
+ iso=iso,
1302
+ )
1303
+ result = {"ok": True, **report.to_dict()}
1304
+ except Exception as error: # noqa: BLE001 - never put the message in the result: it could echo a key or reader text
1305
+ result = {"ok": False, "error": type(error).__name__}
1306
+ try:
1307
+ invoke_snapshot()
1308
+ except Exception as error: # noqa: BLE001
1309
+ result["snapshotError"] = type(error).__name__
1310
+ return result
1311
+
1312
+
1313
+ def handle_staff_event(event: dict[str, Any], *, store: Any, settings: ReaderSettings, now_seconds: float) -> dict[str, Any]:
1314
+ """``moderateReader`` from AppSync: an editor or admin hides, approves, promotes, bans or undoes. Answers a receipt, never an exception."""
1315
+ identity = event.get("identity") or {}
1316
+ claims = identity.get("claims") or {}
1317
+ groups = claims.get("cognito:groups") or identity.get("groups") or []
1318
+ if isinstance(groups, str):
1319
+ groups = [group.strip() for group in groups.strip("[]").replace(",", " ").split()]
1320
+ subject = identity.get("sub")
1321
+ if not isinstance(subject, str) or not subject or not moderation_rules.STAFF_GROUPS.intersection(groups):
1322
+ return moderation_rules.refused("not_staff", "Only editors and admins can moderate.").to_dict()
1323
+ arguments = event.get("arguments") or {}
1324
+ outcome = moderation_rules.apply_action(
1325
+ store,
1326
+ action=arguments.get("action"),
1327
+ target_type=arguments.get("targetType"),
1328
+ target=arguments.get("target"),
1329
+ reason=arguments.get("reason"),
1330
+ undo_of=arguments.get("undoOf"),
1331
+ actor_sub=subject,
1332
+ by=moderation_rules.BY_HUMAN,
1333
+ now_iso=iso(now_seconds),
1334
+ settings=moderation_rules.ModerationSettings(item_type=settings.promote_item_type, section=settings.promote_section),
1335
+ )
1336
+ return outcome.to_dict()
1337
+
1338
+
1339
+ def handler(event: dict[str, Any], _context: Any = None) -> dict[str, Any]:
1340
+ import boto3
1341
+
1342
+ settings = settings_from_environment()
1343
+ store = store_from_environment()
1344
+ now_seconds = time.time()
1345
+ if "arguments" in event and "info" in event:
1346
+ return handle_staff_event(event, store=store, settings=settings, now_seconds=now_seconds)
1347
+ target = snapshot_target_from_environment()
1348
+ if settings.moderation_mode == "ai" and target is not None and _context is not None:
1349
+ if event.get("worker") == AI_WORKER:
1350
+ remaining = _context.get_remaining_time_in_millis() / 1000 - AI_DEADLINE_MARGIN_SECONDS
1351
+ lambda_client = boto3.client("lambda")
1352
+ return run_ai_worker(
1353
+ store=store,
1354
+ settings=settings,
1355
+ now=time.time,
1356
+ deadline=time.time() + max(remaining, 5),
1357
+ read_key=lambda: read_api_key(settings, boto3.client("ssm")),
1358
+ model_factory=lambda key, model: ai_moderation.OpenAIModerationModel(key, model=model),
1359
+ invoke_snapshot=lambda: lambda_client.invoke(FunctionName=target.function_arn, InvocationType="Event", Payload=json.dumps(SNAPSHOT_TASK).encode("utf-8")),
1360
+ )
1361
+ target = target.for_ai_review(_context.invoked_function_arn)
1362
+ scheduler = snapshot_scheduler(store, settings, now_seconds, target, boto3.client("scheduler"))
1363
+ return handle_event(event, store=store, settings=settings, now_seconds=now_seconds, schedule_snapshot=scheduler)