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

Commit 1381563

Browse files
authored
Inline URL preview documentation. (#13261)
Inline URL preview documentation near the implementation.
1 parent a366b75 commit 1381563

File tree

7 files changed

+62
-74
lines changed

7 files changed

+62
-74
lines changed

changelog.d/13233.doc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
Add a link to configuration instructions in the URL preview documentation.
2-
1+
Move the documentation for how URL previews work to the URL preview module.

changelog.d/13261.doc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Move the documentation for how URL previews work to the URL preview module.

docs/SUMMARY.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
- [Application Services](application_services.md)
3636
- [Server Notices](server_notices.md)
3737
- [Consent Tracking](consent_tracking.md)
38-
- [URL Previews](development/url_previews.md)
3938
- [User Directory](user_directory.md)
4039
- [Message Retention Policies](message_retention_policies.md)
4140
- [Pluggable Modules](modules/index.md)

docs/admin_api/user_admin_api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ Gets a list of all local media that a specific `user_id` has created.
544544
These are media that the user has uploaded themselves
545545
([local media](../media_repository.md#local-media)), as well as
546546
[URL preview images](../media_repository.md#url-previews) requested by the user if the
547-
[feature is enabled](../development/url_previews.md).
547+
[feature is enabled](../usage/configuration/config_documentation.md#url_preview_enabled).
548548

549549
By default, the response is ordered by descending creation date and ascending media ID.
550550
The newest media is on top. You can change the order with parameters

docs/development/url_previews.md

Lines changed: 0 additions & 62 deletions
This file was deleted.

docs/media_repository.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ The media repository
77
users.
88
* caches avatars, attachments and their thumbnails for media uploaded by remote
99
users.
10-
* caches resources and thumbnails used for
11-
[URL previews](development/url_previews.md).
10+
* caches resources and thumbnails used for URL previews.
1211

1312
All media in Matrix can be identified by a unique
1413
[MXC URI](https://spec.matrix.org/latest/client-server-api/#matrix-content-mxc-uris),
@@ -59,8 +58,6 @@ remote_thumbnail/matrix.org/aa/bb/cccccccccccccccccccc/128-96-image-jpeg
5958
Note that `remote_thumbnail/` does not have an `s`.
6059

6160
## URL Previews
62-
See [URL Previews](development/url_previews.md) for documentation on the URL preview
63-
process.
6461

6562
When generating previews for URLs, Synapse may download and cache various
6663
resources, including images. These resources are assigned temporary media IDs

synapse/rest/media/v1/preview_url_resource.py

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,64 @@ class MediaInfo:
109109

110110
class PreviewUrlResource(DirectServeJsonResource):
111111
"""
112-
Generating URL previews is a complicated task which many potential pitfalls.
113-
114-
See docs/development/url_previews.md for discussion of the design and
115-
algorithm followed in this module.
112+
The `GET /_matrix/media/r0/preview_url` endpoint provides a generic preview API
113+
for URLs which outputs Open Graph (https://ogp.me/) responses (with some Matrix
114+
specific additions).
115+
116+
This does have trade-offs compared to other designs:
117+
118+
* Pros:
119+
* Simple and flexible; can be used by any clients at any point
120+
* Cons:
121+
* If each homeserver provides one of these independently, all the homeservers in a
122+
room may needlessly DoS the target URI
123+
* The URL metadata must be stored somewhere, rather than just using Matrix
124+
itself to store the media.
125+
* Matrix cannot be used to distribute the metadata between homeservers.
126+
127+
When Synapse is asked to preview a URL it does the following:
128+
129+
1. Checks against a URL blacklist (defined as `url_preview_url_blacklist` in the
130+
config).
131+
2. Checks the URL against an in-memory cache and returns the result if it exists. (This
132+
is also used to de-duplicate processing of multiple in-flight requests at once.)
133+
3. Kicks off a background process to generate a preview:
134+
1. Checks URL and timestamp against the database cache and returns the result if it
135+
has not expired and was successful (a 2xx return code).
136+
2. Checks if the URL matches an oEmbed (https://oembed.com/) pattern. If it
137+
does, update the URL to download.
138+
3. Downloads the URL and stores it into a file via the media storage provider
139+
and saves the local media metadata.
140+
4. If the media is an image:
141+
1. Generates thumbnails.
142+
2. Generates an Open Graph response based on image properties.
143+
5. If the media is HTML:
144+
1. Decodes the HTML via the stored file.
145+
2. Generates an Open Graph response from the HTML.
146+
3. If a JSON oEmbed URL was found in the HTML via autodiscovery:
147+
1. Downloads the URL and stores it into a file via the media storage provider
148+
and saves the local media metadata.
149+
2. Convert the oEmbed response to an Open Graph response.
150+
3. Override any Open Graph data from the HTML with data from oEmbed.
151+
4. If an image exists in the Open Graph response:
152+
1. Downloads the URL and stores it into a file via the media storage
153+
provider and saves the local media metadata.
154+
2. Generates thumbnails.
155+
3. Updates the Open Graph response based on image properties.
156+
6. If the media is JSON and an oEmbed URL was found:
157+
1. Convert the oEmbed response to an Open Graph response.
158+
2. If a thumbnail or image is in the oEmbed response:
159+
1. Downloads the URL and stores it into a file via the media storage
160+
provider and saves the local media metadata.
161+
2. Generates thumbnails.
162+
3. Updates the Open Graph response based on image properties.
163+
7. Stores the result in the database cache.
164+
4. Returns the result.
165+
166+
The in-memory cache expires after 1 hour.
167+
168+
Expired entries in the database cache (and their associated media files) are
169+
deleted every 10 seconds. The default expiration time is 1 hour from download.
116170
"""
117171

118172
isLeaf = True

0 commit comments

Comments
 (0)