Move SX construction from Python to .sx defcomps (phases 0-4)

Eliminate Python s-expression string building across account, orders,
federation, and cart services. Visual rendering logic now lives entirely
in .sx defcomp components; Python files contain only data serialization,
header/layout wiring, and thin wrappers that call defcomps.

Phase 0: Shared DRY extraction — auth/orders header defcomps, format-decimal/
pluralize/escape/route-prefix primitives.
Phase 1: Account — dashboard, newsletters, login/device/check-email content.
Phase 2: Orders — order list, detail, filter, checkout return assembled defcomps.
Phase 3: Federation — social nav, post cards, timeline, search, actors,
notifications, compose, profile assembled defcomps.
Phase 4: Cart — overview, page cart items/calendar/tickets/summary, admin,
payments assembled defcomps; orders rendering reuses Phase 2 shared defcomps.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-03 22:36:34 +00:00
parent 03f0929fdf
commit 193578ef88
23 changed files with 1824 additions and 1795 deletions

View File

@@ -27,22 +27,28 @@ def _register_federation_layouts() -> None:
def _social_full(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import root_header_sx, header_child_sx
from sx.sx_components import _social_header_sx
from shared.sx.helpers import root_header_sx, header_child_sx, sx_call, SxExpr
from shared.sx.parser import serialize
actor = ctx.get("actor")
actor_data = _serialize_actor(actor) if actor else None
nav = sx_call("federation-social-nav",
actor=SxExpr(serialize(actor_data)) if actor_data else None)
social_hdr = sx_call("federation-social-header", nav=SxExpr(nav))
root_hdr = root_header_sx(ctx)
social_hdr = _social_header_sx(actor)
child = header_child_sx(social_hdr)
return "(<> " + root_hdr + " " + child + ")"
def _social_oob(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import root_header_sx, sx_call, SxExpr
from sx.sx_components import _social_header_sx
from shared.sx.parser import serialize
actor = ctx.get("actor")
social_hdr = _social_header_sx(actor)
actor_data = _serialize_actor(actor) if actor else None
nav = sx_call("federation-social-nav",
actor=SxExpr(serialize(actor_data)) if actor_data else None)
social_hdr = sx_call("federation-social-header", nav=SxExpr(nav))
child_oob = sx_call("oob-header-sx",
parent_id="root-header-child",
row=SxExpr(social_hdr))
@@ -69,6 +75,58 @@ def _register_federation_helpers() -> None:
})
def _serialize_actor(actor) -> dict | None:
"""Serialize an actor profile to a dict for sx defcomps."""
if not actor:
return None
return {
"id": actor.id,
"preferred_username": actor.preferred_username,
"display_name": getattr(actor, "display_name", None),
"icon_url": getattr(actor, "icon_url", None),
"summary": getattr(actor, "summary", None),
"actor_url": getattr(actor, "actor_url", ""),
"domain": getattr(actor, "domain", ""),
}
def _serialize_timeline_item(item) -> dict:
"""Serialize a timeline item DTO to a dict for sx defcomps."""
published = getattr(item, "published", None)
return {
"object_id": getattr(item, "object_id", "") or "",
"author_inbox": getattr(item, "author_inbox", "") or "",
"actor_icon": getattr(item, "actor_icon", None),
"actor_name": getattr(item, "actor_name", "?"),
"actor_username": getattr(item, "actor_username", ""),
"actor_domain": getattr(item, "actor_domain", ""),
"content": getattr(item, "content", ""),
"summary": getattr(item, "summary", None),
"published": published.strftime("%b %d, %H:%M") if published else "",
"before_cursor": published.isoformat() if published else "",
"url": getattr(item, "url", None),
"post_type": getattr(item, "post_type", ""),
"boosted_by": getattr(item, "boosted_by", None),
"like_count": getattr(item, "like_count", 0) or 0,
"boost_count": getattr(item, "boost_count", 0) or 0,
"liked_by_me": getattr(item, "liked_by_me", False),
"boosted_by_me": getattr(item, "boosted_by_me", False),
}
def _serialize_remote_actor(a) -> dict:
"""Serialize a remote actor DTO to a dict for sx defcomps."""
return {
"id": getattr(a, "id", None),
"display_name": getattr(a, "display_name", None) or getattr(a, "preferred_username", ""),
"preferred_username": getattr(a, "preferred_username", ""),
"domain": getattr(a, "domain", ""),
"icon_url": getattr(a, "icon_url", None),
"actor_url": getattr(a, "actor_url", ""),
"summary": getattr(a, "summary", None),
}
def _get_actor():
"""Return current user's actor or None."""
from quart import g
@@ -87,32 +145,43 @@ def _require_actor():
async def _h_home_timeline_content(**kw):
from quart import g
from shared.services.registry import services
from shared.sx.helpers import sx_call, SxExpr
from shared.sx.parser import serialize
actor = _require_actor()
items = await services.federation.get_home_timeline(g.s, actor.id)
from sx.sx_components import _timeline_content_sx
return _timeline_content_sx(items, "home", actor)
return sx_call("federation-timeline-content",
items=SxExpr(serialize([_serialize_timeline_item(i) for i in items])),
timeline_type="home",
actor=SxExpr(serialize(_serialize_actor(actor))))
async def _h_public_timeline_content(**kw):
from quart import g
from shared.services.registry import services
from shared.sx.helpers import sx_call, SxExpr
from shared.sx.parser import serialize
actor = _get_actor()
items = await services.federation.get_public_timeline(g.s)
from sx.sx_components import _timeline_content_sx
return _timeline_content_sx(items, "public", actor)
return sx_call("federation-timeline-content",
items=SxExpr(serialize([_serialize_timeline_item(i) for i in items])),
timeline_type="public",
actor=SxExpr(serialize(_serialize_actor(actor))) if actor else None)
async def _h_compose_content(**kw):
from quart import request
actor = _require_actor()
from sx.sx_components import _compose_content_sx
from shared.sx.helpers import sx_call
_require_actor()
reply_to = request.args.get("reply_to")
return _compose_content_sx(actor, reply_to)
return sx_call("federation-compose-content",
reply_to=reply_to or None)
async def _h_search_content(**kw):
from quart import g, request
from shared.services.registry import services
from shared.sx.helpers import sx_call, SxExpr
from shared.sx.parser import serialize
actor = _get_actor()
query = request.args.get("q", "").strip()
actors_list = []
@@ -125,24 +194,34 @@ async def _h_search_content(**kw):
g.s, actor.preferred_username, page=1, per_page=1000,
)
followed_urls = {a.actor_url for a in following}
from sx.sx_components import _search_content_sx
return _search_content_sx(query, actors_list, total, 1, followed_urls, actor)
return sx_call("federation-search-content",
query=query,
actors=SxExpr(serialize([_serialize_remote_actor(a) for a in actors_list])),
total=total,
followed_urls=SxExpr(serialize(list(followed_urls))),
actor=SxExpr(serialize(_serialize_actor(actor))) if actor else None)
async def _h_following_content(**kw):
from quart import g
from shared.services.registry import services
from shared.sx.helpers import sx_call, SxExpr
from shared.sx.parser import serialize
actor = _require_actor()
actors_list, total = await services.federation.get_following(
g.s, actor.preferred_username,
)
from sx.sx_components import _following_content_sx
return _following_content_sx(actors_list, total, actor)
return sx_call("federation-following-content",
actors=SxExpr(serialize([_serialize_remote_actor(a) for a in actors_list])),
total=total,
actor=SxExpr(serialize(_serialize_actor(actor))))
async def _h_followers_content(**kw):
from quart import g
from shared.services.registry import services
from shared.sx.helpers import sx_call, SxExpr
from shared.sx.parser import serialize
actor = _require_actor()
actors_list, total = await services.federation.get_followers_paginated(
g.s, actor.preferred_username,
@@ -151,13 +230,18 @@ async def _h_followers_content(**kw):
g.s, actor.preferred_username, page=1, per_page=1000,
)
followed_urls = {a.actor_url for a in following}
from sx.sx_components import _followers_content_sx
return _followers_content_sx(actors_list, total, followed_urls, actor)
return sx_call("federation-followers-content",
actors=SxExpr(serialize([_serialize_remote_actor(a) for a in actors_list])),
total=total,
followed_urls=SxExpr(serialize(list(followed_urls))),
actor=SxExpr(serialize(_serialize_actor(actor))))
async def _h_actor_timeline_content(id=None, **kw):
from quart import g, abort
from shared.services.registry import services
from shared.sx.helpers import sx_call, SxExpr
from shared.sx.parser import serialize
actor = _get_actor()
actor_id = id
from shared.models.federation import RemoteActor
@@ -184,15 +268,35 @@ async def _h_actor_timeline_content(id=None, **kw):
)
).scalar_one_or_none()
is_following = existing is not None
from sx.sx_components import _actor_timeline_content_sx
return _actor_timeline_content_sx(remote_dto, items, is_following, actor)
return sx_call("federation-actor-timeline-content",
remote_actor=SxExpr(serialize(_serialize_remote_actor(remote_dto))),
items=SxExpr(serialize([_serialize_timeline_item(i) for i in items])),
is_following=is_following,
actor=SxExpr(serialize(_serialize_actor(actor))) if actor else None)
async def _h_notifications_content(**kw):
from quart import g
from shared.services.registry import services
from shared.sx.helpers import sx_call, SxExpr
from shared.sx.parser import serialize
actor = _require_actor()
items = await services.federation.get_notifications(g.s, actor.id)
await services.federation.mark_notifications_read(g.s, actor.id)
from sx.sx_components import _notifications_content_sx
return _notifications_content_sx(items)
notif_dicts = []
for n in items:
created = getattr(n, "created_at", None)
notif_dicts.append({
"from_actor_name": getattr(n, "from_actor_name", "?"),
"from_actor_username": getattr(n, "from_actor_username", ""),
"from_actor_domain": getattr(n, "from_actor_domain", ""),
"from_actor_icon": getattr(n, "from_actor_icon", None),
"notification_type": getattr(n, "notification_type", ""),
"target_content_preview": getattr(n, "target_content_preview", None),
"created_at_formatted": created.strftime("%b %d, %H:%M") if created else "",
"read": getattr(n, "read", True),
"app_domain": getattr(n, "app_domain", ""),
})
return sx_call("federation-notifications-content",
notifications=SxExpr(serialize(notif_dicts)))