Skip to content

Commit 0395b71

Browse files
authored
Fix Mastodon URL previews not showing anything useful (#19231)
Fixes #18444. Inside of UrlPreviewer, we need to combine two dicts (one from oEmbed, and one from OpenGraph metadata in the HTML) and in Mastodon's case they were very different. Single Page Applications (SPAs) seem to sometimes provide better information in the OpenGraph tags than the oEmbed stubs, because the oEmbed stubs are filled in with JavaScript that Synapse does not execute. This change improves previews on Mastodon and YouTube (for the same reason). Tested to not regress previews of Twitter or GitHub.
1 parent 29fd011 commit 0395b71

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

changelog.d/19231.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug where Mastodon posts (and possibly other embeds) have the wrong description for URL previews.

synapse/media/url_previewer.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,16 @@ async def _do_preview(self, url: str, user: UserID, ts: int) -> bytes:
331331
# response failed or is incomplete.
332332
og_from_html = parse_html_to_open_graph(tree)
333333

334-
# Compile the Open Graph response by using the scraped
335-
# information from the HTML and overlaying any information
336-
# from the oEmbed response.
337-
og = {**og_from_html, **og_from_oembed}
334+
# Compile an Open Graph response by combining the oEmbed response
335+
# and the information from the HTML, with information in the HTML
336+
# preferred.
337+
#
338+
# The ordering here is intentional: certain websites (especially
339+
# SPA JavaScript-based ones) including Mastodon and YouTube provide
340+
# almost complete OpenGraph descriptions but only stubs for oEmbed,
341+
# with further oEmbed information being populated with JavaScript,
342+
# that Synapse won't execute.
343+
og = og_from_oembed | og_from_html
338344

339345
await self._precache_image_url(user, media_info, og)
340346
else:

0 commit comments

Comments
 (0)