Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 0661716

Browse files
Fetch images when previewing Twitter URLs. (#11985)
By including "bot" in the User-Agent, which some sites use to decide whether to include additional Open Graph information.
1 parent 79c18e0 commit 0661716

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

changelog.d/11985.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fetch images when previewing Twitter URLs. Contributed by @AndrewRyanChama.

synapse/res/providers.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
"endpoints": [
66
{
77
"schemes": [
8-
"https://twitter.com/*/status/*",
9-
"https://*.twitter.com/*/status/*",
108
"https://twitter.com/*/moments/*",
119
"https://*.twitter.com/*/moments/*"
1210
],
1311
"url": "https://publish.twitter.com/oembed"
1412
}
1513
]
1614
}
17-
]
15+
]

synapse/rest/media/v1/preview_url_resource.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,15 @@ async def _download_url(self, url: str, output_stream: BinaryIO) -> DownloadResu
402402
url,
403403
output_stream=output_stream,
404404
max_size=self.max_spider_size,
405-
headers={"Accept-Language": self.url_preview_accept_language},
405+
headers={
406+
b"Accept-Language": self.url_preview_accept_language,
407+
# Use a custom user agent for the preview because some sites will only return
408+
# Open Graph metadata to crawler user agents. Omit the Synapse version
409+
# string to avoid leaking information.
410+
b"User-Agent": [
411+
"Synapse (bot; +https://github.com/matrix-org/synapse)"
412+
],
413+
},
406414
is_allowed_content_type=_is_previewable,
407415
)
408416
except SynapseError:

0 commit comments

Comments
 (0)