Spaces:
Running
Running
Download backend/messages.py from Success-dev/receipter-pro: direct link, hf CLI and curl.
- Browser
- Download file 2.07 kB
-
https://huggingface.co/spaces/Success-dev/receipter-pro/resolve/main/backend/messages.py
- Command line
-
hf download hf://spaces/Success-dev/receipter-pro/backend/messages.py
-
curl -L -o messages.py https://huggingface.co/spaces/Success-dev/receipter-pro/resolve/main/backend/messages.py
2.07 kB
| """ | |
| The composer's canonical storage (docs/blueprint/adr/0003-html-source-of-truth-composer.md). | |
| HTML is the one stored representation; live preview is just the frontend rendering this | |
| field as-is — there's no backend computation involved in "preview," which is why there's no | |
| separate preview endpoint/function here. Markdown and the block builder (deferred past | |
| Phase 1 per the ADR) will compile down to this same field, one-way, when they're built. | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import uuid | |
| from datetime import datetime, timezone | |
| from backend import firestore_client as db | |
| from backend import orgs | |
| def _now() -> str: | |
| return datetime.now(timezone.utc).isoformat() | |
| def _message_path(org_id: str, message_id: str) -> str: | |
| return f"orgs/{org_id}/messages/{message_id}" | |
| # Firestore documents stop at 1 MiB; the sent HTML always fits, an editable design with | |
| # uploaded images inside it may not, and is then left out (the HTML alone still reopens). | |
| _MAX_SOURCE_BYTES = 600_000 | |
| def create_message(org_id: str, actor_uid: str, subject: str, html_body: str, | |
| mode: str | None = None, design: dict | None = None, automated: bool = False) -> str: | |
| """`mode`/`design` are what the composer was holding when it sent, kept so the email can be | |
| reopened or saved as a preset later exactly as it was built.""" | |
| if not automated: | |
| orgs.require_permission(org_id, actor_uid, "send") | |
| message_id = uuid.uuid4().hex | |
| doc = {"subject": subject, "html_body": html_body, "created_at": _now()} | |
| if mode in ("builder", "html"): | |
| doc["mode"] = mode | |
| if mode == "builder" and isinstance(design, dict) and len(json.dumps(design)) <= _MAX_SOURCE_BYTES: | |
| doc["design"] = design | |
| db.set_doc(_message_path(org_id, message_id), doc) | |
| return message_id | |
| def get_message(org_id: str, message_id: str) -> dict: | |
| doc = db.get_doc(_message_path(org_id, message_id)) | |
| if doc is None: | |
| raise orgs.NotFoundError(f"No message {message_id!r} in org {org_id!r}") | |
| return doc | |