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

Refactor oEmbed previews #10814

Merged
merged 21 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/10814.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve oEmbed previews by processing the author name, photo, and video information.
16 changes: 11 additions & 5 deletions docs/development/url_previews.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,29 @@ When Synapse is asked to preview a URL it does the following:
3. Kicks off a background process to generate a preview:
1. Checks the database cache by URL and timestamp and returns the result if it
has not expired and was successful (a 2xx return code).
2. Checks if the URL matches an oEmbed pattern. If it does, fetch the oEmbed
response. If this is an image, replace the URL to fetch and continue. If
if it is HTML content, use the HTML as the document and continue.
2. Checks if the URL matches an oEmbed pattern. If it does, replace the URL
to fetch.
3. If it doesn't match an oEmbed pattern, downloads the URL and stores it
into a file via the media storage provider and saves the local media
metadata.
5. If the media is an image:
4. If the media is an image:
1. Generates thumbnails.
2. Generates an Open Graph response based on image properties.
6. If the media is HTML:
5. If the media is HTML:
1. Decodes the HTML via the stored file.
2. Generates an Open Graph response from the HTML.
3. If an image exists in the Open Graph response:
1. Downloads the URL and stores it into a file via the media storage
provider and saves the local media metadata.
2. Generates thumbnails.
3. Updates the Open Graph response based on image properties.
6. If the media is JSON and oEmbed was used:
1. Convert the oEmbed response to an Open Graph response.
2. If a thumbnail or image is in the oEmbed response:
1. Downloads the URL and stores it into a file via the media storage
provider and saves the local media metadata.
2. Generates thumbnails.
3. Updates the Open Graph response based on image properties.
7. Stores the result in the database cache.
4. Returns the result.

Expand Down
136 changes: 81 additions & 55 deletions synapse/rest/media/v1/oembed.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,26 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import urllib.parse
from typing import TYPE_CHECKING, Optional

import attr

from synapse.http.client import SimpleHttpClient
from synapse.types import JsonDict
from synapse.util import json_decoder

if TYPE_CHECKING:
from synapse.server import HomeServer

logger = logging.getLogger(__name__)


@attr.s(slots=True, auto_attribs=True)
@attr.s(slots=True, frozen=True, auto_attribs=True)
class OEmbedResult:
# Either HTML content or URL must be provided.
html: Optional[str]
url: Optional[str]
title: Optional[str]
og: JsonDict
clokep marked this conversation as resolved.
Show resolved Hide resolved
# Number of seconds to cache the content.
clokep marked this conversation as resolved.
Show resolved Hide resolved
cache_age: int


class OEmbedError(Exception):
"""An error occurred processing the oEmbed object."""
cache_age: Optional[int]
clokep marked this conversation as resolved.
Show resolved Hide resolved


class OEmbedProvider:
Expand Down Expand Up @@ -81,75 +77,105 @@ def get_oembed_url(self, url: str) -> Optional[str]:
"""
for url_pattern, endpoint in self._oembed_patterns.items():
if url_pattern.fullmatch(url):
return endpoint
# TODO Specify max height / width.

# Note that only the JSON format is supported, some endpoints want
# this in the URL, others want it as an argument.
endpoint = endpoint.replace("{format}", "json")

args = {"url": url, "format": "json"}
query_str = urllib.parse.urlencode(args, True)
return f"{endpoint}?{query_str}"

# No match.
return None

async def get_oembed_content(self, endpoint: str, url: str) -> OEmbedResult:
def parse_oembed_response(self, url: str, body: str) -> OEmbedResult:
"""
Request content from an oEmbed endpoint.
Parse the oEmbed response into an Open Graph response.

Args:
endpoint: The oEmbed API endpoint.
url: The URL to pass to the API.
url: The URL which is being previewed (not the one which was
requested).
body: The oEmbed response as JSON.

Returns:
An object representing the metadata returned.

Raises:
OEmbedError if fetching or parsing of the oEmbed information fails.
json-encoded Open Graph data
"""
try:
logger.debug("Trying to get oEmbed content for url '%s'", url)

# Note that only the JSON format is supported, some endpoints want
# this in the URL, others want it as an argument.
endpoint = endpoint.replace("{format}", "json")

result = await self._client.get_json(
endpoint,
# TODO Specify max height / width.
args={"url": url, "format": "json"},
)
try:
result = json_decoder.decode(body)

# Ensure there's a version of 1.0.
if result.get("version") != "1.0":
raise OEmbedError("Invalid version: %s" % (result.get("version"),))

oembed_type = result.get("type")
oembed_version = result["version"]
if oembed_version != "1.0":
raise RuntimeError(f"Invalid version: {oembed_version}")

# Ensure the cache age is None or an int.
cache_age = result.get("cache_age")
if cache_age:
cache_age = int(cache_age)

oembed_result = OEmbedResult(None, None, result.get("title"), cache_age)
# The results.
og = {"og:title": result.get("title")}

# HTML content.
# If a thumbnail exists, use it. Note that dimensions will be calculated later.
if "thumbnail_url" in result:
og["og:image"] = result["thumbnail_url"]

# Process each type separately.
oembed_type = result["type"]
if oembed_type == "rich":
oembed_result.html = result.get("html")
return oembed_result
calc_description_and_urls(og, result["html"])

if oembed_type == "photo":
oembed_result.url = result.get("url")
return oembed_result
elif oembed_type == "photo":
# If this is a photo, use the full image, not the thumbnail.
og["og:image"] = result["url"]

# TODO Handle link and video types.
else:
raise RuntimeError(f"Unknown oEmbed type: {oembed_type}")

if "thumbnail_url" in result:
oembed_result.url = result.get("thumbnail_url")
return oembed_result
except Exception as e:
# Trap any exception and let the code follow as usual.
logger.warning(f"Error parsing oEmbed metadata from {url}: {e:r}")
og = {}
cache_age = None

raise OEmbedError("Incompatible oEmbed information.")
return OEmbedResult(og, cache_age)

except OEmbedError as e:
# Trap OEmbedErrors first so we can directly re-raise them.
logger.warning("Error parsing oEmbed metadata from %s: %r", url, e)
raise

except Exception as e:
# Trap any exception and let the code follow as usual.
# FIXME: pass through 404s and other error messages nicely
logger.warning("Error downloading oEmbed metadata from %s: %r", url, e)
raise OEmbedError() from e
def calc_description_and_urls(og: JsonDict, body: str) -> None:
"""
Calculate description for an HTML document.

This uses lxml to convert the HTML document into plaintext. If errors
occur during processing of the document, an empty response is returned.

Args:
og: The current Open Graph summary. This is updated with additional fields.
body: The HTML document, as bytes.

Returns:
The summary
"""
# If there's no body, nothing useful is going to be found.
if not body:
return

from lxml import etree

# Create an HTML parser. If this fails, log and return no metadata.
parser = etree.HTMLParser(recover=True, encoding="utf-8")

# Attempt to parse the body. If this fails, log and return no metadata.
tree = etree.fromstring(body, parser)

# The data was successfully parsed, but no tree was found.
if tree is None:
return

from synapse.rest.media.v1.preview_url_resource import _calc_description

description = _calc_description(tree)
if description:
og["og:description"] = description
Loading