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 <noreply@anthropic.com>
This commit is contained in:
PhilandClaude Opus 5.5 committed 2026-11-02 18:11:27 -07:00
1 parent 1ce88b38a5
commit 39c8259f3f
4 files changed
+122 -7

No files matched your search

+1 -1
View File
@@ -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)
+68 -6
View File
@@ -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"<img\b[^>]*>", 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),