2019-12-19 22:46:16 +11:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
2019-06-27 16:31:03 +10:00
|
|
|
import asyncio
|
Big performance refactoring & various improvements
Instead of passing all sorts of events for the JS to handle and manually
add to different data models, we now handle everything we can in Python.
For any change, the python models send a sync event with their
contents (no more than 4 times per second) to JS, and the QSyncable
library's JsonListModel takes care of converting it to a QML ListModel
and sending the appropriate signals.
The SortFilterProxyModel library is not used anymore, the only case
where we need to filter/sort something now is when the user interacts
with the "Filter rooms" or "Filter members" fields. These cases are
handled by a simple JS function.
We now keep separated room and timeline models for different accounts,
the previous approach of sharing all the data we could between accounts
created a lot of complications (local echoes, decrypted messages
replacing others, etc).
The users's own account profile changes are now hidden in the timeline.
On startup, if all events for a room were only own profile changes, more
events will be loaded.
Any kind of image format supported by Qt is now handled by the
pyotherside image provider, instead of just PNG/JPG.
SVGs which previously caused errors are supported as well.
The typing members bar paddings/margins are fixed.
The behavior of the avatar/"upload a profile picture" overlay is fixed.
Config files read from disk are now cached (TODO: make them reloadable
again).
Pylint is not used anymore because of all its annoying false warnings
and lack of understanding for dataclasses, it is replaced by flake8 with
a custom config and various plugins.
Debug mode is now considered on if the program was compiled with
the right option, instead of taking an argument from CLI.
When on, C++ will set a flag in the Window QML component.
The loading screen is now unloaded after the UI is ready, where
previously it just stayed in the background invisible and wasted CPU.
The overall refactoring and improvements make us now able to handle
rooms with thousand of members and no lazy-loading, where previously
everything would freeze and simply scrolling up to load past events
in any room would block the UI for a few seconds.
2019-08-11 22:01:22 +10:00
|
|
|
import logging as log
|
2020-06-12 11:50:26 +10:00
|
|
|
import os
|
2020-08-19 14:17:24 +10:00
|
|
|
import re
|
2019-12-18 21:44:18 +11:00
|
|
|
import sys
|
2020-08-19 14:17:24 +10:00
|
|
|
import time
|
2019-12-18 23:14:35 +11:00
|
|
|
import traceback
|
2020-08-19 14:17:24 +10:00
|
|
|
from datetime import datetime
|
2019-11-13 00:10:00 +11:00
|
|
|
from pathlib import Path
|
2020-07-24 15:30:35 +10:00
|
|
|
from typing import Any, DefaultDict, Dict, List, Optional, Tuple
|
2020-08-19 16:14:46 +10:00
|
|
|
from urllib.parse import urlparse
|
2019-06-27 16:31:03 +10:00
|
|
|
|
2020-08-19 14:17:24 +10:00
|
|
|
import aiohttp
|
2019-12-03 07:29:29 +11:00
|
|
|
from appdirs import AppDirs
|
Big performance refactoring & various improvements
Instead of passing all sorts of events for the JS to handle and manually
add to different data models, we now handle everything we can in Python.
For any change, the python models send a sync event with their
contents (no more than 4 times per second) to JS, and the QSyncable
library's JsonListModel takes care of converting it to a QML ListModel
and sending the appropriate signals.
The SortFilterProxyModel library is not used anymore, the only case
where we need to filter/sort something now is when the user interacts
with the "Filter rooms" or "Filter members" fields. These cases are
handled by a simple JS function.
We now keep separated room and timeline models for different accounts,
the previous approach of sharing all the data we could between accounts
created a lot of complications (local echoes, decrypted messages
replacing others, etc).
The users's own account profile changes are now hidden in the timeline.
On startup, if all events for a room were only own profile changes, more
events will be loaded.
Any kind of image format supported by Qt is now handled by the
pyotherside image provider, instead of just PNG/JPG.
SVGs which previously caused errors are supported as well.
The typing members bar paddings/margins are fixed.
The behavior of the avatar/"upload a profile picture" overlay is fixed.
Config files read from disk are now cached (TODO: make them reloadable
again).
Pylint is not used anymore because of all its annoying false warnings
and lack of understanding for dataclasses, it is replaced by flake8 with
a custom config and various plugins.
Debug mode is now considered on if the program was compiled with
the right option, instead of taking an argument from CLI.
When on, C++ will set a flag in the Window QML component.
The loading screen is now unloaded after the UI is ready, where
previously it just stayed in the background invisible and wasted CPU.
The overall refactoring and improvements make us now able to handle
rooms with thousand of members and no lazy-loading, where previously
everything would freeze and simply scrolling up to load past events
in any room would block the UI for a few seconds.
2019-08-11 22:01:22 +10:00
|
|
|
|
2020-05-06 15:49:25 +10:00
|
|
|
import nio
|
|
|
|
|
2019-12-18 21:55:05 +11:00
|
|
|
from . import __app_name__
|
2020-09-05 01:21:36 +10:00
|
|
|
from .errors import MatrixError, MatrixInvalidAccessToken
|
2019-11-12 23:47:03 +11:00
|
|
|
from .matrix_client import MatrixClient
|
2020-02-12 07:22:05 +11:00
|
|
|
from .media_cache import MediaCache
|
2019-12-03 07:29:29 +11:00
|
|
|
from .models import SyncId
|
2020-08-21 02:21:47 +10:00
|
|
|
from .models.filters import FieldStringFilter
|
2020-08-19 14:17:24 +10:00
|
|
|
from .models.items import Account, Event, Homeserver, PingStatus
|
2020-05-06 15:49:25 +10:00
|
|
|
from .models.model import Model
|
Big performance refactoring & various improvements
Instead of passing all sorts of events for the JS to handle and manually
add to different data models, we now handle everything we can in Python.
For any change, the python models send a sync event with their
contents (no more than 4 times per second) to JS, and the QSyncable
library's JsonListModel takes care of converting it to a QML ListModel
and sending the appropriate signals.
The SortFilterProxyModel library is not used anymore, the only case
where we need to filter/sort something now is when the user interacts
with the "Filter rooms" or "Filter members" fields. These cases are
handled by a simple JS function.
We now keep separated room and timeline models for different accounts,
the previous approach of sharing all the data we could between accounts
created a lot of complications (local echoes, decrypted messages
replacing others, etc).
The users's own account profile changes are now hidden in the timeline.
On startup, if all events for a room were only own profile changes, more
events will be loaded.
Any kind of image format supported by Qt is now handled by the
pyotherside image provider, instead of just PNG/JPG.
SVGs which previously caused errors are supported as well.
The typing members bar paddings/margins are fixed.
The behavior of the avatar/"upload a profile picture" overlay is fixed.
Config files read from disk are now cached (TODO: make them reloadable
again).
Pylint is not used anymore because of all its annoying false warnings
and lack of understanding for dataclasses, it is replaced by flake8 with
a custom config and various plugins.
Debug mode is now considered on if the program was compiled with
the right option, instead of taking an argument from CLI.
When on, C++ will set a flag in the Window QML component.
The loading screen is now unloaded after the UI is ready, where
previously it just stayed in the background invisible and wasted CPU.
The overall refactoring and improvements make us now able to handle
rooms with thousand of members and no lazy-loading, where previously
everything would freeze and simply scrolling up to load past events
in any room would block the UI for a few seconds.
2019-08-11 22:01:22 +10:00
|
|
|
from .models.model_store import ModelStore
|
2020-07-19 08:33:57 +10:00
|
|
|
from .presence import Presence
|
2020-07-26 13:31:13 +10:00
|
|
|
from .sso_server import SSOServer
|
2020-02-12 07:22:05 +11:00
|
|
|
from .user_files import Accounts, History, Theme, UISettings, UIState
|
Big performance refactoring & various improvements
Instead of passing all sorts of events for the JS to handle and manually
add to different data models, we now handle everything we can in Python.
For any change, the python models send a sync event with their
contents (no more than 4 times per second) to JS, and the QSyncable
library's JsonListModel takes care of converting it to a QML ListModel
and sending the appropriate signals.
The SortFilterProxyModel library is not used anymore, the only case
where we need to filter/sort something now is when the user interacts
with the "Filter rooms" or "Filter members" fields. These cases are
handled by a simple JS function.
We now keep separated room and timeline models for different accounts,
the previous approach of sharing all the data we could between accounts
created a lot of complications (local echoes, decrypted messages
replacing others, etc).
The users's own account profile changes are now hidden in the timeline.
On startup, if all events for a room were only own profile changes, more
events will be loaded.
Any kind of image format supported by Qt is now handled by the
pyotherside image provider, instead of just PNG/JPG.
SVGs which previously caused errors are supported as well.
The typing members bar paddings/margins are fixed.
The behavior of the avatar/"upload a profile picture" overlay is fixed.
Config files read from disk are now cached (TODO: make them reloadable
again).
Pylint is not used anymore because of all its annoying false warnings
and lack of understanding for dataclasses, it is replaced by flake8 with
a custom config and various plugins.
Debug mode is now considered on if the program was compiled with
the right option, instead of taking an argument from CLI.
When on, C++ will set a flag in the Window QML component.
The loading screen is now unloaded after the UI is ready, where
previously it just stayed in the background invisible and wasted CPU.
The overall refactoring and improvements make us now able to handle
rooms with thousand of members and no lazy-loading, where previously
everything would freeze and simply scrolling up to load past events
in any room would block the UI for a few seconds.
2019-08-11 22:01:22 +10:00
|
|
|
|
2019-12-18 23:14:35 +11:00
|
|
|
# Logging configuration
|
2019-12-18 21:44:18 +11:00
|
|
|
log.getLogger().setLevel(log.INFO)
|
|
|
|
nio.logger_group.level = nio.log.logbook.ERROR
|
|
|
|
nio.log.logbook.StreamHandler(sys.stderr).push_application()
|
|
|
|
|
2019-06-27 16:31:03 +10:00
|
|
|
|
|
|
|
class Backend:
|
2019-12-19 07:24:43 +11:00
|
|
|
"""Manage matrix clients and provide other useful general methods.
|
|
|
|
|
|
|
|
Attributes:
|
2020-03-13 05:41:00 +11:00
|
|
|
saved_accounts: User config file for saved matrix account.
|
2019-12-19 07:24:43 +11:00
|
|
|
|
|
|
|
ui_settings: User config file for QML interface settings.
|
|
|
|
|
|
|
|
ui_state: User data file for saving/restoring QML UI state.
|
|
|
|
|
2020-03-13 05:41:00 +11:00
|
|
|
history: User data file for saving/restoring text typed into QML
|
2019-12-19 07:24:43 +11:00
|
|
|
components.
|
|
|
|
|
2020-03-13 05:41:00 +11:00
|
|
|
models: A mapping containing our data models that are
|
|
|
|
synchronized between the Python backend and the QML UI.
|
2019-12-19 07:24:43 +11:00
|
|
|
The models should only ever be modified from the backend.
|
|
|
|
|
2020-05-22 10:45:02 +10:00
|
|
|
If a non-existent key is accessed, it is created and an
|
2020-03-13 05:41:00 +11:00
|
|
|
associated `Model` and returned.
|
2019-12-19 07:24:43 +11:00
|
|
|
|
2020-03-13 05:41:00 +11:00
|
|
|
The mapping keys are the `Model`'s synchronization ID,
|
|
|
|
which a strings or tuple of strings.
|
2019-12-19 07:24:43 +11:00
|
|
|
|
|
|
|
Currently used sync ID throughout the code are:
|
|
|
|
|
2020-03-13 05:41:00 +11:00
|
|
|
- `"accounts"`: logged-in accounts;
|
2019-12-19 07:24:43 +11:00
|
|
|
|
2020-03-13 05:41:00 +11:00
|
|
|
- `("<user_id>", "rooms")`: rooms our account `user_id` is part of;
|
2019-12-19 07:24:43 +11:00
|
|
|
|
2020-03-13 05:41:00 +11:00
|
|
|
- `("<user_id>", "uploads")`: ongoing or failed file uploads for
|
|
|
|
our account `user_id`;
|
2019-12-19 07:24:43 +11:00
|
|
|
|
2020-03-13 05:41:00 +11:00
|
|
|
- `("<user_id>", "<room_id>", "members")`: members in the room
|
|
|
|
`room_id` that our account `user_id` is part of;
|
2019-12-19 07:24:43 +11:00
|
|
|
|
2020-03-13 05:41:00 +11:00
|
|
|
- `("<user_id>", "<room_id>", "events")`: state events and messages
|
|
|
|
in the room `room_id` that our account `user_id` is part of.
|
2019-12-19 07:24:43 +11:00
|
|
|
|
2020-05-22 10:45:02 +10:00
|
|
|
Special models:
|
|
|
|
|
|
|
|
- `"all_rooms"`: See `models.special_models.AllRooms` docstring
|
|
|
|
|
|
|
|
- `"matching_accounts"`
|
|
|
|
See `models.special_models.MatchingAccounts` docstring
|
|
|
|
|
|
|
|
- `("<user_id>", "<room_id>", "filtered_members")`:
|
|
|
|
See `models.special_models.FilteredMembers` docstring
|
|
|
|
|
2020-08-19 18:38:29 +10:00
|
|
|
- `("filtered_homeservers")`:
|
|
|
|
See `models.special_models.FilteredHomeservers` docstring
|
2020-05-22 10:45:02 +10:00
|
|
|
|
2020-03-13 05:41:00 +11:00
|
|
|
clients: A `{user_id: MatrixClient}` dict for the logged-in clients
|
|
|
|
we managed. Every client is logged to one matrix account.
|
2019-12-19 07:24:43 +11:00
|
|
|
|
|
|
|
media_cache: A matrix media cache for downloaded files.
|
2020-07-11 00:59:26 +10:00
|
|
|
|
|
|
|
presences: A `{user_id: Presence}` dict for storing presence info about
|
|
|
|
matrix users registered on Mirage.
|
2020-07-21 12:58:02 +10:00
|
|
|
|
|
|
|
mxc_events: A dict storing media `Event` model items for any account
|
|
|
|
that have the same mxc URI
|
2019-12-19 07:24:43 +11:00
|
|
|
"""
|
2019-12-18 23:14:35 +11:00
|
|
|
|
2019-12-18 21:44:18 +11:00
|
|
|
def __init__(self) -> None:
|
2019-12-18 21:55:05 +11:00
|
|
|
self.appdirs = AppDirs(appname=__app_name__, roaming=True)
|
2019-07-05 16:45:30 +10:00
|
|
|
|
2020-05-11 05:12:59 +10:00
|
|
|
self.saved_accounts = Accounts(self)
|
|
|
|
self.ui_settings = UISettings(self)
|
|
|
|
self.ui_state = UIState(self)
|
|
|
|
self.history = History(self)
|
2019-06-27 16:31:03 +10:00
|
|
|
|
2020-05-11 05:12:59 +10:00
|
|
|
self.models = ModelStore()
|
2020-05-11 04:26:26 +10:00
|
|
|
|
|
|
|
self.clients: Dict[str, MatrixClient] = {}
|
2019-07-05 16:45:30 +10:00
|
|
|
|
2020-07-26 13:31:13 +10:00
|
|
|
self._sso_server: Optional[SSOServer] = None
|
|
|
|
self._sso_server_task: Optional[asyncio.Future] = None
|
|
|
|
|
Big performance refactoring & various improvements
Instead of passing all sorts of events for the JS to handle and manually
add to different data models, we now handle everything we can in Python.
For any change, the python models send a sync event with their
contents (no more than 4 times per second) to JS, and the QSyncable
library's JsonListModel takes care of converting it to a QML ListModel
and sending the appropriate signals.
The SortFilterProxyModel library is not used anymore, the only case
where we need to filter/sort something now is when the user interacts
with the "Filter rooms" or "Filter members" fields. These cases are
handled by a simple JS function.
We now keep separated room and timeline models for different accounts,
the previous approach of sharing all the data we could between accounts
created a lot of complications (local echoes, decrypted messages
replacing others, etc).
The users's own account profile changes are now hidden in the timeline.
On startup, if all events for a room were only own profile changes, more
events will be loaded.
Any kind of image format supported by Qt is now handled by the
pyotherside image provider, instead of just PNG/JPG.
SVGs which previously caused errors are supported as well.
The typing members bar paddings/margins are fixed.
The behavior of the avatar/"upload a profile picture" overlay is fixed.
Config files read from disk are now cached (TODO: make them reloadable
again).
Pylint is not used anymore because of all its annoying false warnings
and lack of understanding for dataclasses, it is replaced by flake8 with
a custom config and various plugins.
Debug mode is now considered on if the program was compiled with
the right option, instead of taking an argument from CLI.
When on, C++ will set a flag in the Window QML component.
The loading screen is now unloaded after the UI is ready, where
previously it just stayed in the background invisible and wasted CPU.
The overall refactoring and improvements make us now able to handle
rooms with thousand of members and no lazy-loading, where previously
everything would freeze and simply scrolling up to load past events
in any room would block the UI for a few seconds.
2019-08-11 22:01:22 +10:00
|
|
|
self.profile_cache: Dict[str, nio.ProfileGetResponse] = {}
|
|
|
|
self.get_profile_locks: DefaultDict[str, asyncio.Lock] = \
|
2020-07-17 15:30:51 +10:00
|
|
|
DefaultDict(asyncio.Lock) # {user_id: lock}
|
2019-07-07 15:37:13 +10:00
|
|
|
|
2019-12-17 01:36:59 +11:00
|
|
|
self.send_locks: DefaultDict[str, asyncio.Lock] = \
|
2020-07-17 15:30:51 +10:00
|
|
|
DefaultDict(asyncio.Lock) # {room_id: lock}
|
2019-12-17 01:36:59 +11:00
|
|
|
|
2020-06-12 11:50:26 +10:00
|
|
|
cache_dir = Path(
|
|
|
|
os.environ.get("MIRAGE_CACHE_DIR") or self.appdirs.user_cache_dir,
|
|
|
|
)
|
|
|
|
|
2019-12-19 07:24:43 +11:00
|
|
|
self.media_cache: MediaCache = MediaCache(self, cache_dir)
|
2019-11-13 00:10:00 +11:00
|
|
|
|
2020-07-02 13:27:50 +10:00
|
|
|
self.presences: Dict[str, Presence] = {}
|
2020-07-02 06:55:03 +10:00
|
|
|
|
2020-07-17 14:46:46 +10:00
|
|
|
self.concurrent_get_presence_limit = asyncio.BoundedSemaphore(8)
|
|
|
|
|
2020-07-21 12:58:02 +10:00
|
|
|
self.mxc_events: DefaultDict[str, List[Event]] = DefaultDict(list)
|
|
|
|
|
2019-06-27 16:31:03 +10:00
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"{type(self).__name__}(clients={self.clients!r})"
|
|
|
|
|
|
|
|
|
|
|
|
# Clients management
|
|
|
|
|
2020-07-24 15:30:35 +10:00
|
|
|
async def server_info(self, homeserver: str) -> Tuple[str, List[str]]:
|
|
|
|
"""Return server's real URL and supported login flows.
|
|
|
|
|
|
|
|
Retrieving the real URL uses the `.well-known` API.
|
|
|
|
Possible login methods include `m.login.password` or `m.login.sso`.
|
|
|
|
"""
|
|
|
|
|
2020-08-19 16:14:46 +10:00
|
|
|
if not re.match(r"https?://", homeserver):
|
|
|
|
homeserver = f"http://{homeserver}"
|
|
|
|
|
|
|
|
client = MatrixClient(self, homeserver=homeserver)
|
|
|
|
http_re = re.compile("^http://")
|
|
|
|
is_local = urlparse(client.homeserver).netloc.split(":")[0] in (
|
|
|
|
"localhost", "127.0.0.1", "::1",
|
|
|
|
)
|
2020-07-24 15:30:35 +10:00
|
|
|
|
|
|
|
try:
|
2020-07-31 12:59:24 +10:00
|
|
|
client.homeserver = (await client.discovery_info()).homeserver_url
|
2020-08-19 16:14:46 +10:00
|
|
|
except MatrixError:
|
2020-07-24 15:30:35 +10:00
|
|
|
# This is either already the real URL, or an invalid URL.
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
2020-08-19 16:14:46 +10:00
|
|
|
try:
|
|
|
|
login_response = await client.login_info()
|
|
|
|
except (asyncio.TimeoutError, MatrixError):
|
|
|
|
# Maybe we still have a http URL and server only supports https
|
|
|
|
client.homeserver = http_re.sub("https://", client.homeserver)
|
|
|
|
login_response = await client.login_info()
|
|
|
|
|
|
|
|
# If we still have a http URL and server redirected to https
|
|
|
|
if login_response.transport_response.real_url.scheme == "https":
|
|
|
|
client.homeserver = http_re.sub("https://", client.homeserver)
|
|
|
|
|
|
|
|
# If we still have a http URL and server accept both http and https
|
|
|
|
if http_re.match(client.homeserver) and not is_local:
|
|
|
|
original = client.homeserver
|
|
|
|
client.homeserver = http_re.sub("https://", client.homeserver)
|
|
|
|
|
|
|
|
try:
|
|
|
|
await asyncio.wait_for(client.login_info(), timeout=6)
|
|
|
|
except (asyncio.TimeoutError, MatrixError):
|
|
|
|
client.homeserver = original
|
|
|
|
|
|
|
|
return (client.homeserver, login_response.flows)
|
2020-07-24 15:30:35 +10:00
|
|
|
finally:
|
|
|
|
await client.close()
|
|
|
|
|
|
|
|
|
2020-07-26 13:31:13 +10:00
|
|
|
async def password_auth(
|
|
|
|
self, user: str, password: str, homeserver: str,
|
2019-11-11 21:39:11 +11:00
|
|
|
) -> str:
|
2020-07-26 13:31:13 +10:00
|
|
|
"""Create & register a `MatrixClient`, login using the password
|
|
|
|
and return the user ID we get.
|
|
|
|
"""
|
2019-08-28 04:23:09 +10:00
|
|
|
|
2020-07-26 13:31:13 +10:00
|
|
|
client = MatrixClient(self, user=user, homeserver=homeserver)
|
|
|
|
return await self._do_login(client, password=password)
|
|
|
|
|
|
|
|
|
|
|
|
async def start_sso_auth(self, homeserver: str) -> str:
|
|
|
|
"""Start SSO server and return URL to open in the user's browser.
|
|
|
|
|
|
|
|
See the `sso_server.SSOServer` class documentation.
|
|
|
|
Once the returned URL has been opened in the user's browser
|
|
|
|
(done from QML), `MatrixClient.continue_sso_auth()` should be called.
|
|
|
|
"""
|
|
|
|
|
|
|
|
server = SSOServer(homeserver)
|
|
|
|
self._sso_server = server
|
|
|
|
self._sso_server_task = asyncio.ensure_future(server.wait_for_token())
|
|
|
|
return server.url_to_open
|
|
|
|
|
|
|
|
|
|
|
|
async def continue_sso_auth(self) -> str:
|
|
|
|
"""Wait for the started SSO server to get a token, then login.
|
|
|
|
|
|
|
|
`MatrixClient.start_sso_auth()` must be called first.
|
|
|
|
Creates and register a `MatrixClient` for logging in.
|
|
|
|
Returns the user ID we get from logging in.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not self._sso_server or not self._sso_server_task:
|
|
|
|
raise RuntimeError("Must call Backend.start_sso_auth() first")
|
|
|
|
|
|
|
|
await self._sso_server_task
|
|
|
|
homeserver = self._sso_server.for_homeserver
|
|
|
|
token = self._sso_server_task.result()
|
|
|
|
self._sso_server_task = None
|
|
|
|
self._sso_server = None
|
|
|
|
|
|
|
|
client = MatrixClient(self, homeserver=homeserver)
|
|
|
|
return await self._do_login(client, token=token)
|
|
|
|
|
|
|
|
|
|
|
|
async def _do_login(self, client: MatrixClient, **login_kwargs) -> str:
|
2020-07-27 13:40:15 +10:00
|
|
|
"""Login on a client. If successful, register it and return user ID."""
|
2019-08-28 04:23:09 +10:00
|
|
|
|
2019-08-17 06:30:18 +10:00
|
|
|
try:
|
2020-07-26 13:31:13 +10:00
|
|
|
await client.login(**login_kwargs)
|
2019-11-11 21:39:11 +11:00
|
|
|
except MatrixError:
|
2019-08-28 04:23:09 +10:00
|
|
|
await client.close()
|
2019-11-11 21:39:11 +11:00
|
|
|
raise
|
2019-08-17 06:30:18 +10:00
|
|
|
|
2020-06-29 22:07:40 +10:00
|
|
|
if client.user_id in self.clients:
|
2020-06-28 22:46:34 +10:00
|
|
|
await client.logout()
|
|
|
|
return client.user_id
|
|
|
|
|
2020-07-10 06:06:14 +10:00
|
|
|
self.clients[client.user_id] = client
|
2019-11-11 21:39:11 +11:00
|
|
|
return client.user_id
|
2019-06-27 16:31:03 +10:00
|
|
|
|
|
|
|
|
2020-05-14 17:33:34 +10:00
|
|
|
async def resume_client(
|
|
|
|
self,
|
|
|
|
user_id: str,
|
|
|
|
token: str,
|
|
|
|
device_id: str,
|
2020-07-26 13:31:13 +10:00
|
|
|
homeserver: str,
|
2020-07-03 07:28:41 +10:00
|
|
|
state: str = "online",
|
2020-07-17 06:09:14 +10:00
|
|
|
status_msg: str = "",
|
2020-05-14 17:33:34 +10:00
|
|
|
) -> None:
|
2019-12-18 23:14:35 +11:00
|
|
|
"""Create and register a `MatrixClient` with known account details."""
|
Big performance refactoring & various improvements
Instead of passing all sorts of events for the JS to handle and manually
add to different data models, we now handle everything we can in Python.
For any change, the python models send a sync event with their
contents (no more than 4 times per second) to JS, and the QSyncable
library's JsonListModel takes care of converting it to a QML ListModel
and sending the appropriate signals.
The SortFilterProxyModel library is not used anymore, the only case
where we need to filter/sort something now is when the user interacts
with the "Filter rooms" or "Filter members" fields. These cases are
handled by a simple JS function.
We now keep separated room and timeline models for different accounts,
the previous approach of sharing all the data we could between accounts
created a lot of complications (local echoes, decrypted messages
replacing others, etc).
The users's own account profile changes are now hidden in the timeline.
On startup, if all events for a room were only own profile changes, more
events will be loaded.
Any kind of image format supported by Qt is now handled by the
pyotherside image provider, instead of just PNG/JPG.
SVGs which previously caused errors are supported as well.
The typing members bar paddings/margins are fixed.
The behavior of the avatar/"upload a profile picture" overlay is fixed.
Config files read from disk are now cached (TODO: make them reloadable
again).
Pylint is not used anymore because of all its annoying false warnings
and lack of understanding for dataclasses, it is replaced by flake8 with
a custom config and various plugins.
Debug mode is now considered on if the program was compiled with
the right option, instead of taking an argument from CLI.
When on, C++ will set a flag in the Window QML component.
The loading screen is now unloaded after the UI is ready, where
previously it just stayed in the background invisible and wasted CPU.
The overall refactoring and improvements make us now able to handle
rooms with thousand of members and no lazy-loading, where previously
everything would freeze and simply scrolling up to load past events
in any room would block the UI for a few seconds.
2019-08-11 22:01:22 +10:00
|
|
|
|
2019-06-27 16:31:03 +10:00
|
|
|
client = MatrixClient(
|
2020-07-27 13:45:28 +10:00
|
|
|
self, user=user_id, homeserver=homeserver, device_id=device_id,
|
2019-06-27 16:31:03 +10:00
|
|
|
)
|
Big performance refactoring & various improvements
Instead of passing all sorts of events for the JS to handle and manually
add to different data models, we now handle everything we can in Python.
For any change, the python models send a sync event with their
contents (no more than 4 times per second) to JS, and the QSyncable
library's JsonListModel takes care of converting it to a QML ListModel
and sending the appropriate signals.
The SortFilterProxyModel library is not used anymore, the only case
where we need to filter/sort something now is when the user interacts
with the "Filter rooms" or "Filter members" fields. These cases are
handled by a simple JS function.
We now keep separated room and timeline models for different accounts,
the previous approach of sharing all the data we could between accounts
created a lot of complications (local echoes, decrypted messages
replacing others, etc).
The users's own account profile changes are now hidden in the timeline.
On startup, if all events for a room were only own profile changes, more
events will be loaded.
Any kind of image format supported by Qt is now handled by the
pyotherside image provider, instead of just PNG/JPG.
SVGs which previously caused errors are supported as well.
The typing members bar paddings/margins are fixed.
The behavior of the avatar/"upload a profile picture" overlay is fixed.
Config files read from disk are now cached (TODO: make them reloadable
again).
Pylint is not used anymore because of all its annoying false warnings
and lack of understanding for dataclasses, it is replaced by flake8 with
a custom config and various plugins.
Debug mode is now considered on if the program was compiled with
the right option, instead of taking an argument from CLI.
When on, C++ will set a flag in the Window QML component.
The loading screen is now unloaded after the UI is ready, where
previously it just stayed in the background invisible and wasted CPU.
The overall refactoring and improvements make us now able to handle
rooms with thousand of members and no lazy-loading, where previously
everything would freeze and simply scrolling up to load past events
in any room would block the UI for a few seconds.
2019-08-11 22:01:22 +10:00
|
|
|
|
2020-07-10 06:06:14 +10:00
|
|
|
self.clients[user_id] = client
|
2019-12-16 22:02:42 +11:00
|
|
|
|
2020-07-17 06:09:14 +10:00
|
|
|
await client.resume(user_id, token, device_id, state, status_msg)
|
2019-06-27 16:31:03 +10:00
|
|
|
|
|
|
|
|
2019-12-03 07:29:29 +11:00
|
|
|
async def load_saved_accounts(self) -> List[str]:
|
2019-12-18 23:14:35 +11:00
|
|
|
"""Call `resume_client` for all saved accounts in user config."""
|
|
|
|
|
2020-05-14 17:33:34 +10:00
|
|
|
async def resume(user_id: str, info: Dict[str, Any]) -> str:
|
2020-07-10 06:06:14 +10:00
|
|
|
# Get or create account model
|
|
|
|
self.models["accounts"].setdefault(
|
|
|
|
user_id, Account(user_id, info.get("order", -1)),
|
|
|
|
)
|
|
|
|
|
2019-06-27 16:31:03 +10:00
|
|
|
await self.resume_client(
|
|
|
|
user_id = user_id,
|
|
|
|
token = info["token"],
|
|
|
|
device_id = info["device_id"],
|
|
|
|
homeserver = info["homeserver"],
|
2020-07-03 07:28:41 +10:00
|
|
|
state = info.get("presence", "online"),
|
2020-07-17 06:09:14 +10:00
|
|
|
status_msg = info.get("status_msg", ""),
|
2019-06-27 16:31:03 +10:00
|
|
|
)
|
2020-07-10 06:06:14 +10:00
|
|
|
|
2019-06-27 16:31:03 +10:00
|
|
|
return user_id
|
|
|
|
|
|
|
|
return await asyncio.gather(*(
|
2019-07-19 10:30:41 +10:00
|
|
|
resume(uid, info)
|
|
|
|
for uid, info in (await self.saved_accounts.read()).items()
|
2020-04-06 05:04:40 +10:00
|
|
|
if info.get("enabled", True)
|
2019-06-27 16:31:03 +10:00
|
|
|
))
|
|
|
|
|
|
|
|
|
2019-07-19 10:30:41 +10:00
|
|
|
async def logout_client(self, user_id: str) -> None:
|
2019-12-18 23:14:35 +11:00
|
|
|
"""Log a `MatrixClient` out and unregister it from our models."""
|
|
|
|
|
2019-07-19 10:30:41 +10:00
|
|
|
client = self.clients.pop(user_id, None)
|
2020-06-29 23:22:04 +10:00
|
|
|
|
2019-07-19 10:30:41 +10:00
|
|
|
if client:
|
2020-09-05 01:21:36 +10:00
|
|
|
try:
|
|
|
|
await client.logout()
|
|
|
|
except MatrixInvalidAccessToken:
|
|
|
|
pass
|
|
|
|
|
2019-12-03 07:29:29 +11:00
|
|
|
self.models["accounts"].pop(user_id, None)
|
2020-06-29 23:22:04 +10:00
|
|
|
self.models["matching_accounts"].pop(user_id, None)
|
|
|
|
self.models[user_id, "uploads"].clear()
|
|
|
|
|
|
|
|
for room_id in self.models[user_id, "rooms"]:
|
|
|
|
self.models["all_rooms"].pop(room_id, None)
|
|
|
|
self.models[user_id, room_id, "members"].clear()
|
|
|
|
self.models[user_id, room_id, "events"].clear()
|
|
|
|
self.models[user_id, room_id, "filtered_members"].clear()
|
|
|
|
|
|
|
|
self.models[user_id, "rooms"].clear()
|
|
|
|
|
2019-08-22 07:45:05 +10:00
|
|
|
await self.saved_accounts.delete(user_id)
|
2019-07-04 14:24:21 +10:00
|
|
|
|
|
|
|
|
2020-07-17 13:34:35 +10:00
|
|
|
async def terminate_clients(self) -> None:
|
|
|
|
"""Call every `MatrixClient`'s `terminate()` method."""
|
|
|
|
|
|
|
|
log.info("Setting clients offline...")
|
|
|
|
tasks = [client.terminate() for client in self.clients.values()]
|
|
|
|
await asyncio.gather(*tasks)
|
|
|
|
|
|
|
|
|
2019-12-18 23:14:35 +11:00
|
|
|
async def get_client(self, user_id: str) -> MatrixClient:
|
|
|
|
"""Wait until a `MatrixClient` is registered in model and return it."""
|
|
|
|
|
|
|
|
failures = 0
|
|
|
|
|
2019-11-04 21:56:26 +11:00
|
|
|
while True:
|
|
|
|
if user_id in self.clients:
|
2019-12-18 23:14:35 +11:00
|
|
|
return self.clients[user_id]
|
2019-07-21 22:38:49 +10:00
|
|
|
|
2019-12-18 23:14:35 +11:00
|
|
|
if failures and failures % 100 == 0: # every 10s except first time
|
|
|
|
log.warning(
|
|
|
|
"Client %r not found after %ds, stack trace:\n%s",
|
|
|
|
user_id, failures / 10, traceback.format_stack(),
|
|
|
|
)
|
2019-11-04 04:48:12 +11:00
|
|
|
|
2019-07-21 22:38:49 +10:00
|
|
|
await asyncio.sleep(0.1)
|
2019-12-18 23:14:35 +11:00
|
|
|
failures += 1
|
2019-11-13 00:10:00 +11:00
|
|
|
|
|
|
|
|
2020-08-24 06:57:53 +10:00
|
|
|
# Multi-client Matrix functions
|
2019-12-18 23:14:35 +11:00
|
|
|
|
2020-06-01 23:25:09 +10:00
|
|
|
async def update_room_read_marker(
|
|
|
|
self, room_id: str, event_id: str,
|
|
|
|
) -> None:
|
|
|
|
"""Update room's read marker to an event for all accounts part of it.
|
2020-04-17 03:24:49 +10:00
|
|
|
"""
|
|
|
|
|
2020-06-01 23:25:09 +10:00
|
|
|
async def update(client: MatrixClient) -> None:
|
2020-07-08 00:42:16 +10:00
|
|
|
room = self.models[client.user_id, "rooms"].get(room_id)
|
|
|
|
account = self.models["accounts"][client.user_id]
|
2020-07-02 02:00:50 +10:00
|
|
|
|
|
|
|
if room:
|
|
|
|
room.set_fields(
|
|
|
|
unreads = 0,
|
|
|
|
highlights = 0,
|
|
|
|
local_unreads = False,
|
|
|
|
local_highlights = False,
|
|
|
|
)
|
2020-06-01 23:25:09 +10:00
|
|
|
await client.update_account_unread_counts()
|
2020-07-08 00:42:16 +10:00
|
|
|
|
|
|
|
# Only update server markers if the account is not invisible
|
|
|
|
if account.presence != Presence.State.invisible:
|
|
|
|
await client.update_receipt_marker(room_id, event_id)
|
2020-05-01 18:46:08 +10:00
|
|
|
|
2020-06-01 23:25:09 +10:00
|
|
|
await asyncio.gather(*[update(c) for c in self.clients.values()])
|
2020-04-17 03:24:49 +10:00
|
|
|
|
|
|
|
|
2020-08-24 06:57:53 +10:00
|
|
|
async def verify_device(
|
|
|
|
self, user_id: str, device_id: str, ed25519_key: str,
|
|
|
|
) -> None:
|
|
|
|
"""Mark a device as verified on all our accounts."""
|
|
|
|
|
|
|
|
for client in self.clients.values():
|
|
|
|
try:
|
|
|
|
device = client.device_store[user_id][device_id]
|
|
|
|
except KeyError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if device.ed25519 == ed25519_key:
|
|
|
|
client.verify_device(device)
|
|
|
|
|
|
|
|
|
|
|
|
async def blacklist_device(
|
|
|
|
self, user_id: str, device_id: str, ed25519_key: str,
|
|
|
|
) -> None:
|
|
|
|
"""Mark a device as blacklisted on all our accounts."""
|
|
|
|
|
|
|
|
for client in self.clients.values():
|
|
|
|
try:
|
|
|
|
# This won't include the client's current device, as expected
|
|
|
|
device = client.device_store[user_id][device_id]
|
|
|
|
except KeyError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if device.ed25519 == ed25519_key:
|
|
|
|
client.blacklist_device(device)
|
|
|
|
|
|
|
|
|
2019-12-18 23:14:35 +11:00
|
|
|
# General functions
|
|
|
|
|
2020-03-13 13:16:33 +11:00
|
|
|
async def get_config_dir(self) -> Path:
|
|
|
|
return Path(self.appdirs.user_config_dir)
|
|
|
|
|
|
|
|
|
2019-12-18 23:14:35 +11:00
|
|
|
async def load_settings(self) -> tuple:
|
|
|
|
"""Return parsed user config files."""
|
|
|
|
|
|
|
|
settings = await self.ui_settings.read()
|
|
|
|
ui_state = await self.ui_state.read()
|
|
|
|
history = await self.history.read()
|
|
|
|
theme = await Theme(self, settings["theme"]).read()
|
|
|
|
|
2020-05-14 10:22:48 +10:00
|
|
|
state_data = self.ui_state._data
|
|
|
|
if state_data:
|
|
|
|
for user, collapse in state_data["collapseAccounts"].items():
|
|
|
|
self.models["all_rooms"].set_account_collapse(user, collapse)
|
|
|
|
|
2019-12-18 23:14:35 +11:00
|
|
|
return (settings, ui_state, history, theme)
|
|
|
|
|
|
|
|
|
2020-08-21 02:21:47 +10:00
|
|
|
async def set_string_filter(self, model_id: SyncId, value: str) -> None:
|
|
|
|
"""Set a FieldStringFilter (or derived class) model's filter property.
|
2020-05-22 10:45:02 +10:00
|
|
|
|
|
|
|
This should only be called from QML.
|
|
|
|
"""
|
|
|
|
|
2020-05-11 04:58:59 +10:00
|
|
|
if isinstance(model_id, list): # QML can't pass tuples
|
|
|
|
model_id = tuple(model_id)
|
|
|
|
|
2020-05-06 15:49:25 +10:00
|
|
|
model = Model.proxies[model_id]
|
|
|
|
|
2020-08-21 02:21:47 +10:00
|
|
|
if not isinstance(model, FieldStringFilter):
|
|
|
|
raise TypeError("model_id must point to a FieldStringFilter")
|
2020-05-06 15:49:25 +10:00
|
|
|
|
|
|
|
model.filter = value
|
2020-05-14 10:22:48 +10:00
|
|
|
|
|
|
|
|
|
|
|
async def set_account_collapse(self, user_id: str, collapse: bool) -> None:
|
2020-05-22 10:45:02 +10:00
|
|
|
"""Call `set_account_collapse()` on the `all_rooms` model.
|
|
|
|
|
|
|
|
This should only be called from QML.
|
|
|
|
"""
|
2020-05-14 10:22:48 +10:00
|
|
|
self.models["all_rooms"].set_account_collapse(user_id, collapse)
|
2020-07-10 02:43:21 +10:00
|
|
|
|
|
|
|
|
2020-08-19 14:17:24 +10:00
|
|
|
async def _ping_homeserver(
|
|
|
|
self, session: aiohttp.ClientSession, homeserver_url: str,
|
|
|
|
) -> None:
|
|
|
|
"""Ping a homeserver present in our model and set its `ping` field."""
|
|
|
|
|
|
|
|
item = self.models["homeservers"][homeserver_url]
|
|
|
|
times = []
|
|
|
|
|
|
|
|
for i in range(16):
|
|
|
|
start = time.time()
|
|
|
|
|
|
|
|
try:
|
|
|
|
await session.get(f"{homeserver_url}/_matrix/client/versions")
|
|
|
|
except Exception as err:
|
|
|
|
log.warning("Failed pinging %s: %r", homeserver_url, err)
|
|
|
|
item.status = PingStatus.Failed
|
|
|
|
return
|
|
|
|
|
|
|
|
times.append(round((time.time() - start) * 1000))
|
|
|
|
|
|
|
|
if i == 7 or i == 15:
|
|
|
|
item.set_fields(
|
|
|
|
ping=sum(times) // len(times), status=PingStatus.Done,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-08-23 03:10:04 +10:00
|
|
|
def _get_homeserver_stability(self, logs: List[Dict[str, Any]]) -> float:
|
2020-08-19 14:17:24 +10:00
|
|
|
stability = 100.0
|
|
|
|
|
|
|
|
for period in logs:
|
2020-08-23 03:10:04 +10:00
|
|
|
started_at = datetime.fromtimestamp(period["datetime"])
|
2020-08-19 14:17:24 +10:00
|
|
|
time_since_now = datetime.now() - started_at
|
|
|
|
|
2020-08-23 03:10:04 +10:00
|
|
|
if time_since_now.days > 30 or period["type"] != 1: # 1 = downtime
|
2020-08-19 14:17:24 +10:00
|
|
|
continue
|
|
|
|
|
2020-08-23 03:10:04 +10:00
|
|
|
lasted_minutes = period["duration"] / 60
|
|
|
|
|
2020-08-19 14:17:24 +10:00
|
|
|
stability -= (
|
2020-08-23 03:10:04 +10:00
|
|
|
(lasted_minutes * stability / 1000) /
|
2020-08-19 14:17:24 +10:00
|
|
|
max(1, time_since_now.days / 3)
|
|
|
|
)
|
|
|
|
|
2020-08-23 03:10:04 +10:00
|
|
|
return stability
|
2020-08-19 14:17:24 +10:00
|
|
|
|
|
|
|
|
2020-08-23 03:10:04 +10:00
|
|
|
async def fetch_homeservers(self) -> None:
|
|
|
|
"""Retrieve a list of public homeservers and add them to our model."""
|
|
|
|
|
|
|
|
api_list = "https://publiclist.anchel.nl/publiclist.json"
|
|
|
|
tmout = aiohttp.ClientTimeout(total=20)
|
|
|
|
session = aiohttp.ClientSession(raise_for_status=True, timeout=tmout)
|
|
|
|
response = await session.get(api_list)
|
2020-08-23 03:19:38 +10:00
|
|
|
coros = []
|
2020-08-23 03:10:04 +10:00
|
|
|
|
|
|
|
for server in (await response.json()):
|
|
|
|
homeserver_url = server["homeserver"]
|
|
|
|
|
|
|
|
if homeserver_url.startswith("http://"): # insecure server
|
|
|
|
continue
|
2020-08-19 14:17:24 +10:00
|
|
|
|
|
|
|
if not re.match(r"^https?://.+", homeserver_url):
|
|
|
|
homeserver_url = f"https://{homeserver_url}"
|
|
|
|
|
2020-08-23 03:10:04 +10:00
|
|
|
if server["country"] == "USA":
|
|
|
|
server["country"] = "United States"
|
2020-08-19 14:17:24 +10:00
|
|
|
|
2020-08-23 03:10:04 +10:00
|
|
|
self.models["homeservers"][homeserver_url] = Homeserver(
|
|
|
|
id = homeserver_url,
|
|
|
|
name = server["name"],
|
|
|
|
site_url = server["url"],
|
|
|
|
country = server["country"],
|
|
|
|
stability =
|
|
|
|
self._get_homeserver_stability(server["monitor"]["logs"]),
|
|
|
|
)
|
2020-08-19 14:17:24 +10:00
|
|
|
|
2020-08-23 03:19:38 +10:00
|
|
|
coros.append(self._ping_homeserver(session, homeserver_url))
|
|
|
|
|
|
|
|
await asyncio.gather(*coros)
|
|
|
|
await session.close()
|