2016-01-24 18:47:27 -05:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
# Copyright 2020-2021 The Matrix.org Foundation C.I.C.
|
|
|
|
# Copyright 2016 OpenMarket Ltd
|
2023-11-21 15:29:58 -05:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2016-01-24 18:47:27 -05:00
|
|
|
#
|
|
|
|
#
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2023-10-06 07:22:55 -04:00
|
|
|
import re
|
2023-03-20 14:32:26 -04:00
|
|
|
from typing import TYPE_CHECKING
|
2021-09-07 09:10:34 -04:00
|
|
|
|
2023-10-06 07:22:55 -04:00
|
|
|
from synapse.http.server import respond_with_json_bytes
|
|
|
|
from synapse.http.servlet import RestServlet, parse_integer, parse_string
|
2021-03-12 11:37:57 -05:00
|
|
|
from synapse.http.site import SynapseRequest
|
2023-02-27 08:26:05 -05:00
|
|
|
from synapse.media.media_storage import MediaStorage
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2021-01-15 10:57:37 -05:00
|
|
|
if TYPE_CHECKING:
|
2023-02-27 08:26:05 -05:00
|
|
|
from synapse.media.media_repository import MediaRepository
|
2021-03-23 07:12:48 -04:00
|
|
|
from synapse.server import HomeServer
|
2021-01-15 10:57:37 -05:00
|
|
|
|
2021-09-07 09:10:34 -04:00
|
|
|
|
2023-10-06 07:22:55 -04:00
|
|
|
class PreviewUrlResource(RestServlet):
|
2021-09-07 09:10:34 -04:00
|
|
|
"""
|
2022-07-12 15:01:58 -04:00
|
|
|
The `GET /_matrix/media/r0/preview_url` endpoint provides a generic preview API
|
|
|
|
for URLs which outputs Open Graph (https://ogp.me/) responses (with some Matrix
|
|
|
|
specific additions).
|
|
|
|
|
|
|
|
This does have trade-offs compared to other designs:
|
|
|
|
|
|
|
|
* Pros:
|
|
|
|
* Simple and flexible; can be used by any clients at any point
|
|
|
|
* Cons:
|
|
|
|
* If each homeserver provides one of these independently, all the homeservers in a
|
|
|
|
room may needlessly DoS the target URI
|
|
|
|
* The URL metadata must be stored somewhere, rather than just using Matrix
|
|
|
|
itself to store the media.
|
|
|
|
* Matrix cannot be used to distribute the metadata between homeservers.
|
2021-09-07 09:10:34 -04:00
|
|
|
"""
|
|
|
|
|
2023-10-06 07:22:55 -04:00
|
|
|
PATTERNS = [re.compile("/_matrix/media/(r0|v3|v1)/preview_url$")]
|
2016-01-24 18:47:27 -05:00
|
|
|
|
2021-01-15 10:57:37 -05:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hs: "HomeServer",
|
|
|
|
media_repo: "MediaRepository",
|
|
|
|
media_storage: MediaStorage,
|
|
|
|
):
|
2019-06-29 03:06:55 -04:00
|
|
|
super().__init__()
|
2016-04-19 09:45:05 -04:00
|
|
|
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
2016-04-19 09:51:34 -04:00
|
|
|
self.media_repo = media_repo
|
2018-01-09 09:36:07 -05:00
|
|
|
self.media_storage = media_storage
|
2023-10-06 07:22:55 -04:00
|
|
|
assert self.media_repo.url_previewer is not None
|
|
|
|
self.url_previewer = self.media_repo.url_previewer
|
2016-04-19 09:45:05 -04:00
|
|
|
|
2023-10-06 07:22:55 -04:00
|
|
|
async def on_GET(self, request: SynapseRequest) -> None:
|
2016-04-11 05:39:16 -04:00
|
|
|
# XXX: if get_user_by_req fails, what should we do in an async render?
|
2019-06-29 03:06:55 -04:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2021-07-21 09:47:56 -04:00
|
|
|
url = parse_string(request, "url", required=True)
|
2024-04-16 15:12:36 -04:00
|
|
|
ts = parse_integer(request, "ts", default=self.clock.time_msec())
|
2023-10-06 07:22:55 -04:00
|
|
|
og = await self.url_previewer.preview(url, requester.user, ts)
|
2017-11-10 11:34:33 -05:00
|
|
|
respond_with_json_bytes(request, 200, og, send_cors=True)
|