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
|
2019-12-18 21:44:18 +11:00
|
|
|
import sys
|
2019-12-18 23:14:35 +11:00
|
|
|
import traceback
|
2019-11-13 00:10:00 +11:00
|
|
|
from pathlib import Path
|
2019-12-03 07:29:29 +11:00
|
|
|
from typing import Any, DefaultDict, Dict, List, Optional
|
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
|
|
|
import nio
|
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
|
|
|
|
2019-12-18 21:55:05 +11:00
|
|
|
from . import __app_name__
|
2019-11-12 23:47:03 +11:00
|
|
|
from .errors import MatrixError
|
|
|
|
from .matrix_client import MatrixClient
|
2019-12-03 07:29:29 +11:00
|
|
|
from .models import SyncId
|
|
|
|
from .models.items import Account
|
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
|
|
|
|
|
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:
|
|
|
|
saved_accounts: User config file for saved matrix account details.
|
|
|
|
|
|
|
|
ui_settings: User config file for QML interface settings.
|
|
|
|
|
|
|
|
ui_state: User data file for saving/restoring QML UI state.
|
|
|
|
|
|
|
|
history: User data file for saving/restoring lines typed into QML
|
|
|
|
components.
|
|
|
|
|
|
|
|
models: A dict containing our data models that are synchronized between
|
|
|
|
the Python backend and the QML UI.
|
|
|
|
The models should only ever be modified from the backend.
|
|
|
|
|
|
|
|
The dict keys are the `Model`'s synchronization ID,
|
|
|
|
which are in one of these form:
|
|
|
|
|
|
|
|
- A `ModelItem` type, the type of item that this model stores;
|
|
|
|
- A `(ModelItem, str...)` tuple.
|
|
|
|
|
|
|
|
Currently used sync ID throughout the code are:
|
|
|
|
|
|
|
|
- `Account`: logged-in accounts;
|
|
|
|
|
|
|
|
- `(Room, "<user_id>")`: rooms a `user_id` account is part of;
|
|
|
|
|
|
|
|
- `(Upload, "<user_id>")`: ongoing or failed file uploads for a
|
|
|
|
`user_id` account;
|
|
|
|
|
|
|
|
- `(Member, "<user_id>", "<room_id>")`: members in the room
|
|
|
|
`room_id` that account `user_id` is part of;
|
|
|
|
|
|
|
|
- `(Event, "<user_id>", "<room_id>")`: state events and messages
|
|
|
|
in the room `room_id` that account `user_id` is part of.
|
|
|
|
|
|
|
|
clients: A dict containing the logged `MatrixClient` objects we manage.
|
|
|
|
Each client represents a logged-in account,
|
|
|
|
identified by its `user_id`.
|
|
|
|
|
|
|
|
media_cache: A matrix media cache for downloaded files.
|
|
|
|
"""
|
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
|
|
|
|
2019-12-19 07:24:43 +11:00
|
|
|
from .user_files import Accounts, UISettings, UIState, History
|
|
|
|
self.saved_accounts: Accounts = Accounts(self)
|
|
|
|
self.ui_settings: UISettings = UISettings(self)
|
|
|
|
self.ui_state: UIState = UIState(self)
|
|
|
|
self.history: History = History(self)
|
2019-06-27 16:31:03 +10:00
|
|
|
|
2019-12-19 07:24:43 +11:00
|
|
|
self.models: ModelStore = ModelStore()
|
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.clients: Dict[str, MatrixClient] = {}
|
2019-07-05 16:45:30 +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
|
|
|
self.profile_cache: Dict[str, nio.ProfileGetResponse] = {}
|
|
|
|
self.get_profile_locks: DefaultDict[str, asyncio.Lock] = \
|
|
|
|
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] = \
|
|
|
|
DefaultDict(asyncio.Lock) # {room_id: lock}
|
|
|
|
|
2019-11-13 00:10:00 +11:00
|
|
|
from .media_cache import MediaCache
|
2019-12-19 07:24:43 +11:00
|
|
|
cache_dir = Path(self.appdirs.user_cache_dir)
|
|
|
|
self.media_cache: MediaCache = MediaCache(self, cache_dir)
|
2019-11-13 00:10:00 +11:00
|
|
|
|
2019-06-27 16:31:03 +10:00
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"{type(self).__name__}(clients={self.clients!r})"
|
|
|
|
|
|
|
|
|
|
|
|
# Clients management
|
|
|
|
|
|
|
|
async def login_client(self,
|
2019-08-17 06:30:18 +10:00
|
|
|
user: str,
|
|
|
|
password: str,
|
|
|
|
device_id: Optional[str] = None,
|
2019-09-08 07:24:58 +10:00
|
|
|
homeserver: str = "https://matrix.org",
|
2019-11-11 21:39:11 +11:00
|
|
|
) -> str:
|
2019-12-18 23:14:35 +11:00
|
|
|
"""Create and register a `MatrixClient`, login and return a user ID."""
|
2019-08-28 04:23:09 +10:00
|
|
|
|
2019-06-27 16:31:03 +10:00
|
|
|
client = MatrixClient(
|
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, user=user, homeserver=homeserver, device_id=device_id,
|
2019-06-27 16:31:03 +10:00
|
|
|
)
|
2019-08-28 04:23:09 +10:00
|
|
|
|
2019-08-17 06:30:18 +10:00
|
|
|
try:
|
|
|
|
await client.login(password)
|
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
|
|
|
|
2019-12-03 07:29:29 +11:00
|
|
|
self.clients[client.user_id] = client
|
|
|
|
self.models["accounts"][client.user_id] = Account(client.user_id)
|
2019-11-11 21:39:11 +11:00
|
|
|
return client.user_id
|
2019-06-27 16:31:03 +10:00
|
|
|
|
|
|
|
|
|
|
|
async def resume_client(self,
|
|
|
|
user_id: str,
|
|
|
|
token: str,
|
|
|
|
device_id: str,
|
|
|
|
homeserver: str = "https://matrix.org") -> 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(
|
2019-07-05 09:11:22 +10:00
|
|
|
backend=self,
|
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
|
|
|
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
|
|
|
|
2019-12-03 07:29:29 +11:00
|
|
|
self.clients[user_id] = client
|
|
|
|
self.models["accounts"][user_id] = Account(user_id)
|
2019-12-16 22:02:42 +11:00
|
|
|
|
|
|
|
await client.resume(user_id=user_id, token=token, device_id=device_id)
|
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."""
|
|
|
|
|
2019-06-27 16:31:03 +10:00
|
|
|
async def resume(user_id: str, info: Dict[str, str]) -> str:
|
|
|
|
await self.resume_client(
|
|
|
|
user_id = user_id,
|
|
|
|
token = info["token"],
|
|
|
|
device_id = info["device_id"],
|
|
|
|
homeserver = info["homeserver"],
|
|
|
|
)
|
|
|
|
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()
|
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)
|
|
|
|
if client:
|
2019-12-03 07:29:29 +11:00
|
|
|
self.models["accounts"].pop(user_id, None)
|
2019-07-19 10:30:41 +10:00
|
|
|
await client.logout()
|
2019-06-27 16:31:03 +10:00
|
|
|
|
2019-08-22 07:45:05 +10:00
|
|
|
await self.saved_accounts.delete(user_id)
|
2019-07-04 14:24:21 +10:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2019-12-18 23:14:35 +11:00
|
|
|
async def get_any_client(self) -> MatrixClient:
|
|
|
|
"""Return any healthy syncing `MatrixClient` registered in model."""
|
2019-11-13 00:10:00 +11:00
|
|
|
|
2019-12-16 22:02:42 +11:00
|
|
|
failures = 0
|
|
|
|
|
2019-11-13 00:10:00 +11:00
|
|
|
while True:
|
2019-12-16 22:02:42 +11:00
|
|
|
for client in self.clients.values():
|
|
|
|
if client.syncing:
|
|
|
|
return client
|
|
|
|
|
|
|
|
if failures and failures % 300 == 0:
|
|
|
|
log.warn(
|
2019-12-18 23:14:35 +11:00
|
|
|
"No healthy client found after %ds, stack trace:\n%s",
|
|
|
|
failures / 10, traceback.format_stack(),
|
2019-12-16 22:02:42 +11:00
|
|
|
)
|
2019-11-13 00:10:00 +11:00
|
|
|
|
2019-12-18 23:14:35 +11:00
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
failures += 1
|
|
|
|
|
|
|
|
|
|
|
|
# Client functions that don't need authentification
|
2019-11-13 00:10:00 +11:00
|
|
|
|
|
|
|
async def get_profile(self, user_id: str) -> nio.ProfileGetResponse:
|
2019-12-18 23:14:35 +11:00
|
|
|
"""Cache and return the matrix profile of `user_id`."""
|
|
|
|
|
2019-11-13 00:10:00 +11:00
|
|
|
if user_id in self.profile_cache:
|
|
|
|
return self.profile_cache[user_id]
|
|
|
|
|
|
|
|
async with self.get_profile_locks[user_id]:
|
2019-12-18 23:14:35 +11:00
|
|
|
client = await self.get_any_client()
|
2019-11-15 07:20:30 +11:00
|
|
|
response = await client.get_profile(user_id)
|
2019-11-13 00:10:00 +11:00
|
|
|
|
|
|
|
self.profile_cache[user_id] = response
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
async def thumbnail(
|
|
|
|
self, server_name: str, media_id: str, width: int, height: int,
|
|
|
|
) -> nio.ThumbnailResponse:
|
2019-12-18 23:14:35 +11:00
|
|
|
"""Return thumbnail for a matrix media."""
|
2019-11-13 00:10:00 +11:00
|
|
|
|
2019-12-26 23:16:04 +11:00
|
|
|
args = (server_name, media_id, width, height)
|
|
|
|
client = await self.get_any_client()
|
|
|
|
return await client.thumbnail(*args)
|
2019-11-13 00:10:00 +11:00
|
|
|
|
|
|
|
|
|
|
|
async def download(
|
|
|
|
self, server_name: str, media_id: str,
|
|
|
|
) -> nio.DownloadResponse:
|
2019-12-18 23:14:35 +11:00
|
|
|
"""Return the content of a matrix media."""
|
2019-11-13 00:10:00 +11:00
|
|
|
|
2019-12-26 23:16:04 +11:00
|
|
|
client = await self.get_any_client()
|
|
|
|
return await client.download(server_name, media_id)
|
2019-12-18 23:14:35 +11:00
|
|
|
|
|
|
|
|
|
|
|
# General functions
|
|
|
|
|
|
|
|
async def load_settings(self) -> tuple:
|
|
|
|
"""Return parsed user config files."""
|
|
|
|
|
2019-12-18 23:41:02 +11:00
|
|
|
from .user_files import Theme
|
2019-12-18 23:14:35 +11:00
|
|
|
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()
|
|
|
|
|
|
|
|
return (settings, ui_state, history, theme)
|
|
|
|
|
|
|
|
|
2019-12-03 07:29:29 +11:00
|
|
|
async def await_model_item(
|
|
|
|
self, model_id: SyncId, item_id: Any,
|
|
|
|
) -> Dict[str, Any]:
|
2019-12-18 23:14:35 +11:00
|
|
|
|
2019-12-03 07:29:29 +11:00
|
|
|
if isinstance(model_id, list): # when called from QML
|
|
|
|
model_id = tuple(model_id)
|
2019-12-18 23:14:35 +11:00
|
|
|
|
2019-12-03 07:29:29 +11:00
|
|
|
failures = 0
|
2019-12-18 23:14:35 +11:00
|
|
|
|
2019-12-03 07:29:29 +11:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
return self.models[model_id][item_id].serialized
|
|
|
|
except KeyError:
|
|
|
|
if failures and failures % 300 == 0:
|
|
|
|
log.warn(
|
|
|
|
"Item %r not found in model %r after %ds",
|
|
|
|
item_id, model_id, failures / 10,
|
|
|
|
)
|
|
|
|
|
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
failures += 1
|