diff --git a/TODO.md b/TODO.md index eb3d381b..ea5e13f6 100644 --- a/TODO.md +++ b/TODO.md @@ -4,7 +4,7 @@ - "exception during sync" aren't caught - nio ClientTimeout -- Handle upload file size limit +- Name - Update docstrings - Update TODO.md - Update README.md diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index a75df40a..aa6f8ce5 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -110,9 +110,10 @@ class MatrixClient(nio.AsyncClient): self.backend: "Backend" = backend self.models: ModelStore = self.backend.models - self.profile_task: Optional[asyncio.Future] = None - self.sync_task: Optional[asyncio.Future] = None - self.load_rooms_task: Optional[asyncio.Future] = None + self.profile_task: Optional[asyncio.Future] = None + self.server_config_task: Optional[asyncio.Future] = None + self.sync_task: Optional[asyncio.Future] = None + self.load_rooms_task: Optional[asyncio.Future] = None self.upload_monitors: Dict[UUID, nio.TransferMonitor] = {} @@ -179,7 +180,12 @@ class MatrixClient(nio.AsyncClient): async def logout(self) -> None: """Logout from the server. This will delete the device.""" - for task in (self.profile_task, self.load_rooms_task, self.sync_task): + tasks = ( + self.profile_task, self.load_rooms_task, self.sync_task, + self.server_config_task, + ) + + for task in tasks: if task: task.cancel() with suppress(asyncio.CancelledError): @@ -221,11 +227,34 @@ class MatrixClient(nio.AsyncClient): account.display_name = resp.displayname or "" account.avatar_url = resp.avatar_url or "" + def on_server_config_response(future) -> None: + """Update our model `Account` with the received config details.""" + + exception = future.exception() + + if exception: + log.warn("On %s client startup: %r", self.user_id, exception) + self.server_config_task = asyncio.ensure_future( + self.get_server_config(), + ) + self.server_config_task.add_done_callback( + on_server_config_response, + ) + return + + account = self.models["accounts"][self.user_id] + account.max_upload_size = future.result() + self.profile_task = asyncio.ensure_future( self.backend.get_profile(self.user_id), ) self.profile_task.add_done_callback(on_profile_response) + self.server_config_task = asyncio.ensure_future( + self.get_server_config(), + ) + self.server_config_task.add_done_callback(on_server_config_response) + while True: try: self.sync_task = asyncio.ensure_future( @@ -239,6 +268,11 @@ class MatrixClient(nio.AsyncClient): await asyncio.sleep(2) + async def get_server_config(self) -> int: + """Return the maximum upload size on this server""" + return (await self.content_repository_config()).upload_size + + @property def all_rooms(self) -> Dict[str, nio.MatrixRoom]: """Return dict containing both our joined and invited rooms.""" diff --git a/src/backend/models/items.py b/src/backend/models/items.py index 6c189a0b..2be336a3 100644 --- a/src/backend/models/items.py +++ b/src/backend/models/items.py @@ -35,8 +35,9 @@ class Account(ModelItem): id: str = field() display_name: str = "" avatar_url: str = "" - first_sync_done: bool = False + max_upload_size: int = 0 profile_updated: datetime = ZeroDate + first_sync_done: bool = False def __lt__(self, other: "Account") -> bool: """Sort by display name or user ID.""" diff --git a/src/gui/Pages/Chat/Composer.qml b/src/gui/Pages/Chat/Composer.qml index ecff863b..9e776c5a 100644 --- a/src/gui/Pages/Chat/Composer.qml +++ b/src/gui/Pages/Chat/Composer.qml @@ -3,6 +3,7 @@ import QtQuick 2.12 import QtQuick.Layouts 1.12 import Clipboard 0.1 +import CppUtils 0.1 import "../.." import "../../Base" import "../../Dialogs" @@ -218,7 +219,13 @@ Rectangle { enabled: chat.roomInfo.can_send_messages icon.name: "upload-file" backgroundColor: theme.chat.composer.uploadButton.background - toolTip.text: qsTr("Send files") + toolTip.text: + chat.userInfo.max_upload_size ? + qsTr("Send files (%1 max)").arg( + CppUtils.formattedBytes(chat.userInfo.max_upload_size, 0), + ) : + qsTr("Send files") + onClicked: sendFilePicker.dialog.open() Layout.fillHeight: true