diff --git a/platypush/message/event/matrix.py b/platypush/message/event/matrix.py index 04ed49769a..4a9f8519c4 100644 --- a/platypush/message/event/matrix.py +++ b/platypush/message/event/matrix.py @@ -1,5 +1,5 @@ from datetime import datetime -from typing import Dict, Any +from typing import Any, Dict, Optional from platypush.message.event import Event @@ -13,13 +13,13 @@ class MatrixEvent(Event): self, *args, server_url: str, - sender_id: str | None = None, - sender_display_name: str | None = None, - sender_avatar_url: str | None = None, - room_id: str | None = None, - room_name: str | None = None, - room_topic: str | None = None, - server_timestamp: datetime | None = None, + sender_id: Optional[str] = None, + sender_display_name: Optional[str] = None, + sender_avatar_url: Optional[str] = None, + room_id: Optional[str] = None, + room_name: Optional[str] = None, + room_topic: Optional[str] = None, + server_timestamp: Optional[datetime] = None, **kwargs ): """ @@ -70,11 +70,11 @@ class MatrixMessageEvent(MatrixEvent): self, *args, body: str = '', - url: str | None = None, - thumbnail_url: str | None = None, - mimetype: str | None = None, - formatted_body: str | None = None, - format: str | None = None, + url: Optional[str] = None, + thumbnail_url: Optional[str] = None, + mimetype: Optional[str] = None, + formatted_body: Optional[str] = None, + format: Optional[str] = None, **kwargs ): """ @@ -148,7 +148,7 @@ class MatrixCallEvent(MatrixEvent): """ def __init__( - self, *args, call_id: str, version: int, sdp: str | None = None, **kwargs + self, *args, call_id: str, version: int, sdp: Optional[str] = None, **kwargs ): """ :param call_id: The unique ID of the call. @@ -163,7 +163,7 @@ class MatrixCallInviteEvent(MatrixCallEvent): Event triggered when the user is invited to a call. """ - def __init__(self, *args, invite_validity: float | None = None, **kwargs): + def __init__(self, *args, invite_validity: Optional[float] = None, **kwargs): """ :param invite_validity: For how long the invite will be valid, in seconds. :param sdp: SDP text of the session description. @@ -242,7 +242,9 @@ class MatrixUserPresenceEvent(MatrixEvent): Event triggered when a user comes online or goes offline. """ - def __init__(self, *args, is_active: bool, last_active: datetime | None, **kwargs): + def __init__( + self, *args, is_active: bool, last_active: Optional[datetime], **kwargs + ): """ :param is_active: True if the user is currently online. :param topic: When the user was last active. diff --git a/platypush/plugins/matrix/__init__.py b/platypush/plugins/matrix/__init__.py index 45aeeddc4f..24b759e69d 100644 --- a/platypush/plugins/matrix/__init__.py +++ b/platypush/plugins/matrix/__init__.py @@ -5,7 +5,7 @@ import pathlib import re from dataclasses import dataclass -from typing import Collection, Coroutine, Sequence +from typing import Collection, Coroutine, Optional, Sequence from urllib.parse import urlparse from nio import ( @@ -47,7 +47,7 @@ class Credentials: server_url: str user_id: str access_token: str - device_id: str | None + device_id: Optional[str] = None def to_dict(self) -> dict: return { @@ -98,22 +98,22 @@ class MatrixPlugin(AsyncRunnablePlugin): def __init__( self, server_url: str = 'https://matrix-client.matrix.org', - user_id: str | None = None, - password: str | None = None, - access_token: str | None = None, - device_name: str | None = 'platypush', - device_id: str | None = None, - download_path: str | None = None, + user_id: Optional[str] = None, + password: Optional[str] = None, + access_token: Optional[str] = None, + device_name: Optional[str] = 'platypush', + device_id: Optional[str] = None, + download_path: Optional[str] = None, autojoin_on_invite: bool = True, autotrust_devices: bool = False, - autotrust_devices_whitelist: Collection[str] | None = None, - autotrust_users_whitelist: Collection[str] | None = None, - autotrust_rooms_whitelist: Collection[str] | None = None, + autotrust_devices_whitelist: Optional[Collection[str]] = None, + autotrust_users_whitelist: Optional[Collection[str]] = None, + autotrust_rooms_whitelist: Optional[Collection[str]] = None, **kwargs, ): """ Authentication requires user_id/password on the first login. - Afterwards, session credentials are stored under + Afterward, session credentials are stored under ``<$PLATYPUSH_WORKDIR>/matrix/credentials.json`` (default: ``~/.local/share/platypush/matrix/credentials.json``), and you can remove the cleartext credentials from your configuration file. @@ -299,9 +299,9 @@ class MatrixPlugin(AsyncRunnablePlugin): self, room_id: str, message_type: str = 'text', - body: str | None = None, - attachment: str | None = None, - tx_id: str | None = None, + body: Optional[str] = None, + attachment: Optional[str] = None, + tx_id: Optional[str] = None, ignore_unverified_devices: bool = False, ): """ @@ -388,8 +388,8 @@ class MatrixPlugin(AsyncRunnablePlugin): def get_messages( self, room_id: str, - start: str | None = None, - end: str | None = None, + start: Optional[str] = None, + end: Optional[str] = None, backwards: bool = True, limit: int = 10, ): @@ -442,10 +442,11 @@ class MatrixPlugin(AsyncRunnablePlugin): return MatrixDeviceSchema().dump(self._get_device(device_id)) @action - def update_device(self, device_id: str, display_name: str | None = None): + def update_device(self, device_id: str, display_name: Optional[str] = None): """ Update information about a user's device. + :param device_id: Device ID. :param display_name: New display name. :return: .. schema:: matrix.MatrixDeviceSchema """ @@ -460,8 +461,8 @@ class MatrixPlugin(AsyncRunnablePlugin): def delete_devices( self, devices: Sequence[str], - username: str | None = None, - password: str | None = None, + username: Optional[str] = None, + password: Optional[str] = None, ): """ Delete a list of devices from the user's authorized list and invalidate @@ -564,7 +565,7 @@ class MatrixPlugin(AsyncRunnablePlugin): self.client.unverify_device(device) @action - def mxc_to_http(self, url: str, homeserver: str | None = None) -> str: + def mxc_to_http(self, url: str, homeserver: Optional[str] = None) -> str: """ Convert a Matrix URL (in the format ``mxc://server/media_id``) to an HTTP URL. @@ -587,8 +588,8 @@ class MatrixPlugin(AsyncRunnablePlugin): def download( self, url: str, - download_path: str | None = None, - filename: str | None = None, + download_path: Optional[str] = None, + filename: Optional[str] = None, allow_remote=True, ): """ @@ -641,8 +642,8 @@ class MatrixPlugin(AsyncRunnablePlugin): def upload( self, file: str, - name: str | None = None, - content_type: str | None = None, + name: Optional[str] = None, + content_type: Optional[str] = None, encrypt: bool = False, ) -> str: """ @@ -665,9 +666,9 @@ class MatrixPlugin(AsyncRunnablePlugin): @action def create_room( self, - name: str | None = None, - alias: str | None = None, - topic: str | None = None, + name: Optional[str] = None, + alias: Optional[str] = None, + topic: Optional[str] = None, is_public: bool = False, is_direct: bool = False, federate: bool = True, @@ -729,7 +730,7 @@ class MatrixPlugin(AsyncRunnablePlugin): self._loop_execute(self.client.room_invite(room_id, user_id)) @action - def kick(self, room_id: str, user_id: str, reason: str | None = None): + def kick(self, room_id: str, user_id: str, reason: Optional[str] = None): """ Kick a user out of a room. @@ -740,7 +741,7 @@ class MatrixPlugin(AsyncRunnablePlugin): self._loop_execute(self.client.room_kick(room_id, user_id, reason)) @action - def ban(self, room_id: str, user_id: str, reason: str | None = None): + def ban(self, room_id: str, user_id: str, reason: Optional[str] = None): """ Ban a user from a room. diff --git a/platypush/plugins/matrix/client.py b/platypush/plugins/matrix/client.py index 1cf957e7d2..9bf336792e 100644 --- a/platypush/plugins/matrix/client.py +++ b/platypush/plugins/matrix/client.py @@ -100,7 +100,7 @@ class Credentials: server_url: str user_id: str access_token: str - device_id: str | None + device_id: Optional[str] = None def to_dict(self) -> dict: return { @@ -116,13 +116,13 @@ class MatrixClient(AsyncClient): self, *args, credentials_file: str, - store_path: str | None = None, + store_path: Optional[str] = None, config: Optional[AsyncClientConfig] = None, autojoin_on_invite=True, autotrust_devices=False, - autotrust_devices_whitelist: Collection[str] | None = None, - autotrust_rooms_whitelist: Collection[str] | None = None, - autotrust_users_whitelist: Collection[str] | None = None, + autotrust_devices_whitelist: Optional[Collection[str]] = None, + autotrust_rooms_whitelist: Optional[Collection[str]] = None, + autotrust_users_whitelist: Optional[Collection[str]] = None, **kwargs, ): credentials_file = os.path.abspath(os.path.expanduser(credentials_file)) @@ -158,7 +158,7 @@ class MatrixClient(AsyncClient): store_path, 'attachment_keys.json' ) self._encrypted_attachments_keystore = {} - self._sync_store_timer: threading.Timer | None = None + self._sync_store_timer: Optional[threading.Timer] = None keystore = {} try: @@ -206,9 +206,9 @@ class MatrixClient(AsyncClient): async def login( self, - password: str | None = None, - device_name: str | None = None, - token: str | None = None, + password: Optional[str] = None, + device_name: Optional[str] = None, + token: Optional[str] = None, ) -> LoginResponse: self._load_from_file() login_res = None @@ -289,7 +289,7 @@ class MatrixClient(AsyncClient): @logged_in async def room_messages( - self, room_id: str, start: str | None = None, *args, **kwargs + self, room_id: str, start: Optional[str] = None, *args, **kwargs ) -> RoomMessagesResponse: if not start: start = self._last_batches_by_room.get(room_id, {}).get('prev_batch') @@ -351,9 +351,9 @@ class MatrixClient(AsyncClient): ) def get_devices_by_user( - self, user_id: str | None = None + self, user_id: Optional[str] = None ) -> Dict[str, Dict[str, OlmDevice]] | Dict[str, OlmDevice]: - devices = {user: devices for user, devices in self.device_store.items()} + devices = dict(self.device_store.items()) if user_id: devices = devices.get(user_id, {}) @@ -370,7 +370,7 @@ class MatrixClient(AsyncClient): return self.get_devices().get(device_id) def get_devices_by_room( - self, room_id: str | None = None + self, room_id: Optional[str] = None ) -> Dict[str, Dict[str, OlmDevice]] | Dict[str, OlmDevice]: devices = { room_id: { @@ -432,7 +432,7 @@ class MatrixClient(AsyncClient): @alru_cache(maxsize=500) @client_session - async def get_profile(self, user_id: str | None = None) -> ProfileGetResponse: + async def get_profile(self, user_id: Optional[str] = None) -> ProfileGetResponse: """ Cached version of get_profile. """ @@ -459,7 +459,7 @@ class MatrixClient(AsyncClient): self, server_name: str, media_id: str, - filename: str | None = None, + filename: Optional[str] = None, allow_remote: bool = True, ): response = await super().download(