2019-08-13 07:49:28 -04: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 2014-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]
|
2019-08-13 07:49:28 -04:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import logging
|
2021-11-29 17:19:45 -05:00
|
|
|
from http import HTTPStatus
|
2023-02-09 13:04:24 -05:00
|
|
|
from typing import TYPE_CHECKING, Optional, Tuple
|
2021-01-15 11:18:09 -05:00
|
|
|
|
2023-10-31 13:13:28 -04:00
|
|
|
import attr
|
|
|
|
|
2023-02-01 16:35:24 -05:00
|
|
|
from synapse.api.constants import Direction
|
2021-12-08 11:59:40 -05:00
|
|
|
from synapse.api.errors import Codes, NotFoundError, SynapseError
|
2021-06-07 10:12:34 -04:00
|
|
|
from synapse.http.server import HttpServer
|
2023-02-01 16:35:24 -05:00
|
|
|
from synapse.http.servlet import (
|
|
|
|
RestServlet,
|
|
|
|
parse_boolean,
|
|
|
|
parse_enum,
|
|
|
|
parse_integer,
|
|
|
|
parse_string,
|
|
|
|
)
|
2021-03-12 11:37:57 -05:00
|
|
|
from synapse.http.site import SynapseRequest
|
2019-08-13 07:49:28 -04:00
|
|
|
from synapse.rest.admin._base import (
|
2020-10-26 13:02:28 -04:00
|
|
|
admin_patterns,
|
2019-08-13 07:49:28 -04:00
|
|
|
assert_requester_is_admin,
|
|
|
|
assert_user_is_admin,
|
|
|
|
)
|
2021-08-18 08:25:12 -04:00
|
|
|
from synapse.storage.databases.main.media_repository import MediaSortOrder
|
|
|
|
from synapse.types import JsonDict, UserID
|
2021-01-15 11:18:09 -05:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2021-03-23 07:12:48 -04:00
|
|
|
from synapse.server import HomeServer
|
2019-08-13 07:49:28 -04: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 10:12:34 -04:00
|
|
|
PATTERNS = [
|
2021-12-08 11:59:40 -05:00
|
|
|
*admin_patterns("/room/(?P<room_id>[^/]*)/media/quarantine$"),
|
2020-01-13 13:10:43 -05:00
|
|
|
# This path kept around for legacy reasons
|
2021-12-08 11:59:40 -05:00
|
|
|
*admin_patterns("/quarantine_media/(?P<room_id>[^/]*)$"),
|
2021-06-07 10:12:34 -04:00
|
|
|
]
|
2019-08-13 07:49:28 -04:00
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2019-08-13 07:49:28 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, room_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-10-11 07:17:59 -04:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2022-08-22 09:17:59 -04:00
|
|
|
await assert_user_is_admin(self.auth, requester)
|
2019-08-13 07:49:28 -04:00
|
|
|
|
2020-01-13 13:10:43 -05:00
|
|
|
logging.info("Quarantining room: %s", room_id)
|
|
|
|
|
|
|
|
# Quarantine all media in this room
|
2019-10-11 07:17:59 -04:00
|
|
|
num_quarantined = await self.store.quarantine_media_ids_in_room(
|
2019-08-13 07:49:28 -04:00
|
|
|
room_id, requester.user.to_string()
|
|
|
|
)
|
|
|
|
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, {"num_quarantined": num_quarantined}
|
2019-08-13 07:49:28 -04:00
|
|
|
|
|
|
|
|
2020-01-13 13:10:43 -05: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 11:59:40 -05:00
|
|
|
PATTERNS = admin_patterns("/user/(?P<user_id>[^/]*)/media/quarantine$")
|
2020-01-13 13:10:43 -05:00
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2020-01-13 13:10:43 -05:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, user_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2020-01-13 13:10:43 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2022-08-22 09:17:59 -04:00
|
|
|
await assert_user_is_admin(self.auth, requester)
|
2020-01-13 13:10:43 -05:00
|
|
|
|
2022-06-07 06:53:47 -04:00
|
|
|
logging.info("Quarantining media by user: %s", user_id)
|
2020-01-13 13:10:43 -05: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 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, {"num_quarantined": num_quarantined}
|
2020-01-13 13:10:43 -05: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 16:26:11 -05:00
|
|
|
PATTERNS = admin_patterns(
|
2021-12-08 11:59:40 -05:00
|
|
|
"/media/quarantine/(?P<server_name>[^/]*)/(?P<media_id>[^/]*)$"
|
2020-01-13 13:10:43 -05:00
|
|
|
)
|
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2020-01-13 13:10:43 -05:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
async def on_POST(
|
2021-03-12 11:37:57 -05:00
|
|
|
self, request: SynapseRequest, server_name: str, media_id: str
|
2021-01-15 11:18:09 -05:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2020-01-13 13:10:43 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2022-08-22 09:17:59 -04:00
|
|
|
await assert_user_is_admin(self.auth, requester)
|
2020-01-13 13:10:43 -05:00
|
|
|
|
2022-06-07 06:53:47 -04:00
|
|
|
logging.info("Quarantining media by ID: %s/%s", server_name, media_id)
|
2020-01-13 13:10:43 -05:00
|
|
|
|
|
|
|
# Quarantine this media id
|
|
|
|
await self.store.quarantine_media_by_id(
|
|
|
|
server_name, media_id, requester.user.to_string()
|
|
|
|
)
|
|
|
|
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, {}
|
2020-01-13 13:10:43 -05:00
|
|
|
|
|
|
|
|
2021-06-02 13:50:35 -04: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 11:59:40 -05:00
|
|
|
"/media/unquarantine/(?P<server_name>[^/]*)/(?P<media_id>[^/]*)$"
|
2021-06-02 13:50:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2021-06-02 13:50:35 -04: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 11:59:40 -05:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
2021-06-02 13:50:35 -04:00
|
|
|
|
2022-06-07 06:53:47 -04:00
|
|
|
logging.info("Remove from quarantine media by ID: %s/%s", server_name, media_id)
|
2021-06-02 13:50:35 -04:00
|
|
|
|
|
|
|
# Remove from quarantine this media id
|
|
|
|
await self.store.quarantine_media_by_id(server_name, media_id, None)
|
|
|
|
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, {}
|
2021-06-02 13:50:35 -04:00
|
|
|
|
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
class ProtectMediaByID(RestServlet):
|
|
|
|
"""Protect local media from being quarantined."""
|
|
|
|
|
2021-12-08 11:59:40 -05:00
|
|
|
PATTERNS = admin_patterns("/media/protect/(?P<media_id>[^/]*)$")
|
2021-01-15 11:18:09 -05:00
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2021-01-15 11:18:09 -05:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, media_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2021-12-08 11:59:40 -05:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
2021-01-15 11:18:09 -05:00
|
|
|
|
|
|
|
logging.info("Protecting local media by ID: %s", media_id)
|
|
|
|
|
2021-05-26 06:19:47 -04:00
|
|
|
# Protect this media id
|
|
|
|
await self.store.mark_local_media_as_safe(media_id, safe=True)
|
|
|
|
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, {}
|
2021-05-26 06:19:47 -04:00
|
|
|
|
|
|
|
|
|
|
|
class UnprotectMediaByID(RestServlet):
|
|
|
|
"""Unprotect local media from being quarantined."""
|
|
|
|
|
2021-12-08 11:59:40 -05:00
|
|
|
PATTERNS = admin_patterns("/media/unprotect/(?P<media_id>[^/]*)$")
|
2021-05-26 06:19:47 -04:00
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2021-05-26 06:19:47 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, media_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2021-12-08 11:59:40 -05:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
2021-05-26 06:19:47 -04: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 11:18:09 -05:00
|
|
|
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, {}
|
2021-01-15 11:18:09 -05:00
|
|
|
|
|
|
|
|
2019-08-13 07:49:28 -04:00
|
|
|
class ListMediaInRoom(RestServlet):
|
|
|
|
"""Lists all of the media in a given room."""
|
|
|
|
|
2021-12-08 11:59:40 -05:00
|
|
|
PATTERNS = admin_patterns("/room/(?P<room_id>[^/]*)/media$")
|
2019-08-13 07:49:28 -04:00
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2019-09-03 11:01:30 -04:00
|
|
|
self.auth = hs.get_auth()
|
2019-08-13 07:49:28 -04:00
|
|
|
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, room_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2021-12-08 11:59:40 -05:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
2019-08-13 07:49:28 -04:00
|
|
|
|
2019-10-11 07:17:59 -04:00
|
|
|
local_mxcs, remote_mxcs = await self.store.get_media_mxcs_in_room(room_id)
|
2019-08-13 07:49:28 -04:00
|
|
|
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, {"local": local_mxcs, "remote": remote_mxcs}
|
2019-08-13 07:49:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
class PurgeMediaCacheRestServlet(RestServlet):
|
2021-10-20 10:41:48 -04:00
|
|
|
PATTERNS = admin_patterns("/purge_media_cache$")
|
2019-08-13 07:49:28 -04:00
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2019-08-13 07:49:28 -04:00
|
|
|
self.media_repository = hs.get_media_repository()
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
2019-10-11 07:17:59 -04:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
2019-08-13 07:49:28 -04:00
|
|
|
|
|
|
|
before_ts = parse_integer(request, "before_ts", required=True)
|
|
|
|
logger.info("before_ts: %r", before_ts)
|
|
|
|
|
2021-10-20 10:41:48 -04:00
|
|
|
if before_ts < 0:
|
|
|
|
raise SynapseError(
|
2021-11-29 17:19:45 -05:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2021-10-20 10:41:48 -04: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 17:19:45 -05:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2021-10-20 10:41:48 -04: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 07:17:59 -04:00
|
|
|
ret = await self.media_repository.delete_old_remote_media(before_ts)
|
2019-08-13 07:49:28 -04:00
|
|
|
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, ret
|
2019-08-13 07:49:28 -04:00
|
|
|
|
|
|
|
|
2020-10-26 13:02:28 -04:00
|
|
|
class DeleteMediaByID(RestServlet):
|
|
|
|
"""Delete local media by a given ID. Removes it from this server."""
|
|
|
|
|
2021-12-08 11:59:40 -05:00
|
|
|
PATTERNS = admin_patterns("/media/(?P<server_name>[^/]*)/(?P<media_id>[^/]*)$")
|
2020-10-26 13:02:28 -04:00
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2020-10-26 13:02:28 -04:00
|
|
|
self.auth = hs.get_auth()
|
2023-05-05 10:06:22 -04:00
|
|
|
self._is_mine_server_name = hs.is_mine_server_name
|
2020-10-26 13:02:28 -04:00
|
|
|
self.media_repository = hs.get_media_repository()
|
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
async def on_DELETE(
|
2021-03-12 11:37:57 -05:00
|
|
|
self, request: SynapseRequest, server_name: str, media_id: str
|
2021-01-15 11:18:09 -05:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2020-10-26 13:02:28 -04:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
|
|
|
|
2023-05-05 10:06:22 -04:00
|
|
|
if not self._is_mine_server_name(server_name):
|
2021-11-29 17:19:45 -05:00
|
|
|
raise SynapseError(HTTPStatus.BAD_REQUEST, "Can only delete local media")
|
2020-10-26 13:02:28 -04: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 15:29:59 -04:00
|
|
|
deleted_media, total = await self.media_repository.delete_local_media_ids(
|
|
|
|
[media_id]
|
|
|
|
)
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, {"deleted_media": deleted_media, "total": total}
|
2020-10-26 13:02:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
class DeleteMediaByDateSize(RestServlet):
|
|
|
|
"""Delete local media and local copies of remote media by
|
|
|
|
timestamp and size.
|
|
|
|
"""
|
|
|
|
|
2023-02-09 13:04:24 -05: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 13:02:28 -04:00
|
|
|
|
2021-01-15 11:18:09 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2020-10-26 13:02:28 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.server_name = hs.hostname
|
|
|
|
self.media_repository = hs.get_media_repository()
|
|
|
|
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_POST(
|
2023-02-09 13:04:24 -05:00
|
|
|
self, request: SynapseRequest, server_name: Optional[str] = None
|
2021-03-12 11:37:57 -05:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2020-10-26 13:02:28 -04: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 17:19:45 -05:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2021-10-20 10:41:48 -04: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 17:19:45 -05:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2021-10-20 10:41:48 -04: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 13:02:28 -04:00
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
if size_gt < 0:
|
|
|
|
raise SynapseError(
|
2021-11-29 17:19:45 -05:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2020-10-26 13:02:28 -04:00
|
|
|
"Query parameter size_gt must be a string representing a positive integer.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
2023-02-09 13:04:24 -05: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 17:19:45 -05:00
|
|
|
raise SynapseError(HTTPStatus.BAD_REQUEST, "Can only delete local media")
|
2020-10-26 13:02:28 -04: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 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, {"deleted_media": deleted_media, "total": total}
|
2020-10-26 13:02:28 -04:00
|
|
|
|
|
|
|
|
2021-08-18 08:25:12 -04: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 11:59:40 -05:00
|
|
|
PATTERNS = admin_patterns("/users/(?P<user_id>[^/]*)/media$")
|
2021-08-18 08:25:12 -04:00
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
|
|
|
self.is_mine = hs.is_mine
|
|
|
|
self.auth = hs.get_auth()
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2021-08-18 08:25:12 -04: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 17:19:45 -05:00
|
|
|
raise SynapseError(HTTPStatus.BAD_REQUEST, "Can only look up local users")
|
2021-08-18 08:25:12 -04: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 17:19:45 -05:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2021-08-18 08:25:12 -04:00
|
|
|
"Query parameter from must be a string representing a positive integer.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
|
|
|
if limit < 0:
|
|
|
|
raise SynapseError(
|
2021-11-29 17:19:45 -05:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2021-08-18 08:25:12 -04: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 16:35:24 -05:00
|
|
|
direction = Direction.BACKWARDS
|
2021-08-18 08:25:12 -04:00
|
|
|
else:
|
|
|
|
order_by = parse_string(
|
|
|
|
request,
|
|
|
|
"order_by",
|
|
|
|
default=MediaSortOrder.CREATED_TS.value,
|
2021-12-08 11:59:40 -05:00
|
|
|
allowed_values=[sort_order.value for sort_order in MediaSortOrder],
|
2021-08-18 08:25:12 -04:00
|
|
|
)
|
2023-02-01 16:35:24 -05:00
|
|
|
direction = parse_enum(
|
|
|
|
request, "dir", Direction, default=Direction.FORWARDS
|
2021-08-18 08:25:12 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
media, total = await self.store.get_local_media_by_user_paginate(
|
|
|
|
start, limit, user_id, order_by, direction
|
|
|
|
)
|
|
|
|
|
2023-10-31 13:13:28 -04:00
|
|
|
ret = {"media": [attr.asdict(m) for m in media], "total": total}
|
2021-08-18 08:25:12 -04:00
|
|
|
if (start + limit) < total:
|
|
|
|
ret["next_token"] = start + len(media)
|
|
|
|
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, ret
|
2021-08-18 08:25:12 -04: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 17:19:45 -05:00
|
|
|
raise SynapseError(HTTPStatus.BAD_REQUEST, "Can only look up local users")
|
2021-08-18 08:25:12 -04: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 17:19:45 -05:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2021-08-18 08:25:12 -04:00
|
|
|
"Query parameter from must be a string representing a positive integer.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
|
|
|
if limit < 0:
|
|
|
|
raise SynapseError(
|
2021-11-29 17:19:45 -05:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2021-08-18 08:25:12 -04: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 16:35:24 -05:00
|
|
|
direction = Direction.BACKWARDS
|
2021-08-18 08:25:12 -04:00
|
|
|
else:
|
|
|
|
order_by = parse_string(
|
|
|
|
request,
|
|
|
|
"order_by",
|
|
|
|
default=MediaSortOrder.CREATED_TS.value,
|
2021-12-08 11:59:40 -05:00
|
|
|
allowed_values=[sort_order.value for sort_order in MediaSortOrder],
|
2021-08-18 08:25:12 -04:00
|
|
|
)
|
2023-02-01 16:35:24 -05:00
|
|
|
direction = parse_enum(
|
|
|
|
request, "dir", Direction, default=Direction.FORWARDS
|
2021-08-18 08:25:12 -04: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(
|
2023-10-31 13:13:28 -04:00
|
|
|
[m.media_id for m in media]
|
2021-08-18 08:25:12 -04:00
|
|
|
)
|
|
|
|
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, {"deleted_media": deleted_media, "total": total}
|
2021-08-18 08:25:12 -04:00
|
|
|
|
|
|
|
|
2021-06-07 10:12:34 -04:00
|
|
|
def register_servlets_for_media_repo(hs: "HomeServer", http_server: HttpServer) -> None:
|
2019-08-13 07:49:28 -04:00
|
|
|
"""
|
|
|
|
Media repo specific APIs.
|
|
|
|
"""
|
|
|
|
PurgeMediaCacheRestServlet(hs).register(http_server)
|
|
|
|
QuarantineMediaInRoom(hs).register(http_server)
|
2020-01-13 13:10:43 -05:00
|
|
|
QuarantineMediaByID(hs).register(http_server)
|
2021-06-02 13:50:35 -04:00
|
|
|
UnquarantineMediaByID(hs).register(http_server)
|
2020-01-13 13:10:43 -05:00
|
|
|
QuarantineMediaByUser(hs).register(http_server)
|
2021-01-15 11:18:09 -05:00
|
|
|
ProtectMediaByID(hs).register(http_server)
|
2021-05-26 06:19:47 -04:00
|
|
|
UnprotectMediaByID(hs).register(http_server)
|
2019-08-13 07:49:28 -04:00
|
|
|
ListMediaInRoom(hs).register(http_server)
|
2023-02-09 13:04:24 -05:00
|
|
|
# XXX DeleteMediaByDateSize must be registered before DeleteMediaByID as
|
|
|
|
# their URL routes overlap.
|
2020-10-26 13:02:28 -04:00
|
|
|
DeleteMediaByDateSize(hs).register(http_server)
|
2023-02-09 13:04:24 -05:00
|
|
|
DeleteMediaByID(hs).register(http_server)
|
2021-08-18 08:25:12 -04:00
|
|
|
UserMediaRestServlet(hs).register(http_server)
|