From 39c8259f3f385b1ea86be262b7ac236342e2570e Mon Sep 17 00:00:00 2001 From: Phil Date: Mon, 2 Nov 2026 18:11:27 -0700 Subject: [PATCH] fix: Crossposts include photos embedded in the post text (v2.6.6) Some Micropub clients (OutPost) upload a photo to the media endpoint and then embed it in content instead of sending it as `photo`. Those posts have no attached media, so crossposts went out without the image. Images in the text that are in this site's media library are now sent like attached photos, with their alt text, and GoToSocial's text no longer carries the image Markdown. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 6 ++++ starpunk/__init__.py | 2 +- starpunk/syndication.py | 74 +++++++++++++++++++++++++++++++++++---- tests/test_syndication.py | 47 +++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 245832b..46e4005 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [2.6.6] - 2026-11-02 + +### Fixed + +- Crossposts left out photos that were embedded in the post text instead of attached. Some Micropub clients (OutPost, for one) upload a photo to the media endpoint and then put it in `content` as an image rather than sending it as `photo`; both are valid Micropub. Images in the text that are in this site's media library are now sent to GoToSocial, Bluesky and Threads like attached photos, with their alt text, after any attached ones and within each target's photo limit. GoToSocial's text no longer includes the image Markdown. Images hosted on other sites are still left out + ## [2.6.5] - 2026-11-01 ### Fixed diff --git a/starpunk/__init__.py b/starpunk/__init__.py index e18ac98..4376c78 100644 --- a/starpunk/__init__.py +++ b/starpunk/__init__.py @@ -464,5 +464,5 @@ def create_app(config=None): # Package version (Semantic Versioning 2.0.0) # See docs/standards/versioning-strategy.md for details -__version__ = "2.6.5" +__version__ = "2.6.6" __version_info__ = (2, 6, 4) diff --git a/starpunk/syndication.py b/starpunk/syndication.py index f45a59c..365f78b 100644 --- a/starpunk/syndication.py +++ b/starpunk/syndication.py @@ -138,7 +138,9 @@ class MastodonClient: def _prepare(self, post: "Post") -> tuple: if post.kind == "repost": return compose_repost_text(post, post.note.content, post.max_chars), [] - text = compose_text(post.note, post.permalink, post.tags, post.max_chars) + # Images in the text are sent as attachments instead (v2.6.6) + text = compose_text(post.note, post.permalink, post.tags, post.max_chars, + body=strip_images(post.note.content)) media_ids = [self.upload_media(f["path"], f["mime_type"], f["description"]) for f in post.media] return text, media_ids @@ -1135,25 +1137,84 @@ def bluesky_facets(text: str, anchors: List[tuple] = ()) -> List[dict]: return facets -def _media_files(note_id: int, limit: int) -> List[dict]: - """Image files for a note: the 'large' variant when there is one""" +_MD_IMAGE_RE = re.compile(r"!\[[^\]]*\]\([^)]*\)") +_HTML_IMAGE_RE = re.compile(r"]*>", re.IGNORECASE) + + +def inline_images(markdown_text: str) -> List[tuple]: + """(url, alt) for each image embedded in a note's text, in reading order""" + images = [] + for img in _note_soup(markdown_text).find_all("img"): + if img.get("src"): + images.append((img["src"], img.get("alt") or "")) + return images + + +def strip_images(markdown_text: str) -> str: + """A note's Markdown without its embedded images""" + text = _HTML_IMAGE_RE.sub("", _MD_IMAGE_RE.sub("", markdown_text)) + return re.sub(r"\n{3,}", "\n\n", text).strip() + + +def _own_media_path(url: str) -> Optional[str]: + """Path in the media library for one of this site's /media/ URLs""" + site = current_app.config["SITE_URL"].rstrip("/") + if url.startswith(site): + url = url[len(site):] + return url[len("/media/"):] if url.startswith("/media/") else None + + +def _media_files(note_id: int, limit: int, content: str = "") -> List[dict]: + """ + Image files for a note: the 'large' variant when there is one. + + Attached photos come first, then images embedded in the text that are in + this site's media library. Some Micropub clients (OutPost, for one) + upload a photo and put it in the content rather than in `photo` (v2.6.6). + """ from starpunk.media import get_note_media media_dir = Path(current_app.config["DATA_PATH"]) / "media" site = current_app.config["SITE_URL"].rstrip("/") + items = [(m, m.get("caption") or "") for m in get_note_media(note_id)] + seen = {m["path"] for m, _ in items} + for url, alt in inline_images(content) if content else []: + path = _own_media_path(url) + if not path or path in seen: + continue + media = _media_by_path(path) + if media: + seen.add(path) + items.append((media, alt or media.get("caption") or "")) + files = [] - for media in get_note_media(note_id)[:limit]: + for media, description in items[:limit]: variant = (media.get("variants") or {}).get("large") rel = variant["path"] if variant else media["path"] files.append({ "path": media_dir / rel, "url": f"{site}/media/{rel}", # for targets that fetch images themselves "mime_type": media.get("mime_type") or "image/jpeg", - "description": media.get("caption") or "", + "description": description, }) return files +def _media_by_path(path: str) -> Optional[dict]: + """A media row with its variants, found by its path in the library""" + db = get_db(current_app) + row = db.execute("SELECT id, path, mime_type FROM media WHERE path = ?", (path,)).fetchone() + if not row: + return None + variants = { + v["variant_type"]: {"path": v["path"]} + for v in db.execute( + "SELECT variant_type, path FROM media_variants WHERE media_id = ?", (row["id"],) + ) + } + return {"path": row["path"], "mime_type": row["mime_type"], "variants": variants, "caption": ""} + + # --- Worker ----------------------------------------------------------------- def _claim(db, row_id: int) -> bool: @@ -1242,7 +1303,8 @@ def _reconcile(row, target: Target) -> tuple: permalink=f"{site}/note/{note.slug}", tags=[t["display_name"] for t in get_note_tags(note.id, entered_order=True)], max_chars=target.max_chars, - media=_media_files(note.id, target.max_media) if note.kind == "note" else [], + media=_media_files(note.id, target.max_media, note.content) + if note.kind == "note" else [], kind=note.kind, target_url=note.reacts_to, target_title=_target_title(note.reacts_to), diff --git a/tests/test_syndication.py b/tests/test_syndication.py index 1aff5dc..2b4a709 100644 --- a/tests/test_syndication.py +++ b/tests/test_syndication.py @@ -32,6 +32,7 @@ class FakeServer: self.statuses = {} self.calls = [] self.media = 0 + self.uploads = [] # raw multipart bodies self.fail_next = None # status code to return once def __call__(self, request: httpx.Request) -> httpx.Response: @@ -43,6 +44,7 @@ class FakeServer: path = request.url.path if path == "/api/v2/media": self.media += 1 + self.uploads.append(request.read()) return httpx.Response(200, json={"id": f"m{self.media}"}) if path == "/api/v1/statuses" and request.method == "POST": body = json.loads(request.content) @@ -271,6 +273,51 @@ class TestMedia: assert server.statuses["1"]["media_ids"] == ["m1"] +class TestInlineImages: + """Clients like OutPost upload a photo and embed it in the content (v2.6.6)""" + + def _upload(self, app): + from starpunk.media import save_media + + buf = io.BytesIO() + Image.new("RGB", (800, 600), "orange").save(buf, format="JPEG") + with app.test_request_context(): + return save_media(buf.getvalue(), "cat.jpg") + + def test_embedded_own_image_is_attached(self, app, server): + media = self._upload(app) + url = f"https://blog.example.com/media/{media['path']}" + publish(app, content=f"![Tortie cat on a chair]({url})") + status = server.statuses["1"] + assert status["media_ids"] == ["m1"] + assert b"Tortie cat on a chair" in server.uploads[0] + assert "![" not in status["status"] and url not in status["status"] + assert status["status"].startswith("https://blog.example.com/note/") + + def test_text_around_image_is_kept(self, app, server): + media = self._upload(app) + publish(app, content=f"Look\n\n\"cat\"\n\nNice") + status = server.statuses["1"] + assert status["media_ids"] == ["m1"] + assert status["status"].startswith("Look\n\nNice") + + def test_image_both_attached_and_embedded_is_sent_once(self, app, server): + from starpunk.media import attach_media_to_note + + media = self._upload(app) + with app.test_request_context(): + note = create_note(content=f"![x](/media/{media['path']})", published=True) + attach_media_to_note(note.id, [media["id"]], ["caption"]) + select_targets(note.id, ["gotosocial"]) + process_due() + assert server.statuses["1"]["media_ids"] == ["m1"] + + def test_other_sites_images_are_ignored(self, app, server): + publish(app, content="![x](https://elsewhere.example/cat.jpg) hi") + assert server.statuses["1"]["media_ids"] == [] + assert server.uploads == [] + + class TestEntryPoints: @pytest.fixture def admin(self, app):