From e92c27a84c3f3552649777ec7f7c5044589e584e Mon Sep 17 00:00:00 2001 From: Phil Date: Sat, 31 Oct 2026 14:19:17 -0600 Subject: [PATCH] feat: Crosspost notes to Bluesky (v2.2.0) BlueskyClient behind the ADR-065 reconcile loop: plain text with link and hashtag facets, images under 1 MB with alt text, link cards, app-password sessions with refresh. Deletes follow; edits don't (ADR-066). Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 11 + docs/decisions/ADR-066-bluesky-syndication.md | 37 ++ starpunk/__init__.py | 7 +- starpunk/config.py | 5 + starpunk/syndication.py | 338 ++++++++++++++++-- tests/test_syndication_bluesky.py | 274 ++++++++++++++ 6 files changed, 648 insertions(+), 24 deletions(-) create mode 100644 docs/decisions/ADR-066-bluesky-syndication.md create mode 100644 tests/test_syndication_bluesky.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 72d3235..993c71d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [2.2.0] - 2026-10-31 + +### Added + +- **Crossposting to Bluesky**, ADR-066 + - Plain-text note (Markdown stripped) with clickable link and hashtag facets, shortened to 300 characters with the permalink kept + - Images (up to 4, shrunk under Bluesky's 1 MB limit) with alt text; notes without images get a link card + - Unpublishing, unticking and deleting remove the Bluesky post; edits don't follow (Bluesky has no edits) + - Logs in with an app password: `BLUESKY_HANDLE`, `BLUESKY_APP_PASSWORD` (optional `BLUESKY_SERVICE`, default `https://bsky.social`) + - Shown in the editor and Micropub `syndicate-to` next to GoToSocial + ## [2.1.0] - 2026-10-31 ### Added diff --git a/docs/decisions/ADR-066-bluesky-syndication.md b/docs/decisions/ADR-066-bluesky-syndication.md new file mode 100644 index 0000000..5acc3eb --- /dev/null +++ b/docs/decisions/ADR-066-bluesky-syndication.md @@ -0,0 +1,37 @@ +# ADR-066: Crossposting to Bluesky + +## Status + +Accepted (v2.2.0, 2026-10-31) + +## Context + +ADR-065 added direct crossposting to GoToSocial. Bluesky was still fed by +Echofeed, with the same problems: no per-post control, no record of the +copy, deletes never follow. + +## Decision + +1. **Second client, same loop.** `BlueskyClient` sits behind the reconcile + loop and `syndications` table from ADR-065, as target id `bluesky`. + Clients share a small interface: `create`, `update`, `delete`, `can_edit`. +2. **Auth.** An app password (`BLUESKY_HANDLE`, `BLUESKY_APP_PASSWORD`). + `createSession` against `BLUESKY_SERVICE` (default `https://bsky.social`); + writes go to the PDS named in the returned DID document. Expired access + tokens are refreshed, falling back to a new session. +3. **Content.** Plain text (Markdown rendered and stripped), then the + permalink and hashtags (at most a third of the space), shortened to 300 + characters at a word boundary. Link and hashtag facets use UTF-8 byte + offsets. Images (up to 4) are re-encoded as JPEG if over 1 MB, with alt + text and aspect ratio; a note without images gets a link card. +4. **Edits don't follow.** Bluesky has no edit; rewriting the record would + break likes and replies, and delete-and-repost would lose them. The post + stays as first sent. Unpublish, untick and delete remove it. +5. **No double posts.** When a create is retried, recent posts are checked + for one already linking to the note (a send whose reply was lost). + +## Consequences + +- Echofeed is no longer needed; it must stop posting to Bluesky or posts + appear twice. +- A post deleted by hand on Bluesky is not re-created. diff --git a/starpunk/__init__.py b/starpunk/__init__.py index 7a8616b..9aa669e 100644 --- a/starpunk/__init__.py +++ b/starpunk/__init__.py @@ -337,9 +337,8 @@ def create_app(config=None): app.webmention_worker.stop() # Start syndication worker when a target is configured (v2.1.0) - if (app.config.get("GOTOSOCIAL_URL") and app.config.get("GOTOSOCIAL_ACCESS_TOKEN") - and not app.config.get("TESTING", False)): - from starpunk.syndication import SyndicationWorker + from starpunk.syndication import SyndicationWorker, targets_configured + if targets_configured(app.config) and not app.config.get("TESTING", False): app.syndication_worker = SyndicationWorker( app, poll_interval=app.config["SYNDICATION_POLL_INTERVAL"] ) @@ -450,5 +449,5 @@ def create_app(config=None): # Package version (Semantic Versioning 2.0.0) # See docs/standards/versioning-strategy.md for details -__version__ = "2.1.0" +__version__ = "2.2.0" __version_info__ = (2, 1, 0) diff --git a/starpunk/config.py b/starpunk/config.py index 45d7e7c..4132765 100644 --- a/starpunk/config.py +++ b/starpunk/config.py @@ -132,6 +132,11 @@ def load_config(app, config_override=None): app.config["GOTOSOCIAL_VISIBILITY"] = os.getenv("GOTOSOCIAL_VISIBILITY", "public") app.config["GOTOSOCIAL_MAX_CHARS"] = int(os.getenv("GOTOSOCIAL_MAX_CHARS", "5000")) app.config["GOTOSOCIAL_MAX_MEDIA"] = int(os.getenv("GOTOSOCIAL_MAX_MEDIA", "4")) + app.config["BLUESKY_HANDLE"] = os.getenv("BLUESKY_HANDLE", "") + app.config["BLUESKY_APP_PASSWORD"] = os.getenv("BLUESKY_APP_PASSWORD", "") + app.config["BLUESKY_SERVICE"] = os.getenv("BLUESKY_SERVICE", "https://bsky.social") + app.config["BLUESKY_NAME"] = os.getenv("BLUESKY_NAME", "Bluesky") + app.config["BLUESKY_MAX_CHARS"] = int(os.getenv("BLUESKY_MAX_CHARS", "300")) app.config["SYNDICATION_POLL_INTERVAL"] = int(os.getenv("SYNDICATION_POLL_INTERVAL", "10")) # Apply overrides if provided diff --git a/starpunk/syndication.py b/starpunk/syndication.py index 322108c..b30aeab 100644 --- a/starpunk/syndication.py +++ b/starpunk/syndication.py @@ -1,9 +1,10 @@ """ -Crossposting (POSSE) for StarPunk (v2.1.0, ADR-065) +Crossposting (POSSE) for StarPunk (v2.1.0 ADR-065, v2.2.0 ADR-066) Published notes are copied to syndication targets, and later edits and -deletes follow them. Currently one target kind exists: a GoToSocial (or any -Mastodon-API) account. +deletes follow them. Two target kinds exist: a GoToSocial (or any +Mastodon-API) account, and a Bluesky account. Bluesky posts can't be edited, +so there only deletes follow. Each (note, target) pair has a row in `syndications`. Changing a note marks its rows pending; a background worker then reconciles the remote post with @@ -16,9 +17,11 @@ the note: Rows carry a version so an edit made while a send is in flight is not lost. """ +import io import re import threading -from dataclasses import dataclass +from dataclasses import dataclass, field +from datetime import datetime, timezone from pathlib import Path from typing import Dict, List, Optional @@ -108,18 +111,233 @@ class MastodonClient: except RemoteGone: pass # already gone + # Interface used by the reconcile loop + can_edit = True + + def _prepare(self, post: "Post") -> tuple: + text = compose_text(post.note, post.permalink, post.tags, post.max_chars) + media_ids = [self.upload_media(f["path"], f["mime_type"], f["description"]) for f in post.media] + return text, media_ids + + def create(self, post: "Post", key: str, retrying: bool = False) -> dict: + text, media_ids = self._prepare(post) + return self.create_status(text, media_ids, idempotency_key=key) + + def update(self, remote_id: str, post: "Post") -> dict: + text, media_ids = self._prepare(post) + return self.update_status(remote_id, text, media_ids) + + def delete(self, remote_id: str) -> None: + self.delete_status(remote_id) + + +BLUESKY_BLOB_LIMIT = 1_000_000 # app.bsky.embed.images maxSize +_TID_CHARS = "234567abcdefghijklmnopqrstuvwxyz" + + +class BlueskyClient: + """ + Minimal AT Protocol client for posting to Bluesky with an app password + + Posts are plain text with link and hashtag facets. Images go in an + images embed; a note without images gets a link card instead. + """ + + can_edit = False # Bluesky has no edits; only deletes follow the note + POST = "app.bsky.feed.post" + + def __init__(self, service: str, identifier: str, password: str, + http: Optional[httpx.Client] = None): + self.service = service.rstrip("/") + self.identifier = identifier + self.password = password + self.http = http or httpx.Client(timeout=30.0) + self.pds = self.service + self.did: Optional[str] = None + self.handle = identifier + self._access: Optional[str] = None + self._refresh: Optional[str] = None + self._lock = threading.Lock() + + # --- XRPC plumbing --- + + def _raw(self, method: str, nsid: str, base: str, token: Optional[str], **kwargs) -> httpx.Response: + headers = kwargs.pop("headers", {}) + if token: + headers["Authorization"] = f"Bearer {token}" + try: + return self.http.request(method, f"{base}/xrpc/{nsid}", headers=headers, **kwargs) + except httpx.HTTPError as e: + raise SyndicationError(f"{nsid}: {e}") + + @staticmethod + def _error_name(response: httpx.Response) -> str: + try: + return response.json().get("error") or "" + except ValueError: + return "" + + def _raise_for(self, nsid: str, response: httpx.Response) -> None: + if response.status_code < 400: + return + permanent = 400 <= response.status_code < 500 and response.status_code != 429 + raise SyndicationError( + f"{nsid}: HTTP {response.status_code} {response.text[:200]}", permanent=permanent + ) + + def _login(self) -> None: + response = None + if self._refresh: + response = self._raw("POST", "com.atproto.server.refreshSession", self.service, self._refresh) + if response.status_code >= 400: + response = None + if response is None: + response = self._raw("POST", "com.atproto.server.createSession", self.service, None, + json={"identifier": self.identifier, "password": self.password}) + self._raise_for("com.atproto.server.createSession", response) + data = response.json() + self._access, self._refresh = data["accessJwt"], data["refreshJwt"] + self.did = data["did"] + self.handle = data.get("handle") or self.handle + for service in (data.get("didDoc") or {}).get("service", []): + if service.get("id", "").endswith("#atproto_pds") and service.get("serviceEndpoint"): + self.pds = service["serviceEndpoint"].rstrip("/") + + def _call(self, method: str, nsid: str, **kwargs) -> httpx.Response: + with self._lock: + if not self._access: + self._login() + response = self._raw(method, nsid, self.pds, self._access, **kwargs) + if response.status_code == 401 or self._error_name(response) == "ExpiredToken": + self._access = None + self._login() + response = self._raw(method, nsid, self.pds, self._access, **kwargs) + self._raise_for(nsid, response) + return response + + # --- Posts --- + + def upload_blob(self, data: bytes, mime_type: str) -> dict: + response = self._call("POST", "com.atproto.repo.uploadBlob", content=data, + headers={"Content-Type": mime_type}) + return response.json()["blob"] + + def _image(self, media: dict) -> dict: + data, mime_type, width, height = _fit_image(media["path"], media["mime_type"], BLUESKY_BLOB_LIMIT) + image = {"image": self.upload_blob(data, mime_type), "alt": media["description"] or ""} + if width and height: + image["aspectRatio"] = {"width": width, "height": height} + return image + + def _record(self, post: "Post") -> dict: + text = compose_text(post.note, post.permalink, post.tags, post.max_chars, + body=plain_text(post.note.content)) + record = { + "$type": self.POST, + "text": text, + "createdAt": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z", + } + facets = bluesky_facets(text) + if facets: + record["facets"] = facets + if post.media: + record["embed"] = {"$type": "app.bsky.embed.images", + "images": [self._image(m) for m in post.media]} + else: + record["embed"] = {"$type": "app.bsky.embed.external", "external": { + "uri": post.permalink, + "title": post.note.title[:300], + "description": plain_text(post.note.content)[:300], + }} + return record + + def _web_url(self, uri: str) -> str: + rkey = uri.rsplit("/", 1)[1] + return f"https://bsky.app/profile/{self.handle or self.did}/post/{rkey}" + + def _find_existing(self, permalink: str) -> Optional[dict]: + """A recent post that already links to this note (an earlier send whose reply was lost)""" + response = self._call("GET", "com.atproto.repo.listRecords", + params={"repo": self.did, "collection": self.POST, "limit": 25}) + for item in response.json().get("records", []): + if permalink in (item.get("value") or {}).get("text", ""): + return {"id": item["uri"], "url": self._web_url(item["uri"])} + return None + + def create(self, post: "Post", key: str, retrying: bool = False) -> dict: + if retrying: + if not self.did: + with self._lock: + self._login() + existing = self._find_existing(post.permalink) + if existing: + return existing + record = self._record(post) + response = self._call("POST", "com.atproto.repo.createRecord", + json={"repo": self.did or self.identifier, "collection": self.POST, + "record": record}) + uri = response.json()["uri"] + return {"id": uri, "url": self._web_url(uri)} + + def update(self, remote_id: str, post: "Post") -> dict: + raise NotImplementedError("Bluesky posts can't be edited") + + def delete(self, remote_id: str) -> None: + # at:///app.bsky.feed.post/; deleting a missing record succeeds + repo, collection, rkey = remote_id[len("at://"):].split("/", 2) + self._call("POST", "com.atproto.repo.deleteRecord", + json={"repo": repo, "collection": collection, "rkey": rkey}) + + +def _fit_image(path: Path, mime_type: str, limit: int) -> tuple: + """Image bytes no bigger than `limit`, re-encoded as JPEG if needed. Returns (data, mime, w, h).""" + from PIL import Image + + data = Path(path).read_bytes() + with Image.open(io.BytesIO(data)) as img: + width, height = img.size + if len(data) <= limit: + return data, mime_type, width, height + img = img.convert("RGB") + for max_side, quality in ((2000, 85), (2000, 75), (1600, 70), (1200, 65), (1000, 60)): + copy = img.copy() + copy.thumbnail((max_side, max_side)) + buf = io.BytesIO() + copy.save(buf, format="JPEG", quality=quality, optimize=True) + if buf.tell() <= limit: + return buf.getvalue(), "image/jpeg", copy.size[0], copy.size[1] + raise SyndicationError(f"{Path(path).name}: image too large for Bluesky", permanent=True) + @dataclass class Target: id: str # stored in syndications.target uid: str # Micropub syndicate-to uid name: str # shown in the editor and Micropub clients - client: MastodonClient + client: object # MastodonClient or BlueskyClient max_chars: int = 5000 max_media: int = 4 -_client_cache: Dict[str, MastodonClient] = {} +@dataclass +class Post: + """What a client needs to publish a note""" + note: object + permalink: str + tags: List[str] + max_chars: int + media: List[dict] = field(default_factory=list) + + +_client_cache: Dict[str, object] = {} + + +def targets_configured(config) -> bool: + """Whether any crossposting target has credentials (used to start the worker)""" + return bool( + (config.get("GOTOSOCIAL_URL") and config.get("GOTOSOCIAL_ACCESS_TOKEN")) + or (config.get("BLUESKY_HANDLE") and config.get("BLUESKY_APP_PASSWORD")) + ) def get_targets() -> Dict[str, Target]: @@ -142,6 +360,24 @@ def get_targets() -> Dict[str, Target]: max_chars=int(config.get("GOTOSOCIAL_MAX_CHARS", 5000)), max_media=int(config.get("GOTOSOCIAL_MAX_MEDIA", 4)), ) + + handle = (config.get("BLUESKY_HANDLE") or "").lstrip("@") + password = config.get("BLUESKY_APP_PASSWORD") or "" + if handle and password: + service = (config.get("BLUESKY_SERVICE") or "https://bsky.social").rstrip("/") + key = f"bluesky:{service}:{handle}" + client = _client_cache.get(key) + if client is None: + client = BlueskyClient(service, handle, password) + _client_cache[key] = client + targets["bluesky"] = Target( + id="bluesky", + uid=f"https://bsky.app/profile/{handle}", + name=config.get("BLUESKY_NAME", "Bluesky"), + client=client, + max_chars=int(config.get("BLUESKY_MAX_CHARS", 300)), + max_media=4, + ) return targets @@ -242,17 +478,75 @@ def _hashtag(display_name: str) -> str: return f"#{word}" if word else "" -def compose_text(note, permalink: str, tags: List[str], max_chars: int) -> str: - """Full note as Markdown, then the permalink and hashtags; shortened if too long""" - hashtags = " ".join(h for h in (_hashtag(t) for t in tags) if h) +def compose_text(note, permalink: str, tags: List[str], max_chars: int, + body: Optional[str] = None) -> str: + """ + Full note (Markdown unless `body` is given), then the permalink and + hashtags; shortened at a word boundary if too long. On short targets the + hashtags may use at most a third of the space. + """ + hashtags = "" + for h in (_hashtag(t) for t in tags): + if h and len(hashtags) + len(h) + 1 <= max(max_chars // 3, 1): + hashtags = f"{hashtags} {h}".strip() tail = f"\n\n{permalink}" + (f"\n\n{hashtags}" if hashtags else "") - body = note.content.strip() + body = (note.content if body is None else body).strip() if len(body) + len(tail) > max_chars: room = max_chars - len(tail) - 1 body = body[:room].rsplit(" ", 1)[0].rstrip() + "…" return body + tail +_BLOCKS = ["p", "h1", "h2", "h3", "h4", "h5", "h6", "pre", "blockquote", "ul", "ol", "hr", "table"] + + +def plain_text(markdown_text: str) -> str: + """Markdown as readable plain text (for targets without Markdown)""" + import markdown + from bs4 import BeautifulSoup + + soup = BeautifulSoup(markdown.markdown(markdown_text, extensions=["fenced_code"]), "html.parser") + for img in soup.find_all("img"): + img.decompose() + for node in soup.find_all(string=True): + if not node.strip() and "\n" in node and node.parent.name != "pre": + node.extract() # layout newlines between tags; we add our own + for br in soup.find_all("br"): + br.replace_with("\n") + for li in soup.find_all("li"): + li.insert(0, "• ") + li.append("\n") + for block in soup.find_all(_BLOCKS): + block.append("\n\n") + text = soup.get_text() + text = re.sub(r"[ \t]+\n", "\n", text) + return re.sub(r"\n{3,}", "\n\n", text).strip() + + +_URL_RE = re.compile(r"https?://[^\s<>\"]+") +_TAG_RE = re.compile(r"(? List[dict]: + """Link and hashtag facets (UTF-8 byte ranges) for a Bluesky post""" + def span(start: int, end: int) -> dict: + return {"byteStart": len(text[:start].encode()), "byteEnd": len(text[:end].encode())} + + facets = [] + for m in _URL_RE.finditer(text): + url = m.group(0).rstrip(".,;:!?)\"'…") + facets.append({"index": span(m.start(), m.start() + len(url)), + "features": [{"$type": "app.bsky.richtext.facet#link", "uri": url}]}) + urls = [(f["index"]["byteStart"], f["index"]["byteEnd"]) for f in facets] + for m in _TAG_RE.finditer(text): + index = span(m.start(), m.end()) + if any(a <= index["byteStart"] < b for a, b in urls): + continue # a #fragment inside a URL + facets.append({"index": index, + "features": [{"$type": "app.bsky.richtext.facet#tag", "tag": m.group(1)}]}) + return facets + + def _media_files(note_id: int, limit: int) -> List[dict]: """Image files for a note: the 'large' variant when there is one""" from starpunk.media import get_note_media @@ -331,26 +625,30 @@ def _reconcile(row, target: Target) -> tuple: if not wanted: if row["remote_id"]: - target.client.delete_status(row["remote_id"]) + target.client.delete(row["remote_id"]) return None, None + if row["remote_id"] and not target.client.can_edit: + return row["remote_id"], row["remote_url"] # posted already; edits don't follow + site = current_app.config["SITE_URL"].rstrip("/") - tags = [t["display_name"] for t in get_note_tags(note.id)] - text = compose_text(note, f"{site}/note/{note.slug}", tags, target.max_chars) - media_ids = [ - target.client.upload_media(f["path"], f["mime_type"], f["description"]) - for f in _media_files(note.id, target.max_media) - ] + post = Post( + note=note, + permalink=f"{site}/note/{note.slug}", + tags=[t["display_name"] for t in get_note_tags(note.id)], + max_chars=target.max_chars, + media=_media_files(note.id, target.max_media), + ) if row["remote_id"]: try: - result = target.client.update_status(row["remote_id"], text, media_ids) + result = target.client.update(row["remote_id"], post) return result["id"], result["url"] except RemoteGone: pass # deleted on the remote side; post it again - result = target.client.create_status( - text, media_ids, idempotency_key=f"starpunk-{row['id']}-{row['version']}" + result = target.client.create( + post, key=f"starpunk-{row['id']}-{row['version']}", retrying=row["attempts"] > 0 ) return result["id"], result["url"] diff --git a/tests/test_syndication_bluesky.py b/tests/test_syndication_bluesky.py new file mode 100644 index 0000000..ead2a1e --- /dev/null +++ b/tests/test_syndication_bluesky.py @@ -0,0 +1,274 @@ +""" +Tests for crossposting to Bluesky (starpunk/syndication.py, v2.2.0) + +A fake PDS (httpx.MockTransport) records what StarPunk sends. +""" + +import io +import json + +import httpx +import pytest +from PIL import Image + +from starpunk import create_app, syndication +from starpunk.notes import create_note, delete_note, update_note +from starpunk.syndication import ( + BlueskyClient, + bluesky_facets, + get_note_syndications, + plain_text, + process_due, + select_targets, +) + +SERVICE = "https://bsky.example" +PDS = "https://pds.example" +DID = "did:plc:abc123" + + +class FakePDS: + def __init__(self): + self.records = {} + self.blobs = [] + self.calls = [] + self.logins = 0 + self.expire_next = False + self.lose_next_reply = False + self.fail_next = None + + def _json(self, code, body): + return httpx.Response(code, json=body) + + def __call__(self, request: httpx.Request) -> httpx.Response: + nsid = request.url.path.removeprefix("/xrpc/") + self.calls.append((request.url.host, nsid)) + if nsid == "com.atproto.server.createSession": + body = json.loads(request.content) + if body["password"] != "app-pass": + return self._json(401, {"error": "AuthenticationRequired"}) + self.logins += 1 + return self._json(200, { + "did": DID, "handle": "phil.example", "accessJwt": f"access{self.logins}", + "refreshJwt": "refresh", + "didDoc": {"service": [{"id": "#atproto_pds", "serviceEndpoint": PDS}]}, + }) + if nsid == "com.atproto.server.refreshSession": + self.logins += 1 + return self._json(200, {"did": DID, "handle": "phil.example", + "accessJwt": f"access{self.logins}", "refreshJwt": "refresh"}) + assert request.url.host == "pds.example" + if request.headers.get("Authorization") != f"Bearer access{self.logins}" or self.expire_next: + self.expire_next = False + return self._json(400, {"error": "ExpiredToken"}) + if self.fail_next: + code, self.fail_next = self.fail_next, None + return self._json(code, {"error": "Nope"}) + if nsid == "com.atproto.repo.uploadBlob": + self.blobs.append(len(request.content)) + assert len(request.content) <= 1_000_000 + return self._json(200, {"blob": {"$type": "blob", "ref": {"$link": f"b{len(self.blobs)}"}, + "mimeType": request.headers["Content-Type"], + "size": len(request.content)}}) + if nsid == "com.atproto.repo.createRecord": + body = json.loads(request.content) + uri = f"at://{DID}/app.bsky.feed.post/r{len(self.records) + 1}" + self.records[uri] = body["record"] + if self.lose_next_reply: + self.lose_next_reply = False + raise httpx.ReadTimeout("lost reply") + return self._json(200, {"uri": uri, "cid": "c"}) + if nsid == "com.atproto.repo.listRecords": + return self._json(200, {"records": [{"uri": u, "value": v} for u, v in self.records.items()]}) + if nsid == "com.atproto.repo.deleteRecord": + body = json.loads(request.content) + self.records.pop(f"at://{body['repo']}/{body['collection']}/{body['rkey']}", None) + return self._json(200, {}) + return self._json(500, {"error": "unknown"}) + + +@pytest.fixture +def pds(): + return FakePDS() + + +@pytest.fixture +def app(tmp_path, pds): + data = tmp_path / "data" + app = create_app({ + "TESTING": True, + "SITE_URL": "https://blog.example.com", + "ADMIN_ME": "https://example.com", + "SESSION_SECRET": "test-secret", + "DATA_PATH": data, + "NOTES_PATH": data / "notes", + "DATABASE_PATH": data / "starpunk.db", + "BLUESKY_HANDLE": "phil.example", + "BLUESKY_APP_PASSWORD": "app-pass", + "BLUESKY_SERVICE": SERVICE, + }) + syndication._client_cache[f"bluesky:{SERVICE}:phil.example"] = BlueskyClient( + SERVICE, "phil.example", "app-pass", http=httpx.Client(transport=httpx.MockTransport(pds)) + ) + yield app + syndication._client_cache.clear() + + +def publish(app, content="Hello Bluesky", tags=None): + with app.test_request_context(): + note = create_note(content=content, published=True, tags=tags) + select_targets(note.id, ["bluesky"]) + process_due() + return note + + +def only_record(pds): + (record,) = pds.records.values() + return record + + +def row(app, note): + with app.app_context(): + (r,) = get_note_syndications(note.id) + return r + + +class TestText: + def test_markdown_becomes_plain_text(self): + md = "# Title\n\nSome *emphasis* and a [link](https://x.example).\n\n- one\n- two\n\n![pic](/m.jpg)" + assert plain_text(md) == "Title\n\nSome emphasis and a link.\n\n• one\n• two" + + def test_facets_use_utf8_byte_offsets(self): + text = "Café ☕ https://blog.example.com/note/x #IndieWeb" + link, tag = bluesky_facets(text) + raw = text.encode() + assert raw[link["index"]["byteStart"]:link["index"]["byteEnd"]] == b"https://blog.example.com/note/x" + assert raw[tag["index"]["byteStart"]:tag["index"]["byteEnd"]] == b"#IndieWeb" + assert tag["features"][0]["tag"] == "IndieWeb" + + def test_fragment_in_url_is_not_a_tag(self): + facets = bluesky_facets("see https://x.example/page#section and #42 too") + assert [f["features"][0]["$type"] for f in facets] == ["app.bsky.richtext.facet#link"] + + +class TestLifecycle: + def test_publish_posts_with_link_card(self, app, pds): + note = publish(app, content="Hello **Bluesky**", tags=["IndieWeb"]) + record = only_record(pds) + permalink = f"https://blog.example.com/note/{note.slug}" + assert record["text"] == f"Hello Bluesky\n\n{permalink}\n\n#IndieWeb" + assert record["embed"]["external"]["uri"] == permalink + kinds = {f["features"][0]["$type"] for f in record["facets"]} + assert kinds == {"app.bsky.richtext.facet#link", "app.bsky.richtext.facet#tag"} + r = row(app, note) + assert r["state"] == "synced" + assert r["remote_id"] == f"at://{DID}/app.bsky.feed.post/r1" + assert r["remote_url"] == "https://bsky.app/profile/phil.example/post/r1" + + def test_long_note_fits_300(self, app, pds): + note = publish(app, content="word " * 500) + text = only_record(pds)["text"] + assert len(text) <= 300 + assert text.endswith(f"https://blog.example.com/note/{note.slug}") + + def test_edit_leaves_post_alone(self, app, pds): + note = publish(app, content="first") + with app.test_request_context(): + update_note(id=note.id, content="second") + process_due() + assert only_record(pds)["text"].startswith("first") + assert row(app, note)["state"] == "synced" + + def test_delete_and_unpublish_remove_post(self, app, pds): + note = publish(app) + with app.test_request_context(): + update_note(id=note.id, published=False) + process_due() + assert pds.records == {} + update_note(id=note.id, published=True) + process_due() + assert len(pds.records) == 1 + delete_note(id=note.id) + process_due() + assert pds.records == {} + + +class TestSession: + def test_logs_in_once_and_uses_pds(self, app, pds): + publish(app, content="one") + publish(app, content="two") + assert pds.logins == 1 + assert ("bsky.example", "com.atproto.server.createSession") in pds.calls + assert ("pds.example", "com.atproto.repo.createRecord") in pds.calls + + def test_expired_token_is_refreshed(self, app, pds): + publish(app, content="one") + pds.expire_next = True + publish(app, content="two") + assert len(pds.records) == 2 + assert ("bsky.example", "com.atproto.server.refreshSession") in pds.calls + + def test_bad_password_fails_permanently(self, app, pds): + syndication._client_cache[f"bluesky:{SERVICE}:phil.example"].password = "wrong" + note = publish(app) + r = row(app, note) + assert r["state"] == "failed" and "401" in r["last_error"] + + def test_lost_reply_does_not_double_post(self, app, pds): + pds.lose_next_reply = True + note = publish(app) + assert row(app, note)["state"] == "pending" + with app.test_request_context(): + from starpunk.syndication import retry + retry(row(app, note)["id"]) + # retry() resets attempts; simulate the scheduled retry instead + from starpunk.database import get_db + get_db(app).execute("UPDATE syndications SET attempts = 1") + get_db(app).commit() + process_due() + assert len(pds.records) == 1 + assert row(app, note)["state"] == "synced" + + def test_server_error_retries(self, app, pds): + publish(app, content="warm up") # log in first + pds.fail_next = 502 + note = publish(app, content="later") + assert row(app, note)["state"] == "pending" + + +class TestMedia: + def test_large_image_is_shrunk_with_alt_and_aspect_ratio(self, app, pds): + from starpunk.media import attach_media_to_note, save_media + import os + + img = Image.frombytes("RGB", (2400, 1800), os.urandom(2400 * 1800 * 3)) + buf = io.BytesIO() + img.save(buf, format="PNG") + assert buf.tell() > 1_000_000 + with app.test_request_context(): + note = create_note(content="noisy", published=True) + media = save_media(buf.getvalue(), "noise.png") + attach_media_to_note(note.id, [media["id"]], ["Static"]) + select_targets(note.id, ["bluesky"]) + process_due() + record = only_record(pds) + (image,) = record["embed"]["images"] + assert record["embed"]["$type"] == "app.bsky.embed.images" + assert image["alt"] == "Static" + assert image["aspectRatio"]["width"] > 0 + + +def test_both_targets_listed_for_micropub(tmp_path): + data = tmp_path / "d" + app = create_app({ + "TESTING": True, "SITE_URL": "https://b.example", "ADMIN_ME": "https://e.example", + "SESSION_SECRET": "x", "DATA_PATH": data, "NOTES_PATH": data / "notes", + "DATABASE_PATH": data / "s.db", + "GOTOSOCIAL_URL": "https://social.example", "GOTOSOCIAL_ACCESS_TOKEN": "t", + "BLUESKY_HANDLE": "phil.example", "BLUESKY_APP_PASSWORD": "p", + }) + with app.app_context(): + from starpunk.syndication import get_targets, resolve_targets + assert set(get_targets()) == {"gotosocial", "bluesky"} + assert resolve_targets(["https://bsky.app/profile/phil.example"]) == ["bluesky"] + syndication._client_cache.clear()