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

Commit

Permalink
Move author_name out of the OpenGraph response from OEmbed
Browse files Browse the repository at this point in the history
  • Loading branch information
blastrock committed Jan 13, 2022
1 parent 73164af commit cb2375d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
6 changes: 3 additions & 3 deletions synapse/rest/media/v1/oembed.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
class OEmbedResult:
# The Open Graph result (converted from the oEmbed result).
open_graph_result: JsonDict
# The author_name of the OEmbed result
author_name: Optional[str]
# Number of milliseconds to cache the content, according to the oEmbed response.
#
# This will be None if no cache-age is provided in the oEmbed response (or
Expand Down Expand Up @@ -159,8 +161,6 @@ def parse_oembed_response(self, url: str, raw_body: bytes) -> OEmbedResult:
open_graph_response["og:title"] = title

author_name = oembed.get("author_name")
if author_name:
open_graph_response["og:author_name"] = author_name

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

return OEmbedResult(open_graph_response, cache_age)
return OEmbedResult(open_graph_response, author_name, cache_age)


def _fetch_urls(tree: "etree.Element", tag_name: str) -> List[str]:
Expand Down
19 changes: 12 additions & 7 deletions synapse/rest/media/v1/preview_url_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ async def _do_preview(self, url: str, user: UserID, ts: int) -> bytes:

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

if _is_media(media_info.media_type):
file_id = media_info.filesystem_id
Expand Down Expand Up @@ -297,7 +298,11 @@ async def _do_preview(self, url: str, user: UserID, ts: int) -> bytes:
og_from_oembed: JsonDict = {}
if oembed_url:
oembed_info = await self._download_url(oembed_url, user)
og_from_oembed, expiration_ms = await self._handle_oembed_response(
(
og_from_oembed,
author_name,
expiration_ms,
) = await self._handle_oembed_response(
url, oembed_info, expiration_ms
)

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

elif oembed_url:
# Handle the oEmbed information.
og, expiration_ms = await self._handle_oembed_response(
og, author_name, expiration_ms = await self._handle_oembed_response(
url, media_info, expiration_ms
)
await self._precache_image_url(user, media_info, og)
Expand All @@ -326,8 +331,8 @@ async def _do_preview(self, url: str, user: UserID, ts: int) -> bytes:

# If we don't have a title but we have author_name, copy it as
# title
if not og.get("og:title") and og.get("og:author_name"):
og["og:title"] = og["og:author_name"]
if not og.get("og:title") and author_name:
og["og:title"] = author_name

# filter out any stupidly long values
keys_to_remove = []
Expand Down Expand Up @@ -492,7 +497,7 @@ async def _precache_image_url(

async def _handle_oembed_response(
self, url: str, media_info: MediaInfo, expiration_ms: int
) -> Tuple[JsonDict, int]:
) -> Tuple[JsonDict, Optional[str], int]:
"""
Parse the downloaded oEmbed info.
Expand All @@ -509,7 +514,7 @@ async def _handle_oembed_response(
"""
# If JSON was not returned, there's nothing to do.
if not _is_json(media_info.media_type):
return {}, expiration_ms
return {}, None, expiration_ms

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

return open_graph_result, expiration_ms
return open_graph_result, oembed_response.author_name, expiration_ms

def _start_expire_url_cache_data(self) -> Deferred:
return run_as_background_process(
Expand Down
1 change: 0 additions & 1 deletion tests/rest/media/v1/test_url_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,6 @@ def test_oembed_rich(self):
"og:url": "http://twitter.com/matrixdotorg/status/12345",
"og:title": "Alice",
"og:description": "Content Preview",
"og:author_name": "Alice",
},
)

Expand Down

0 comments on commit cb2375d

Please sign in to comment.