Weekly summary task (phase 3, step 5)
CI / validate-monitoring (push) Successful in 10s
CI / lint (push) Successful in 16s
CI / federation-interop (push) Successful in 1m2s
CI / test (push) Successful in 2m40s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 6s
CI / validate-monitoring (push) Successful in 10s
CI / lint (push) Successful in 16s
CI / federation-interop (push) Successful in 1m2s
CI / test (push) Successful in 2m40s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 6s
protocol/schedule.py (pure): target_week(now_utc, tz) resolves to exactly one Monday-Sunday week — the most recent one whose Monday 09:00-local posting gate has passed — computed via wall-clock arithmetic in the user's own zone so it's correct across a DST change (a week can be 6h23/7h01 of real time, never silently 7 flat days). Because it's a pure function of "now", a worker that was down for weeks naturally catches up with one post, not a backlog. Also resolve_timezone()/is_valid_timezone() for a settings field that can otherwise crash a task or silently do nothing forever. protocol/content.py (pure): the small HTML subset shared by every post kind (p, strong, em, ol, li), with escaping split from formatting so a hostile artist/track name can't inject markup. publishing/weekly.py: build_content() (the template) and check_and_post()/check_all() (the federation.weekly periodic task, every 15 minutes). An empty week is simply never posted, and — since the target week only ever advances — never reconsidered either, with no separate marker needed. has_post() lets the common "already posted this week" pass skip the stats queries entirely. Core: accounts.get_user(id) (federation needs it and can't import the User model directly); sharing.EDITABLE and update() gain timezone/post_weekly_summary/post_milestones, with the same now-not-later validation visibility already had. Tested: the DST week and gate boundaries against real computed reference dates (not guessed ones — one guess was wrong and the test caught it before it shipped), hostile/emoji/RTL names in the template, and the full task against a real database (empty vs non-empty weeks, posting twice, disabled settings, a worker down for weeks, a bad stored timezone not stopping other users). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
This commit is contained in:
1 parent
b16ba116c4
commit
0766c09fb3
13 files changed
+576
-3
No files matched your search
@@ -0,0 +1,31 @@
|
||||
"""Safe post content: plain text and matching HTML, shared by every post kind so
|
||||
they're formatted consistently and every track/artist name is escaped the same way.
|
||||
|
||||
Only the tags Mastodon keeps: p, strong, em, ol, li. No br is needed here since each
|
||||
line of a post is its own paragraph.
|
||||
"""
|
||||
|
||||
import html
|
||||
|
||||
HASHTAG = "Scrobbler"
|
||||
|
||||
|
||||
def escape(text: str) -> str:
|
||||
"""HTML-escape a name (artist, track, ...) for embedding in generated markup."""
|
||||
return html.escape(text, quote=False)
|
||||
|
||||
|
||||
def paragraph(inner_html: str) -> str:
|
||||
return f"<p>{inner_html}</p>"
|
||||
|
||||
|
||||
def numbered_list(items_html: list[str]) -> str:
|
||||
return "<ol>" + "".join(f"<li>{item}</li>" for item in items_html) + "</ol>"
|
||||
|
||||
|
||||
def hashtag_line() -> str:
|
||||
return f"#{HASHTAG}"
|
||||
|
||||
|
||||
def plural(count: int, word: str) -> str:
|
||||
return word if count == 1 else f"{word}s"
|
||||
@@ -0,0 +1,51 @@
|
||||
"""Pure date/time math for scheduled posts: which week is due, in a user's own zone.
|
||||
|
||||
Weeks run Monday 00:00 to Sunday 24:00 *local time*, computed in the zone itself (not as
|
||||
a fixed UTC offset), so the window is correct across a daylight-saving change.
|
||||
"""
|
||||
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
|
||||
|
||||
POST_HOUR = 9 # local time a week's summary becomes postable, on the following Monday
|
||||
|
||||
|
||||
def target_week(now_utc: datetime, tz: ZoneInfo) -> tuple[datetime, datetime, str]:
|
||||
"""The [start, end) UTC window, and ISO year-week key (e.g. "2026-W02"), of the most
|
||||
recent Monday-Sunday week whose Monday-POST_HOUR-local posting gate has passed.
|
||||
|
||||
Always resolves to exactly one week, so a worker that was down for a while catches up
|
||||
with a single post (the latest eligible week), not one per week it missed.
|
||||
"""
|
||||
local_now = now_utc.astimezone(tz)
|
||||
gate_monday = local_now.date() - timedelta(days=local_now.weekday())
|
||||
gate = datetime(gate_monday.year, gate_monday.month, gate_monday.day, POST_HOUR, tzinfo=tz)
|
||||
if local_now < gate:
|
||||
gate_monday -= timedelta(days=7)
|
||||
week_start = gate_monday - timedelta(days=7)
|
||||
|
||||
start_local = datetime(week_start.year, week_start.month, week_start.day, tzinfo=tz)
|
||||
end_local = start_local + timedelta(days=7) # wall-clock +7 days: DST-correct on conversion
|
||||
year, week, _ = week_start.isocalendar()
|
||||
return start_local.astimezone(UTC), end_local.astimezone(UTC), f"{year}-W{week:02d}"
|
||||
|
||||
|
||||
def resolve_timezone(name: str) -> ZoneInfo:
|
||||
"""A user's timezone setting, or UTC for anything that isn't a valid IANA name (a
|
||||
bad or since-removed name in one user's settings must never crash the task for
|
||||
everyone else)."""
|
||||
try:
|
||||
return ZoneInfo(name)
|
||||
except (ZoneInfoNotFoundError, ValueError):
|
||||
return ZoneInfo("UTC")
|
||||
|
||||
|
||||
def is_valid_timezone(name: str) -> bool:
|
||||
"""For validating user input (reject a bad name with an error), as opposed to
|
||||
resolve_timezone's silent fallback (never crash the periodic task over one user's
|
||||
stale setting)."""
|
||||
try:
|
||||
ZoneInfo(name)
|
||||
except (ZoneInfoNotFoundError, ValueError):
|
||||
return False
|
||||
return True
|
||||
Reference in new issue
Block a user