|
| 1 | +# |
| 2 | +# This file is licensed under the Affero General Public License (AGPL) version 3. |
| 3 | +# |
| 4 | +# Copyright (C) 2025 New Vector, Ltd |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Affero General Public License as |
| 8 | +# published by the Free Software Foundation, either version 3 of the |
| 9 | +# License, or (at your option) any later version. |
| 10 | +# |
| 11 | +# See the GNU Affero General Public License for more details: |
| 12 | +# <https://www.gnu.org/licenses/agpl-3.0.html>. |
| 13 | +# |
| 14 | + |
| 15 | +import logging |
| 16 | +from typing import TYPE_CHECKING, Awaitable, Callable, List, Optional |
| 17 | + |
| 18 | +from synapse.types import JsonDict |
| 19 | +from synapse.util.async_helpers import delay_cancellation |
| 20 | +from synapse.util.metrics import Measure |
| 21 | + |
| 22 | +if TYPE_CHECKING: |
| 23 | + from synapse.server import HomeServer |
| 24 | + |
| 25 | +logger = logging.getLogger(__name__) |
| 26 | + |
| 27 | +GET_MEDIA_CONFIG_FOR_USER_CALLBACK = Callable[[str], Awaitable[Optional[JsonDict]]] |
| 28 | + |
| 29 | +IS_USER_ALLOWED_TO_UPLOAD_MEDIA_OF_SIZE_CALLBACK = Callable[[str, int], Awaitable[bool]] |
| 30 | + |
| 31 | + |
| 32 | +class MediaRepositoryModuleApiCallbacks: |
| 33 | + def __init__(self, hs: "HomeServer") -> None: |
| 34 | + self.clock = hs.get_clock() |
| 35 | + self._get_media_config_for_user_callbacks: List[ |
| 36 | + GET_MEDIA_CONFIG_FOR_USER_CALLBACK |
| 37 | + ] = [] |
| 38 | + self._is_user_allowed_to_upload_media_of_size_callbacks: List[ |
| 39 | + IS_USER_ALLOWED_TO_UPLOAD_MEDIA_OF_SIZE_CALLBACK |
| 40 | + ] = [] |
| 41 | + |
| 42 | + def register_callbacks( |
| 43 | + self, |
| 44 | + get_media_config_for_user: Optional[GET_MEDIA_CONFIG_FOR_USER_CALLBACK] = None, |
| 45 | + is_user_allowed_to_upload_media_of_size: Optional[ |
| 46 | + IS_USER_ALLOWED_TO_UPLOAD_MEDIA_OF_SIZE_CALLBACK |
| 47 | + ] = None, |
| 48 | + ) -> None: |
| 49 | + """Register callbacks from module for each hook.""" |
| 50 | + if get_media_config_for_user is not None: |
| 51 | + self._get_media_config_for_user_callbacks.append(get_media_config_for_user) |
| 52 | + |
| 53 | + if is_user_allowed_to_upload_media_of_size is not None: |
| 54 | + self._is_user_allowed_to_upload_media_of_size_callbacks.append( |
| 55 | + is_user_allowed_to_upload_media_of_size |
| 56 | + ) |
| 57 | + |
| 58 | + async def get_media_config_for_user(self, user_id: str) -> Optional[JsonDict]: |
| 59 | + for callback in self._get_media_config_for_user_callbacks: |
| 60 | + with Measure(self.clock, f"{callback.__module__}.{callback.__qualname__}"): |
| 61 | + res: Optional[JsonDict] = await delay_cancellation(callback(user_id)) |
| 62 | + if res: |
| 63 | + return res |
| 64 | + |
| 65 | + return None |
| 66 | + |
| 67 | + async def is_user_allowed_to_upload_media_of_size( |
| 68 | + self, user_id: str, size: int |
| 69 | + ) -> bool: |
| 70 | + for callback in self._is_user_allowed_to_upload_media_of_size_callbacks: |
| 71 | + with Measure(self.clock, f"{callback.__module__}.{callback.__qualname__}"): |
| 72 | + res: bool = await delay_cancellation(callback(user_id, size)) |
| 73 | + if not res: |
| 74 | + return res |
| 75 | + |
| 76 | + return True |
0 commit comments