Show link previews for posts that link to an article - #712
Merged
Merged
Conversation
Fediverse posts that point at a news article arrive as plain text plus a link; the preview card Mastodon renders is generated by each server and is not part of the ActivityPub object, so it never reaches us. Build it locally instead. A status/link/aside post that carries no media of its own gets its first link (skipping mentions, hashtags, rel=tag, links back to the poster's own host and direct file links) looked up in the background via cron. The Open Graph/Twitter Card metadata of that page is stored in post meta and rendered as a card below the post, in all five themes. New posts are queued when a feed is retrieved; posts that predate this are queued lazily the first time they are displayed and get their card on the next page load. Lookups that turn up nothing are remembered for a week so they aren't retried on every view. Can be turned off with the "Disable link previews for incoming posts" setting, for people who'd rather their site not contact the linked websites. Fixes #708 Claude-Session: https://claude.ai/code/session_01J7aozaEFaZ3vLaLXith1qm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #708
Mastodon renders the preview card itself, per server — it is not part of the ActivityPub object, so nothing about it reaches us. A shared news article therefore arrives as text plus a bare link. This builds the card locally instead.
Changes
Friends\Link_Previewclass.status,linkoraside(filter:friends_link_preview_post_formats), and only when the content carries no media of its own (<img>,<video>,<audio>,<iframe>,<embed>,<object>) — the same rule Mastodon uses, so an image post doesn't get a second image.<a>that isn't a mention, hashtag,rel="tag"/rel="author", a link back to the poster's own host, or a direct file link (filter:friends_link_preview_url, returnfalseto suppress).wp_safe_remote_get()in awp_schedule_single_event(), capped at 300 KB vialimit_response_size, 10s timeout, 3 redirects, HTML content types only. Extractsog:*withtwitter:*/<title>/<meta name="description">fallbacks; relative image URLs are resolved, non-http(s) image URLs dropped.link_previewpost meta (filter:friends_link_preview), pluslink_preview_checkedso a link that yields nothing isn't retried for a week.friends_retrieved_new_posts, so nothing is added to the latency of a feed refresh. Pre-existing posts are queued lazily on first display and show their card on the next page load.frontend/parts/link-previewtemplate part, rendered fromfrontend/parts/entry-content(so all four PHP themes pick it up) and appended to thefriends/post-contentblock for the block theme. Styled infriends.css,friends-blocks.css,mastodon.css,twitter.cssandgoogle-reader.css.friends_disable_link_previews). On by default; off means no card is shown and no outbound request is ever made.Testing instructions
/friends/— a card with the article's title, description and image appears under the status. Posts that already contain an image are unchanged.arstechnica.com,en.wikipedia.org,theguardian.com,example.com) and against fixtures covering hashtag/mention-only statuses, statuses that already show an image, links to the poster's own host, direct image links, relativeog:imagepaths,data:image URLs,>inside quoted attribute values, and pages with no metadata at all.main, so it's the fixture, not this branch.Changelog entry
Changelog Entry Details
Type
Message
Show a link preview card for posts that link to an article, with a setting to turn it off.
https://claude.ai/code/session_01J7aozaEFaZ3vLaLXith1qm
Test this PR in WordPress Playground
You can test this pull request directly in WordPress Playground:
Launch WordPress Playground
This will install and activate the plugin with the changes from this PR.