Replace sx_call() with render_to_sx() across all services
Python no longer generates s-expression strings. All SX rendering now goes through render_to_sx() which builds AST from native Python values and evaluates via async_eval_to_sx() — no SX string literals in Python. - Add render_to_sx()/render_to_html() infrastructure in shared/sx/helpers.py - Add (abort status msg) IO primitive in shared/sx/primitives_io.py - Convert all 9 services: ~650 sx_call() invocations replaced - Convert shared helpers (root_header_sx, full_page_sx, etc.) to async - Fix likes service import bug (likes.models → models) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -156,7 +156,7 @@ def register(url_prefix="/social"):
|
||||
else:
|
||||
list_type = "following"
|
||||
from sx.sx_components import render_actor_card
|
||||
return sx_response(render_actor_card(remote_dto, actor, followed_urls, list_type=list_type))
|
||||
return sx_response(await render_actor_card(remote_dto, actor, followed_urls, list_type=list_type))
|
||||
|
||||
# -- Interactions ----------------------------------------------------------
|
||||
|
||||
@@ -243,7 +243,7 @@ def register(url_prefix="/social"):
|
||||
)).scalar())
|
||||
|
||||
from sx.sx_components import render_interaction_buttons
|
||||
return sx_response(render_interaction_buttons(
|
||||
return sx_response(await render_interaction_buttons(
|
||||
object_id=object_id,
|
||||
author_inbox=author_inbox,
|
||||
like_count=like_count,
|
||||
|
||||
@@ -13,9 +13,8 @@ from typing import Any
|
||||
from markupsafe import escape
|
||||
|
||||
from shared.sx.jinja_bridge import load_service_components
|
||||
from shared.sx.parser import serialize
|
||||
from shared.sx.helpers import (
|
||||
sx_call, SxExpr,
|
||||
render_to_sx,
|
||||
root_header_sx, full_page_sx, header_child_sx,
|
||||
)
|
||||
|
||||
@@ -81,16 +80,16 @@ def _serialize_remote_actor(a) -> dict:
|
||||
# Social page shell
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _social_page(ctx: dict, actor: Any, *, content: str,
|
||||
async def _social_page(ctx: dict, actor: Any, *, content: str,
|
||||
title: str = "Rose Ash", meta_html: str = "") -> str:
|
||||
from shared.sx.parser import SxExpr
|
||||
actor_data = _serialize_actor(actor)
|
||||
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))
|
||||
hdr = root_header_sx(ctx)
|
||||
child = header_child_sx(social_hdr)
|
||||
nav = await render_to_sx("federation-social-nav", actor=actor_data)
|
||||
social_hdr = await render_to_sx("federation-social-header", nav=SxExpr(nav))
|
||||
hdr = await root_header_sx(ctx)
|
||||
child = await header_child_sx(social_hdr)
|
||||
header_rows = "(<> " + hdr + " " + child + ")"
|
||||
return full_page_sx(ctx, header_rows=header_rows, content=content,
|
||||
return await full_page_sx(ctx, header_rows=header_rows, content=content,
|
||||
meta_html=meta_html or f'<title>{escape(title)}</title>')
|
||||
|
||||
|
||||
@@ -99,24 +98,24 @@ def _social_page(ctx: dict, actor: Any, *, content: str,
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
async def render_federation_home(ctx: dict) -> str:
|
||||
hdr = root_header_sx(ctx)
|
||||
return full_page_sx(ctx, header_rows=hdr)
|
||||
hdr = await root_header_sx(ctx)
|
||||
return await full_page_sx(ctx, header_rows=hdr)
|
||||
|
||||
|
||||
async def render_login_page(ctx: dict) -> str:
|
||||
error = ctx.get("error", "")
|
||||
email = ctx.get("email", "")
|
||||
content = sx_call("account-login-content",
|
||||
content = await render_to_sx("account-login-content",
|
||||
error=error or None, email=str(escape(email)))
|
||||
return _social_page(ctx, None, content=content, title="Login \u2014 Rose Ash")
|
||||
return await _social_page(ctx, None, content=content, title="Login \u2014 Rose Ash")
|
||||
|
||||
|
||||
async def render_check_email_page(ctx: dict) -> str:
|
||||
email = ctx.get("email", "")
|
||||
email_error = ctx.get("email_error")
|
||||
content = sx_call("account-check-email-content",
|
||||
content = await render_to_sx("account-check-email-content",
|
||||
email=str(escape(email)), email_error=email_error)
|
||||
return _social_page(ctx, None, content=content,
|
||||
return await _social_page(ctx, None, content=content,
|
||||
title="Check your email \u2014 Rose Ash")
|
||||
|
||||
|
||||
@@ -124,6 +123,7 @@ async def render_choose_username_page(ctx: dict) -> str:
|
||||
from shared.browser.app.csrf import generate_csrf_token
|
||||
from quart import url_for
|
||||
from shared.config import config
|
||||
from shared.sx.parser import SxExpr
|
||||
|
||||
csrf = generate_csrf_token()
|
||||
error = ctx.get("error", "")
|
||||
@@ -132,15 +132,15 @@ async def render_choose_username_page(ctx: dict) -> str:
|
||||
check_url = url_for("identity.check_username")
|
||||
actor = ctx.get("actor")
|
||||
|
||||
error_sx = sx_call("auth-error-banner", error=error) if error else ""
|
||||
content = sx_call(
|
||||
error_sx = await render_to_sx("auth-error-banner", error=error) if error else ""
|
||||
content = await render_to_sx(
|
||||
"federation-choose-username",
|
||||
domain=str(escape(ap_domain)),
|
||||
error=SxExpr(error_sx) if error_sx else None,
|
||||
csrf=csrf, username=str(escape(username)),
|
||||
check_url=check_url,
|
||||
)
|
||||
return _social_page(ctx, actor, content=content,
|
||||
return await _social_page(ctx, actor, content=content,
|
||||
title="Choose Username \u2014 Rose Ash")
|
||||
|
||||
|
||||
@@ -164,10 +164,10 @@ async def render_timeline_items(items: list, timeline_type: str,
|
||||
else:
|
||||
next_url = url_for(f"social.{timeline_type}_timeline_page", before=before)
|
||||
|
||||
return sx_call("federation-timeline-items",
|
||||
items=SxExpr(serialize(item_dicts)),
|
||||
return await render_to_sx("federation-timeline-items",
|
||||
items=item_dicts,
|
||||
timeline_type=timeline_type,
|
||||
actor=SxExpr(serialize(actor_data)) if actor_data else None,
|
||||
actor=actor_data,
|
||||
next_url=next_url)
|
||||
|
||||
|
||||
@@ -178,14 +178,14 @@ async def render_search_results(actors: list, query: str, page: int,
|
||||
actor_data = _serialize_actor(actor)
|
||||
parts = []
|
||||
for ad in actor_dicts:
|
||||
parts.append(sx_call("federation-actor-card-from-data",
|
||||
a=SxExpr(serialize(ad)),
|
||||
actor=SxExpr(serialize(actor_data)) if actor_data else None,
|
||||
followed_urls=SxExpr(serialize(list(followed_urls))),
|
||||
parts.append(await render_to_sx("federation-actor-card-from-data",
|
||||
a=ad,
|
||||
actor=actor_data,
|
||||
followed_urls=list(followed_urls),
|
||||
list_type="search"))
|
||||
if len(actors) >= 20:
|
||||
next_url = url_for("social.search_page", q=query, page=page + 1)
|
||||
parts.append(sx_call("federation-scroll-sentinel", url=next_url))
|
||||
parts.append(await render_to_sx("federation-scroll-sentinel", url=next_url))
|
||||
return "(<> " + " ".join(parts) + ")" if parts else ""
|
||||
|
||||
|
||||
@@ -195,14 +195,14 @@ async def render_following_items(actors: list, page: int, actor: Any) -> str:
|
||||
actor_data = _serialize_actor(actor)
|
||||
parts = []
|
||||
for ad in actor_dicts:
|
||||
parts.append(sx_call("federation-actor-card-from-data",
|
||||
a=SxExpr(serialize(ad)),
|
||||
actor=SxExpr(serialize(actor_data)) if actor_data else None,
|
||||
followed_urls=SxExpr(serialize([])),
|
||||
parts.append(await render_to_sx("federation-actor-card-from-data",
|
||||
a=ad,
|
||||
actor=actor_data,
|
||||
followed_urls=[],
|
||||
list_type="following"))
|
||||
if len(actors) >= 20:
|
||||
next_url = url_for("social.following_list_page", page=page + 1)
|
||||
parts.append(sx_call("federation-scroll-sentinel", url=next_url))
|
||||
parts.append(await render_to_sx("federation-scroll-sentinel", url=next_url))
|
||||
return "(<> " + " ".join(parts) + ")" if parts else ""
|
||||
|
||||
|
||||
@@ -213,14 +213,14 @@ async def render_followers_items(actors: list, page: int,
|
||||
actor_data = _serialize_actor(actor)
|
||||
parts = []
|
||||
for ad in actor_dicts:
|
||||
parts.append(sx_call("federation-actor-card-from-data",
|
||||
a=SxExpr(serialize(ad)),
|
||||
actor=SxExpr(serialize(actor_data)) if actor_data else None,
|
||||
followed_urls=SxExpr(serialize(list(followed_urls))),
|
||||
parts.append(await render_to_sx("federation-actor-card-from-data",
|
||||
a=ad,
|
||||
actor=actor_data,
|
||||
followed_urls=list(followed_urls),
|
||||
list_type="followers"))
|
||||
if len(actors) >= 20:
|
||||
next_url = url_for("social.followers_list_page", page=page + 1)
|
||||
parts.append(sx_call("federation-scroll-sentinel", url=next_url))
|
||||
parts.append(await render_to_sx("federation-scroll-sentinel", url=next_url))
|
||||
return "(<> " + " ".join(parts) + ")" if parts else ""
|
||||
|
||||
|
||||
@@ -233,13 +233,14 @@ async def render_actor_timeline_items(items: list, actor_id: int,
|
||||
# Public API: POST handler fragment renderers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def render_interaction_buttons(object_id: str, author_inbox: str,
|
||||
async def render_interaction_buttons(object_id: str, author_inbox: str,
|
||||
like_count: int, boost_count: int,
|
||||
liked_by_me: bool, boosted_by_me: bool,
|
||||
actor: Any) -> str:
|
||||
"""Render interaction buttons fragment for POST response."""
|
||||
from shared.browser.app.csrf import generate_csrf_token
|
||||
from quart import url_for
|
||||
from shared.sx.parser import SxExpr
|
||||
|
||||
csrf = generate_csrf_token()
|
||||
safe_id = object_id.replace("/", "_").replace(":", "_")
|
||||
@@ -262,31 +263,31 @@ def render_interaction_buttons(object_id: str, author_inbox: str,
|
||||
boost_cls = "hover:text-green-600"
|
||||
|
||||
reply_url = url_for("social.defpage_compose_form", reply_to=object_id) if object_id else ""
|
||||
reply_sx = sx_call("federation-reply-link", url=reply_url) if reply_url else ""
|
||||
reply_sx = await render_to_sx("federation-reply-link", url=reply_url) if reply_url else ""
|
||||
|
||||
like_form = sx_call("federation-like-form",
|
||||
like_form = await render_to_sx("federation-like-form",
|
||||
action=like_action, target=target, oid=object_id, ainbox=author_inbox,
|
||||
csrf=csrf, cls=f"flex items-center gap-1 {like_cls}",
|
||||
icon=like_icon, count=str(like_count))
|
||||
|
||||
boost_form = sx_call("federation-boost-form",
|
||||
boost_form = await render_to_sx("federation-boost-form",
|
||||
action=boost_action, target=target, oid=object_id, ainbox=author_inbox,
|
||||
csrf=csrf, cls=f"flex items-center gap-1 {boost_cls}",
|
||||
count=str(boost_count))
|
||||
|
||||
return sx_call("federation-interaction-buttons",
|
||||
return await render_to_sx("federation-interaction-buttons",
|
||||
like=SxExpr(like_form),
|
||||
boost=SxExpr(boost_form),
|
||||
reply=SxExpr(reply_sx) if reply_sx else None)
|
||||
|
||||
|
||||
def render_actor_card(actor_dto: Any, actor: Any, followed_urls: set,
|
||||
async def render_actor_card(actor_dto: Any, actor: Any, followed_urls: set,
|
||||
*, list_type: str = "following") -> str:
|
||||
"""Render a single actor card fragment for POST response."""
|
||||
actor_data = _serialize_actor(actor)
|
||||
ad = _serialize_remote_actor(actor_dto)
|
||||
return sx_call("federation-actor-card-from-data",
|
||||
a=SxExpr(serialize(ad)),
|
||||
actor=SxExpr(serialize(actor_data)) if actor_data else None,
|
||||
followed_urls=SxExpr(serialize(list(followed_urls))),
|
||||
return await render_to_sx("federation-actor-card-from-data",
|
||||
a=ad,
|
||||
actor=actor_data,
|
||||
followed_urls=list(followed_urls),
|
||||
list_type=list_type)
|
||||
|
||||
@@ -26,33 +26,31 @@ def _register_federation_layouts() -> None:
|
||||
register_custom_layout("social", _social_full, _social_oob)
|
||||
|
||||
|
||||
def _social_full(ctx: dict, **kw: Any) -> str:
|
||||
from shared.sx.helpers import root_header_sx, header_child_sx, sx_call, SxExpr
|
||||
from shared.sx.parser import serialize
|
||||
async def _social_full(ctx: dict, **kw: Any) -> str:
|
||||
from shared.sx.helpers import root_header_sx, header_child_sx, render_to_sx
|
||||
from shared.sx.parser import SxExpr
|
||||
|
||||
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)
|
||||
child = header_child_sx(social_hdr)
|
||||
nav = await render_to_sx("federation-social-nav", actor=actor_data)
|
||||
social_hdr = await render_to_sx("federation-social-header", nav=SxExpr(nav))
|
||||
root_hdr = await root_header_sx(ctx)
|
||||
child = await 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 shared.sx.parser import serialize
|
||||
async def _social_oob(ctx: dict, **kw: Any) -> str:
|
||||
from shared.sx.helpers import root_header_sx, render_to_sx
|
||||
from shared.sx.parser import SxExpr
|
||||
|
||||
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))
|
||||
child_oob = sx_call("oob-header-sx",
|
||||
nav = await render_to_sx("federation-social-nav", actor=actor_data)
|
||||
social_hdr = await render_to_sx("federation-social-header", nav=SxExpr(nav))
|
||||
child_oob = await render_to_sx("oob-header-sx",
|
||||
parent_id="root-header-child",
|
||||
row=SxExpr(social_hdr))
|
||||
root_hdr_oob = root_header_sx(ctx, oob=True)
|
||||
root_hdr_oob = await root_header_sx(ctx, oob=True)
|
||||
return "(<> " + child_oob + " " + root_hdr_oob + ")"
|
||||
|
||||
|
||||
@@ -145,43 +143,40 @@ 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
|
||||
from shared.sx.helpers import render_to_sx
|
||||
actor = _require_actor()
|
||||
items = await services.federation.get_home_timeline(g.s, actor.id)
|
||||
return sx_call("federation-timeline-content",
|
||||
items=SxExpr(serialize([_serialize_timeline_item(i) for i in items])),
|
||||
return await render_to_sx("federation-timeline-content",
|
||||
items=[_serialize_timeline_item(i) for i in items],
|
||||
timeline_type="home",
|
||||
actor=SxExpr(serialize(_serialize_actor(actor))))
|
||||
actor=_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
|
||||
from shared.sx.helpers import render_to_sx
|
||||
actor = _get_actor()
|
||||
items = await services.federation.get_public_timeline(g.s)
|
||||
return sx_call("federation-timeline-content",
|
||||
items=SxExpr(serialize([_serialize_timeline_item(i) for i in items])),
|
||||
return await render_to_sx("federation-timeline-content",
|
||||
items=[_serialize_timeline_item(i) for i in items],
|
||||
timeline_type="public",
|
||||
actor=SxExpr(serialize(_serialize_actor(actor))) if actor else None)
|
||||
actor=_serialize_actor(actor))
|
||||
|
||||
|
||||
async def _h_compose_content(**kw):
|
||||
from quart import request
|
||||
from shared.sx.helpers import sx_call
|
||||
from shared.sx.helpers import render_to_sx
|
||||
_require_actor()
|
||||
reply_to = request.args.get("reply_to")
|
||||
return sx_call("federation-compose-content",
|
||||
return await render_to_sx("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
|
||||
from shared.sx.helpers import render_to_sx
|
||||
actor = _get_actor()
|
||||
query = request.args.get("q", "").strip()
|
||||
actors_list = []
|
||||
@@ -194,34 +189,32 @@ 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}
|
||||
return sx_call("federation-search-content",
|
||||
return await render_to_sx("federation-search-content",
|
||||
query=query,
|
||||
actors=SxExpr(serialize([_serialize_remote_actor(a) for a in actors_list])),
|
||||
actors=[_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)
|
||||
followed_urls=list(followed_urls),
|
||||
actor=_serialize_actor(actor))
|
||||
|
||||
|
||||
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
|
||||
from shared.sx.helpers import render_to_sx
|
||||
actor = _require_actor()
|
||||
actors_list, total = await services.federation.get_following(
|
||||
g.s, actor.preferred_username,
|
||||
)
|
||||
return sx_call("federation-following-content",
|
||||
actors=SxExpr(serialize([_serialize_remote_actor(a) for a in actors_list])),
|
||||
return await render_to_sx("federation-following-content",
|
||||
actors=[_serialize_remote_actor(a) for a in actors_list],
|
||||
total=total,
|
||||
actor=SxExpr(serialize(_serialize_actor(actor))))
|
||||
actor=_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
|
||||
from shared.sx.helpers import render_to_sx
|
||||
actor = _require_actor()
|
||||
actors_list, total = await services.federation.get_followers_paginated(
|
||||
g.s, actor.preferred_username,
|
||||
@@ -230,18 +223,17 @@ 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}
|
||||
return sx_call("federation-followers-content",
|
||||
actors=SxExpr(serialize([_serialize_remote_actor(a) for a in actors_list])),
|
||||
return await render_to_sx("federation-followers-content",
|
||||
actors=[_serialize_remote_actor(a) for a in actors_list],
|
||||
total=total,
|
||||
followed_urls=SxExpr(serialize(list(followed_urls))),
|
||||
actor=SxExpr(serialize(_serialize_actor(actor))))
|
||||
followed_urls=list(followed_urls),
|
||||
actor=_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
|
||||
from shared.sx.helpers import render_to_sx
|
||||
actor = _get_actor()
|
||||
actor_id = id
|
||||
from shared.models.federation import RemoteActor
|
||||
@@ -268,18 +260,17 @@ async def _h_actor_timeline_content(id=None, **kw):
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
is_following = existing is not None
|
||||
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])),
|
||||
return await render_to_sx("federation-actor-timeline-content",
|
||||
remote_actor=_serialize_remote_actor(remote_dto),
|
||||
items=[_serialize_timeline_item(i) for i in items],
|
||||
is_following=is_following,
|
||||
actor=SxExpr(serialize(_serialize_actor(actor))) if actor else None)
|
||||
actor=_serialize_actor(actor))
|
||||
|
||||
|
||||
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
|
||||
from shared.sx.helpers import render_to_sx
|
||||
actor = _require_actor()
|
||||
items = await services.federation.get_notifications(g.s, actor.id)
|
||||
await services.federation.mark_notifications_read(g.s, actor.id)
|
||||
@@ -298,5 +289,5 @@ async def _h_notifications_content(**kw):
|
||||
"read": getattr(n, "read", True),
|
||||
"app_domain": getattr(n, "app_domain", ""),
|
||||
})
|
||||
return sx_call("federation-notifications-content",
|
||||
notifications=SxExpr(serialize(notif_dicts)))
|
||||
return await render_to_sx("federation-notifications-content",
|
||||
notifications=notif_dicts)
|
||||
|
||||
Reference in New Issue
Block a user