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

Commit cb2375d

Browse files
committed
Move author_name out of the OpenGraph response from OEmbed
1 parent 73164af commit cb2375d

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

synapse/rest/media/v1/oembed.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
class OEmbedResult:
3434
# The Open Graph result (converted from the oEmbed result).
3535
open_graph_result: JsonDict
36+
# The author_name of the OEmbed result
37+
author_name: Optional[str]
3638
# Number of milliseconds to cache the content, according to the oEmbed response.
3739
#
3840
# This will be None if no cache-age is provided in the oEmbed response (or
@@ -159,8 +161,6 @@ def parse_oembed_response(self, url: str, raw_body: bytes) -> OEmbedResult:
159161
open_graph_response["og:title"] = title
160162

161163
author_name = oembed.get("author_name")
162-
if author_name:
163-
open_graph_response["og:author_name"] = author_name
164164

165165
# Use the provider name and as the site.
166166
provider_name = oembed.get("provider_name")
@@ -198,7 +198,7 @@ def parse_oembed_response(self, url: str, raw_body: bytes) -> OEmbedResult:
198198
open_graph_response = {}
199199
cache_age = None
200200

201-
return OEmbedResult(open_graph_response, cache_age)
201+
return OEmbedResult(open_graph_response, author_name, cache_age)
202202

203203

204204
def _fetch_urls(tree: "etree.Element", tag_name: str) -> List[str]:

synapse/rest/media/v1/preview_url_resource.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ async def _do_preview(self, url: str, user: UserID, ts: int) -> bytes:
262262

263263
# The number of milliseconds that the response should be considered valid.
264264
expiration_ms = media_info.expires
265+
author_name: Optional[str] = None
265266

266267
if _is_media(media_info.media_type):
267268
file_id = media_info.filesystem_id
@@ -297,7 +298,11 @@ async def _do_preview(self, url: str, user: UserID, ts: int) -> bytes:
297298
og_from_oembed: JsonDict = {}
298299
if oembed_url:
299300
oembed_info = await self._download_url(oembed_url, user)
300-
og_from_oembed, expiration_ms = await self._handle_oembed_response(
301+
(
302+
og_from_oembed,
303+
author_name,
304+
expiration_ms,
305+
) = await self._handle_oembed_response(
301306
url, oembed_info, expiration_ms
302307
)
303308

@@ -315,7 +320,7 @@ async def _do_preview(self, url: str, user: UserID, ts: int) -> bytes:
315320

316321
elif oembed_url:
317322
# Handle the oEmbed information.
318-
og, expiration_ms = await self._handle_oembed_response(
323+
og, author_name, expiration_ms = await self._handle_oembed_response(
319324
url, media_info, expiration_ms
320325
)
321326
await self._precache_image_url(user, media_info, og)
@@ -326,8 +331,8 @@ async def _do_preview(self, url: str, user: UserID, ts: int) -> bytes:
326331

327332
# If we don't have a title but we have author_name, copy it as
328333
# title
329-
if not og.get("og:title") and og.get("og:author_name"):
330-
og["og:title"] = og["og:author_name"]
334+
if not og.get("og:title") and author_name:
335+
og["og:title"] = author_name
331336

332337
# filter out any stupidly long values
333338
keys_to_remove = []
@@ -492,7 +497,7 @@ async def _precache_image_url(
492497

493498
async def _handle_oembed_response(
494499
self, url: str, media_info: MediaInfo, expiration_ms: int
495-
) -> Tuple[JsonDict, int]:
500+
) -> Tuple[JsonDict, Optional[str], int]:
496501
"""
497502
Parse the downloaded oEmbed info.
498503
@@ -509,7 +514,7 @@ async def _handle_oembed_response(
509514
"""
510515
# If JSON was not returned, there's nothing to do.
511516
if not _is_json(media_info.media_type):
512-
return {}, expiration_ms
517+
return {}, None, expiration_ms
513518

514519
with open(media_info.filename, "rb") as file:
515520
body = file.read()
@@ -521,7 +526,7 @@ async def _handle_oembed_response(
521526
if open_graph_result and oembed_response.cache_age is not None:
522527
expiration_ms = oembed_response.cache_age
523528

524-
return open_graph_result, expiration_ms
529+
return open_graph_result, oembed_response.author_name, expiration_ms
525530

526531
def _start_expire_url_cache_data(self) -> Deferred:
527532
return run_as_background_process(

tests/rest/media/v1/test_url_preview.py

-1
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,6 @@ def test_oembed_rich(self):
674674
"og:url": "http://twitter.com/matrixdotorg/status/12345",
675675
"og:title": "Alice",
676676
"og:description": "Content Preview",
677-
"og:author_name": "Alice",
678677
},
679678
)
680679

0 commit comments

Comments
 (0)