Skip to content

Commit c57765e

Browse files
committed
oembed: ignore oembed results from mastodon
This adds a check in the oembed preview for cases where the oembed is going to contain less information than the OpenGraph; in these cases, the oembed is ignored and the OpenGraph data is retained. At the moment, the only case in here is Mastodon posts, but it could be expanded in the future. Mastodon's oembed is JavaScript based and only contains stub HTML that's intended to be filled in by client-side JavaScript that synapse doesn't execute. It doesn't contain the actual post text. The og:description, on the other hand, does contain the post text. When synapse unilaterally prefers the oembed over OpenGraph, it ends up accidentally filling in a generic and non-useful preview in place of the actual post content.
1 parent 09fd264 commit c57765e

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

changelog.d/19286.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improved previews for Mastodon posts.

synapse/media/oembed.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,18 @@ def _fetch_urls(tree: "etree._Element", tag_name: str) -> list[str]:
232232
return results
233233

234234

235+
def _should_reject_description(description: str) -> bool:
236+
"""
237+
Determines whether or not this description should be preferred over the
238+
og:description. Certain web apps with client-side JavaScript will serve
239+
stub documents via oembed that are intended to be filled in by client-
240+
side javascript that synapse won't execute; in these cases, the
241+
og:description will actually be more complete.
242+
"""
243+
244+
return description.startswith("Post by @") and "View on Mastodon" in description
245+
246+
235247
def calc_description_and_urls(open_graph_response: JsonDict, html_body: str) -> None:
236248
"""
237249
Calculate description for an HTML document.
@@ -273,5 +285,5 @@ def calc_description_and_urls(open_graph_response: JsonDict, html_body: str) ->
273285
open_graph_response["og:video"] = video_urls[0]
274286

275287
description = parse_html_description(tree)
276-
if description:
288+
if description and not _should_reject_description(description):
277289
open_graph_response["og:description"] = description

0 commit comments

Comments
 (0)