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-07-04 14:24:21 +10:00
|
|
|
import random
|
2019-08-18 12:46:54 +10:00
|
|
|
from typing import Any, DefaultDict, Dict, List, Optional, Tuple, Union
|
2019-07-24 16:14:34 +10:00
|
|
|
|
|
|
|
import hsluv
|
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-06-27 16:31:03 +10:00
|
|
|
from .app import App
|
2019-11-11 21:39:11 +11:00
|
|
|
from .matrix_client import MatrixClient, MatrixError
|
2019-11-06 09:31:16 +11:00
|
|
|
from .models.items import Account, Device, Event, Member, Room, Upload
|
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
|
|
|
|
|
|
|
|
ProfileResponse = Union[nio.ProfileGetResponse, nio.ProfileGetError]
|
2019-06-27 16:31:03 +10:00
|
|
|
|
|
|
|
|
|
|
|
class Backend:
|
|
|
|
def __init__(self, app: App) -> None:
|
|
|
|
self.app = app
|
2019-07-05 16:45:30 +10:00
|
|
|
|
2019-07-19 10:30:41 +10:00
|
|
|
from . import config_files
|
|
|
|
self.saved_accounts = config_files.Accounts(self)
|
|
|
|
self.ui_settings = config_files.UISettings(self)
|
2019-07-21 21:14:16 +10:00
|
|
|
self.ui_state = config_files.UIState(self)
|
2019-07-19 10:30:41 +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.models = ModelStore(allowed_key_types={
|
|
|
|
Account, # Logged-in accounts
|
|
|
|
(Device, str), # Devices of user_id
|
|
|
|
(Room, str), # Rooms for user_id
|
|
|
|
(Member, str), # Members in room_id
|
2019-11-06 09:31:16 +11:00
|
|
|
(Upload, str), # Uploads running in room_id
|
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
|
|
|
(Event, str, str), # Events for account user_id for room_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
|
|
|
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-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-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
|
|
|
|
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[client.user_id] = client
|
|
|
|
self.models[Account][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:
|
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
|
|
|
)
|
|
|
|
await client.resume(user_id=user_id, token=token, device_id=device_id)
|
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[client.user_id] = client
|
|
|
|
self.models[Account][client.user_id] = Account(client.user_id)
|
2019-06-27 16:31:03 +10:00
|
|
|
|
|
|
|
|
|
|
|
async def load_saved_accounts(self) -> Tuple[str, ...]:
|
|
|
|
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:
|
|
|
|
client = self.clients.pop(user_id, None)
|
|
|
|
if client:
|
2019-08-22 07:45:05 +10:00
|
|
|
self.models[Account].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-11-04 21:56:26 +11:00
|
|
|
async def wait_until_any_client_exists(self) -> None:
|
2019-07-21 22:38:49 +10:00
|
|
|
while True:
|
2019-11-04 21:56:26 +11:00
|
|
|
if self.clients:
|
2019-07-21 22:38:49 +10:00
|
|
|
return
|
|
|
|
|
2019-11-04 21:56:26 +11:00
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
|
|
|
|
|
|
|
|
async def wait_until_client_exists(self, user_id: str) -> None:
|
|
|
|
loops = 0
|
|
|
|
while True:
|
|
|
|
if user_id in self.clients:
|
2019-07-21 22:38:49 +10:00
|
|
|
return
|
|
|
|
|
2019-11-04 04:48:12 +11:00
|
|
|
if loops and loops % 100 == 0: # every 10s except first time
|
|
|
|
log.warning("Waiting for account %s to exist, %ds passed",
|
|
|
|
user_id, loops // 10)
|
|
|
|
|
2019-07-21 22:38:49 +10:00
|
|
|
await asyncio.sleep(0.1)
|
2019-11-04 04:48:12 +11:00
|
|
|
loops += 1
|
2019-07-21 22:38:49 +10:00
|
|
|
|
|
|
|
|
2019-07-04 14:24:21 +10:00
|
|
|
# General functions
|
|
|
|
|
2019-08-18 12:46:54 +10:00
|
|
|
@staticmethod
|
|
|
|
def hsluv(hue: int, saturation: int, lightness: int) -> List[float]:
|
|
|
|
# (0-360, 0-100, 0-100) -> [0-1, 0-1, 0-1]
|
|
|
|
return hsluv.hsluv_to_rgb([hue, saturation, lightness])
|
|
|
|
|
|
|
|
|
2019-11-05 01:46:06 +11:00
|
|
|
@staticmethod
|
|
|
|
async def mxc_to_http(mxc: str) -> Optional[str]:
|
|
|
|
return nio.Api.mxc_to_http(mxc)
|
|
|
|
|
|
|
|
|
2019-08-28 12:25:13 +10:00
|
|
|
@staticmethod
|
2019-08-28 13:51:38 +10:00
|
|
|
async def check_exported_keys_passphrase(file_path: str, passphrase: str,
|
|
|
|
) -> Union[bool, Tuple[str, bool]]:
|
|
|
|
"""Check if the exported keys file can be decrypted with passphrase.
|
|
|
|
|
|
|
|
Returns True on success, False is the passphrase is invalid, or
|
|
|
|
an (error_message, error_is_translated) tuple if another error occured.
|
|
|
|
"""
|
|
|
|
|
2019-08-28 12:25:13 +10:00
|
|
|
try:
|
2019-08-28 13:51:38 +10:00
|
|
|
nio.crypto.key_export.decrypt_and_read(file_path, passphrase)
|
2019-08-28 12:25:13 +10:00
|
|
|
return True
|
2019-08-28 13:51:38 +10:00
|
|
|
|
2019-10-28 23:06:22 +11:00
|
|
|
except OSError as err: # XXX raise
|
2019-08-28 13:51:38 +10:00
|
|
|
return (f"{file_path}: {err.strerror}", True)
|
|
|
|
|
2019-10-28 23:06:22 +11:00
|
|
|
except ValueError as err: # XXX raise
|
2019-08-28 13:51:38 +10:00
|
|
|
if str(err).startswith("HMAC check failed"):
|
|
|
|
return False
|
|
|
|
|
|
|
|
return (str(err), False)
|
2019-08-28 12:25:13 +10:00
|
|
|
|
|
|
|
|
2019-07-23 17:14:02 +10:00
|
|
|
async def load_settings(self) -> tuple:
|
|
|
|
from .config_files import Theme
|
|
|
|
settings = await self.ui_settings.read()
|
|
|
|
ui_state = await self.ui_state.read()
|
|
|
|
theme = await Theme(self, settings["theme"]).read()
|
|
|
|
|
|
|
|
return (settings, ui_state, theme)
|
2019-07-21 21:14:16 +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
|
|
|
async def get_profile(self, user_id: str) -> ProfileResponse:
|
|
|
|
if user_id in self.profile_cache:
|
|
|
|
return self.profile_cache[user_id]
|
2019-07-21 21:14:16 +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
|
|
|
async with self.get_profile_locks[user_id]:
|
|
|
|
if not self.clients:
|
2019-11-04 21:56:26 +11:00
|
|
|
await self.wait_until_any_client_exists()
|
2019-07-04 14:24:21 +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
|
|
|
client = self.clients.get(
|
|
|
|
user_id,
|
|
|
|
random.choice(tuple(self.clients.values())),
|
|
|
|
)
|
2019-07-04 14:24:21 +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
|
|
|
response = await client.get_profile(user_id)
|
|
|
|
|
|
|
|
if isinstance(response, nio.ProfileGetError):
|
|
|
|
log.warning("%s: %s", user_id, response)
|
|
|
|
|
|
|
|
self.profile_cache[user_id] = response
|
|
|
|
return response
|
2019-07-24 16:14:34 +10:00
|
|
|
|
|
|
|
|
2019-08-18 12:46:54 +10:00
|
|
|
async def get_flat_sidepane_data(self) -> List[Dict[str, Any]]:
|
|
|
|
data = []
|
|
|
|
|
|
|
|
for account in sorted(self.models[Account].values()):
|
|
|
|
data.append({
|
2019-08-18 17:27:00 +10:00
|
|
|
"type": "Account",
|
|
|
|
"id": account.user_id,
|
|
|
|
"user_id": account.user_id,
|
2019-08-20 01:34:51 +10:00
|
|
|
"data": account.serialized,
|
2019-08-18 12:46:54 +10:00
|
|
|
})
|
|
|
|
|
|
|
|
for room in sorted(self.models[Room, account.user_id].values()):
|
|
|
|
data.append({
|
2019-08-18 17:27:00 +10:00
|
|
|
"type": "Room",
|
|
|
|
"id": "/".join((account.user_id, room.room_id)),
|
|
|
|
"user_id": account.user_id,
|
2019-08-20 01:34:51 +10:00
|
|
|
"data": room.serialized,
|
2019-08-18 12:46:54 +10:00
|
|
|
})
|
|
|
|
|
|
|
|
return data
|