2019-08-13 05:49:28 -06:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
|
|
|
# Copyright 2018-2019 New Vector Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import logging
|
2021-11-29 15:19:45 -07:00
|
|
|
from http import HTTPStatus
|
2023-02-09 11:04:24 -07:00
|
|
|
from typing import TYPE_CHECKING, Optional, Tuple
|
2021-01-15 09:18:09 -07:00
|
|
|
|
2023-02-01 14:35:24 -07:00
|
|
|
from synapse.api.constants import Direction
|
2021-12-08 09:59:40 -07:00
|
|
|
from synapse.api.errors import Codes, NotFoundError, SynapseError
|
2021-06-07 08:12:34 -06:00
|
|
|
from synapse.http.server import HttpServer
|
2023-02-01 14:35:24 -07:00
|
|
|
from synapse.http.servlet import (
|
|
|
|
RestServlet,
|
|
|
|
parse_boolean,
|
|
|
|
parse_enum,
|
|
|
|
parse_integer,
|
|
|
|
parse_string,
|
|
|
|
)
|
2021-03-12 09:37:57 -07:00
|
|
|
from synapse.http.site import SynapseRequest
|
2019-08-13 05:49:28 -06:00
|
|
|
from synapse.rest.admin._base import (
|
2020-10-26 11:02:28 -06:00
|
|
|
admin_patterns,
|
2019-08-13 05:49:28 -06:00
|
|
|
assert_requester_is_admin,
|
|
|
|
assert_user_is_admin,
|
|
|
|
)
|
2021-08-18 06:25:12 -06:00
|
|
|
from synapse.storage.databases.main.media_repository import MediaSortOrder
|
|
|
|
from synapse.types import JsonDict, UserID
|
2021-01-15 09:18:09 -07:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2021-03-23 05:12:48 -06:00
|
|
|
from synapse.server import HomeServer
|
2019-08-13 05:49:28 -06:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class QuarantineMediaInRoom(RestServlet):
|
|
|
|
"""Quarantines all media in a room so that no one can download it via
|
|
|
|
this server.
|
|
|
|
"""
|
|
|
|
|
2021-06-07 08:12:34 -06:00
|
|
|
PATTERNS = [
|
2021-12-08 09:59:40 -07:00
|
|
|
*admin_patterns("/room/(?P<room_id>[^/]*)/media/quarantine$"),
|
2020-01-13 11:10:43 -07:00
|
|
|
# This path kept around for legacy reasons
|
2021-12-08 09:59:40 -07:00
|
|
|
*admin_patterns("/quarantine_media/(?P<room_id>[^/]*)$"),
|
2021-06-07 08:12:34 -06:00
|
|
|
]
|
2019-08-13 05:49:28 -06:00
|
|
|
|
2021-01-15 09:18:09 -07:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 04:04:02 -07:00
|
|
|
self.store = hs.get_datastores().main
|
2019-08-13 05:49:28 -06:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
2021-03-12 09:37:57 -07:00
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, room_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-10-11 05:17:59 -06:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2022-08-22 07:17:59 -06:00
|
|
|
await assert_user_is_admin(self.auth, requester)
|
2019-08-13 05:49:28 -06:00
|
|
|
|
2020-01-13 11:10:43 -07:00
|
|
|
logging.info("Quarantining room: %s", room_id)
|
|
|
|
|
|
|
|
# Quarantine all media in this room
|
2019-10-11 05:17:59 -06:00
|
|
|
num_quarantined = await self.store.quarantine_media_ids_in_room(
|
2019-08-13 05:49:28 -06:00
|
|
|
room_id, requester.user.to_string()
|
|
|
|
)
|
|
|
|
|
2021-11-29 15:19:45 -07:00
|
|
|
return HTTPStatus.OK, {"num_quarantined": num_quarantined}
|
2019-08-13 05:49:28 -06:00
|
|
|
|
|
|
|
|
2020-01-13 11:10:43 -07:00
|
|
|
class QuarantineMediaByUser(RestServlet):
|
|
|
|
"""Quarantines all local media by a given user so that no one can download it via
|
|
|
|
this server.
|
|
|
|
"""
|
|
|
|
|
2021-12-08 09:59:40 -07:00
|
|
|
PATTERNS = admin_patterns("/user/(?P<user_id>[^/]*)/media/quarantine$")
|
2020-01-13 11:10:43 -07:00
|
|
|
|
2021-01-15 09:18:09 -07:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 04:04:02 -07:00
|
|
|
self.store = hs.get_datastores().main
|
2020-01-13 11:10:43 -07:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
2021-03-12 09:37:57 -07:00
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, user_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2020-01-13 11:10:43 -07:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2022-08-22 07:17:59 -06:00
|
|
|
await assert_user_is_admin(self.auth, requester)
|
2020-01-13 11:10:43 -07:00
|
|
|
|
2022-06-07 04:53:47 -06:00
|
|
|
logging.info("Quarantining media by user: %s", user_id)
|
2020-01-13 11:10:43 -07:00
|
|
|
|
|
|
|
# Quarantine all media this user has uploaded
|
|
|
|
num_quarantined = await self.store.quarantine_media_ids_by_user(
|
|
|
|
user_id, requester.user.to_string()
|
|
|
|
)
|
|
|
|
|
2021-11-29 15:19:45 -07:00
|
|
|
return HTTPStatus.OK, {"num_quarantined": num_quarantined}
|
2020-01-13 11:10:43 -07:00
|
|
|
|
|
|
|
|
|
|
|
class QuarantineMediaByID(RestServlet):
|
|
|
|
"""Quarantines local or remote media by a given ID so that no one can download
|
|
|
|
it via this server.
|
|
|
|
"""
|
|
|
|
|
2020-11-25 14:26:11 -07:00
|
|
|
PATTERNS = admin_patterns(
|
2021-12-08 09:59:40 -07:00
|
|
|
"/media/quarantine/(?P<server_name>[^/]*)/(?P<media_id>[^/]*)$"
|
2020-01-13 11:10:43 -07:00
|
|
|
)
|
|
|
|
|
2021-01-15 09:18:09 -07:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 04:04:02 -07:00
|
|
|
self.store = hs.get_datastores().main
|
2020-01-13 11:10:43 -07:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
2021-01-15 09:18:09 -07:00
|
|
|
async def on_POST(
|
2021-03-12 09:37:57 -07:00
|
|
|
self, request: SynapseRequest, server_name: str, media_id: str
|
2021-01-15 09:18:09 -07:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2020-01-13 11:10:43 -07:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2022-08-22 07:17:59 -06:00
|
|
|
await assert_user_is_admin(self.auth, requester)
|
2020-01-13 11:10:43 -07:00
|
|
|
|
2022-06-07 04:53:47 -06:00
|
|
|
logging.info("Quarantining media by ID: %s/%s", server_name, media_id)
|
2020-01-13 11:10:43 -07:00
|
|
|
|
|
|
|
# Quarantine this media id
|
|
|
|
await self.store.quarantine_media_by_id(
|
|
|
|
server_name, media_id, requester.user.to_string()
|
|
|
|
)
|
|
|
|
|
2021-11-29 15:19:45 -07:00
|
|
|
return HTTPStatus.OK, {}
|
2020-01-13 11:10:43 -07:00
|
|
|
|
|
|
|
|
2021-06-02 11:50:35 -06:00
|
|
|
class UnquarantineMediaByID(RestServlet):
|
|
|
|
"""Quarantines local or remote media by a given ID so that no one can download
|
|
|
|
it via this server.
|
|
|
|
"""
|
|
|
|
|
|
|
|
PATTERNS = admin_patterns(
|
2021-12-08 09:59:40 -07:00
|
|
|
"/media/unquarantine/(?P<server_name>[^/]*)/(?P<media_id>[^/]*)$"
|
2021-06-02 11:50:35 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 04:04:02 -07:00
|
|
|
self.store = hs.get_datastores().main
|
2021-06-02 11:50:35 -06:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, server_name: str, media_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2021-12-08 09:59:40 -07:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
2021-06-02 11:50:35 -06:00
|
|
|
|
2022-06-07 04:53:47 -06:00
|
|
|
logging.info("Remove from quarantine media by ID: %s/%s", server_name, media_id)
|
2021-06-02 11:50:35 -06:00
|
|
|
|
|
|
|
# Remove from quarantine this media id
|
|
|
|
await self.store.quarantine_media_by_id(server_name, media_id, None)
|
|
|
|
|
2021-11-29 15:19:45 -07:00
|
|
|
return HTTPStatus.OK, {}
|
2021-06-02 11:50:35 -06:00
|
|
|
|
|
|
|
|
2021-01-15 09:18:09 -07:00
|
|
|
class ProtectMediaByID(RestServlet):
|
|
|
|
"""Protect local media from being quarantined."""
|
|
|
|
|
2021-12-08 09:59:40 -07:00
|
|
|
PATTERNS = admin_patterns("/media/protect/(?P<media_id>[^/]*)$")
|
2021-01-15 09:18:09 -07:00
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 04:04:02 -07:00
|
|
|
self.store = hs.get_datastores().main
|
2021-01-15 09:18:09 -07:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
2021-03-12 09:37:57 -07:00
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, media_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2021-12-08 09:59:40 -07:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
2021-01-15 09:18:09 -07:00
|
|
|
|
|
|
|
logging.info("Protecting local media by ID: %s", media_id)
|
|
|
|
|
2021-05-26 04:19:47 -06:00
|
|
|
# Protect this media id
|
|
|
|
await self.store.mark_local_media_as_safe(media_id, safe=True)
|
|
|
|
|
2021-11-29 15:19:45 -07:00
|
|
|
return HTTPStatus.OK, {}
|
2021-05-26 04:19:47 -06:00
|
|
|
|
|
|
|
|
|
|
|
class UnprotectMediaByID(RestServlet):
|
|
|
|
"""Unprotect local media from being quarantined."""
|
|
|
|
|
2021-12-08 09:59:40 -07:00
|
|
|
PATTERNS = admin_patterns("/media/unprotect/(?P<media_id>[^/]*)$")
|
2021-05-26 04:19:47 -06:00
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 04:04:02 -07:00
|
|
|
self.store = hs.get_datastores().main
|
2021-05-26 04:19:47 -06:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, media_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2021-12-08 09:59:40 -07:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
2021-05-26 04:19:47 -06:00
|
|
|
|
|
|
|
logging.info("Unprotecting local media by ID: %s", media_id)
|
|
|
|
|
|
|
|
# Unprotect this media id
|
|
|
|
await self.store.mark_local_media_as_safe(media_id, safe=False)
|
2021-01-15 09:18:09 -07:00
|
|
|
|
2021-11-29 15:19:45 -07:00
|
|
|
return HTTPStatus.OK, {}
|
2021-01-15 09:18:09 -07:00
|
|
|
|
|
|
|
|
2019-08-13 05:49:28 -06:00
|
|
|
class ListMediaInRoom(RestServlet):
|
|
|
|
"""Lists all of the media in a given room."""
|
|
|
|
|
2021-12-08 09:59:40 -07:00
|
|
|
PATTERNS = admin_patterns("/room/(?P<room_id>[^/]*)/media$")
|
2019-08-13 05:49:28 -06:00
|
|
|
|
2021-01-15 09:18:09 -07:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 04:04:02 -07:00
|
|
|
self.store = hs.get_datastores().main
|
2019-09-03 09:01:30 -06:00
|
|
|
self.auth = hs.get_auth()
|
2019-08-13 05:49:28 -06:00
|
|
|
|
2021-03-12 09:37:57 -07:00
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, room_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2021-12-08 09:59:40 -07:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
2019-08-13 05:49:28 -06:00
|
|
|
|
2019-10-11 05:17:59 -06:00
|
|
|
local_mxcs, remote_mxcs = await self.store.get_media_mxcs_in_room(room_id)
|
2019-08-13 05:49:28 -06:00
|
|
|
|
2021-11-29 15:19:45 -07:00
|
|
|
return HTTPStatus.OK, {"local": local_mxcs, "remote": remote_mxcs}
|
2019-08-13 05:49:28 -06:00
|
|
|
|
|
|
|
|
|
|
|
class PurgeMediaCacheRestServlet(RestServlet):
|
2021-10-20 08:41:48 -06:00
|
|
|
PATTERNS = admin_patterns("/purge_media_cache$")
|
2019-08-13 05:49:28 -06:00
|
|
|
|
2021-01-15 09:18:09 -07:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2019-08-13 05:49:28 -06:00
|
|
|
self.media_repository = hs.get_media_repository()
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
2021-03-12 09:37:57 -07:00
|
|
|
async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
2019-10-11 05:17:59 -06:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
2019-08-13 05:49:28 -06:00
|
|
|
|
|
|
|
before_ts = parse_integer(request, "before_ts", required=True)
|
|
|
|
logger.info("before_ts: %r", before_ts)
|
|
|
|
|
2021-10-20 08:41:48 -06:00
|
|
|
if before_ts < 0:
|
|
|
|
raise SynapseError(
|
2021-11-29 15:19:45 -07:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2021-10-20 08:41:48 -06:00
|
|
|
"Query parameter before_ts must be a positive integer.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
elif before_ts < 30000000000: # Dec 1970 in milliseconds, Aug 2920 in seconds
|
|
|
|
raise SynapseError(
|
2021-11-29 15:19:45 -07:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2021-10-20 08:41:48 -06:00
|
|
|
"Query parameter before_ts you provided is from the year 1970. "
|
|
|
|
+ "Double check that you are providing a timestamp in milliseconds.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
2019-10-11 05:17:59 -06:00
|
|
|
ret = await self.media_repository.delete_old_remote_media(before_ts)
|
2019-08-13 05:49:28 -06:00
|
|
|
|
2021-11-29 15:19:45 -07:00
|
|
|
return HTTPStatus.OK, ret
|
2019-08-13 05:49:28 -06:00
|
|
|
|
|
|
|
|
2020-10-26 11:02:28 -06:00
|
|
|
class DeleteMediaByID(RestServlet):
|
|
|
|
"""Delete local media by a given ID. Removes it from this server."""
|
|
|
|
|
2021-12-08 09:59:40 -07:00
|
|
|
PATTERNS = admin_patterns("/media/(?P<server_name>[^/]*)/(?P<media_id>[^/]*)$")
|
2020-10-26 11:02:28 -06:00
|
|
|
|
2021-01-15 09:18:09 -07:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 04:04:02 -07:00
|
|
|
self.store = hs.get_datastores().main
|
2020-10-26 11:02:28 -06:00
|
|
|
self.auth = hs.get_auth()
|
2023-05-05 08:06:22 -06:00
|
|
|
self._is_mine_server_name = hs.is_mine_server_name
|
2020-10-26 11:02:28 -06:00
|
|
|
self.media_repository = hs.get_media_repository()
|
|
|
|
|
2021-01-15 09:18:09 -07:00
|
|
|
async def on_DELETE(
|
2021-03-12 09:37:57 -07:00
|
|
|
self, request: SynapseRequest, server_name: str, media_id: str
|
2021-01-15 09:18:09 -07:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2020-10-26 11:02:28 -06:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
|
|
|
|
2023-05-05 08:06:22 -06:00
|
|
|
if not self._is_mine_server_name(server_name):
|
2021-11-29 15:19:45 -07:00
|
|
|
raise SynapseError(HTTPStatus.BAD_REQUEST, "Can only delete local media")
|
2020-10-26 11:02:28 -06:00
|
|
|
|
|
|
|
if await self.store.get_local_media(media_id) is None:
|
|
|
|
raise NotFoundError("Unknown media")
|
|
|
|
|
|
|
|
logging.info("Deleting local media by ID: %s", media_id)
|
|
|
|
|
2021-08-11 13:29:59 -06:00
|
|
|
deleted_media, total = await self.media_repository.delete_local_media_ids(
|
|
|
|
[media_id]
|
|
|
|
)
|
2021-11-29 15:19:45 -07:00
|
|
|
return HTTPStatus.OK, {"deleted_media": deleted_media, "total": total}
|
2020-10-26 11:02:28 -06:00
|
|
|
|
|
|
|
|
|
|
|
class DeleteMediaByDateSize(RestServlet):
|
|
|
|
"""Delete local media and local copies of remote media by
|
|
|
|
timestamp and size.
|
|
|
|
"""
|
|
|
|
|
2023-02-09 11:04:24 -07:00
|
|
|
PATTERNS = [
|
|
|
|
*admin_patterns("/media/delete$"),
|
|
|
|
# This URL kept around for legacy reasons, it is undesirable since it
|
|
|
|
# overlaps with the DeleteMediaByID servlet.
|
|
|
|
*admin_patterns("/media/(?P<server_name>[^/]*)/delete$"),
|
|
|
|
]
|
2020-10-26 11:02:28 -06:00
|
|
|
|
2021-01-15 09:18:09 -07:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 04:04:02 -07:00
|
|
|
self.store = hs.get_datastores().main
|
2020-10-26 11:02:28 -06:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.server_name = hs.hostname
|
|
|
|
self.media_repository = hs.get_media_repository()
|
|
|
|
|
2021-03-12 09:37:57 -07:00
|
|
|
async def on_POST(
|
2023-02-09 11:04:24 -07:00
|
|
|
self, request: SynapseRequest, server_name: Optional[str] = None
|
2021-03-12 09:37:57 -07:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2020-10-26 11:02:28 -06:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
|
|
|
|
|
|
|
before_ts = parse_integer(request, "before_ts", required=True)
|
|
|
|
size_gt = parse_integer(request, "size_gt", default=0)
|
|
|
|
keep_profiles = parse_boolean(request, "keep_profiles", default=True)
|
|
|
|
|
|
|
|
if before_ts < 0:
|
|
|
|
raise SynapseError(
|
2021-11-29 15:19:45 -07:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2021-10-20 08:41:48 -06:00
|
|
|
"Query parameter before_ts must be a positive integer.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
elif before_ts < 30000000000: # Dec 1970 in milliseconds, Aug 2920 in seconds
|
|
|
|
raise SynapseError(
|
2021-11-29 15:19:45 -07:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2021-10-20 08:41:48 -06:00
|
|
|
"Query parameter before_ts you provided is from the year 1970. "
|
|
|
|
+ "Double check that you are providing a timestamp in milliseconds.",
|
2020-10-26 11:02:28 -06:00
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
if size_gt < 0:
|
|
|
|
raise SynapseError(
|
2021-11-29 15:19:45 -07:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2020-10-26 11:02:28 -06:00
|
|
|
"Query parameter size_gt must be a string representing a positive integer.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
2023-02-09 11:04:24 -07:00
|
|
|
# This check is useless, we keep it for the legacy endpoint only.
|
|
|
|
if server_name is not None and self.server_name != server_name:
|
2021-11-29 15:19:45 -07:00
|
|
|
raise SynapseError(HTTPStatus.BAD_REQUEST, "Can only delete local media")
|
2020-10-26 11:02:28 -06:00
|
|
|
|
|
|
|
logging.info(
|
|
|
|
"Deleting local media by timestamp: %s, size larger than: %s, keep profile media: %s"
|
|
|
|
% (before_ts, size_gt, keep_profiles)
|
|
|
|
)
|
|
|
|
|
|
|
|
deleted_media, total = await self.media_repository.delete_old_local_media(
|
|
|
|
before_ts, size_gt, keep_profiles
|
|
|
|
)
|
2021-11-29 15:19:45 -07:00
|
|
|
return HTTPStatus.OK, {"deleted_media": deleted_media, "total": total}
|
2020-10-26 11:02:28 -06:00
|
|
|
|
|
|
|
|
2021-08-18 06:25:12 -06:00
|
|
|
class UserMediaRestServlet(RestServlet):
|
|
|
|
"""
|
|
|
|
Gets information about all uploaded local media for a specific `user_id`.
|
|
|
|
With DELETE request you can delete all this media.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
http://localhost:8008/_synapse/admin/v1/users/@user:server/media
|
|
|
|
|
|
|
|
Args:
|
|
|
|
The parameters `from` and `limit` are required for pagination.
|
|
|
|
By default, a `limit` of 100 is used.
|
|
|
|
Returns:
|
|
|
|
A list of media and an integer representing the total number of
|
|
|
|
media that exist given for this user
|
|
|
|
"""
|
|
|
|
|
2021-12-08 09:59:40 -07:00
|
|
|
PATTERNS = admin_patterns("/users/(?P<user_id>[^/]*)/media$")
|
2021-08-18 06:25:12 -06:00
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
|
|
|
self.is_mine = hs.is_mine
|
|
|
|
self.auth = hs.get_auth()
|
2022-02-23 04:04:02 -07:00
|
|
|
self.store = hs.get_datastores().main
|
2021-08-18 06:25:12 -06:00
|
|
|
self.media_repository = hs.get_media_repository()
|
|
|
|
|
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, user_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
|
|
|
# This will always be set by the time Twisted calls us.
|
|
|
|
assert request.args is not None
|
|
|
|
|
|
|
|
await assert_requester_is_admin(self.auth, request)
|
|
|
|
|
|
|
|
if not self.is_mine(UserID.from_string(user_id)):
|
2021-11-29 15:19:45 -07:00
|
|
|
raise SynapseError(HTTPStatus.BAD_REQUEST, "Can only look up local users")
|
2021-08-18 06:25:12 -06:00
|
|
|
|
|
|
|
user = await self.store.get_user_by_id(user_id)
|
|
|
|
if user is None:
|
|
|
|
raise NotFoundError("Unknown user")
|
|
|
|
|
|
|
|
start = parse_integer(request, "from", default=0)
|
|
|
|
limit = parse_integer(request, "limit", default=100)
|
|
|
|
|
|
|
|
if start < 0:
|
|
|
|
raise SynapseError(
|
2021-11-29 15:19:45 -07:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2021-08-18 06:25:12 -06:00
|
|
|
"Query parameter from must be a string representing a positive integer.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
|
|
|
if limit < 0:
|
|
|
|
raise SynapseError(
|
2021-11-29 15:19:45 -07:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2021-08-18 06:25:12 -06:00
|
|
|
"Query parameter limit must be a string representing a positive integer.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
|
|
|
# If neither `order_by` nor `dir` is set, set the default order
|
|
|
|
# to newest media is on top for backward compatibility.
|
|
|
|
if b"order_by" not in request.args and b"dir" not in request.args:
|
|
|
|
order_by = MediaSortOrder.CREATED_TS.value
|
2023-02-01 14:35:24 -07:00
|
|
|
direction = Direction.BACKWARDS
|
2021-08-18 06:25:12 -06:00
|
|
|
else:
|
|
|
|
order_by = parse_string(
|
|
|
|
request,
|
|
|
|
"order_by",
|
|
|
|
default=MediaSortOrder.CREATED_TS.value,
|
2021-12-08 09:59:40 -07:00
|
|
|
allowed_values=[sort_order.value for sort_order in MediaSortOrder],
|
2021-08-18 06:25:12 -06:00
|
|
|
)
|
2023-02-01 14:35:24 -07:00
|
|
|
direction = parse_enum(
|
|
|
|
request, "dir", Direction, default=Direction.FORWARDS
|
2021-08-18 06:25:12 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
media, total = await self.store.get_local_media_by_user_paginate(
|
|
|
|
start, limit, user_id, order_by, direction
|
|
|
|
)
|
|
|
|
|
|
|
|
ret = {"media": media, "total": total}
|
|
|
|
if (start + limit) < total:
|
|
|
|
ret["next_token"] = start + len(media)
|
|
|
|
|
2021-11-29 15:19:45 -07:00
|
|
|
return HTTPStatus.OK, ret
|
2021-08-18 06:25:12 -06:00
|
|
|
|
|
|
|
async def on_DELETE(
|
|
|
|
self, request: SynapseRequest, user_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
|
|
|
# This will always be set by the time Twisted calls us.
|
|
|
|
assert request.args is not None
|
|
|
|
|
|
|
|
await assert_requester_is_admin(self.auth, request)
|
|
|
|
|
|
|
|
if not self.is_mine(UserID.from_string(user_id)):
|
2021-11-29 15:19:45 -07:00
|
|
|
raise SynapseError(HTTPStatus.BAD_REQUEST, "Can only look up local users")
|
2021-08-18 06:25:12 -06:00
|
|
|
|
|
|
|
user = await self.store.get_user_by_id(user_id)
|
|
|
|
if user is None:
|
|
|
|
raise NotFoundError("Unknown user")
|
|
|
|
|
|
|
|
start = parse_integer(request, "from", default=0)
|
|
|
|
limit = parse_integer(request, "limit", default=100)
|
|
|
|
|
|
|
|
if start < 0:
|
|
|
|
raise SynapseError(
|
2021-11-29 15:19:45 -07:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2021-08-18 06:25:12 -06:00
|
|
|
"Query parameter from must be a string representing a positive integer.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
|
|
|
if limit < 0:
|
|
|
|
raise SynapseError(
|
2021-11-29 15:19:45 -07:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2021-08-18 06:25:12 -06:00
|
|
|
"Query parameter limit must be a string representing a positive integer.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
|
|
|
# If neither `order_by` nor `dir` is set, set the default order
|
|
|
|
# to newest media is on top for backward compatibility.
|
|
|
|
if b"order_by" not in request.args and b"dir" not in request.args:
|
|
|
|
order_by = MediaSortOrder.CREATED_TS.value
|
2023-02-01 14:35:24 -07:00
|
|
|
direction = Direction.BACKWARDS
|
2021-08-18 06:25:12 -06:00
|
|
|
else:
|
|
|
|
order_by = parse_string(
|
|
|
|
request,
|
|
|
|
"order_by",
|
|
|
|
default=MediaSortOrder.CREATED_TS.value,
|
2021-12-08 09:59:40 -07:00
|
|
|
allowed_values=[sort_order.value for sort_order in MediaSortOrder],
|
2021-08-18 06:25:12 -06:00
|
|
|
)
|
2023-02-01 14:35:24 -07:00
|
|
|
direction = parse_enum(
|
|
|
|
request, "dir", Direction, default=Direction.FORWARDS
|
2021-08-18 06:25:12 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
media, _ = await self.store.get_local_media_by_user_paginate(
|
|
|
|
start, limit, user_id, order_by, direction
|
|
|
|
)
|
|
|
|
|
|
|
|
deleted_media, total = await self.media_repository.delete_local_media_ids(
|
2022-01-05 10:53:05 -07:00
|
|
|
[row["media_id"] for row in media]
|
2021-08-18 06:25:12 -06:00
|
|
|
)
|
|
|
|
|
2021-11-29 15:19:45 -07:00
|
|
|
return HTTPStatus.OK, {"deleted_media": deleted_media, "total": total}
|
2021-08-18 06:25:12 -06:00
|
|
|
|
|
|
|
|
2021-06-07 08:12:34 -06:00
|
|
|
def register_servlets_for_media_repo(hs: "HomeServer", http_server: HttpServer) -> None:
|
2019-08-13 05:49:28 -06:00
|
|
|
"""
|
|
|
|
Media repo specific APIs.
|
|
|
|
"""
|
|
|
|
PurgeMediaCacheRestServlet(hs).register(http_server)
|
|
|
|
QuarantineMediaInRoom(hs).register(http_server)
|
2020-01-13 11:10:43 -07:00
|
|
|
QuarantineMediaByID(hs).register(http_server)
|
2021-06-02 11:50:35 -06:00
|
|
|
UnquarantineMediaByID(hs).register(http_server)
|
2020-01-13 11:10:43 -07:00
|
|
|
QuarantineMediaByUser(hs).register(http_server)
|
2021-01-15 09:18:09 -07:00
|
|
|
ProtectMediaByID(hs).register(http_server)
|
2021-05-26 04:19:47 -06:00
|
|
|
UnprotectMediaByID(hs).register(http_server)
|
2019-08-13 05:49:28 -06:00
|
|
|
ListMediaInRoom(hs).register(http_server)
|
2023-02-09 11:04:24 -07:00
|
|
|
# XXX DeleteMediaByDateSize must be registered before DeleteMediaByID as
|
|
|
|
# their URL routes overlap.
|
2020-10-26 11:02:28 -06:00
|
|
|
DeleteMediaByDateSize(hs).register(http_server)
|
2023-02-09 11:04:24 -07:00
|
|
|
DeleteMediaByID(hs).register(http_server)
|
2021-08-18 06:25:12 -06:00
|
|
|
UserMediaRestServlet(hs).register(http_server)
|