From 67c0411fd8cbd1e963f5b5a8ade5336dbf4681b2 Mon Sep 17 00:00:00 2001 From: Phil Date: Fri, 13 Nov 2026 15:32:14 -0700 Subject: [PATCH] feat: crossposted replies look like replies (v2.20.0) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Native replies on the same network: GoToSocial in_reply_to_id (resolved by our instance) with the author's @mention; Bluesky reply root/parent - Replies to our own notes (permalink or any crosspost URL) thread under that note's crosspost on every network, Threads included - Otherwise "↩ Replying to " and the link; on Bluesky the name is the link. compose_text keeps the line whole when shortening Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 10 + .../v2.20.0/2026-11-13-reply-crossposts.md | 78 +++++++ starpunk/__init__.py | 4 +- starpunk/syndication.py | 179 +++++++++++++-- tests/test_reply_crossposts.py | 216 ++++++++++++++++++ 5 files changed, 467 insertions(+), 20 deletions(-) create mode 100644 docs/design/v2.20.0/2026-11-13-reply-crossposts.md create mode 100644 tests/test_reply_crossposts.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 088b997..74596bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [2.20.0] - 2026-11-13 + +### Added + +- Crossposted replies look like replies. A reply to a post on the same network is posted as a native reply: on GoToSocial it threads under the fediverse post and starts with the author's @mention (so they're notified and their server gets it); on Bluesky it threads under the Bluesky post +- A reply to one of your own posts (its page here, or any of its crossposts) threads under that post's crosspost on every network, Threads included +- A reply that can't thread on a network (a fediverse post crossposted to Bluesky or Threads, a blog post anywhere) starts with "↩ Replying to " and the post's link. On Bluesky the author's name is the link, to save characters + +Crossposts made before this release are unchanged: edits can't move a post into a thread. + ## [2.19.0] - 2026-11-13 ### Added diff --git a/docs/design/v2.20.0/2026-11-13-reply-crossposts.md b/docs/design/v2.20.0/2026-11-13-reply-crossposts.md new file mode 100644 index 0000000..8cfec8d --- /dev/null +++ b/docs/design/v2.20.0/2026-11-13-reply-crossposts.md @@ -0,0 +1,78 @@ +# v2.20.0: Crossposted replies look like replies + +## Why + +Crossposting ignored `in_reply_to`. A reply to +`https://defcon.social/@deviantollam/117611186265622833` went to GoToSocial +and Bluesky as an ordinary post (text, permalink, hashtags), with nothing to +say it answered anyone. + +## Behaviour + +For each target, in order: + +| Replying to | GoToSocial | Bluesky | Threads | +|---|---|---|---| +| One of our notes (its page, or a crosspost of it on any network) | Native reply to that note's GoToSocial status | Native reply to its Bluesky post | Native reply (`reply_to_id`) to its Threads post | +| A fediverse post | Native reply (`in_reply_to_id`, resolved by our instance) starting with `@acct` | Reply line | Reply line | +| A Bluesky post | Reply line | Native reply (`reply.root`/`reply.parent`) | Reply line | +| Anything else | Reply line | Reply line | Reply line | + +The reply line is `↩ Replying to `, where *who* is the reply +context's author name, else its title, else the URL's host: + +- GoToSocial and Threads: the line, then the URL, then a blank line and the note. + On Threads the URL counts towards Meta's limit of five links, so the + body gets one link fewer. +- Bluesky: the line without the URL. *Who* is a link facet to the post, + which saves up to ~100 of the 300 characters. + +`compose_text(..., lead=...)` keeps the line whole and shortens the body +instead when the post is too long. + +### The @mention + +The user chose to start a native fediverse reply with the author's +mention, as Mastodon and GoToSocial clients do. Without it the author isn't +notified, and their server may never receive the reply, so it wouldn't show +in their thread. There's no mention when replying to our own account (the +search result's account id equals `verify_credentials`), or when the note +already contains the mention. + +### Our own posts + +`_own_note_id(url)` maps a URL to a note: a synced `syndications.remote_url` +on any target, or `/note/` and `/article/` on this site. +`_reply_fields` then takes that note's `remote_id` on the target being +posted to, as `Post.reply_remote_id`. GoToSocial skips the ids of +favourites and boosts (`favourite:…`, `reblog:…`). Bluesky only takes +`app.bsky.feed.post` URIs. Threads only takes numeric media ids. The +Threads API can't look up other people's posts by URL, so only our own +posts thread there. + +### Bluesky threads + +A reply's `root` must be the top of the thread. The parent is fetched with +`app.bsky.feed.getPosts`. If the parent is itself a reply, its +`record.reply.root` is used; otherwise the parent is the root. + +## Not changed + +- Crossposts made before this release. Edits can't move a status into a + thread (GoToSocial, Mastodon), and Bluesky and Threads posts can't be edited at + all. Re-threading would mean deleting and reposting, which loses likes and + boosts. The user chose to leave them. +- An edit of a reply on GoToSocial keeps the status where it is, and its + text keeps the mention (`_prepare` resolves the parent again). +- Fediverse authors bridged into Bluesky by Bridgy Fed have a copy of + their post there. There is no reliable way to find its URI from the + original URL, so these get the reply line. + +## Tests + +`tests/test_reply_crossposts.py`: native fediverse reply with mention, no +mention for our own account, reply line with name and host fallback, +Bluesky root/parent, Bluesky reply line with its link facet, Threads native +reply only to our own posts, the line surviving shortening, and a reply to +our own note threading on GoToSocial and Bluesky. The reply is addressed both +by the note's permalink and by its GoToSocial crosspost URL. diff --git a/starpunk/__init__.py b/starpunk/__init__.py index 0dab7f2..c4ffcc9 100644 --- a/starpunk/__init__.py +++ b/starpunk/__init__.py @@ -476,5 +476,5 @@ def create_app(config=None): # Package version (Semantic Versioning 2.0.0) # See docs/standards/versioning-strategy.md for details -__version__ = "2.19.0" -__version_info__ = (2, 19, 0) +__version__ = "2.20.0" +__version_info__ = (2, 20, 0) diff --git a/starpunk/syndication.py b/starpunk/syndication.py index 92646b8..4691931 100644 --- a/starpunk/syndication.py +++ b/starpunk/syndication.py @@ -25,6 +25,7 @@ import logging import os import re import threading +from urllib.parse import urlparse import time from dataclasses import dataclass, field from datetime import datetime, timezone @@ -102,10 +103,14 @@ class MastodonClient: "content_type": "text/markdown", } - def create_status(self, text: str, media_ids: List[str], idempotency_key: str) -> dict: + def create_status(self, text: str, media_ids: List[str], idempotency_key: str, + in_reply_to_id: Optional[str] = None) -> dict: + body = self._status_body(text, media_ids) + if in_reply_to_id: + body["in_reply_to_id"] = in_reply_to_id response = self._call( "POST", "/api/v1/statuses", - json=self._status_body(text, media_ids), + json=body, headers={"Idempotency-Key": idempotency_key}, ) data = response.json() @@ -143,14 +148,45 @@ class MastodonClient: status = self.resolve_status(url) return status["id"] if status else None + def _reply_parent(self, post: "Post") -> tuple: + """(status id, '@acct ' mention) a reply threads under; (None, '') if it can't (v2.20.0) + + Our own crosspost of the replied-to note, else the replied-to post as + this instance resolves it (any fediverse post). Replies to others + start with their @mention, as Mastodon and GoToSocial clients do, so + they are notified and their server gets the reply.""" + if not post.reply_to: + return None, "" + if post.reply_remote_id and post.reply_remote_id.split(":", 1)[0] not in self.NATIVE: + return post.reply_remote_id, "" + try: + status = self.resolve_status(post.reply_to) + except SyndicationError as e: + current_app.logger.info(f"Could not resolve {post.reply_to}: {e}") + return None, "" + if not status: + return None, "" + account = status.get("account") or {} + try: + mine = account.get("id") == self._account_id() + except SyndicationError: + mine = False + mention = "" if mine or not account.get("acct") else f"@{account['acct']} " + return status["id"], mention + def _prepare(self, post: "Post") -> tuple: + """(text, media ids, id of the status it replies to)""" if post.kind == "repost": - return compose_repost_text(post, post.note.content, post.max_chars), [] + return compose_repost_text(post, post.note.content, post.max_chars), [], None + parent, mention = self._reply_parent(post) # 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)) + body = strip_images(post.note.content) + if mention.strip() and mention.strip() not in body: + body = mention + body.lstrip() + text = compose_text(post.note, post.permalink, post.tags, post.max_chars, body=body, + lead="" if parent else reply_line(post)) media_ids = [self.upload_media(f["path"], f["mime_type"], f["description"]) for f in post.media] - return text, media_ids + return text, media_ids, parent def create(self, post: "Post", key: str, retrying: bool = False) -> dict: if post.kind in ("like", "repost") and post.target_url: @@ -165,13 +201,13 @@ class MastodonClient: return {"id": f"{action}:{status_id}", "url": None} if post.kind == "like": return {"id": None, "url": None} # nothing to like here - text, media_ids = self._prepare(post) - return self.create_status(text, media_ids, idempotency_key=key) + text, media_ids, parent = self._prepare(post) + return self.create_status(text, media_ids, idempotency_key=key, in_reply_to_id=parent) def update(self, remote_id: str, post: "Post") -> dict: if remote_id.split(":", 1)[0] in self.NATIVE: return {"id": remote_id, "url": None} # a favourite/boost has nothing to edit - text, media_ids = self._prepare(post) + text, media_ids, _ = self._prepare(post) # edits can't move a status in a thread return self.update_status(remote_id, text, media_ids) # --- Backfeed (v2.13.0, ADR-073) --- @@ -359,14 +395,44 @@ class BlueskyClient: current_app.logger.info(f"No link card image for {url}: {e}") return {"$type": "app.bsky.embed.external", "external": external} + def _reply_ref(self, post: "Post") -> Optional[dict]: + """{'root', 'parent'} for a reply to a Bluesky post (ours or anyone's); None otherwise (v2.20.0)""" + if not post.reply_to: + return None + uri = post.reply_remote_id if (post.reply_remote_id or "").startswith("at://") and \ + f"/{self.POST}/" in post.reply_remote_id else None + try: + if uri is None: + found = self.resolve_post(post.reply_to) + if not found: + return None + uri = found["uri"] + views = self._call("GET", "app.bsky.feed.getPosts", params={"uris": uri}).json().get("posts") or [] + except SyndicationError as e: + current_app.logger.info(f"Could not resolve {post.reply_to} on Bluesky: {e}") + return None + if not views: + return None + parent = {"uri": views[0]["uri"], "cid": views[0]["cid"]} + root = ((views[0].get("record") or {}).get("reply") or {}).get("root") or parent + return {"root": {"uri": root["uri"], "cid": root["cid"]}, "parent": parent} + def _record(self, post: "Post", text: Optional[str] = None, embed: Optional[dict] = None, - anchors: List[tuple] = ()) -> dict: + anchors: List[tuple] = (), reply: Optional[dict] = None) -> dict: if text is None: # An article is posted as a teaser (compose_text), so its links aren't in the text body, anchors = (None, []) if post.note.title else \ plain_text_links(post.note.content, links="anchor") - text = compose_text(post.note, post.permalink, post.tags, post.max_chars, body=body) + lead = "" + if post.reply_to and not reply: + # "↩ Replying to ", the name linking to the post (v2.20.0) + lead = reply_line(post, with_url=False) + anchors = [(reply_label(post), post.reply_to), *anchors] + text = compose_text(post.note, post.permalink, post.tags, post.max_chars, body=body, + lead=lead) record = {"$type": self.POST, "text": text, "createdAt": self._now()} + if reply: + record["reply"] = reply facets = bluesky_facets(text, anchors) if facets: record["facets"] = facets @@ -464,7 +530,7 @@ class BlueskyClient: existing = self._find_existing(post.permalink) if existing: return existing - record = self._record(post) + record = self._record(post, reply=self._reply_ref(post)) response = self._call("POST", "com.atproto.repo.createRecord", json={"repo": self.did or self.identifier, "collection": self.POST, "record": record}) @@ -843,15 +909,26 @@ class ThreadsClient: def _text(self, post: "Post") -> str: # Links written "text (url)"; the first one gets Threads' preview card. # Meta allows 5 different links per post and the permalink is one. - body = plain_text(post.note.content, links="inline", max_urls=THREADS_MAX_LINKS - 1) + # A reply to someone else's post says so, with its link (v2.20.0); + # one to our own Threads post is a native reply instead + lead = "" if self._reply_to_id(post) else reply_line(post) + links = THREADS_MAX_LINKS - 1 - (1 if lead else 0) + body = plain_text(post.note.content, links="inline", max_urls=links) limit = post.max_chars while True: - text = compose_text(post.note, post.permalink, [], limit, body=body) + text = compose_text(post.note, post.permalink, [], limit, body=body, lead=lead) over = threads_length(text) - post.max_chars if over <= 0: return text limit -= over + @staticmethod + def _reply_to_id(post: "Post") -> Optional[str]: + """Media id of our own Threads post this replies to. The API can't look + up other people's posts by URL, so only those thread natively.""" + remote = post.reply_remote_id or "" + return remote if post.reply_to and remote.isdigit() else None + def _image_container(self, uid: str, media: dict, **extra) -> str: params = {"media_type": "IMAGE", "image_url": media["url"], **extra} if media["description"]: @@ -874,6 +951,8 @@ class ThreadsClient: return self._call("POST", f"{uid}/threads", media_type="TEXT", text=self._repost_text(post))["id"] common = {"text": self._text(post)} + if self._reply_to_id(post): + common["reply_to_id"] = self._reply_to_id(post) topic = threads_topic(post.tags) if topic: common["topic_tag"] = topic @@ -1011,6 +1090,22 @@ class Post: kind: str = "note" # note, like or repost (v2.5.0) target_url: Optional[str] = None # what a like/repost points at target_title: Optional[str] = None # its fetched title, if any + reply_to: Optional[str] = None # what a reply answers (v2.20.0) + reply_name: Optional[str] = None # its author (or title), from the reply context + reply_remote_id: Optional[str] = None # its crosspost on this target, if it's one of ours + + +def reply_line(post: "Post", with_url: bool = True) -> str: + """'↩ Replying to ' (and the URL) for replies that can't thread natively (v2.20.0)""" + if not post.reply_to: + return "" + who = post.reply_name or urlparse(post.reply_to).hostname or post.reply_to + return f"↩ Replying to {who}" + (f"\n{post.reply_to}" if with_url else "") + + +def reply_label(post: "Post") -> str: + """The linked part of a Bluesky reply line""" + return post.reply_name or urlparse(post.reply_to).hostname or post.reply_to def compose_repost_text(post: "Post", comment: str, max_chars: int) -> str: @@ -1200,7 +1295,7 @@ def article_teaser(note) -> str: def compose_text(note, permalink: str, tags: List[str], max_chars: int, - body: Optional[str] = None) -> str: + body: Optional[str] = None, lead: str = "") -> 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 @@ -1221,9 +1316,14 @@ def compose_text(note, permalink: str, tags: List[str], max_chars: int, if getattr(note, "bookmark_of", None): # A bookmark leads with what it bookmarks (v2.9.0) body = f"🔖 {note.bookmark_of}\n\n{body}".strip() - if len(body) + len(tail) > max_chars: - room = max_chars - len(tail) - 1 - body = body[:room].rsplit(" ", 1)[0].rstrip() + "…" + # A reply that can't thread says what it replies to (v2.20.0); the lead + # is kept whole and the body shortened instead + lead = lead.strip() + head = f"{lead}\n\n" if lead and body else lead + if len(head) + len(body) + len(tail) > max_chars: + room = max_chars - len(tail) - len(head) - 1 + body = body[:max(room, 0)].rsplit(" ", 1)[0].rstrip() + "…" + body = head + body # A photo post may have no text: start with the link (v2.6.5) return (body + tail).lstrip() @@ -1510,6 +1610,48 @@ def _target_title(url: Optional[str]) -> Optional[str]: return " ".join(title.split())[:200] or None +def _own_note_id(url: str) -> Optional[int]: + """The note a URL is, if it's one of ours: its page on this site, or one + of its crossposts on any network (v2.20.0)""" + db = get_db(current_app) + row = db.execute("SELECT note_id FROM syndications WHERE remote_url = ? AND state = 'synced'", + (url,)).fetchone() + if row: + return row["note_id"] + site = urlparse(current_app.config.get("SITE_URL", "")) + parsed = urlparse(url) + parts = [p for p in parsed.path.split("/") if p] + if parsed.netloc != site.netloc or len(parts) != 2 or \ + parts[0] not in ("note", "notes", "article", "articles"): + return None + row = db.execute("SELECT id FROM notes WHERE slug = ? AND deleted_at IS NULL", + (parts[1],)).fetchone() + return row["id"] if row else None + + +def _reply_fields(url: Optional[str], target_id: str) -> dict: + """Post fields for a reply: who it answers, and its crosspost on this target if it's ours""" + if not url: + return {} + fields = {"reply_to": url} + try: + from starpunk.webmention_queries import get_reply_context + context = get_reply_context(url) or {} + if context.get("fetch_status") == "fetched": + name = context.get("author_name") or context.get("title") + fields["reply_name"] = " ".join(name.split())[:100] if name else None + note_id = _own_note_id(url) + if note_id is not None: + row = get_db(current_app).execute( + "SELECT remote_id FROM syndications WHERE note_id = ? AND target = ? " + "AND state = 'synced' AND remote_id IS NOT NULL", (note_id, target_id)).fetchone() + if row: + fields["reply_remote_id"] = row["remote_id"] + except Exception as e: + current_app.logger.info(f"Reply details for {url}: {e}") + return fields + + def _reconcile(row, target: Target) -> tuple: """Bring the remote post in line with the note. Returns (remote_id, remote_url).""" from starpunk.notes import get_note @@ -1538,6 +1680,7 @@ def _reconcile(row, target: Target) -> tuple: kind=note.kind, target_url=note.reacts_to, target_title=_target_title(note.reacts_to), + **(_reply_fields(note.in_reply_to, target.id) if note.kind == "note" else {}), ) if row["remote_id"]: diff --git a/tests/test_reply_crossposts.py b/tests/test_reply_crossposts.py new file mode 100644 index 0000000..06f1f1c --- /dev/null +++ b/tests/test_reply_crossposts.py @@ -0,0 +1,216 @@ +""" +Crossposted replies look like replies (v2.20.0) + +A reply to a post on the same network threads under it natively (with an +@mention on the fediverse); a reply to one of our own notes threads under +that note's crosspost on every network; anything else starts with +"↩ Replying to " and the link. +""" + +import json +from types import SimpleNamespace + +import httpx +import pytest + +from starpunk import create_app, syndication +from starpunk.notes import create_note, get_note +from starpunk.syndication import (BlueskyClient, MastodonClient, Post, ThreadsClient, + compose_text, get_note_syndications, process_due, + select_targets) + +SITE = "https://blog.example.com" +GTS = "https://social.example.com" +FEDI_POST = "https://mastodon.example/@alice/111" +OWN_FEDI_POST = "https://social.example.com/@phil/statuses/01OWN" +BSKY_POST = "https://bsky.app/profile/alice.example/post/3abc" +WEB_PAGE = "https://someblog.example/2026/a-post" + + +def note(content="Agreed, this cannot be good"): + return SimpleNamespace(content=content, title=None, bookmark_of=None, label=content[:30], + summary=None, excerpt=content) + + +def post(reply_to=None, **kwargs): + return Post(note=note(), permalink=f"{SITE}/note/1", tags=[], max_chars=500, + reply_to=reply_to, **kwargs) + + +class FakeGTS: + def __init__(self): + self.created = [] + + def __call__(self, request): + path = request.url.path + if path == "/api/v1/accounts/verify_credentials": + return httpx.Response(200, json={"id": "me"}) + if path == "/api/v2/search": + q = request.url.params["q"] + accounts = {FEDI_POST: {"id": "a2", "acct": "alice@mastodon.example"}, + OWN_FEDI_POST: {"id": "me", "acct": "phil"}} + if q in accounts: + return httpx.Response(200, json={"statuses": [{"id": "s1", "account": accounts[q]}]}) + return httpx.Response(200, json={"statuses": []}) + if path == "/api/v1/statuses" and request.method == "POST": + self.created.append(json.loads(request.content)) + sid = f"p{len(self.created)}" + return httpx.Response(200, json={"id": sid, "url": f"{GTS}/@phil/statuses/{sid}"}) + return httpx.Response(500) + + +class FakePDS: + def __init__(self): + self.records = [] + + def __call__(self, request): + nsid = request.url.path.removeprefix("/xrpc/") + if nsid == "com.atproto.server.createSession": + return httpx.Response(200, json={"did": "did:plc:me", "handle": "me.example", + "accessJwt": "a", "refreshJwt": "r"}) + if nsid == "com.atproto.identity.resolveHandle": + return httpx.Response(200, json={"did": "did:plc:alice"}) + if nsid == "app.bsky.feed.getPosts": + uri = request.url.params["uris"] + record = {"text": "parent"} + if uri.endswith("/3abc"): # itself a reply in a thread + record["reply"] = {"root": {"uri": "at://did:plc:alice/app.bsky.feed.post/root", + "cid": "rootcid"}, "parent": {}} + return httpx.Response(200, json={"posts": [{"uri": uri, "cid": "cid1", "record": record}]}) + if nsid == "com.atproto.repo.createRecord": + body = json.loads(request.content) + self.records.append(body["record"]) + uri = f"at://did:plc:me/{body['collection']}/r{len(self.records)}" + return httpx.Response(200, json={"uri": uri, "cid": "c"}) + return httpx.Response(500, json={"error": nsid}) + + +@pytest.fixture +def app(tmp_path): + data = tmp_path / "data" + app = create_app({ + "TESTING": True, "SITE_URL": SITE, "ADMIN_ME": "https://example.com", "SESSION_SECRET": "x", + "DATA_PATH": data, "NOTES_PATH": data / "notes", "DATABASE_PATH": data / "starpunk.db", + "GOTOSOCIAL_URL": GTS, "GOTOSOCIAL_ACCESS_TOKEN": "t", + "BLUESKY_HANDLE": "me.example", "BLUESKY_APP_PASSWORD": "p", "BLUESKY_SERVICE": "https://pds.example", + }) + yield app + syndication._client_cache.clear() + + +@pytest.fixture +def gts(): + fake = FakeGTS() + return fake, MastodonClient(GTS, "t", http=httpx.Client(transport=httpx.MockTransport(fake))) + + +@pytest.fixture +def pds(): + fake = FakePDS() + return fake, BlueskyClient("https://pds.example", "me.example", "p", + http=httpx.Client(transport=httpx.MockTransport(fake))) + + +# --- GoToSocial --------------------------------------------------------------- + +def test_fediverse_reply_threads_and_mentions(app, gts): + fake, client = gts + with app.app_context(): + client.create(post(FEDI_POST, reply_name="Alice"), key="k") + (body,) = fake.created + assert body["in_reply_to_id"] == "s1" + assert body["status"].startswith("@alice@mastodon.example Agreed") + assert "Replying to" not in body["status"] + + +def test_reply_to_own_fediverse_post_has_no_mention(app, gts): + fake, client = gts + with app.app_context(): + client.create(post(OWN_FEDI_POST), key="k") + assert fake.created[0]["in_reply_to_id"] == "s1" + assert fake.created[0]["status"].startswith("Agreed") + + +def test_reply_to_a_web_page_says_so_on_the_fediverse(app, gts): + fake, client = gts + with app.app_context(): + client.create(post(WEB_PAGE, reply_name="Ada"), key="k1") + client.create(post(WEB_PAGE), key="k2") + named, unnamed = fake.created + assert "in_reply_to_id" not in named + assert named["status"].startswith(f"↩ Replying to Ada\n{WEB_PAGE}\n\nAgreed") + assert unnamed["status"].startswith(f"↩ Replying to someblog.example\n{WEB_PAGE}") + + +# --- Bluesky ------------------------------------------------------------------ + +def test_bluesky_reply_threads_under_the_post_and_its_root(app, pds): + fake, client = pds + with app.app_context(): + client.create(post(BSKY_POST, reply_name="Alice"), key="k") + (record,) = fake.records + assert record["reply"]["parent"] == {"uri": "at://did:plc:alice/app.bsky.feed.post/3abc", "cid": "cid1"} + assert record["reply"]["root"] == {"uri": "at://did:plc:alice/app.bsky.feed.post/root", "cid": "rootcid"} + assert record["text"].startswith("Agreed") + + +def test_reply_to_a_fediverse_post_says_so_on_bluesky(app, pds): + fake, client = pds + with app.app_context(): + client.create(post(FEDI_POST, reply_name="Alice"), key="k") + (record,) = fake.records + assert "reply" not in record + assert record["text"].startswith("↩ Replying to Alice\n\nAgreed") + links = [f for f in record["facets"] if f["features"][0]["uri"] == FEDI_POST] + (link,) = links + start = record["text"].encode().index(b"Alice") + assert link["index"] == {"byteStart": start, "byteEnd": start + len(b"Alice")} + + +# --- Threads ------------------------------------------------------------------ + +def test_threads_replies_natively_only_to_our_own_posts(app): + client = ThreadsClient("tok") + calls = [] + client._call = lambda method, path, **params: calls.append(params) or {"id": "c1"} + with app.app_context(): + client._container("me", post(f"{SITE}/note/0", reply_remote_id="1789")) + client._container("me", post(FEDI_POST, reply_name="Alice")) + own, other = calls + assert own["reply_to_id"] == "1789" and not own["text"].startswith("↩") + assert "reply_to_id" not in other + assert other["text"].startswith(f"↩ Replying to Alice\n{FEDI_POST}\n\nAgreed") + + +# --- Composing ---------------------------------------------------------------- + +def test_reply_line_survives_shortening(): + text = compose_text(note("word " * 200), f"{SITE}/note/1", [], 300, + lead=f"↩ Replying to Alice\n{FEDI_POST}") + assert text.startswith(f"↩ Replying to Alice\n{FEDI_POST}\n\nword") + assert len(text) <= 300 and text.endswith(f"{SITE}/note/1") + + +# --- Replies to our own notes thread under their crossposts ---------------------- + +@pytest.mark.parametrize("via", ["permalink", "crosspost"]) +def test_reply_to_own_note_threads_on_every_network(app, gts, pds, monkeypatch, via): + monkeypatch.setattr("starpunk.reply_context.fetch_reply_context", lambda *a, **k: None) + (gts_fake, gts_client), (pds_fake, pds_client) = gts, pds + syndication._client_cache[GTS] = gts_client + syndication._client_cache["bluesky:https://pds.example:me.example"] = pds_client + + def crosspost(**kwargs): + with app.test_request_context(): + n = create_note("Plain words without links", published=True, **kwargs) + select_targets(n.id, ["gotosocial", "bluesky"]) + process_due(limit=10) + return get_note(id=n.id).url, {r["target"]: r for r in get_note_syndications(n.id)} + + first, rows = crosspost() + target = first if via == "permalink" else rows["gotosocial"]["remote_url"] + second, _ = crosspost(in_reply_to=target) + + assert gts_fake.created[-1]["in_reply_to_id"] == rows["gotosocial"]["remote_id"] + assert not gts_fake.created[-1]["status"].startswith(("@", "↩")) + assert pds_fake.records[-1]["reply"]["parent"]["uri"] == rows["bluesky"]["remote_id"]