2020-09-23 19:57:54 -04:00
|
|
|
# Copyright Mirage authors & contributors <https://github.com/mirukana/mirage>
|
2019-12-19 07:46:16 -04:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
2020-05-22 17:27:57 -04:00
|
|
|
"""Matrix client to interact with a homeserver and other related classes."""
|
2019-12-18 13:16:15 -04:00
|
|
|
|
2019-07-02 13:59:52 -04:00
|
|
|
import asyncio
|
2019-07-02 22:22:29 -04:00
|
|
|
import html
|
2019-10-29 16:42:56 -04:00
|
|
|
import io
|
2019-07-02 13:59:52 -04:00
|
|
|
import logging as log
|
2021-03-02 12:32:48 -04:00
|
|
|
import os
|
2019-07-02 13:59:52 -04:00
|
|
|
import platform
|
2019-11-09 09:52:16 -04:00
|
|
|
import re
|
2020-06-25 16:10:44 -04:00
|
|
|
import textwrap
|
2019-10-30 11:10:40 -04:00
|
|
|
import traceback
|
2019-07-02 13:59:52 -04:00
|
|
|
from contextlib import suppress
|
2020-04-06 14:47:27 -04:00
|
|
|
from copy import deepcopy
|
2020-03-09 17:36:47 -04:00
|
|
|
from datetime import datetime, timedelta
|
2019-12-13 08:32:18 -04:00
|
|
|
from functools import partial
|
2019-07-15 16:14:08 -04:00
|
|
|
from pathlib import Path
|
2020-07-15 15:10:34 -04:00
|
|
|
from tempfile import NamedTemporaryFile
|
2019-11-04 15:18:01 -04:00
|
|
|
from typing import (
|
2020-09-05 16:47:34 -04:00
|
|
|
TYPE_CHECKING, Any, Callable, ClassVar, Coroutine, DefaultDict, Dict, List,
|
|
|
|
NamedTuple, Optional, Set, Tuple, Type, Union,
|
2019-11-04 15:18:01 -04:00
|
|
|
)
|
2019-11-09 09:52:16 -04:00
|
|
|
from urllib.parse import urlparse
|
2019-12-02 05:06:21 -04:00
|
|
|
from uuid import UUID, uuid4
|
2019-07-02 13:59:52 -04:00
|
|
|
|
2019-11-06 07:50:31 -04:00
|
|
|
import cairosvg
|
2019-11-04 15:18:01 -04:00
|
|
|
import nio
|
2019-11-17 13:31:00 -04:00
|
|
|
from nio.crypto import AsyncDataT as UploadData
|
|
|
|
from nio.crypto import async_generator_from_data
|
2020-11-15 14:57:00 -04:00
|
|
|
from PIL import Image as PILImage
|
|
|
|
from pymediainfo import MediaInfo
|
2019-11-04 15:18:01 -04:00
|
|
|
|
2020-09-22 21:22:38 -04:00
|
|
|
from . import __display_name__, __reverse_dns__, utils
|
2019-11-12 08:38:43 -04:00
|
|
|
from .errors import (
|
2020-05-29 16:45:08 -04:00
|
|
|
BadMimeType, InvalidUserId, InvalidUserInContext, MatrixBadGateway,
|
2020-09-04 11:21:36 -04:00
|
|
|
MatrixError, MatrixForbidden, MatrixInvalidAccessToken, MatrixNotFound,
|
2020-09-13 10:30:40 -04:00
|
|
|
MatrixTooLarge, MatrixUnauthorized, MatrixUnrecognized, UneededThumbnail,
|
2020-09-04 11:21:36 -04:00
|
|
|
UserFromOtherServerDisallowed,
|
2019-11-12 08:38:43 -04:00
|
|
|
)
|
2019-12-18 09:33:22 -04:00
|
|
|
from .html_markdown import HTML_PROCESSOR as HTML
|
2020-02-11 16:22:05 -04:00
|
|
|
from .media_cache import Media, Thumbnail
|
2020-06-01 21:00:22 -04:00
|
|
|
from .models.items import (
|
2021-02-28 09:52:51 -04:00
|
|
|
ZERO_DATE, Account, Event, Member, PushRule, Room,
|
|
|
|
RoomNotificationOverride, Transfer, TransferStatus, TypeSpecifier,
|
2020-06-01 21:00:22 -04: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 08:01:22 -04:00
|
|
|
from .models.model_store import ModelStore
|
2020-02-11 16:22:05 -04:00
|
|
|
from .nio_callbacks import NioCallbacks
|
2020-07-18 18:33:57 -04:00
|
|
|
from .presence import Presence
|
2020-09-04 11:21:36 -04:00
|
|
|
from .pyotherside_events import (
|
2020-09-16 11:31:34 -04:00
|
|
|
InvalidAccessToken, LoopException, NotificationRequested,
|
2020-09-04 11:21:36 -04:00
|
|
|
)
|
2019-07-02 13:59:52 -04:00
|
|
|
|
2020-02-11 16:22:05 -04:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from .backend import Backend
|
|
|
|
|
2021-02-22 11:32:34 -04:00
|
|
|
PushAction = Union[Dict[str, Any], nio.PushAction]
|
|
|
|
PushCondition = Union[Dict[str, Any], nio.PushCondition]
|
|
|
|
CryptDict = Dict[str, Any]
|
|
|
|
PathCallable = Union[
|
2020-07-16 16:00:11 -04:00
|
|
|
str, Path, Callable[[], Coroutine[None, None, Union[str, Path]]],
|
|
|
|
]
|
2019-11-17 13:31:00 -04:00
|
|
|
|
2020-09-16 13:06:34 -04:00
|
|
|
IS_WINDOWS = platform.system() == "Windows"
|
|
|
|
|
2020-11-15 14:57:00 -04:00
|
|
|
MATRIX_TO = "https://matrix.to/#"
|
|
|
|
|
2020-05-20 06:17:14 -04:00
|
|
|
REPLY_FALLBACK = (
|
2020-11-15 14:57:00 -04:00
|
|
|
"<mx-reply>"
|
|
|
|
"<blockquote>"
|
|
|
|
'<a href="{matrix_to}/{room_id}/{event_id}">In reply to</a> '
|
|
|
|
'<a href="{matrix_to}/{user_id}">{user_id}</a>'
|
|
|
|
"<br>"
|
|
|
|
"{content}"
|
|
|
|
"</blockquote>"
|
|
|
|
"</mx-reply>"
|
|
|
|
"{reply_content}"
|
2020-05-20 06:17:14 -04:00
|
|
|
)
|
|
|
|
|
2019-11-17 13:31:00 -04:00
|
|
|
|
2020-07-30 22:53:43 -04:00
|
|
|
class SyncFilterIds(NamedTuple):
|
|
|
|
"""Uploaded filter IDs for various API."""
|
|
|
|
|
|
|
|
first: str
|
|
|
|
others: str
|
|
|
|
|
2020-11-15 14:57:00 -04:00
|
|
|
|
2019-11-17 13:31:00 -04:00
|
|
|
class UploadReturn(NamedTuple):
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Details for an uploaded file."""
|
|
|
|
|
2019-11-17 13:31:00 -04:00
|
|
|
mxc: str
|
|
|
|
mime: str
|
|
|
|
decryption_dict: Dict[str, Any]
|
|
|
|
|
|
|
|
|
|
|
|
class MatrixImageInfo(NamedTuple):
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Image informations to be passed for Matrix file events."""
|
|
|
|
|
|
|
|
width: int
|
|
|
|
height: int
|
|
|
|
mime: str
|
|
|
|
size: int
|
|
|
|
|
|
|
|
def as_dict(self) -> Dict[str, Union[int, str]]:
|
|
|
|
"""Return a dict ready to be included in a Matrix file events."""
|
|
|
|
|
|
|
|
return {
|
|
|
|
"w": self.width,
|
|
|
|
"h": self.height,
|
|
|
|
"mimetype": self.mime,
|
|
|
|
"size": self.size,
|
|
|
|
}
|
2019-11-04 15:18:01 -04:00
|
|
|
|
2019-07-02 13:59:52 -04:00
|
|
|
|
|
|
|
class MatrixClient(nio.AsyncClient):
|
2019-12-18 13:16:15 -04:00
|
|
|
"""A client for an account to interact with a matrix homeserver."""
|
|
|
|
|
2019-12-11 12:42:59 -04:00
|
|
|
user_id_regex = re.compile(r"^@.+:.+")
|
|
|
|
room_id_or_alias_regex = re.compile(r"^[#!].+:.+")
|
2020-04-06 14:47:27 -04:00
|
|
|
http_s_url_regex = re.compile(r"^https?://")
|
2019-12-11 12:42:59 -04:00
|
|
|
|
2020-04-06 14:47:27 -04:00
|
|
|
lazy_load_filter: ClassVar[Dict[str, Any]] = {
|
|
|
|
"room": {
|
|
|
|
"ephemeral": {"lazy_load_members": True},
|
|
|
|
"state": {"lazy_load_members": True},
|
|
|
|
"timeline": {"lazy_load_members": True},
|
|
|
|
"account_data": {"lazy_load_members": True},
|
2020-04-04 07:41:06 -04:00
|
|
|
},
|
2020-04-06 14:47:27 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 16:35:13 -04:00
|
|
|
low_limit_filter: ClassVar[Dict[str, Any]] = {
|
2020-04-03 19:52:50 -04:00
|
|
|
"room": {
|
2020-04-09 05:52:33 -04:00
|
|
|
"ephemeral": {"limit": 1},
|
|
|
|
"timeline": {
|
2020-04-16 16:35:13 -04:00
|
|
|
"limit": 5,
|
2020-04-09 05:52:33 -04:00
|
|
|
# This kind says another event was redacted, but we wouldn't
|
|
|
|
# have it in our model, so nothing would be shown
|
|
|
|
"not_types": ["m.room.redaction"],
|
|
|
|
},
|
2020-04-03 19:52:50 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-04-07 09:54:32 -04:00
|
|
|
no_unknown_events_filter: ClassVar[Dict[str, Any]] = {
|
|
|
|
"room": {
|
|
|
|
"timeline": {
|
|
|
|
"not_types": [
|
|
|
|
"m.room.message.feedback",
|
|
|
|
"m.room.pinned_events",
|
|
|
|
"m.call.*",
|
2020-04-16 16:35:13 -04:00
|
|
|
"m.room.third_party_invite",
|
2020-04-07 09:54:32 -04:00
|
|
|
"m.room.tombstone",
|
|
|
|
"m.reaction",
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-12-18 13:16:15 -04:00
|
|
|
|
2020-07-24 01:30:35 -04:00
|
|
|
def __init__(
|
|
|
|
self,
|
2020-11-15 14:57:00 -04:00
|
|
|
backend,
|
|
|
|
user: str = "",
|
|
|
|
homeserver: str = "https://matrix.org",
|
|
|
|
device_id: Optional[str] = None,
|
2020-07-24 01:30:35 -04:00
|
|
|
) -> None:
|
2019-07-04 19:49:55 -04:00
|
|
|
|
2019-12-18 06:44:18 -04:00
|
|
|
store = Path(backend.appdirs.user_data_dir) / "encryption"
|
2019-07-24 20:00:01 -04:00
|
|
|
store.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
2021-03-02 12:32:48 -04:00
|
|
|
proxy: Optional[str]
|
|
|
|
proxy = os.environ.get("http_proxy", backend.settings.General.proxy)
|
2021-03-02 12:11:54 -04:00
|
|
|
host = re.sub(r":\d+$", "", urlparse(homeserver).netloc)
|
|
|
|
|
|
|
|
if host in ("127.0.0.1", "localhost", "::1"):
|
|
|
|
proxy = None
|
|
|
|
|
2019-07-08 13:08:46 -04:00
|
|
|
super().__init__(
|
|
|
|
homeserver = homeserver,
|
|
|
|
user = user,
|
|
|
|
device_id = device_id,
|
2019-07-24 20:00:01 -04:00
|
|
|
store_path = store,
|
2021-03-02 12:11:54 -04:00
|
|
|
proxy = proxy,
|
2019-08-19 10:31:53 -04:00
|
|
|
config = nio.AsyncClientConfig(
|
|
|
|
max_timeout_retry_wait_time = 10,
|
2019-11-12 09:13:45 -04:00
|
|
|
# TODO: pass a custom encryption DB pickle key?
|
2019-08-19 10:31:53 -04:00
|
|
|
),
|
2019-07-08 13:08:46 -04:00
|
|
|
)
|
2019-07-02 13:59:52 -04:00
|
|
|
|
2020-06-01 09:25:09 -04:00
|
|
|
self.backend: "Backend" = backend
|
|
|
|
self.models: ModelStore = self.backend.models
|
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 08:01:22 -04:00
|
|
|
|
2020-03-09 09:51:55 -04:00
|
|
|
self.profile_task: Optional[asyncio.Future] = None
|
|
|
|
self.server_config_task: Optional[asyncio.Future] = None
|
|
|
|
self.sync_task: Optional[asyncio.Future] = None
|
2020-06-28 09:46:34 -03:00
|
|
|
self.start_task: Optional[asyncio.Future] = None
|
2020-03-08 05:24:07 -04:00
|
|
|
|
2021-01-20 15:50:04 -04:00
|
|
|
self.transfer_monitors: Dict[UUID, nio.TransferMonitor] = {}
|
|
|
|
self.transfer_tasks: Dict[UUID, asyncio.Task] = {}
|
2020-04-03 07:13:33 -04:00
|
|
|
self.send_message_tasks: Dict[UUID, asyncio.Task] = {}
|
2020-03-08 05:24:07 -04:00
|
|
|
|
2020-07-30 22:53:43 -04:00
|
|
|
self._presence: str = ""
|
|
|
|
self._sync_filter_ids: Optional[SyncFilterIds] = None
|
|
|
|
self._sync_filter_ids_lock: asyncio.Lock = asyncio.Lock()
|
|
|
|
self.first_sync_done: asyncio.Event = asyncio.Event()
|
|
|
|
self.first_sync_date: Optional[datetime] = None
|
|
|
|
self.last_sync_error: Optional[Exception] = 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 08:01:22 -04:00
|
|
|
|
2019-09-08 11:40:39 -04:00
|
|
|
self.past_tokens: Dict[str, str] = {} # {room_id: token}
|
|
|
|
self.fully_loaded_rooms: Set[str] = set() # {room_id}
|
|
|
|
self.loaded_once_rooms: Set[str] = set() # {room_id}
|
|
|
|
self.cleared_events_rooms: Set[str] = set() # {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 08:01:22 -04:00
|
|
|
|
2020-09-05 16:47:34 -04:00
|
|
|
self.event_to_echo_ids: Dict[str, str] = {}
|
|
|
|
|
|
|
|
# {(room_id, user_id): event_id}
|
|
|
|
self.unassigned_member_last_read_event: Dict[Tuple[str, str], str] = {}
|
|
|
|
|
|
|
|
# {event_id: {user_id: server_timestamp}}
|
|
|
|
self.unassigned_event_last_read_by: DefaultDict[str, Dict[str, int]] =\
|
|
|
|
DefaultDict(dict)
|
|
|
|
|
2020-11-03 07:29:32 -04:00
|
|
|
self.push_rules: nio.PushRulesEvent = nio.PushRulesEvent()
|
2020-09-16 10:53:29 -04:00
|
|
|
|
2020-07-13 21:13:20 -04:00
|
|
|
# {room_id: event}
|
|
|
|
self.power_level_events: Dict[str, nio.PowerLevelsEvent] = {}
|
2020-07-12 18:48:34 -04:00
|
|
|
|
2020-09-04 11:21:36 -04:00
|
|
|
self.invalid_disconnecting: bool = False
|
|
|
|
|
2019-11-08 08:30:11 -04:00
|
|
|
self.nio_callbacks = NioCallbacks(self)
|
2019-07-02 13:59:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return "%s(user_id=%r, homeserver=%r, device_id=%r)" % (
|
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 08:01:22 -04:00
|
|
|
type(self).__name__, self.user_id, self.homeserver, self.device_id,
|
2019-07-02 13:59:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-07-25 23:31:13 -04:00
|
|
|
@property
|
|
|
|
def default_device_name(self) -> str:
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Device name to set at login if the user hasn't set a custom one."""
|
|
|
|
|
2020-03-21 23:39:57 -04:00
|
|
|
os_name = platform.system()
|
|
|
|
|
|
|
|
if not os_name: # unknown OS
|
|
|
|
return __display_name__
|
|
|
|
|
|
|
|
# On Linux, the kernel version is returned, so for a one-time-set
|
|
|
|
# device name it would quickly be outdated.
|
|
|
|
os_ver = platform.release() if os_name == "Windows" else ""
|
|
|
|
return f"{__display_name__} on {os_name} {os_ver}".rstrip()
|
2019-07-02 13:59:52 -04:00
|
|
|
|
|
|
|
|
2020-07-25 23:31:13 -04:00
|
|
|
async def _send(self, *args, **kwargs) -> nio.Response:
|
|
|
|
"""Raise a `MatrixError` subclass for any `nio.ErrorResponse`.
|
|
|
|
|
|
|
|
This function is called by `nio.AsyncClient`'s methods to send
|
|
|
|
requests to the server. Return normal responses, but catch any
|
|
|
|
`ErrorResponse` to turn them into `MatrixError` exceptions we raise.
|
|
|
|
"""
|
|
|
|
|
|
|
|
response = await super()._send(*args, **kwargs)
|
|
|
|
|
|
|
|
if isinstance(response, nio.ErrorResponse):
|
2020-09-04 11:21:36 -04:00
|
|
|
try:
|
|
|
|
raise MatrixError.from_nio(response)
|
|
|
|
except MatrixInvalidAccessToken:
|
|
|
|
if not self.invalid_disconnecting:
|
|
|
|
self.invalid_disconnecting = True
|
|
|
|
InvalidAccessToken(self.user_id)
|
|
|
|
await self.backend.logout_client(self.user_id)
|
|
|
|
|
|
|
|
raise
|
2020-07-25 23:31:13 -04:00
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2020-07-09 17:06:14 -03:00
|
|
|
async def login(
|
2020-07-25 23:31:13 -04:00
|
|
|
self, password: Optional[str] = None, token: Optional[str] = None,
|
2020-07-09 17:06:14 -03:00
|
|
|
) -> None:
|
2020-07-25 23:31:13 -04:00
|
|
|
"""Login to server using `m.login.password` or `m.login.token` flows.
|
2019-12-18 13:16:15 -04:00
|
|
|
|
2020-07-25 23:31:13 -04:00
|
|
|
Login can be done with the account's password (if the server supports
|
|
|
|
this flow) OR a token obtainable through various means.
|
|
|
|
|
|
|
|
One of the way to obtain a token is to follow the `m.login.sso` flow
|
|
|
|
first, see `Backend.start_sso_auth()` & `Backend.continue_sso_auth()`.
|
|
|
|
"""
|
2020-07-02 00:27:50 -03:00
|
|
|
|
2020-07-25 23:31:13 -04:00
|
|
|
await super().login(password, self.default_device_name, token)
|
2020-07-10 11:59:26 -03:00
|
|
|
|
2020-07-25 23:31:13 -04:00
|
|
|
order = 0
|
2020-10-05 03:06:07 -04:00
|
|
|
saved_accounts = self.backend.saved_accounts
|
2020-07-25 23:31:13 -04:00
|
|
|
|
|
|
|
if saved_accounts:
|
2020-07-09 17:06:14 -03:00
|
|
|
order = max(
|
2020-07-10 11:59:26 -03:00
|
|
|
account.get("order", i)
|
2020-07-25 23:31:13 -04:00
|
|
|
for i, account in enumerate(saved_accounts.values())
|
2020-07-09 17:06:14 -03:00
|
|
|
) + 1
|
|
|
|
|
2020-07-25 23:31:13 -04:00
|
|
|
# We need to create account model item here, because _start() needs it
|
|
|
|
item = self.models["accounts"].setdefault(
|
2020-07-09 17:06:14 -03:00
|
|
|
self.user_id, Account(self.user_id, order),
|
|
|
|
)
|
|
|
|
|
2020-08-21 05:29:42 -04:00
|
|
|
# TODO: be able to set presence before logging in
|
2020-07-25 23:31:13 -04:00
|
|
|
item.set_fields(presence=Presence.State.online, connecting=True)
|
|
|
|
self._presence = "online"
|
|
|
|
self.start_task = asyncio.ensure_future(self._start())
|
2019-07-02 13:59:52 -04:00
|
|
|
|
|
|
|
|
2020-07-02 00:27:50 -03:00
|
|
|
async def resume(
|
|
|
|
self,
|
2020-07-26 23:45:28 -04:00
|
|
|
user_id: str,
|
|
|
|
access_token: str,
|
|
|
|
device_id: str,
|
|
|
|
state: str = "online",
|
|
|
|
status_msg: str = "",
|
2020-07-02 00:27:50 -03:00
|
|
|
) -> None:
|
2020-07-26 23:45:28 -04:00
|
|
|
"""Restore a previous login to the server with a saved access token."""
|
2019-12-18 13:16:15 -04:00
|
|
|
|
2020-07-26 23:45:28 -04:00
|
|
|
self.restore_login(user_id, device_id, access_token)
|
2019-07-02 13:59:52 -04:00
|
|
|
|
2020-07-26 23:45:28 -04:00
|
|
|
account = self.models["accounts"][user_id]
|
2020-07-18 18:19:56 -04:00
|
|
|
self._presence = "offline" if state == "invisible" else state
|
2020-07-26 23:45:28 -04:00
|
|
|
|
2020-07-18 18:19:56 -04:00
|
|
|
account.set_fields(
|
|
|
|
presence=Presence.State(state), status_msg=status_msg,
|
|
|
|
)
|
2019-07-02 13:59:52 -04:00
|
|
|
|
2020-07-09 17:06:14 -03:00
|
|
|
if state != "offline":
|
|
|
|
account.connecting = True
|
2020-07-25 23:31:13 -04:00
|
|
|
self.start_task = asyncio.ensure_future(self._start())
|
2020-07-03 02:28:27 -03:00
|
|
|
|
2019-11-12 18:19:48 -04:00
|
|
|
|
2019-11-22 04:27:20 -04:00
|
|
|
async def logout(self) -> None:
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Logout from the server. This will delete the device."""
|
|
|
|
|
2020-07-09 17:06:14 -03:00
|
|
|
await self._stop()
|
2019-08-21 17:45:05 -04:00
|
|
|
await super().logout()
|
2019-07-02 13:59:52 -04:00
|
|
|
await self.close()
|
|
|
|
|
|
|
|
|
2020-07-16 23:34:35 -04:00
|
|
|
async def terminate(self) -> None:
|
|
|
|
"""Stop tasks, Set our presence offline and close HTTP connections."""
|
|
|
|
|
|
|
|
await self._stop()
|
|
|
|
|
|
|
|
if self._presence != "offline":
|
2020-07-16 23:50:44 -04:00
|
|
|
try:
|
|
|
|
await asyncio.wait_for(
|
|
|
|
self.set_presence("offline", save=False),
|
|
|
|
timeout = 10,
|
|
|
|
)
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
log.warn("%s timed out", self.user_id)
|
2020-07-16 23:34:35 -04:00
|
|
|
|
|
|
|
await self.close()
|
|
|
|
|
2019-12-16 07:02:42 -04:00
|
|
|
|
2019-12-18 13:16:15 -04:00
|
|
|
async def _start(self) -> None:
|
2020-03-09 13:08:09 -04:00
|
|
|
"""Fetch our user profile, server config and enter the sync loop."""
|
2019-12-18 13:16:15 -04:00
|
|
|
|
2020-03-09 09:51:55 -04:00
|
|
|
def on_server_config_response(future) -> None:
|
|
|
|
"""Update our model `Account` with the received config details."""
|
|
|
|
|
2020-03-15 16:13:58 -04:00
|
|
|
if future.cancelled(): # Account logged out
|
|
|
|
return
|
|
|
|
|
2020-05-21 14:14:27 -04:00
|
|
|
try:
|
2020-07-19 15:37:05 -04:00
|
|
|
account.max_upload_size = future.result() or 0
|
2020-11-15 14:57:00 -04:00
|
|
|
except MatrixError:
|
2020-05-21 14:14:27 -04:00
|
|
|
trace = traceback.format_exc().rstrip()
|
|
|
|
log.warn(
|
|
|
|
"On %s server config retrieval: %s", self.user_id, trace,
|
|
|
|
)
|
2020-03-09 09:51:55 -04:00
|
|
|
self.server_config_task = asyncio.ensure_future(
|
|
|
|
self.get_server_config(),
|
|
|
|
)
|
|
|
|
self.server_config_task.add_done_callback(
|
|
|
|
on_server_config_response,
|
|
|
|
)
|
|
|
|
|
2020-07-09 17:06:14 -03:00
|
|
|
account = self.models["accounts"][self.user_id]
|
|
|
|
|
|
|
|
# Get or create presence for account
|
|
|
|
presence = self.backend.presences.setdefault(self.user_id, Presence())
|
|
|
|
presence.account = account
|
|
|
|
presence.presence = Presence.State(self._presence)
|
|
|
|
|
2020-06-02 20:40:17 -04:00
|
|
|
self.profile_task = asyncio.ensure_future(self.update_own_profile())
|
2019-07-02 13:59:52 -04:00
|
|
|
|
2020-03-09 09:51:55 -04:00
|
|
|
self.server_config_task = asyncio.ensure_future(
|
|
|
|
self.get_server_config(),
|
|
|
|
)
|
|
|
|
self.server_config_task.add_done_callback(on_server_config_response)
|
|
|
|
|
2020-07-09 12:27:47 -04:00
|
|
|
await self.auto_verify_all_other_accounts()
|
|
|
|
|
2019-10-30 11:10:40 -04:00
|
|
|
while True:
|
|
|
|
try:
|
2020-07-30 22:53:43 -04:00
|
|
|
sync_filter_ids = await self.sync_filter_ids()
|
|
|
|
|
2020-04-03 19:52:50 -04:00
|
|
|
self.sync_task = asyncio.ensure_future(self.sync_forever(
|
2020-04-06 14:47:27 -04:00
|
|
|
timeout = 10_000,
|
2020-05-15 16:37:37 -04:00
|
|
|
loop_sleep_time = 1000,
|
2020-07-30 22:53:43 -04:00
|
|
|
first_sync_filter = sync_filter_ids.first,
|
|
|
|
sync_filter = sync_filter_ids.others,
|
2020-04-03 19:52:50 -04:00
|
|
|
))
|
2019-11-22 04:27:20 -04:00
|
|
|
await self.sync_task
|
2020-11-15 14:57:00 -04:00
|
|
|
self.last_sync_error = None
|
2019-11-22 04:27:20 -04:00
|
|
|
break # task cancelled
|
2020-11-15 14:57:00 -04:00
|
|
|
except Exception as err: # noqa
|
2020-07-20 16:03:46 -04:00
|
|
|
self.last_sync_error = err
|
|
|
|
|
2019-10-30 11:10:40 -04:00
|
|
|
trace = traceback.format_exc().rstrip()
|
2020-03-09 13:08:09 -04:00
|
|
|
|
|
|
|
if isinstance(err, MatrixError) and err.http_code >= 500:
|
2020-03-16 00:31:31 -04:00
|
|
|
log.warning(
|
|
|
|
"Server failure during sync for %s:\n%s",
|
|
|
|
self.user_id,
|
|
|
|
trace,
|
|
|
|
)
|
2020-03-09 13:08:09 -04:00
|
|
|
else:
|
|
|
|
LoopException(str(err), err, trace)
|
|
|
|
|
2020-05-05 21:19:43 -04:00
|
|
|
await asyncio.sleep(5)
|
2019-07-07 01:37:13 -04:00
|
|
|
|
2019-07-02 13:59:52 -04:00
|
|
|
|
2020-07-09 17:06:14 -03:00
|
|
|
async def _stop(self) -> None:
|
|
|
|
"""Stop client tasks. Will prevent client to receive further events."""
|
|
|
|
|
2020-07-16 17:09:14 -03:00
|
|
|
# Remove account model from presence update
|
|
|
|
presence = self.backend.presences.get(self.user_id, None)
|
|
|
|
|
|
|
|
if presence:
|
|
|
|
presence.account = None
|
|
|
|
|
2020-07-09 17:06:14 -03:00
|
|
|
tasks = (
|
|
|
|
self.profile_task,
|
|
|
|
self.sync_task,
|
|
|
|
self.server_config_task,
|
|
|
|
self.start_task,
|
|
|
|
)
|
|
|
|
|
|
|
|
for task in tasks:
|
|
|
|
if task:
|
|
|
|
task.cancel()
|
|
|
|
with suppress(asyncio.CancelledError):
|
|
|
|
await task
|
|
|
|
|
|
|
|
self.first_sync_done.clear()
|
|
|
|
|
|
|
|
|
2020-08-23 16:18:54 -04:00
|
|
|
async def get_profile(
|
|
|
|
self, user_id: str, use_cache: bool = True,
|
|
|
|
) -> nio.ProfileGetResponse:
|
|
|
|
"""Cache and return the matrix profile of `user_id`."""
|
|
|
|
|
|
|
|
async with self.backend.get_profile_locks[user_id]:
|
|
|
|
if use_cache and user_id in self.backend.profile_cache:
|
|
|
|
return self.backend.profile_cache[user_id]
|
|
|
|
|
|
|
|
response = await super().get_profile(user_id)
|
|
|
|
|
|
|
|
self.backend.profile_cache[user_id] = response
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2020-06-02 20:40:17 -04:00
|
|
|
async def update_own_profile(self) -> None:
|
|
|
|
"""Fetch our profile from server and Update our model `Account`."""
|
|
|
|
|
2020-08-23 16:18:54 -04:00
|
|
|
resp = await self.get_profile(self.user_id, use_cache=False)
|
2020-06-02 20:40:17 -04:00
|
|
|
|
2020-07-01 12:00:50 -04:00
|
|
|
account = self.models["accounts"][self.user_id]
|
|
|
|
account.set_fields(
|
|
|
|
profile_updated = datetime.now(),
|
|
|
|
display_name = resp.displayname or "",
|
|
|
|
avatar_url = resp.avatar_url or "",
|
|
|
|
)
|
2020-06-02 20:40:17 -04:00
|
|
|
|
|
|
|
|
2020-03-09 09:51:55 -04:00
|
|
|
async def get_server_config(self) -> int:
|
|
|
|
"""Return the maximum upload size on this server"""
|
|
|
|
return (await self.content_repository_config()).upload_size
|
|
|
|
|
|
|
|
|
2020-07-30 22:53:43 -04:00
|
|
|
async def sync_filter_ids(self) -> SyncFilterIds:
|
|
|
|
"""Return our sync/messages filter IDs, upload them if needed."""
|
|
|
|
|
|
|
|
async with self._sync_filter_ids_lock:
|
|
|
|
if self._sync_filter_ids:
|
|
|
|
return self._sync_filter_ids
|
|
|
|
|
|
|
|
others = deepcopy(self.lazy_load_filter)
|
|
|
|
first = deepcopy(others)
|
|
|
|
|
|
|
|
utils.dict_update_recursive(first, self.low_limit_filter)
|
|
|
|
|
2020-10-07 20:12:32 -04:00
|
|
|
if not self.backend.settings.Chat.show_unknown_events:
|
2020-07-30 22:53:43 -04:00
|
|
|
first["room"]["timeline"]["not_types"].extend(
|
|
|
|
self.no_unknown_events_filter
|
|
|
|
["room"]["timeline"]["not_types"],
|
|
|
|
)
|
|
|
|
|
|
|
|
others_id = (await self.upload_filter(**others)).filter_id
|
|
|
|
first_id = others_id
|
|
|
|
|
|
|
|
if others != first:
|
|
|
|
resp = await self.upload_filter(**first)
|
|
|
|
first_id = resp.filter_id
|
|
|
|
|
2020-09-04 14:40:00 -04:00
|
|
|
self._sync_filter_ids = SyncFilterIds(first_id, others_id)
|
2020-07-30 22:53:43 -04:00
|
|
|
return self._sync_filter_ids
|
|
|
|
|
|
|
|
|
2020-07-10 12:46:22 -04:00
|
|
|
async def pause_while_offline(self) -> None:
|
|
|
|
"""Block until our account is online."""
|
|
|
|
while (
|
|
|
|
self.models["accounts"][self.user_id].presence ==
|
|
|
|
Presence.State.offline
|
|
|
|
):
|
|
|
|
await asyncio.sleep(0.2)
|
|
|
|
|
|
|
|
|
2020-04-19 16:50:19 -04:00
|
|
|
async def can_kick(self, room_id: str, target_user_id: str) -> bool:
|
|
|
|
"""Return whether we can kick a certain user in a room."""
|
|
|
|
|
|
|
|
levels = self.all_rooms[room_id].power_levels
|
|
|
|
return levels.can_user_kick(self.user_id, target_user_id)
|
|
|
|
|
|
|
|
|
|
|
|
async def can_ban(self, room_id: str, target_user_id: str) -> bool:
|
|
|
|
"""Return whether we can ban/unbun a certain user in a room."""
|
|
|
|
|
|
|
|
levels = self.all_rooms[room_id].power_levels
|
|
|
|
return levels.can_user_ban(self.user_id, target_user_id)
|
|
|
|
|
|
|
|
|
2019-07-05 02:45:30 -04:00
|
|
|
@property
|
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 08:01:22 -04:00
|
|
|
def all_rooms(self) -> Dict[str, nio.MatrixRoom]:
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Return dict containing both our joined and invited rooms."""
|
|
|
|
|
2019-07-05 02:45:30 -04:00
|
|
|
return {**self.invited_rooms, **self.rooms}
|
|
|
|
|
|
|
|
|
2020-05-20 06:17:14 -04:00
|
|
|
async def send_text(
|
2020-08-21 01:17:29 -04:00
|
|
|
self,
|
|
|
|
room_id: str,
|
|
|
|
text: str,
|
2020-08-23 09:41:41 -04:00
|
|
|
display_name_mentions: Optional[Dict[str, str]] = None, # {id: name}
|
2020-08-21 01:17:29 -04:00
|
|
|
reply_to_event_id: Optional[str] = None,
|
2020-05-20 06:17:14 -04:00
|
|
|
) -> None:
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Send a markdown `m.text` or `m.notice` (with `/me`) message ."""
|
|
|
|
|
2020-08-21 01:17:29 -04:00
|
|
|
from_md = partial(
|
|
|
|
HTML.from_markdown, display_name_mentions=display_name_mentions,
|
|
|
|
)
|
2020-03-22 16:21:29 -04:00
|
|
|
|
2019-07-19 20:55:52 -04:00
|
|
|
escape = False
|
|
|
|
if text.startswith("//") or text.startswith(r"\/"):
|
|
|
|
escape = True
|
|
|
|
text = text[1:]
|
|
|
|
|
2020-05-20 06:17:14 -04:00
|
|
|
content: Dict[str, Any]
|
|
|
|
|
2019-07-19 20:55:52 -04:00
|
|
|
if text.startswith("/me ") and not escape:
|
2019-10-28 13:49:55 -04:00
|
|
|
event_type = nio.RoomMessageEmote
|
2020-11-15 14:57:00 -04:00
|
|
|
text = text[len("/me "):]
|
2019-07-19 20:55:52 -04:00
|
|
|
content = {"body": text, "msgtype": "m.emote"}
|
2020-03-22 16:21:29 -04:00
|
|
|
to_html = from_md(text, inline=True, outgoing=True)
|
|
|
|
echo_body = from_md(text, inline=True)
|
2019-07-19 20:55:52 -04:00
|
|
|
else:
|
2019-10-28 13:49:55 -04:00
|
|
|
event_type = nio.RoomMessageText
|
2019-07-19 20:55:52 -04:00
|
|
|
content = {"body": text, "msgtype": "m.text"}
|
2020-03-22 16:21:29 -04:00
|
|
|
to_html = from_md(text, outgoing=True)
|
|
|
|
echo_body = from_md(text)
|
2019-07-19 18:13:04 -04:00
|
|
|
|
2019-07-21 17:41:43 -04:00
|
|
|
if to_html not in (html.escape(text), f"<p>{html.escape(text)}</p>"):
|
2019-07-19 18:13:04 -04:00
|
|
|
content["format"] = "org.matrix.custom.html"
|
|
|
|
content["formatted_body"] = to_html
|
2019-07-03 21:20:49 -04:00
|
|
|
|
2020-05-20 06:17:14 -04:00
|
|
|
if reply_to_event_id:
|
|
|
|
to: Event = \
|
|
|
|
self.models[self.user_id, room_id, "events"][reply_to_event_id]
|
|
|
|
|
2020-05-20 06:37:50 -04:00
|
|
|
source_body = getattr(to.source, "body", "")
|
|
|
|
|
2020-05-20 06:17:14 -04:00
|
|
|
content["format"] = "org.matrix.custom.html"
|
2020-06-29 09:07:44 -04:00
|
|
|
plain_source_body = "\n".join(
|
|
|
|
f"> <{to.sender_id}> {line}" if i == 0 else f"> {line}"
|
|
|
|
for i, line in enumerate(source_body.splitlines())
|
2020-11-15 14:57:00 -04:00
|
|
|
)
|
2020-06-29 09:07:44 -04:00
|
|
|
content["body"] = f"{plain_source_body}\n\n{text}"
|
2020-05-20 06:17:14 -04:00
|
|
|
|
|
|
|
to_html = REPLY_FALLBACK.format(
|
2020-11-15 14:57:00 -04:00
|
|
|
matrix_to = MATRIX_TO,
|
|
|
|
room_id = room_id,
|
|
|
|
event_id = to.event_id,
|
|
|
|
user_id = to.sender_id,
|
|
|
|
content =
|
2020-05-20 06:37:50 -04:00
|
|
|
getattr(to.source, "formatted_body", "") or
|
|
|
|
source_body or
|
|
|
|
html.escape(to.source.source["type"] if to.source else ""),
|
2020-05-20 06:17:14 -04:00
|
|
|
|
|
|
|
reply_content = to_html,
|
|
|
|
)
|
|
|
|
|
|
|
|
echo_body = HTML.filter(to_html)
|
|
|
|
content["formatted_body"] = HTML.filter(to_html, outgoing=True)
|
|
|
|
|
|
|
|
content["m.relates_to"] = {
|
2020-11-15 14:57:00 -04:00
|
|
|
"m.in_reply_to": {"event_id": to.event_id},
|
2020-05-20 06:17:14 -04:00
|
|
|
}
|
|
|
|
|
2019-12-16 10:36:59 -04:00
|
|
|
# Can't use the standard Matrix transaction IDs; they're only visible
|
|
|
|
# to the sender so our other accounts wouldn't be able to replace
|
|
|
|
# local echoes by real messages.
|
|
|
|
tx_id = uuid4()
|
2020-09-22 21:22:38 -04:00
|
|
|
content[f"{__reverse_dns__}.transaction_id"] = str(tx_id)
|
2019-10-28 13:49:55 -04:00
|
|
|
|
2020-03-23 14:23:32 -04:00
|
|
|
mentions = HTML.mentions_in_html(echo_body)
|
|
|
|
await self._local_echo(
|
2020-05-20 06:17:14 -04:00
|
|
|
room_id,
|
|
|
|
tx_id,
|
|
|
|
event_type,
|
2020-05-20 06:37:50 -04:00
|
|
|
content = echo_body,
|
|
|
|
mentions = mentions,
|
2020-03-23 14:23:32 -04:00
|
|
|
)
|
|
|
|
|
2020-07-10 12:46:22 -04:00
|
|
|
await self.pause_while_offline()
|
2020-04-03 07:13:33 -04:00
|
|
|
await self._send_message(room_id, content, tx_id)
|
2019-10-28 15:27:36 -04:00
|
|
|
|
|
|
|
|
2021-01-20 15:50:04 -04:00
|
|
|
async def toggle_pause_transfer(
|
2020-03-08 05:24:07 -04:00
|
|
|
self, room_id: str, uuid: Union[str, UUID],
|
|
|
|
) -> None:
|
|
|
|
if isinstance(uuid, str):
|
|
|
|
uuid = UUID(uuid)
|
|
|
|
|
2021-01-20 15:50:04 -04:00
|
|
|
pause = not self.transfer_monitors[uuid].pause
|
2020-03-08 05:24:07 -04:00
|
|
|
|
2021-01-20 15:50:04 -04:00
|
|
|
self.transfer_monitors[uuid].pause = pause
|
|
|
|
self.models[room_id, "transfers"][str(uuid)].paused = pause
|
2020-03-08 05:24:07 -04:00
|
|
|
|
|
|
|
|
2021-01-20 15:50:04 -04:00
|
|
|
async def cancel_transfer(self, uuid: Union[str, UUID]) -> None:
|
2020-03-08 05:24:07 -04:00
|
|
|
if isinstance(uuid, str):
|
|
|
|
uuid = UUID(uuid)
|
|
|
|
|
2021-01-20 15:50:04 -04:00
|
|
|
self.transfer_tasks[uuid].cancel()
|
2020-03-08 05:24:07 -04:00
|
|
|
|
|
|
|
|
2020-08-24 10:17:04 -04:00
|
|
|
async def send_clipboard_image(
|
|
|
|
self,
|
|
|
|
room_id: str,
|
|
|
|
image: bytes,
|
|
|
|
reply_to_event_id: Optional[str] = None,
|
|
|
|
) -> None:
|
2020-07-15 15:10:34 -04:00
|
|
|
"""Send a clipboard image passed from QML as a `m.image` message."""
|
|
|
|
|
|
|
|
prefix = datetime.now().strftime("%Y%m%d-%H%M%S.")
|
|
|
|
|
|
|
|
with NamedTemporaryFile(prefix=prefix, suffix=".png") as temp:
|
|
|
|
|
2020-07-16 16:00:11 -04:00
|
|
|
async def get_path() -> Path:
|
2020-07-21 23:35:16 -04:00
|
|
|
# optimize is too slow for large images
|
|
|
|
compressed = await utils.compress_image(image, optimize=False)
|
2020-07-15 15:10:34 -04:00
|
|
|
|
2020-11-15 14:57:00 -04:00
|
|
|
async with utils.aiopen(temp.name, "wb") as file:
|
2020-07-21 23:35:16 -04:00
|
|
|
await file.write(compressed)
|
2020-07-15 15:10:34 -04:00
|
|
|
|
2020-07-16 16:00:11 -04:00
|
|
|
return Path(temp.name)
|
|
|
|
|
2020-08-24 10:17:04 -04:00
|
|
|
await self.send_file(room_id, get_path, reply_to_event_id)
|
|
|
|
|
2020-07-16 16:00:11 -04:00
|
|
|
|
2020-08-24 10:17:04 -04:00
|
|
|
async def send_file(
|
|
|
|
self,
|
|
|
|
room_id: str,
|
|
|
|
path: PathCallable,
|
|
|
|
reply_to_event_id: Optional[str] = None,
|
|
|
|
) -> None:
|
|
|
|
"""Send a `m.file`, `m.image`, `m.audio` or `m.video` message.
|
2020-07-16 16:00:11 -04:00
|
|
|
|
2020-08-24 10:17:04 -04:00
|
|
|
The Matrix client-server API states that media messages can't have a
|
|
|
|
reply attached.
|
|
|
|
Thus, if a `reply_to_event_id` is passed, we send a pseudo-reply as two
|
|
|
|
events: a `m.text` one with the reply but an empty body, then the
|
|
|
|
actual media.
|
|
|
|
"""
|
2019-12-18 13:16:15 -04:00
|
|
|
|
2019-12-02 05:06:21 -04:00
|
|
|
item_uuid = uuid4()
|
|
|
|
|
|
|
|
try:
|
2020-08-24 10:17:04 -04:00
|
|
|
await self._send_file(item_uuid, room_id, path, reply_to_event_id)
|
2019-12-05 10:00:23 -04:00
|
|
|
except (nio.TransferCancelledError, asyncio.CancelledError):
|
2021-01-20 15:50:04 -04:00
|
|
|
self.transfer_monitors.pop(item_uuid, None)
|
|
|
|
self.transfer_tasks.pop(item_uuid, None)
|
|
|
|
self.models[room_id, "transfers"].pop(str(item_uuid), None)
|
2019-12-02 05:06:21 -04:00
|
|
|
|
|
|
|
|
|
|
|
async def _send_file(
|
2020-08-24 10:17:04 -04:00
|
|
|
self,
|
|
|
|
item_uuid: UUID,
|
|
|
|
room_id: str,
|
|
|
|
path: PathCallable,
|
|
|
|
reply_to_event_id: Optional[str] = None,
|
2019-12-02 05:06:21 -04:00
|
|
|
) -> None:
|
2020-08-24 10:17:04 -04:00
|
|
|
"""Upload and monitor a file + thumbnail and send the built event(s)"""
|
2019-12-18 13:16:15 -04:00
|
|
|
|
2020-02-11 16:22:05 -04:00
|
|
|
# TODO: this function is way too complex, and most of it should be
|
2019-12-18 13:16:15 -04:00
|
|
|
# refactored into nio.
|
2019-11-17 13:31:00 -04:00
|
|
|
|
2021-01-20 15:50:04 -04:00
|
|
|
self.transfer_tasks[item_uuid] = utils.current_task() # type: ignore
|
2020-07-16 16:00:11 -04:00
|
|
|
|
2021-01-20 15:50:04 -04:00
|
|
|
transfer = Transfer(item_uuid, is_upload=True)
|
|
|
|
self.models[room_id, "transfers"][str(item_uuid)] = transfer
|
2020-07-16 16:00:11 -04:00
|
|
|
|
2020-07-19 15:34:06 -04:00
|
|
|
transaction_id = uuid4()
|
|
|
|
path = Path(await path() if callable(path) else path)
|
|
|
|
encrypt = room_id in self.encrypted_rooms
|
|
|
|
|
|
|
|
thumb_crypt_dict: Dict[str, Any] = {}
|
|
|
|
crypt_dict: Dict[str, Any] = {}
|
2019-10-30 10:34:20 -04:00
|
|
|
|
2019-12-05 09:51:31 -04:00
|
|
|
try:
|
|
|
|
size = path.resolve().stat().st_size
|
|
|
|
except (PermissionError, FileNotFoundError):
|
|
|
|
# This error will be caught again by the try block later below
|
|
|
|
size = 0
|
|
|
|
|
2021-01-20 15:50:04 -04:00
|
|
|
transfer.set_fields(
|
|
|
|
status=TransferStatus.Transfering, filepath=path, total_size=size,
|
2020-07-16 16:00:11 -04:00
|
|
|
)
|
2019-12-05 10:00:23 -04:00
|
|
|
|
2020-07-16 16:00:11 -04:00
|
|
|
monitor = nio.TransferMonitor(size)
|
2021-01-20 15:50:04 -04:00
|
|
|
self.transfer_monitors[item_uuid] = monitor
|
2020-03-12 11:27:56 -04:00
|
|
|
|
2019-12-17 16:18:00 -04:00
|
|
|
def on_transferred(transferred: int) -> None:
|
2021-01-20 15:50:04 -04:00
|
|
|
transfer.transferred = transferred
|
2019-12-05 10:00:23 -04:00
|
|
|
|
2019-12-14 11:31:43 -04:00
|
|
|
def on_speed_changed(speed: float) -> None:
|
2021-01-20 15:50:04 -04:00
|
|
|
transfer.set_fields(
|
2020-07-01 12:00:50 -04:00
|
|
|
speed = speed,
|
|
|
|
time_left = monitor.remaining_time or timedelta(0),
|
|
|
|
)
|
2019-12-05 10:00:23 -04:00
|
|
|
|
2019-12-17 16:18:00 -04:00
|
|
|
monitor.on_transferred = on_transferred
|
2019-12-14 11:31:43 -04:00
|
|
|
monitor.on_speed_changed = on_speed_changed
|
2019-11-05 18:31:16 -04:00
|
|
|
|
2020-07-10 12:46:22 -04:00
|
|
|
await self.pause_while_offline()
|
2020-07-10 12:27:06 -04:00
|
|
|
|
2019-12-02 02:57:47 -04:00
|
|
|
try:
|
|
|
|
url, mime, crypt_dict = await self.upload(
|
2019-12-14 17:40:31 -04:00
|
|
|
lambda *_: path,
|
|
|
|
filename = path.name,
|
2020-03-09 10:23:08 -04:00
|
|
|
filesize = size,
|
2020-03-08 05:24:07 -04:00
|
|
|
encrypt = encrypt,
|
|
|
|
monitor = monitor,
|
2019-12-02 02:57:47 -04:00
|
|
|
)
|
2020-03-08 05:24:07 -04:00
|
|
|
|
|
|
|
# FIXME: nio might not catch the cancel in time
|
|
|
|
if monitor.cancel:
|
|
|
|
raise nio.TransferCancelledError()
|
|
|
|
|
2019-12-05 09:51:31 -04:00
|
|
|
except (MatrixError, OSError) as err:
|
2021-01-20 15:50:04 -04:00
|
|
|
transfer.set_fields(
|
|
|
|
status = TransferStatus.Error,
|
2020-07-01 12:00:50 -04:00
|
|
|
error = type(err),
|
|
|
|
error_args = err.args,
|
|
|
|
)
|
2019-12-02 05:06:21 -04:00
|
|
|
|
2019-12-05 10:00:23 -04:00
|
|
|
# Wait for cancellation from UI, see parent send_file() method
|
2019-12-02 05:06:21 -04:00
|
|
|
while True:
|
|
|
|
await asyncio.sleep(0.1)
|
2019-10-30 10:34:20 -04:00
|
|
|
|
2021-01-20 15:50:04 -04:00
|
|
|
transfer.status = TransferStatus.Caching
|
|
|
|
local_media = await Media.from_existing_file(
|
2020-08-23 16:57:53 -04:00
|
|
|
self.backend.media_cache, self.user_id, url, path,
|
2020-07-20 22:58:02 -04:00
|
|
|
)
|
2019-11-06 09:43:05 -04:00
|
|
|
|
2019-10-30 10:34:20 -04:00
|
|
|
kind = (mime or "").split("/")[0]
|
|
|
|
|
2019-11-17 13:31:00 -04:00
|
|
|
thumb_url: str = ""
|
|
|
|
thumb_info: Optional[MatrixImageInfo] = None
|
2019-11-06 09:43:05 -04:00
|
|
|
|
2019-10-28 15:27:36 -04:00
|
|
|
content: dict = {
|
2020-09-22 21:22:38 -04:00
|
|
|
f"{__reverse_dns__}.transaction_id": str(transaction_id),
|
2019-12-16 10:36:59 -04:00
|
|
|
|
2019-10-28 15:27:36 -04:00
|
|
|
"body": path.name,
|
|
|
|
"info": {
|
|
|
|
"mimetype": mime,
|
2021-01-20 15:50:04 -04:00
|
|
|
"size": transfer.total_size,
|
2019-10-28 15:27:36 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-10-30 10:34:20 -04:00
|
|
|
if encrypt:
|
|
|
|
content["file"] = {"url": url, **crypt_dict}
|
|
|
|
else:
|
|
|
|
content["url"] = url
|
|
|
|
|
2019-10-28 15:27:36 -04:00
|
|
|
if kind == "image":
|
2019-11-06 07:50:31 -04:00
|
|
|
is_svg = mime == "image/svg+xml"
|
|
|
|
|
2019-11-04 14:37:25 -04:00
|
|
|
event_type = \
|
|
|
|
nio.RoomEncryptedImage if encrypt else nio.RoomMessageImage
|
|
|
|
|
2019-10-28 15:27:36 -04:00
|
|
|
content["msgtype"] = "m.image"
|
|
|
|
|
2019-11-06 07:50:31 -04:00
|
|
|
content["info"]["w"], content["info"]["h"] = (
|
2019-11-17 13:31:00 -04:00
|
|
|
await utils.svg_dimensions(path) if is_svg else
|
2019-10-28 15:27:36 -04:00
|
|
|
PILImage.open(path).size
|
2019-11-06 07:50:31 -04:00
|
|
|
)
|
2019-10-28 15:27:36 -04:00
|
|
|
|
2019-10-29 16:42:56 -04:00
|
|
|
try:
|
2019-11-17 13:31:00 -04:00
|
|
|
thumb_data, thumb_info = await self.generate_thumbnail(
|
|
|
|
path, is_svg=is_svg,
|
|
|
|
)
|
|
|
|
except UneededThumbnail:
|
2019-10-29 16:42:56 -04:00
|
|
|
pass
|
2020-11-15 14:57:00 -04:00
|
|
|
except Exception: # noqa
|
2020-03-20 13:51:32 -04:00
|
|
|
trace = traceback.format_exc().rstrip()
|
|
|
|
log.warning("Failed thumbnailing %s:\n%s", path, trace)
|
2019-10-29 16:42:56 -04:00
|
|
|
else:
|
2020-03-09 10:10:21 -04:00
|
|
|
thumb_ext = "png" if thumb_info.mime == "image/png" else "jpg"
|
|
|
|
thumb_name = f"{path.stem}_thumbnail.{thumb_ext}"
|
2019-12-06 16:44:25 -04:00
|
|
|
|
2021-01-20 15:50:04 -04:00
|
|
|
transfer.set_fields(
|
|
|
|
status = TransferStatus.Transfering,
|
2020-07-01 12:00:50 -04:00
|
|
|
filepath = Path(thumb_name),
|
|
|
|
total_size = len(thumb_data),
|
|
|
|
)
|
2019-11-17 13:31:00 -04:00
|
|
|
|
2019-12-02 02:57:47 -04:00
|
|
|
try:
|
2021-01-20 15:50:04 -04:00
|
|
|
transfer.total_size = thumb_info.size
|
2020-03-08 08:05:41 -04:00
|
|
|
|
|
|
|
monitor = nio.TransferMonitor(thumb_info.size)
|
|
|
|
monitor.on_transferred = on_transferred
|
|
|
|
monitor.on_speed_changed = on_speed_changed
|
|
|
|
|
2021-01-20 15:50:04 -04:00
|
|
|
self.transfer_monitors[item_uuid] = monitor
|
2020-03-08 05:24:07 -04:00
|
|
|
|
2019-12-02 02:57:47 -04:00
|
|
|
thumb_url, _, thumb_crypt_dict = await self.upload(
|
2019-12-14 17:40:31 -04:00
|
|
|
lambda *_: thumb_data,
|
2020-03-09 17:33:07 -04:00
|
|
|
filename = f"{path.stem}_sample{path.suffix}",
|
2020-03-09 10:23:08 -04:00
|
|
|
filesize = thumb_info.size,
|
2019-12-02 02:57:47 -04:00
|
|
|
encrypt = encrypt,
|
2020-03-08 05:24:07 -04:00
|
|
|
monitor = monitor,
|
2019-12-02 02:57:47 -04:00
|
|
|
)
|
2020-03-08 05:24:07 -04:00
|
|
|
|
|
|
|
# FIXME: nio might not catch the cancel in time
|
|
|
|
if monitor.cancel:
|
|
|
|
raise nio.TransferCancelledError()
|
2019-12-02 02:57:47 -04:00
|
|
|
except MatrixError as err:
|
|
|
|
log.warning(f"Failed uploading thumbnail {path}: {err}")
|
2019-10-30 10:34:20 -04:00
|
|
|
else:
|
2021-01-20 15:50:04 -04:00
|
|
|
transfer.status = TransferStatus.Caching
|
2019-12-02 02:57:47 -04:00
|
|
|
|
|
|
|
await Thumbnail.from_bytes(
|
|
|
|
self.backend.media_cache,
|
2020-08-23 16:57:53 -04:00
|
|
|
self.user_id,
|
2019-12-02 02:57:47 -04:00
|
|
|
thumb_url,
|
2020-03-09 17:33:07 -04:00
|
|
|
path.name,
|
2019-12-02 02:57:47 -04:00
|
|
|
thumb_data,
|
|
|
|
wanted_size = (content["info"]["w"],
|
|
|
|
content["info"]["h"]),
|
|
|
|
)
|
|
|
|
|
|
|
|
if encrypt:
|
|
|
|
content["info"]["thumbnail_file"] = {
|
|
|
|
"url": thumb_url,
|
|
|
|
**thumb_crypt_dict,
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
content["info"]["thumbnail_url"] = thumb_url
|
|
|
|
|
2019-12-18 13:16:15 -04:00
|
|
|
content["info"]["thumbnail_info"] = thumb_info.as_dict()
|
2019-10-29 16:42:56 -04:00
|
|
|
|
2019-10-28 15:27:36 -04:00
|
|
|
elif kind == "audio":
|
2019-11-04 14:37:25 -04:00
|
|
|
event_type = \
|
|
|
|
nio.RoomEncryptedAudio if encrypt else nio.RoomMessageAudio
|
|
|
|
|
2019-10-28 15:27:36 -04:00
|
|
|
content["msgtype"] = "m.audio"
|
|
|
|
content["info"]["duration"] = getattr(
|
|
|
|
MediaInfo.parse(path).tracks[0], "duration", 0,
|
|
|
|
) or 0
|
|
|
|
|
|
|
|
elif kind == "video":
|
2019-11-04 14:37:25 -04:00
|
|
|
event_type = \
|
|
|
|
nio.RoomEncryptedVideo if encrypt else nio.RoomMessageVideo
|
|
|
|
|
2019-10-28 15:27:36 -04:00
|
|
|
content["msgtype"] = "m.video"
|
|
|
|
|
|
|
|
tracks = MediaInfo.parse(path).tracks
|
|
|
|
|
|
|
|
content["info"]["duration"] = \
|
|
|
|
getattr(tracks[0], "duration", 0) or 0
|
|
|
|
|
|
|
|
content["info"]["w"] = max(
|
|
|
|
getattr(t, "width", 0) or 0 for t in tracks
|
|
|
|
)
|
|
|
|
content["info"]["h"] = max(
|
|
|
|
getattr(t, "height", 0) or 0 for t in tracks
|
|
|
|
)
|
|
|
|
|
|
|
|
else:
|
2019-11-04 14:37:25 -04:00
|
|
|
event_type = \
|
|
|
|
nio.RoomEncryptedFile if encrypt else nio.RoomMessageFile
|
|
|
|
|
2019-10-28 15:27:36 -04:00
|
|
|
content["msgtype"] = "m.file"
|
|
|
|
content["filename"] = path.name
|
|
|
|
|
2021-01-20 15:50:04 -04:00
|
|
|
del self.transfer_monitors[item_uuid]
|
|
|
|
del self.transfer_tasks[item_uuid]
|
|
|
|
del self.models[room_id, "transfers"][str(transfer.id)]
|
2019-11-05 18:31:16 -04:00
|
|
|
|
2020-08-24 10:17:04 -04:00
|
|
|
if reply_to_event_id:
|
|
|
|
await self.send_text(
|
|
|
|
room_id=room_id, text="", reply_to_event_id=reply_to_event_id,
|
|
|
|
)
|
|
|
|
|
2019-10-28 15:27:36 -04:00
|
|
|
await self._local_echo(
|
2019-12-16 10:36:59 -04:00
|
|
|
room_id,
|
|
|
|
transaction_id,
|
|
|
|
event_type,
|
2020-07-19 15:00:23 -04:00
|
|
|
inline_content = content["body"],
|
|
|
|
|
2019-11-06 09:43:05 -04:00
|
|
|
media_url = url,
|
2020-07-20 23:09:28 -04:00
|
|
|
media_http_url = await self.mxc_to_http(url),
|
2019-11-06 09:43:05 -04:00
|
|
|
media_title = path.name,
|
|
|
|
media_width = content["info"].get("w", 0),
|
|
|
|
media_height = content["info"].get("h", 0),
|
|
|
|
media_duration = content["info"].get("duration", 0),
|
|
|
|
media_size = content["info"]["size"],
|
|
|
|
media_mime = content["info"]["mimetype"],
|
2020-07-19 15:00:23 -04:00
|
|
|
media_crypt_dict = crypt_dict,
|
2020-07-20 22:58:02 -04:00
|
|
|
media_local_path = await local_media.get_local(),
|
2020-07-19 15:00:23 -04:00
|
|
|
|
|
|
|
thumbnail_url = thumb_url,
|
|
|
|
thumbnail_crypt_dict = thumb_crypt_dict,
|
|
|
|
|
2019-11-17 13:31:00 -04:00
|
|
|
thumbnail_width =
|
|
|
|
content["info"].get("thumbnail_info", {}).get("w", 0),
|
|
|
|
thumbnail_height =
|
|
|
|
content["info"].get("thumbnail_info", {}).get("h", 0),
|
2020-03-09 12:06:58 -04:00
|
|
|
thumbnail_mime =
|
|
|
|
content["info"].get("thumbnail_info", {}).get("mimetype", ""),
|
2019-10-28 15:27:36 -04:00
|
|
|
)
|
|
|
|
|
2020-04-03 07:13:33 -04:00
|
|
|
await self._send_message(room_id, content, transaction_id)
|
2019-10-28 13:49:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
async def _local_echo(
|
2019-12-18 13:16:15 -04:00
|
|
|
self,
|
|
|
|
room_id: str,
|
|
|
|
transaction_id: UUID,
|
|
|
|
event_type: Type[nio.Event],
|
|
|
|
**event_fields,
|
2019-10-28 13:49:55 -04:00
|
|
|
) -> None:
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Register a local model `Event` while waiting for the server.
|
|
|
|
|
|
|
|
When the user sends a message, we want to show instant feedback in
|
|
|
|
the UI timeline without waiting for the servers to receive our message
|
|
|
|
and retransmit it to us.
|
|
|
|
|
|
|
|
The event will be locally echoed for all our accounts that are members
|
|
|
|
of the `room_id` room.
|
|
|
|
This allows sending messages from other accounts within the same
|
|
|
|
composer without having to go to another page in the UI,
|
|
|
|
and getting direct feedback for these accounts in the timeline.
|
|
|
|
|
|
|
|
When we do get the real event retransmited by the server, it will
|
|
|
|
replace the local one we registered.
|
|
|
|
"""
|
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 08:01:22 -04:00
|
|
|
|
2020-06-05 01:39:17 -04:00
|
|
|
our_info = self.models["accounts"][self.user_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 08:01:22 -04:00
|
|
|
|
2020-02-11 17:32:22 -04:00
|
|
|
content = event_fields.get("content", "").strip()
|
|
|
|
|
|
|
|
if content and "inline_content" not in event_fields:
|
2020-08-21 01:17:29 -04:00
|
|
|
event_fields["inline_content"] = HTML.filter(content, inline=True)
|
2020-02-11 17:32:22 -04:00
|
|
|
|
2019-10-28 13:49:55 -04:00
|
|
|
event = Event(
|
2020-02-11 17:32:22 -04:00
|
|
|
id = f"echo-{transaction_id}",
|
|
|
|
event_id = "",
|
|
|
|
event_type = event_type,
|
|
|
|
date = datetime.now(),
|
|
|
|
sender_id = self.user_id,
|
|
|
|
sender_name = our_info.display_name,
|
|
|
|
sender_avatar = our_info.avatar_url,
|
|
|
|
is_local_echo = True,
|
|
|
|
links = Event.parse_links(content),
|
2019-10-28 15:27:36 -04:00
|
|
|
**event_fields,
|
2019-07-03 21:20:49 -04:00
|
|
|
)
|
2019-10-28 13:49:55 -04:00
|
|
|
|
2019-12-02 16:29:29 -04:00
|
|
|
for user_id in self.models["accounts"]:
|
|
|
|
if user_id in self.models[self.user_id, room_id, "members"]:
|
2019-12-16 10:36:59 -04:00
|
|
|
key = f"echo-{transaction_id}"
|
2020-04-06 14:47:27 -04:00
|
|
|
self.models[user_id, room_id, "events"][key] = deepcopy(event)
|
2019-07-03 21:20:49 -04:00
|
|
|
|
2019-10-28 13:49:55 -04:00
|
|
|
await self.set_room_last_event(room_id, event)
|
2019-10-28 13:34:59 -04:00
|
|
|
|
|
|
|
|
2020-04-03 07:13:33 -04:00
|
|
|
async def _send_message(
|
|
|
|
self, room_id: str, content: dict, transaction_id: UUID,
|
|
|
|
) -> None:
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Send a message event with `content` dict to a room."""
|
2019-08-16 12:23:34 -04:00
|
|
|
|
2020-04-03 07:13:33 -04:00
|
|
|
self.send_message_tasks[transaction_id] = \
|
2021-01-20 15:50:04 -04:00
|
|
|
utils.current_task() # type: ignore
|
2020-04-03 07:13:33 -04:00
|
|
|
|
2019-12-16 10:36:59 -04:00
|
|
|
async with self.backend.send_locks[room_id]:
|
2019-12-26 08:16:04 -04:00
|
|
|
await self.room_send(
|
2019-07-08 13:08:46 -04:00
|
|
|
room_id = room_id,
|
|
|
|
message_type = "m.room.message",
|
|
|
|
content = content,
|
|
|
|
ignore_unverified_devices = True,
|
|
|
|
)
|
2019-07-04 22:25:06 -04:00
|
|
|
|
2019-07-03 21:20:49 -04:00
|
|
|
|
2020-04-16 16:08:35 -04:00
|
|
|
async def load_all_room_members(self, room_id: str) -> None:
|
2020-04-19 15:22:09 -04:00
|
|
|
"""Request a room's full member list if it hasn't already been loaded.
|
|
|
|
|
|
|
|
Member lazy-loading is used to accelerate the initial sync with the
|
|
|
|
server. This method will be called from QML to load a room's entire
|
|
|
|
member list when the user is currently viewing the room.
|
|
|
|
"""
|
|
|
|
|
2020-06-05 01:39:17 -04:00
|
|
|
# Room may be gone by the time this is called due to room_forget()
|
|
|
|
room = self.all_rooms.get(room_id)
|
2020-04-16 16:08:35 -04:00
|
|
|
|
2020-06-05 01:39:17 -04:00
|
|
|
if room and not room.members_synced:
|
2020-04-16 16:08:35 -04:00
|
|
|
await super().joined_members(room_id)
|
2020-05-17 15:29:23 -04:00
|
|
|
await self.register_nio_room(room, force_register_members=True)
|
2020-04-16 16:08:35 -04: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 08:01:22 -04:00
|
|
|
async def load_past_events(self, room_id: str) -> bool:
|
2020-07-27 03:59:41 -04:00
|
|
|
"""Ask the server for previous events of the room.
|
|
|
|
|
|
|
|
If it's the first time that the room is being loaded, 10 events
|
|
|
|
will be requested (to give the user something to read quickly), else
|
|
|
|
100 events will be requested.
|
2019-12-18 13:16:15 -04:00
|
|
|
|
|
|
|
Events from before the client was started will be requested and
|
|
|
|
registered into our models.
|
|
|
|
|
|
|
|
Returns whether there are any messages left to load.
|
|
|
|
"""
|
|
|
|
|
2019-09-08 11:40:39 -04:00
|
|
|
if room_id in self.fully_loaded_rooms or \
|
|
|
|
room_id in self.invited_rooms or \
|
2020-04-19 17:30:16 -04:00
|
|
|
room_id in self.cleared_events_rooms or \
|
|
|
|
self.models[self.user_id, "rooms"][room_id].left:
|
2019-07-05 02:45:30 -04:00
|
|
|
return False
|
|
|
|
|
2019-08-27 15:00:50 -04:00
|
|
|
await self.first_sync_done.wait()
|
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 08:01:22 -04:00
|
|
|
|
2019-12-14 13:50:21 -04:00
|
|
|
while not self.past_tokens.get(room_id):
|
|
|
|
# If a new room was added, wait for onSyncResponse to set the token
|
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
|
2019-07-05 02:45:30 -04:00
|
|
|
response = await self.room_messages(
|
2020-04-03 19:52:50 -04:00
|
|
|
room_id = room_id,
|
|
|
|
start = self.past_tokens[room_id],
|
2020-07-27 03:59:41 -04:00
|
|
|
limit = 100 if room_id in self.loaded_once_rooms else 10,
|
2020-04-06 14:47:27 -04:00
|
|
|
message_filter = self.lazy_load_filter,
|
2019-07-05 02:45:30 -04: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 08:01:22 -04:00
|
|
|
self.loaded_once_rooms.add(room_id)
|
2019-07-05 02:45:30 -04:00
|
|
|
more_to_load = True
|
|
|
|
|
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 08:01:22 -04:00
|
|
|
self.past_tokens[room_id] = response.end
|
2019-07-05 02:45:30 -04:00
|
|
|
|
|
|
|
for event in response.chunk:
|
2019-08-30 10:28:53 -04:00
|
|
|
if isinstance(event, nio.RoomCreateEvent):
|
|
|
|
self.fully_loaded_rooms.add(room_id)
|
|
|
|
more_to_load = False
|
|
|
|
|
2019-07-05 02:45:30 -04:00
|
|
|
for cb in self.event_callbacks:
|
|
|
|
if (cb.filter is None or isinstance(event, cb.filter)):
|
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 08:01:22 -04:00
|
|
|
await cb.func(self.all_rooms[room_id], event)
|
2019-07-05 02:45:30 -04:00
|
|
|
|
|
|
|
return more_to_load
|
|
|
|
|
|
|
|
|
2019-11-09 14:20:53 -04:00
|
|
|
async def new_direct_chat(self, invite: str, encrypt: bool = False) -> str:
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Create a room and invite a single user in it for a direct chat."""
|
|
|
|
|
2019-11-09 14:20:53 -04:00
|
|
|
if invite == self.user_id:
|
|
|
|
raise InvalidUserInContext(invite)
|
|
|
|
|
2019-12-11 12:42:59 -04:00
|
|
|
if not self.user_id_regex.match(invite):
|
2019-12-04 09:59:14 -04:00
|
|
|
raise InvalidUserId(invite)
|
|
|
|
|
2019-12-26 08:16:04 -04:00
|
|
|
# Raise MatrixNotFound if profile doesn't exist
|
|
|
|
await self.get_profile(invite)
|
2019-11-09 14:20:53 -04:00
|
|
|
|
2019-12-29 14:30:15 -04:00
|
|
|
response = await super().room_create(
|
2019-11-09 14:20:53 -04:00
|
|
|
invite = [invite],
|
|
|
|
is_direct = True,
|
|
|
|
visibility = nio.RoomVisibility.private,
|
|
|
|
initial_state =
|
|
|
|
[nio.EnableEncryptionBuilder().as_dict()] if encrypt else [],
|
2019-12-29 14:30:15 -04:00
|
|
|
)
|
|
|
|
return response.room_id
|
2019-11-09 14:20:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
async def new_group_chat(
|
2019-11-08 15:32:12 -04:00
|
|
|
self,
|
|
|
|
name: Optional[str] = None,
|
|
|
|
topic: Optional[str] = None,
|
|
|
|
public: bool = False,
|
2019-11-09 13:37:51 -04:00
|
|
|
encrypt: bool = False,
|
2019-11-08 15:32:12 -04:00
|
|
|
federate: bool = True,
|
|
|
|
) -> str:
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Create a new matrix room with the purpose of being a group chat."""
|
2019-11-08 15:32:12 -04:00
|
|
|
|
2019-12-29 14:30:15 -04:00
|
|
|
response = await super().room_create(
|
2019-11-09 13:41:12 -04:00
|
|
|
name = name or None,
|
|
|
|
topic = topic or None,
|
2019-11-08 15:32:12 -04:00
|
|
|
federate = federate,
|
|
|
|
visibility =
|
|
|
|
nio.RoomVisibility.public if public else
|
|
|
|
nio.RoomVisibility.private,
|
2019-11-09 13:37:51 -04:00
|
|
|
initial_state =
|
|
|
|
[nio.EnableEncryptionBuilder().as_dict()] if encrypt else [],
|
2019-12-29 14:30:15 -04:00
|
|
|
)
|
|
|
|
return response.room_id
|
2019-11-08 15:32:12 -04:00
|
|
|
|
2019-11-09 09:52:16 -04:00
|
|
|
async def room_join(self, alias_or_id_or_url: str) -> str:
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Join an existing matrix room."""
|
|
|
|
|
2019-11-09 09:52:16 -04:00
|
|
|
string = alias_or_id_or_url.strip()
|
|
|
|
|
2020-04-06 14:47:27 -04:00
|
|
|
if self.http_s_url_regex.match(string):
|
2019-11-09 09:52:16 -04:00
|
|
|
for part in urlparse(string).fragment.split("/"):
|
2019-12-11 12:42:59 -04:00
|
|
|
if self.room_id_or_alias_regex.match(part):
|
2019-11-09 09:52:16 -04:00
|
|
|
string = part
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise ValueError(f"No alias or room id found in url {string}")
|
|
|
|
|
2019-12-11 12:42:59 -04:00
|
|
|
if not self.room_id_or_alias_regex.match(string):
|
2019-11-09 10:20:16 -04:00
|
|
|
raise ValueError("Not an alias or room id")
|
|
|
|
|
2019-12-29 14:30:15 -04:00
|
|
|
response = await super().join(string)
|
|
|
|
return response.room_id
|
2019-11-09 09:52:16 -04:00
|
|
|
|
2021-02-25 05:51:48 -04:00
|
|
|
async def toggle_room_pin(self, room_id: str) -> None:
|
|
|
|
room = self.models[self.user_id, "rooms"][room_id]
|
|
|
|
room.pinned = not room.pinned
|
2020-10-16 23:36:59 +02:00
|
|
|
|
2021-02-25 05:51:48 -04:00
|
|
|
settings = self.backend.settings
|
|
|
|
pinned = settings.RoomList.Pinned
|
|
|
|
user_pinned = pinned.setdefault(self.user_id, [])
|
2020-10-16 23:36:59 +02:00
|
|
|
|
2021-02-25 05:51:48 -04:00
|
|
|
if room.pinned and room_id not in user_pinned:
|
|
|
|
user_pinned.append(room_id)
|
2020-10-07 20:12:32 -04:00
|
|
|
|
2021-02-25 05:51:48 -04:00
|
|
|
while not room.pinned and room_id in user_pinned:
|
|
|
|
user_pinned.remove(room_id)
|
2020-10-07 20:12:32 -04:00
|
|
|
|
|
|
|
# Changes inside dicts/lists aren't monitored, need to reassign
|
2021-02-25 05:51:48 -04:00
|
|
|
settings.RoomList.Pinned[self.user_id] = user_pinned
|
2020-10-07 20:12:32 -04:00
|
|
|
self.backend.settings.save()
|
2019-11-09 09:52:16 -04:00
|
|
|
|
2019-07-07 22:19:17 -04:00
|
|
|
async def room_forget(self, room_id: str) -> None:
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Leave a joined room (or decline an invite) and forget its history.
|
|
|
|
|
|
|
|
If all the members of a room leave and forget it, that room
|
|
|
|
will be marked as suitable for destruction by the server.
|
|
|
|
"""
|
|
|
|
|
2019-12-02 16:29:29 -04:00
|
|
|
self.models[self.user_id, "rooms"].pop(room_id, None)
|
|
|
|
self.models.pop((self.user_id, room_id, "events"), None)
|
|
|
|
self.models.pop((self.user_id, room_id, "members"), None)
|
|
|
|
|
2020-06-05 01:16:27 -04:00
|
|
|
await self.update_account_unread_counts()
|
|
|
|
|
2019-12-02 16:29:29 -04:00
|
|
|
try:
|
|
|
|
await super().room_leave(room_id)
|
|
|
|
except MatrixNotFound: # already left
|
|
|
|
pass
|
|
|
|
|
2019-07-07 22:19:17 -04:00
|
|
|
await super().room_forget(room_id)
|
|
|
|
|
2019-12-11 12:42:59 -04:00
|
|
|
async def room_mass_invite(
|
|
|
|
self, room_id: str, *user_ids: str,
|
|
|
|
) -> Tuple[List[str], List[Tuple[str, Exception]]]:
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Invite users to a room in parallel.
|
|
|
|
|
|
|
|
Returns a tuple with:
|
|
|
|
|
|
|
|
- A list of users we successfully invited
|
|
|
|
- A list of `(user_id, Exception)` tuples for those failed to invite.
|
|
|
|
"""
|
2019-12-11 12:42:59 -04:00
|
|
|
|
|
|
|
user_ids = tuple(
|
|
|
|
uid for uid in user_ids
|
|
|
|
# Server would return a 403 forbidden for users already in the room
|
|
|
|
if uid not in self.all_rooms[room_id].users
|
|
|
|
)
|
|
|
|
|
2020-03-09 07:59:23 -04:00
|
|
|
async def invite(user_id: str):
|
|
|
|
if not self.user_id_regex.match(user_id):
|
|
|
|
return InvalidUserId(user_id)
|
|
|
|
|
|
|
|
if not self.rooms[room_id].federate:
|
|
|
|
_, user_server = user_id.split(":", maxsplit=1)
|
|
|
|
_, room_server = room_id.split(":", maxsplit=1)
|
|
|
|
|
|
|
|
user_server = re.sub(r":443$", "", user_server)
|
|
|
|
room_server = re.sub(r":443$", "", room_server)
|
|
|
|
|
|
|
|
if user_server != room_server:
|
|
|
|
return UserFromOtherServerDisallowed(user_id)
|
2019-12-11 12:42:59 -04:00
|
|
|
|
2019-12-26 08:16:04 -04:00
|
|
|
try:
|
2020-03-09 07:59:23 -04:00
|
|
|
await self.get_profile(user_id)
|
2020-05-29 16:45:08 -04:00
|
|
|
except (MatrixNotFound, MatrixBadGateway) as err:
|
2019-12-26 08:16:04 -04:00
|
|
|
return err
|
2019-12-11 12:42:59 -04:00
|
|
|
|
2020-03-09 07:59:23 -04:00
|
|
|
return await self.room_invite(room_id, user_id)
|
2019-12-11 12:42:59 -04:00
|
|
|
|
|
|
|
coros = [invite(uid) for uid in user_ids]
|
|
|
|
successes = []
|
|
|
|
errors: list = []
|
|
|
|
responses = await asyncio.gather(*coros)
|
|
|
|
|
|
|
|
for user_id, response in zip(user_ids, responses):
|
|
|
|
if isinstance(response, nio.RoomInviteError):
|
|
|
|
errors.append((user_id, MatrixError.from_nio(response)))
|
|
|
|
|
|
|
|
elif isinstance(response, Exception):
|
|
|
|
errors.append((user_id, response))
|
|
|
|
|
|
|
|
else:
|
|
|
|
successes.append(user_id)
|
|
|
|
|
|
|
|
return (successes, errors)
|
|
|
|
|
2020-04-01 14:33:19 -03:00
|
|
|
|
2020-06-03 04:52:33 -04:00
|
|
|
async def room_put_state_builder(
|
|
|
|
self, room_id: str, builder: nio.EventBuilder,
|
|
|
|
) -> str:
|
|
|
|
"""Send state event to room based from a `nio.EventBuilder` object."""
|
|
|
|
|
|
|
|
dct = builder.as_dict()
|
|
|
|
|
|
|
|
response = await self.room_put_state(
|
|
|
|
room_id = room_id,
|
|
|
|
event_type = dct["type"],
|
|
|
|
content = dct["content"],
|
|
|
|
state_key = dct["state_key"],
|
|
|
|
)
|
|
|
|
return response.event_id
|
|
|
|
|
|
|
|
|
|
|
|
async def room_set(
|
|
|
|
self,
|
|
|
|
room_id: str,
|
|
|
|
name: Optional[str] = None,
|
|
|
|
topic: Optional[str] = None,
|
|
|
|
encrypt: Optional[bool] = None,
|
|
|
|
require_invite: Optional[bool] = None,
|
|
|
|
forbid_guests: Optional[bool] = None,
|
|
|
|
) -> None:
|
|
|
|
"""Send setting state events for arguments that aren't `None`."""
|
|
|
|
|
|
|
|
builders: List[nio.EventBuilder] = []
|
|
|
|
|
|
|
|
if name is not None:
|
|
|
|
builders.append(nio.ChangeNameBuilder(name=name))
|
|
|
|
|
|
|
|
if topic is not None:
|
|
|
|
builders.append(nio.ChangeTopicBuilder(topic=topic))
|
|
|
|
|
|
|
|
if encrypt is False:
|
|
|
|
raise ValueError("Cannot disable encryption in a E2E room")
|
|
|
|
|
|
|
|
if encrypt is True:
|
|
|
|
builders.append(nio.EnableEncryptionBuilder())
|
|
|
|
|
|
|
|
if require_invite is not None:
|
|
|
|
builders.append(nio.ChangeJoinRulesBuilder(
|
|
|
|
rule="invite" if require_invite else "public",
|
|
|
|
))
|
|
|
|
|
|
|
|
if forbid_guests is not None:
|
|
|
|
builders.append(nio.ChangeGuestAccessBuilder(
|
|
|
|
access = "forbidden" if forbid_guests else "can_join",
|
|
|
|
))
|
|
|
|
|
|
|
|
await asyncio.gather(*[
|
|
|
|
self.room_put_state_builder(room_id, b) for b in builders
|
|
|
|
])
|
|
|
|
|
|
|
|
|
2020-07-12 18:48:34 -04:00
|
|
|
async def room_set_member_power(
|
|
|
|
self, room_id: str, user_id: str, level: int,
|
|
|
|
) -> None:
|
|
|
|
"""Set a room member's power level."""
|
|
|
|
|
2020-07-13 21:13:20 -04:00
|
|
|
while room_id not in self.power_level_events:
|
2020-07-12 18:48:34 -04:00
|
|
|
await asyncio.sleep(0.2)
|
|
|
|
|
2020-07-13 21:13:20 -04:00
|
|
|
content = deepcopy(self.power_level_events[room_id].source["content"])
|
2020-07-12 18:48:34 -04:00
|
|
|
content.setdefault("users", {})[user_id] = level
|
|
|
|
|
|
|
|
await self.room_put_state(room_id, "m.room.power_levels", content)
|
|
|
|
|
|
|
|
|
2020-07-07 11:42:16 -03:00
|
|
|
async def room_typing(
|
|
|
|
self, room_id: str, typing_state: bool = True, timeout: int = 5000,
|
|
|
|
):
|
|
|
|
"""Set typing notice to the server."""
|
|
|
|
|
2021-01-10 15:26:19 -04:00
|
|
|
if not utils.config_get_account_room_rule(
|
|
|
|
rules = self.backend.settings.Chat.Composer.TypingNotifications,
|
|
|
|
user_id = self.user_id,
|
|
|
|
room_id = room_id,
|
|
|
|
):
|
|
|
|
return
|
|
|
|
|
2020-07-10 11:59:26 -03:00
|
|
|
presence = self.models["accounts"][self.user_id].presence
|
2020-08-24 05:44:25 -04:00
|
|
|
|
2020-07-10 11:59:26 -03:00
|
|
|
if presence not in [Presence.State.invisible, Presence.State.offline]:
|
2020-07-07 11:42:16 -03:00
|
|
|
await super().room_typing(room_id, typing_state, timeout)
|
|
|
|
|
|
|
|
|
2020-04-03 05:50:02 -04:00
|
|
|
async def get_redacted_event_content(
|
2020-04-03 06:50:24 -04:00
|
|
|
self,
|
|
|
|
nio_type: Type[nio.Event],
|
|
|
|
redacter: str,
|
|
|
|
sender: str,
|
|
|
|
reason: str = "",
|
2020-04-03 05:50:02 -04:00
|
|
|
) -> str:
|
2020-04-03 06:50:24 -04:00
|
|
|
"""Get content to be displayed in place of a redacted event."""
|
2020-04-03 05:50:02 -04:00
|
|
|
|
2020-09-04 14:52:10 -04:00
|
|
|
content = "%1 removed this message" if redacter == sender else \
|
|
|
|
"%1's message was removed by %2"
|
2020-04-03 05:50:02 -04:00
|
|
|
|
|
|
|
if reason:
|
2020-10-15 20:28:23 -04:00
|
|
|
content = f"{content}, reason: {reason}"
|
2020-04-03 05:50:02 -04:00
|
|
|
|
|
|
|
return content
|
|
|
|
|
|
|
|
|
2020-03-26 17:31:57 -03:00
|
|
|
async def room_mass_redact(
|
2020-04-03 06:50:24 -04:00
|
|
|
self, room_id: str, reason: str, *event_client_ids: str,
|
2020-04-03 05:50:02 -04:00
|
|
|
) -> List[nio.RoomRedactResponse]:
|
|
|
|
"""Redact events from a room in parallel."""
|
2020-03-26 17:31:57 -03:00
|
|
|
|
2020-04-03 05:50:02 -04:00
|
|
|
tasks = []
|
2020-04-02 21:51:53 -03:00
|
|
|
|
2020-04-03 06:50:24 -04:00
|
|
|
for user_id in self.backend.clients:
|
|
|
|
for client_id in event_client_ids:
|
|
|
|
|
|
|
|
event = self.models[user_id, room_id, "events"].get(client_id)
|
|
|
|
|
|
|
|
if not event:
|
|
|
|
continue
|
|
|
|
|
2020-04-03 07:13:33 -04:00
|
|
|
if event.is_local_echo:
|
|
|
|
if user_id == self.user_id:
|
|
|
|
uuid = UUID(event.id.replace("echo-", ""))
|
|
|
|
self.send_message_tasks[uuid].cancel()
|
|
|
|
|
|
|
|
event.is_local_echo = False
|
|
|
|
else:
|
|
|
|
if user_id == self.user_id:
|
|
|
|
tasks.append(
|
|
|
|
self.room_redact(room_id, event.event_id, reason),
|
|
|
|
)
|
2020-04-03 05:50:02 -04:00
|
|
|
|
2020-04-03 07:13:33 -04:00
|
|
|
event.is_local_echo = True
|
2020-04-03 05:50:02 -04:00
|
|
|
|
2020-09-04 14:47:47 -04:00
|
|
|
event.set_fields(
|
|
|
|
content = await self.get_redacted_event_content(
|
2020-11-15 14:57:00 -04:00
|
|
|
event.event_type, self.user_id, event.sender_id,
|
|
|
|
reason,
|
2020-09-04 14:47:47 -04:00
|
|
|
),
|
|
|
|
|
|
|
|
event_type = nio.RedactedEvent,
|
|
|
|
mentions = [],
|
|
|
|
type_specifier = TypeSpecifier.Unset,
|
|
|
|
media_url = "",
|
|
|
|
media_http_url = "",
|
|
|
|
media_title = "",
|
|
|
|
media_local_path = "",
|
|
|
|
thumbnail_url = "",
|
2020-04-03 05:50:02 -04:00
|
|
|
)
|
|
|
|
|
2020-07-10 12:46:22 -04:00
|
|
|
await self.pause_while_offline()
|
2020-04-03 05:50:02 -04:00
|
|
|
return await asyncio.gather(*tasks)
|
2019-12-11 12:42:59 -04:00
|
|
|
|
2020-04-01 14:33:19 -03:00
|
|
|
|
2019-11-17 13:31:00 -04:00
|
|
|
async def generate_thumbnail(
|
|
|
|
self, data: UploadData, is_svg: bool = False,
|
|
|
|
) -> Tuple[bytes, MatrixImageInfo]:
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Create a thumbnail from an image, return the bytes and info."""
|
2019-11-04 14:37:25 -04:00
|
|
|
|
|
|
|
png_modes = ("1", "L", "P", "RGBA")
|
|
|
|
|
2019-11-17 13:31:00 -04:00
|
|
|
data = b"".join([c async for c in async_generator_from_data(data)])
|
|
|
|
is_svg = await utils.guess_mime(data) == "image/svg+xml"
|
2019-10-29 16:42:56 -04:00
|
|
|
|
2019-11-17 13:31:00 -04:00
|
|
|
if is_svg:
|
|
|
|
svg_width, svg_height = await utils.svg_dimensions(data)
|
2019-10-29 16:42:56 -04:00
|
|
|
|
2019-11-17 13:31:00 -04:00
|
|
|
data = cairosvg.svg2png(
|
|
|
|
bytestring = data,
|
|
|
|
parent_width = svg_width,
|
|
|
|
parent_height = svg_height,
|
|
|
|
)
|
2019-10-29 16:42:56 -04:00
|
|
|
|
2019-11-17 13:31:00 -04:00
|
|
|
thumb = PILImage.open(io.BytesIO(data))
|
2019-11-05 18:31:16 -04:00
|
|
|
|
2019-11-17 13:31:00 -04:00
|
|
|
small = thumb.width <= 800 and thumb.height <= 600
|
|
|
|
is_jpg_png = thumb.format in ("JPEG", "PNG")
|
|
|
|
jpgable_png = thumb.format == "PNG" and thumb.mode not in png_modes
|
2019-07-15 16:14:08 -04:00
|
|
|
|
2019-11-17 13:31:00 -04:00
|
|
|
if small and is_jpg_png and not jpgable_png and not is_svg:
|
|
|
|
raise UneededThumbnail()
|
2019-10-29 16:42:56 -04:00
|
|
|
|
2019-11-17 13:31:00 -04:00
|
|
|
if not small:
|
2020-03-21 18:56:47 -04:00
|
|
|
thumb.thumbnail((800, 600))
|
2019-10-30 10:34:20 -04:00
|
|
|
|
2020-07-21 23:35:16 -04:00
|
|
|
if thumb.mode in png_modes:
|
|
|
|
thumb_data = await utils.compress_image(thumb)
|
|
|
|
mime = "image/png"
|
|
|
|
else:
|
|
|
|
thumb = thumb.convert("RGB")
|
|
|
|
thumb_data = await utils.compress_image(thumb, "JPEG")
|
|
|
|
mime = "image/jpeg"
|
2019-11-05 18:31:16 -04:00
|
|
|
|
2020-07-21 23:35:16 -04:00
|
|
|
thumb_size = len(thumb_data)
|
2019-10-29 16:42:56 -04:00
|
|
|
|
2020-03-09 10:03:22 -04:00
|
|
|
if thumb_size >= len(data) and is_jpg_png and not is_svg:
|
2020-03-08 08:10:27 -04:00
|
|
|
raise UneededThumbnail()
|
|
|
|
|
|
|
|
info = MatrixImageInfo(thumb.width, thumb.height, mime, thumb_size)
|
|
|
|
return (thumb_data, info)
|
2019-10-29 16:42:56 -04:00
|
|
|
|
|
|
|
|
2019-11-17 13:31:00 -04:00
|
|
|
async def upload(
|
2019-11-05 18:31:16 -04:00
|
|
|
self,
|
2019-12-14 17:40:31 -04:00
|
|
|
data_provider: nio.DataProvider,
|
|
|
|
filename: Optional[str] = None,
|
2020-03-09 10:23:08 -04:00
|
|
|
filesize: Optional[int] = None,
|
|
|
|
mime: Optional[str] = None,
|
2019-12-14 17:40:31 -04:00
|
|
|
encrypt: bool = False,
|
|
|
|
monitor: Optional[nio.TransferMonitor] = None,
|
2019-11-17 13:31:00 -04:00
|
|
|
) -> UploadReturn:
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Upload a file to the matrix homeserver."""
|
2019-10-29 16:42:56 -04:00
|
|
|
|
2020-07-19 16:24:16 -04:00
|
|
|
max_size = self.models["accounts"][self.user_id].max_upload_size
|
|
|
|
|
|
|
|
if max_size and filesize > max_size:
|
2020-03-09 10:23:08 -04:00
|
|
|
raise MatrixTooLarge()
|
|
|
|
|
2019-12-14 17:40:31 -04:00
|
|
|
mime = mime or await utils.guess_mime(data_provider(0, 0))
|
2019-10-29 16:42:56 -04:00
|
|
|
|
2019-11-17 13:31:00 -04:00
|
|
|
response, decryption_dict = await super().upload(
|
2020-05-21 20:52:42 -04:00
|
|
|
data_provider = data_provider,
|
|
|
|
content_type = "application/octet-stream" if encrypt else mime,
|
|
|
|
filename = filename,
|
|
|
|
encrypt = encrypt,
|
|
|
|
monitor = monitor,
|
|
|
|
filesize = filesize,
|
2019-11-17 13:31:00 -04:00
|
|
|
)
|
2019-07-15 16:14:08 -04:00
|
|
|
|
2020-09-04 14:57:09 -04:00
|
|
|
return UploadReturn(response.content_uri, mime, decryption_dict or {})
|
2019-07-15 16:14:08 -04:00
|
|
|
|
|
|
|
|
2019-10-28 08:06:22 -04:00
|
|
|
async def set_avatar_from_file(self, path: Union[Path, str]) -> None:
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Upload an image to the homeserver and set it as our avatar."""
|
|
|
|
|
2020-03-09 10:23:08 -04:00
|
|
|
path = Path(path)
|
2019-11-17 13:31:00 -04:00
|
|
|
mime = await utils.guess_mime(path)
|
2019-11-12 09:48:11 -04:00
|
|
|
|
|
|
|
if mime.split("/")[0] != "image":
|
|
|
|
raise BadMimeType(wanted="image/*", got=mime)
|
|
|
|
|
2020-03-09 10:23:08 -04:00
|
|
|
mxc, *_ = await self.upload(
|
|
|
|
data_provider = lambda *_: path,
|
|
|
|
filename = path.name,
|
|
|
|
filesize = path.resolve().stat().st_size,
|
|
|
|
mime = mime,
|
|
|
|
)
|
2019-11-17 13:31:00 -04:00
|
|
|
await self.set_avatar(mxc)
|
2019-07-15 16:14:08 -04:00
|
|
|
|
|
|
|
|
2020-07-17 00:46:46 -04:00
|
|
|
async def get_offline_presence(self, user_id: str) -> None:
|
|
|
|
"""Get a offline room member's presence and set it on model item.
|
|
|
|
|
|
|
|
This is called by QML when a member list delegate or profile that
|
|
|
|
is offline is displayed.
|
|
|
|
Since we don't get last seen times for offline in users in syncs,
|
|
|
|
we have to fetch those manually.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self.backend.presences.get(user_id):
|
|
|
|
return
|
|
|
|
|
|
|
|
if not self.models["accounts"][self.user_id].presence_support:
|
|
|
|
return
|
|
|
|
|
2020-07-21 21:02:28 -04:00
|
|
|
try:
|
|
|
|
async with self.backend.concurrent_get_presence_limit:
|
|
|
|
resp = await self.get_presence(user_id)
|
2020-09-13 10:30:40 -04:00
|
|
|
except (MatrixForbidden, MatrixUnrecognized):
|
2020-07-21 21:02:28 -04:00
|
|
|
return
|
2020-07-17 00:46:46 -04:00
|
|
|
|
|
|
|
await self.nio_callbacks.onPresenceEvent(nio.PresenceEvent(
|
|
|
|
user_id = resp.user_id,
|
|
|
|
presence = resp.presence,
|
|
|
|
last_active_ago = resp.last_active_ago,
|
|
|
|
currently_active = resp.currently_active,
|
|
|
|
status_msg = resp.status_msg,
|
|
|
|
))
|
|
|
|
|
|
|
|
|
2020-07-03 02:28:27 -03:00
|
|
|
async def set_presence(
|
2020-07-11 00:51:53 -04:00
|
|
|
self,
|
|
|
|
presence: str,
|
|
|
|
status_msg: Optional[str] = None,
|
|
|
|
save: bool = True,
|
2020-07-03 02:28:27 -03:00
|
|
|
) -> None:
|
2020-07-02 00:27:50 -03:00
|
|
|
"""Set presence state for this account."""
|
|
|
|
|
2020-07-09 17:06:14 -03:00
|
|
|
account = self.models["accounts"][self.user_id]
|
2020-07-07 11:42:16 -03:00
|
|
|
status_msg = status_msg if status_msg is not None else (
|
2020-07-03 02:28:27 -03:00
|
|
|
self.models["accounts"][self.user_id].status_msg
|
|
|
|
)
|
2020-07-16 17:09:14 -03:00
|
|
|
set_status_msg = True
|
2020-07-03 02:28:27 -03:00
|
|
|
|
2020-07-09 17:06:14 -03:00
|
|
|
if presence == "offline":
|
|
|
|
# Do not do anything if account is offline and setting to offline
|
|
|
|
if account.presence == Presence.State.offline:
|
|
|
|
return
|
|
|
|
|
|
|
|
await self._stop()
|
|
|
|
|
2020-07-10 11:59:26 -03:00
|
|
|
# Update manually since we may not receive the presence event back
|
2020-07-09 17:06:14 -03:00
|
|
|
# in time
|
2020-07-18 18:19:56 -04:00
|
|
|
account.set_fields(
|
|
|
|
presence = Presence.State.offline,
|
|
|
|
currently_active = False,
|
|
|
|
)
|
2020-07-09 17:06:14 -03:00
|
|
|
elif (
|
2020-07-10 11:59:26 -03:00
|
|
|
account.presence == Presence.State.offline and
|
|
|
|
presence != "offline"
|
2020-07-09 17:06:14 -03:00
|
|
|
):
|
2020-07-16 17:09:14 -03:00
|
|
|
# In this case we will not run super().set_presence()
|
|
|
|
set_status_msg = False
|
2020-07-09 17:06:14 -03:00
|
|
|
account.connecting = True
|
2020-07-10 11:59:26 -03:00
|
|
|
self.start_task = asyncio.ensure_future(self._start())
|
2020-07-02 00:27:50 -03:00
|
|
|
|
2020-07-16 17:09:14 -03:00
|
|
|
self._presence = "offline" if presence == "invisible" else presence
|
|
|
|
|
2020-07-09 20:53:25 -03:00
|
|
|
if (
|
2020-07-10 11:59:26 -03:00
|
|
|
Presence.State(presence) != account.presence and
|
|
|
|
presence != "offline"
|
2020-07-09 20:53:25 -03:00
|
|
|
):
|
|
|
|
account.presence = Presence.State("echo_" + presence)
|
2020-07-09 17:06:14 -03:00
|
|
|
|
|
|
|
if not account.presence_support:
|
|
|
|
account.presence = Presence.State(presence)
|
|
|
|
|
2020-07-11 00:51:53 -04:00
|
|
|
if save:
|
|
|
|
account.save_presence = True
|
2020-10-05 03:06:07 -04:00
|
|
|
await self.backend.saved_accounts.set(
|
2020-07-16 17:09:14 -03:00
|
|
|
self.user_id, presence=presence, status_msg=status_msg,
|
2020-07-11 00:51:53 -04:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
account.save_presence = False
|
2020-07-09 17:06:14 -03:00
|
|
|
|
2020-07-16 17:09:14 -03:00
|
|
|
if set_status_msg:
|
2020-07-16 19:25:02 -03:00
|
|
|
account.status_msg = status_msg
|
|
|
|
|
2020-07-16 17:09:14 -03:00
|
|
|
await super().set_presence(
|
2020-11-15 14:57:00 -04:00
|
|
|
"offline" if presence == "invisible" else presence,
|
2020-07-16 17:09:14 -03:00
|
|
|
status_msg,
|
|
|
|
)
|
2020-07-02 00:27:50 -03:00
|
|
|
|
|
|
|
|
2019-08-28 11:42:52 -04:00
|
|
|
async def import_keys(self, infile: str, passphrase: str) -> None:
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Import decryption keys from a file, then retry decrypting events."""
|
|
|
|
|
2019-11-23 11:14:14 -04:00
|
|
|
await super().import_keys(infile, passphrase)
|
2019-08-28 13:23:12 -04:00
|
|
|
await self.retry_decrypting_events()
|
2019-08-28 03:54:53 -04:00
|
|
|
|
|
|
|
|
2019-09-07 19:17:32 -04:00
|
|
|
async def export_keys(self, outfile: str, passphrase: str) -> None:
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Export our decryption keys to a file."""
|
|
|
|
|
2019-09-07 19:17:32 -04:00
|
|
|
path = Path(outfile)
|
|
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
2019-09-08 12:17:08 -04:00
|
|
|
# The QML dialog asks the user if he wants to overwrite before this
|
|
|
|
if path.exists():
|
|
|
|
path.unlink()
|
2019-09-07 19:17:32 -04:00
|
|
|
|
|
|
|
await super().export_keys(outfile, passphrase)
|
|
|
|
|
|
|
|
|
2019-08-28 13:23:12 -04:00
|
|
|
async def retry_decrypting_events(self) -> None:
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Retry decrypting room `Event`s in our model we failed to decrypt."""
|
|
|
|
|
2019-08-28 13:23:12 -04:00
|
|
|
for sync_id, model in self.models.items():
|
|
|
|
if not (isinstance(sync_id, tuple) and
|
2020-03-15 16:00:29 -04:00
|
|
|
len(sync_id) == 3 and
|
2019-12-02 16:29:29 -04:00
|
|
|
sync_id[0] == self.user_id and
|
|
|
|
sync_id[2] == "events"):
|
2019-08-28 13:23:12 -04:00
|
|
|
continue
|
|
|
|
|
2020-03-15 16:00:29 -04:00
|
|
|
_, room_id, _ = sync_id
|
2019-08-28 13:23:12 -04:00
|
|
|
|
2020-05-28 14:16:43 -04:00
|
|
|
with model.write_lock:
|
|
|
|
for ev in model.values():
|
|
|
|
room = self.all_rooms[room_id]
|
2019-08-28 13:23:12 -04:00
|
|
|
|
2020-05-28 14:16:43 -04:00
|
|
|
if isinstance(ev.source, nio.MegolmEvent):
|
|
|
|
try:
|
|
|
|
decrypted = self.decrypt_event(ev.source)
|
2019-08-28 13:23:12 -04:00
|
|
|
|
2020-05-28 14:16:43 -04:00
|
|
|
if not decrypted:
|
|
|
|
raise nio.EncryptionError()
|
2019-08-28 13:23:12 -04:00
|
|
|
|
2020-05-28 14:16:43 -04:00
|
|
|
except nio.EncryptionError:
|
|
|
|
continue
|
2019-08-28 13:23:12 -04:00
|
|
|
|
2020-05-28 14:16:43 -04:00
|
|
|
for callback in self.event_callbacks:
|
|
|
|
filter_ = callback.filter
|
|
|
|
if not filter_ or isinstance(decrypted, filter_):
|
|
|
|
coro = asyncio.coroutine(callback.func)
|
|
|
|
await coro(room, decrypted)
|
2019-08-28 13:23:12 -04:00
|
|
|
|
|
|
|
|
2019-09-08 11:40:39 -04:00
|
|
|
async def clear_events(self, room_id: str) -> None:
|
2020-03-12 14:41:00 -04:00
|
|
|
"""Remove every `Event` of a room we registered in our model.
|
2019-12-18 13:16:15 -04:00
|
|
|
|
|
|
|
The events will be gone from the UI, until the client is restarted.
|
|
|
|
"""
|
|
|
|
|
2019-09-08 11:40:39 -04:00
|
|
|
self.cleared_events_rooms.add(room_id)
|
2020-03-16 12:36:39 -04:00
|
|
|
|
2019-12-02 16:29:29 -04:00
|
|
|
model = self.models[self.user_id, room_id, "events"]
|
2019-09-08 11:49:47 -04:00
|
|
|
if model:
|
|
|
|
model.clear()
|
2019-09-08 11:40:39 -04:00
|
|
|
|
2020-03-16 12:36:39 -04:00
|
|
|
self.models[self.user_id, "rooms"][room_id].last_event_date = \
|
2020-07-10 11:38:23 -04:00
|
|
|
ZERO_DATE
|
2019-09-08 11:40:39 -04:00
|
|
|
|
2020-05-31 19:41:05 -04:00
|
|
|
|
2020-06-12 23:09:17 -04:00
|
|
|
async def devices_info(self) -> List[Dict[str, Any]]:
|
2020-06-25 12:11:11 -04:00
|
|
|
"""Get sorted list of devices and their info for our user."""
|
2020-06-12 23:09:17 -04:00
|
|
|
|
2020-06-25 08:32:08 -04:00
|
|
|
def get_type(device_id: str) -> str:
|
2020-06-25 16:10:44 -04:00
|
|
|
# Return "current", "no_keys", "verified", "blacklisted",
|
|
|
|
# "ignored" or "unset"
|
2020-06-25 08:32:08 -04:00
|
|
|
|
|
|
|
if device_id == self.device_id:
|
|
|
|
return "current"
|
2020-06-12 23:09:17 -04:00
|
|
|
|
2020-07-08 11:33:05 -04:00
|
|
|
if device_id not in self.device_store[self.user_id]:
|
2020-06-25 16:10:44 -04:00
|
|
|
return "no_keys"
|
2020-06-12 23:09:17 -04:00
|
|
|
|
2020-07-08 11:33:05 -04:00
|
|
|
trust = self.device_store[self.user_id][device_id].trust_state
|
2020-06-12 23:09:17 -04:00
|
|
|
return trust.name
|
|
|
|
|
2020-06-25 16:10:44 -04:00
|
|
|
def get_ed25519(device_id: str) -> str:
|
|
|
|
key = ""
|
|
|
|
|
|
|
|
if device_id == self.device_id:
|
|
|
|
key = self.olm.account.identity_keys["ed25519"]
|
2020-07-08 11:33:05 -04:00
|
|
|
elif device_id in self.device_store[self.user_id]:
|
|
|
|
key = self.device_store[self.user_id][device_id].ed25519
|
2020-06-25 16:10:44 -04:00
|
|
|
|
|
|
|
return " ".join(textwrap.wrap(key, 4))
|
|
|
|
|
2020-06-12 23:09:17 -04:00
|
|
|
devices = [
|
|
|
|
{
|
|
|
|
"id": device.id,
|
|
|
|
"display_name": device.display_name or "",
|
2020-06-25 08:32:08 -04:00
|
|
|
"last_seen_ip": (device.last_seen_ip or "").strip(" -"),
|
2020-07-10 11:38:23 -04:00
|
|
|
"last_seen_date": device.last_seen_date or ZERO_DATE,
|
2020-06-12 23:09:17 -04:00
|
|
|
"last_seen_country": "",
|
2020-06-25 08:32:08 -04:00
|
|
|
"type": get_type(device.id),
|
2020-06-25 16:10:44 -04:00
|
|
|
"ed25519_key": get_ed25519(device.id),
|
2020-06-12 23:09:17 -04:00
|
|
|
}
|
|
|
|
for device in (await self.devices()).devices
|
|
|
|
]
|
|
|
|
|
2020-06-25 08:32:08 -04:00
|
|
|
# Reversed due to sorted(reverse=True) call below
|
|
|
|
types_order = {
|
2020-06-25 16:10:44 -04:00
|
|
|
"current": 5,
|
|
|
|
"unset": 4,
|
|
|
|
"no_keys": 3,
|
2020-06-25 08:32:08 -04:00
|
|
|
"verified": 2,
|
|
|
|
"ignored": 1,
|
|
|
|
"blacklisted": 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
# Sort by type, then by descending date
|
2020-06-12 23:09:17 -04:00
|
|
|
return sorted(
|
|
|
|
devices,
|
2020-06-25 08:32:08 -04:00
|
|
|
key = lambda d: (types_order[d["type"]], d["last_seen_date"]),
|
2020-06-12 23:09:17 -04:00
|
|
|
reverse = True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-07-08 11:33:05 -04:00
|
|
|
async def member_devices(self, user_id: str) -> List[Dict[str, Any]]:
|
|
|
|
"""Get list of E2E-aware devices for a user we share a room with."""
|
|
|
|
|
|
|
|
devices = [
|
|
|
|
# types: "verified", "blacklisted", "ignored" or "unset"
|
|
|
|
{
|
|
|
|
"id": device.id,
|
|
|
|
"display_name": device.display_name or "",
|
|
|
|
"type": device.trust_state.name,
|
2020-07-15 11:25:01 -04:00
|
|
|
"ed25519_key": " ".join(textwrap.wrap(device.ed25519, 4)),
|
2020-07-08 11:33:05 -04:00
|
|
|
}
|
|
|
|
for device in self.device_store.active_user_devices(user_id)
|
|
|
|
]
|
|
|
|
|
|
|
|
types_order = {
|
|
|
|
"unset": 0, "verified": 1, "ignored": 2, "blacklisted": 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
# Sort by type, then by display name, then by ID
|
|
|
|
return sorted(
|
|
|
|
devices,
|
|
|
|
key = lambda d:
|
|
|
|
(types_order[d["type"]], d["display_name"], d["id"]),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-06-25 12:11:11 -04:00
|
|
|
async def rename_device(self, device_id: str, name: str) -> bool:
|
|
|
|
"""Rename one of our device, return `False` if it doesn't exist."""
|
|
|
|
|
|
|
|
try:
|
|
|
|
await self.update_device(device_id, {"display_name": name})
|
|
|
|
return True
|
|
|
|
except MatrixNotFound:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-07-09 12:27:47 -04:00
|
|
|
async def auto_verify_all_other_accounts(self) -> None:
|
|
|
|
"""Automatically verify/blacklist our other accounts's devices."""
|
|
|
|
|
|
|
|
for client in self.backend.clients.values():
|
|
|
|
await self.auto_verify_account(client)
|
|
|
|
|
|
|
|
|
|
|
|
async def auto_verify_account(self, client: "MatrixClient") -> None:
|
|
|
|
"""Automatically verify/blacklist one of our accounts's devices."""
|
|
|
|
|
|
|
|
if client.user_id == self.user_id:
|
|
|
|
return
|
|
|
|
|
|
|
|
for device in self.device_store.active_user_devices(client.user_id):
|
|
|
|
if device.device_id != client.device_id:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if device.verified or device.blacklisted:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if device.ed25519 == client.olm.account.identity_keys["ed25519"]:
|
|
|
|
self.verify_device(device)
|
|
|
|
else:
|
|
|
|
self.blacklist_device(device)
|
|
|
|
|
|
|
|
|
2020-06-29 10:30:44 -04:00
|
|
|
async def delete_devices_with_password(
|
|
|
|
self, device_ids: List[str], password: str,
|
|
|
|
) -> None:
|
|
|
|
"""Delete devices, authentifying using the account's password."""
|
|
|
|
|
|
|
|
auth = {
|
|
|
|
"type": "m.login.password",
|
|
|
|
"user": self.user_id,
|
|
|
|
"password": password,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = await super().delete_devices(device_ids, auth)
|
|
|
|
|
|
|
|
if isinstance(resp, nio.DeleteDevicesAuthResponse):
|
|
|
|
raise MatrixUnauthorized()
|
|
|
|
|
|
|
|
|
2021-02-22 11:32:34 -04:00
|
|
|
async def edit_pushrule(
|
|
|
|
self,
|
|
|
|
kind: Union[nio.PushRuleKind, str],
|
|
|
|
rule_id: str,
|
|
|
|
old_kind: Union[None, nio.PushRuleKind, str] = None,
|
|
|
|
old_rule_id: Optional[str] = None,
|
|
|
|
move_before_rule_id: Optional[str] = None,
|
|
|
|
move_after_rule_id: Optional[str] = None,
|
|
|
|
enable: Optional[bool] = None,
|
|
|
|
conditions: Optional[List[PushCondition]] = None,
|
|
|
|
pattern: Optional[str] = None,
|
|
|
|
actions: Optional[List[PushAction]] = None,
|
|
|
|
) -> None:
|
2021-02-24 14:35:43 -04:00
|
|
|
"""Create or edit an existing non-builtin pushrule.
|
|
|
|
For builtin server ("default") rules, only actions can be edited.
|
|
|
|
"""
|
2021-02-22 11:32:34 -04:00
|
|
|
|
|
|
|
# Convert arguments that were passed as basic types (usually from QML)
|
|
|
|
|
|
|
|
if isinstance(old_kind, str):
|
|
|
|
old_kind = nio.PushRuleKind[old_kind]
|
|
|
|
|
|
|
|
kind = nio.PushRuleKind[kind] if isinstance(kind, str) else kind
|
|
|
|
|
|
|
|
conditions = [
|
|
|
|
nio.PushCondition.from_dict(c) if isinstance(c, dict) else c
|
|
|
|
for c in conditions
|
|
|
|
] if isinstance(conditions, list) else None
|
|
|
|
|
|
|
|
actions = [
|
2021-02-24 14:35:43 -04:00
|
|
|
nio.PushAction.from_dict(a) if isinstance(a, (str, dict)) else a
|
2021-02-22 11:32:34 -04:00
|
|
|
for a in actions
|
|
|
|
] if isinstance(actions, list) else None
|
|
|
|
|
|
|
|
# Now edit the rule
|
|
|
|
|
|
|
|
old: Optional[PushRule] = None
|
2021-02-28 09:00:36 -04:00
|
|
|
key = (old_kind.value if old_kind else None, old_rule_id)
|
2021-02-22 11:32:34 -04:00
|
|
|
|
|
|
|
if None not in key:
|
|
|
|
old = self.models[self.user_id, "pushrules"].get(key)
|
|
|
|
|
2021-02-23 08:12:38 -04:00
|
|
|
kind_change = old and old_kind and old_kind != kind
|
|
|
|
rule_id_change = old and old_rule_id and old_rule_id != rule_id
|
2021-02-22 11:32:34 -04:00
|
|
|
explicit_move = move_before_rule_id or move_after_rule_id
|
|
|
|
|
|
|
|
if old and not kind_change and not explicit_move:
|
|
|
|
# If user edits a rule without specifying a new position,
|
|
|
|
# the server would move it to the first position
|
|
|
|
move_after_rule_id = old.rule_id
|
|
|
|
|
|
|
|
if old and actions is None:
|
|
|
|
# Matrix API forces us to always pass a non-null actions paramater
|
|
|
|
actions = [nio.PushAction.from_dict(a) for a in old.actions]
|
|
|
|
|
2021-02-24 14:35:43 -04:00
|
|
|
if old and old.default:
|
|
|
|
await self.set_pushrule_actions("global", kind, rule_id, actions)
|
|
|
|
else:
|
|
|
|
await self.set_pushrule(
|
|
|
|
scope = "global",
|
|
|
|
kind = kind,
|
|
|
|
rule_id = rule_id,
|
|
|
|
before = move_before_rule_id,
|
|
|
|
after = move_after_rule_id,
|
|
|
|
actions = actions or [],
|
|
|
|
conditions = conditions,
|
|
|
|
pattern = pattern,
|
|
|
|
)
|
2021-02-22 11:32:34 -04:00
|
|
|
|
|
|
|
# If we're editing an existing rule but its kind or ID is changed,
|
|
|
|
# set_pushrule creates a new rule, thus we must delete the old one
|
|
|
|
if kind_change or rule_id_change:
|
|
|
|
await self.delete_pushrule("global", old_kind, old_rule_id)
|
|
|
|
|
|
|
|
if enable is not None and (old.enabled if old else True) != enable:
|
|
|
|
await self.enable_pushrule("global", kind, rule_id, enable)
|
2021-03-02 08:07:50 -04:00
|
|
|
elif kind_change or rule_id_change and old and not old.enabled:
|
2021-02-28 17:31:17 -04:00
|
|
|
await self.enable_pushrule("global", kind, rule_id, False)
|
2021-02-22 11:32:34 -04:00
|
|
|
|
|
|
|
|
2021-02-25 05:14:42 -04:00
|
|
|
async def tweak_pushrule_actions(
|
2020-11-01 00:55:29 -04:00
|
|
|
self,
|
2020-11-03 06:36:31 -04:00
|
|
|
kind: Union[nio.PushRuleKind, str],
|
2020-11-01 00:55:29 -04:00
|
|
|
rule_id: str,
|
|
|
|
notify: Optional[bool] = None,
|
|
|
|
highlight: Optional[bool] = None,
|
|
|
|
bubble: Optional[bool] = None,
|
2020-11-01 03:15:00 -04:00
|
|
|
sound: Optional[str] = None,
|
2020-11-01 00:55:29 -04:00
|
|
|
urgency_hint: Optional[bool] = None,
|
|
|
|
) -> None:
|
2021-02-25 05:14:42 -04:00
|
|
|
"""Edit individual actions for any existing push rule."""
|
2020-11-01 00:55:29 -04:00
|
|
|
|
2020-11-03 06:36:31 -04:00
|
|
|
kind = nio.PushRuleKind[kind] if isinstance(kind, str) else kind
|
2020-11-01 01:10:49 -04:00
|
|
|
|
|
|
|
current: PushRule = \
|
|
|
|
self.models[self.user_id, "pushrules"][kind.value, rule_id]
|
2020-11-01 00:55:29 -04:00
|
|
|
|
|
|
|
actions: List[nio.PushAction] = []
|
|
|
|
|
|
|
|
if notify or (notify is None and current.notify):
|
|
|
|
actions.append(nio.PushNotify())
|
|
|
|
|
|
|
|
if highlight or (highlight is None and current.highlight):
|
|
|
|
actions.append(nio.PushSetTweak("highlight", True))
|
|
|
|
|
|
|
|
if bubble or (bubble is None and current.bubble):
|
|
|
|
actions.append(nio.PushSetTweak("bubble", True))
|
|
|
|
elif bubble is False or (bubble is None and not current.bubble):
|
|
|
|
actions.append(nio.PushSetTweak("bubble", False))
|
|
|
|
|
|
|
|
if sound or (sound is None and current.sound):
|
2020-11-01 03:15:00 -04:00
|
|
|
actions.append(nio.PushSetTweak("sound", sound))
|
2020-11-01 00:55:29 -04:00
|
|
|
|
|
|
|
hint = urgency_hint
|
|
|
|
|
|
|
|
if hint or (hint is None and current.urgency_hint):
|
|
|
|
actions.append(nio.PushSetTweak("urgency_hint", True))
|
|
|
|
elif hint is False or (hint is None and not current.urgency_hint):
|
|
|
|
actions.append(nio.PushSetTweak("urgency_hint", False))
|
|
|
|
|
2020-11-03 06:36:31 -04:00
|
|
|
await self.set_pushrule_actions("global", kind, rule_id, actions)
|
2020-11-01 00:55:29 -04:00
|
|
|
|
|
|
|
|
2021-02-25 05:14:42 -04:00
|
|
|
async def mass_tweak_pushrules_actions(self, *tweaks_kwargs) -> None:
|
|
|
|
coros = [self.tweak_pushrule_actions(**kws) for kws in tweaks_kwargs]
|
2020-11-01 00:55:29 -04:00
|
|
|
await asyncio.gather(*coros)
|
|
|
|
|
|
|
|
|
2021-02-22 11:39:55 -04:00
|
|
|
async def remove_pushrule(
|
|
|
|
self, kind: Union[str, nio.PushRuleKind], rule_id: str,
|
|
|
|
) -> None:
|
|
|
|
"""Remove an existing non-builtin pushrule."""
|
|
|
|
|
|
|
|
kind = nio.PushRuleKind[kind] if isinstance(kind, str) else kind
|
2021-02-28 09:00:36 -04:00
|
|
|
|
|
|
|
if (kind.value, rule_id) in self.models[self.user_id, "pushrules"]:
|
|
|
|
await self.delete_pushrule("global", kind, rule_id)
|
|
|
|
|
|
|
|
|
2021-02-28 09:52:51 -04:00
|
|
|
def _rule_overrides_room(self, rule: PushRule) -> Optional[str]:
|
|
|
|
override = rule.kind is nio.PushRuleKind.override
|
|
|
|
one_cnd = len(rule.conditions) == 1
|
|
|
|
|
|
|
|
if not one_cnd:
|
|
|
|
return None
|
|
|
|
|
|
|
|
cnd = nio.PushCondition.from_dict(rule.conditions[0])
|
|
|
|
ev_match = isinstance(cnd, nio.PushEventMatch)
|
|
|
|
|
|
|
|
if override and ev_match and cnd.key == "room_id":
|
|
|
|
return cnd.pattern
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2021-02-28 09:00:36 -04:00
|
|
|
async def _remove_room_override_rule(self, room_id: str) -> None:
|
|
|
|
for rule in self.models[self.user_id, "pushrules"].values():
|
2021-02-28 09:52:51 -04:00
|
|
|
if self._rule_overrides_room(rule) == room_id:
|
2021-02-28 09:00:36 -04:00
|
|
|
await self.remove_pushrule(rule.kind, rule.rule_id)
|
|
|
|
|
|
|
|
|
|
|
|
async def room_pushrule_use_default(self, room_id: str) -> None:
|
|
|
|
await self._remove_room_override_rule(room_id)
|
|
|
|
await self.remove_pushrule(nio.PushRuleKind.room, room_id)
|
|
|
|
|
|
|
|
|
|
|
|
async def room_pushrule_all_events(self, room_id: str) -> None:
|
|
|
|
await self._remove_room_override_rule(room_id)
|
|
|
|
await self.edit_pushrule(
|
|
|
|
kind = nio.PushRuleKind.room,
|
|
|
|
rule_id = room_id,
|
|
|
|
actions = [nio.PushNotify(), nio.PushSetTweak("sound", "default")],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def room_pushrule_highlights_only(self, room_id: str) -> None:
|
|
|
|
await self._remove_room_override_rule(room_id)
|
|
|
|
await self.edit_pushrule(nio.PushRuleKind.room, room_id, actions=[])
|
|
|
|
|
|
|
|
|
|
|
|
async def room_pushrule_ignore_all(self, room_id: str) -> None:
|
|
|
|
await self._remove_room_override_rule(room_id)
|
|
|
|
await self.remove_pushrule(nio.PushRuleKind.room, room_id)
|
|
|
|
|
|
|
|
cnd = nio.PushEventMatch("room_id", room_id)
|
|
|
|
await self.edit_pushrule(
|
|
|
|
nio.PushRuleKind.override, room_id, conditions=[cnd], actions=[],
|
|
|
|
)
|
2021-02-22 11:39:55 -04:00
|
|
|
|
|
|
|
|
2020-06-01 09:25:09 -04:00
|
|
|
# Functions to register/modify data into models
|
|
|
|
|
2020-05-31 19:41:05 -04:00
|
|
|
async def update_account_unread_counts(self) -> None:
|
|
|
|
"""Recalculate total unread notifications/highlights for our account"""
|
|
|
|
|
2020-11-03 06:43:30 -04:00
|
|
|
unreads = 0
|
|
|
|
highlights = 0
|
|
|
|
local_unreads = False
|
2020-05-31 19:41:05 -04:00
|
|
|
|
|
|
|
for room in self.models[self.user_id, "rooms"].values():
|
|
|
|
unreads += room.unreads
|
|
|
|
highlights += room.highlights
|
|
|
|
|
2020-06-26 05:43:49 -04:00
|
|
|
if room.local_unreads:
|
|
|
|
local_unreads = True
|
|
|
|
|
2020-07-01 12:00:50 -04:00
|
|
|
account = self.models["accounts"][self.user_id]
|
|
|
|
account.set_fields(
|
|
|
|
total_unread = unreads,
|
|
|
|
total_highlights = highlights,
|
|
|
|
local_unreads = local_unreads,
|
|
|
|
)
|
2020-05-31 19:41:05 -04:00
|
|
|
|
|
|
|
|
2019-08-16 14:27:25 -04:00
|
|
|
async def event_is_past(self, ev: Union[nio.Event, Event]) -> bool:
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Return whether an event was created before this client started."""
|
|
|
|
|
2019-08-16 14:27:25 -04:00
|
|
|
if not self.first_sync_date:
|
|
|
|
return True
|
|
|
|
|
|
|
|
if isinstance(ev, Event):
|
|
|
|
return ev.date < self.first_sync_date
|
|
|
|
|
|
|
|
date = datetime.fromtimestamp(ev.server_timestamp / 1000)
|
|
|
|
return date < self.first_sync_date
|
|
|
|
|
|
|
|
|
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 08:01:22 -04:00
|
|
|
async def set_room_last_event(self, room_id: str, item: Event) -> None:
|
2019-12-18 13:16:15 -04:00
|
|
|
"""Set the `last_event` for a `Room` using data in our `Event` model.
|
|
|
|
|
|
|
|
The `last_event` is notably displayed in the UI room subtitles.
|
|
|
|
"""
|
|
|
|
|
2019-12-02 16:29:29 -04:00
|
|
|
room = self.models[self.user_id, "rooms"][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 08:01:22 -04:00
|
|
|
|
2019-12-02 16:29:29 -04:00
|
|
|
if item.date > room.last_event_date:
|
|
|
|
room.last_event_date = item.date
|
2019-08-31 16:13:50 -04: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 08:01:22 -04:00
|
|
|
|
2021-03-02 08:53:50 -04:00
|
|
|
async def lock_room_position(self, room_id: str, lock: bool) -> None:
|
|
|
|
"""Set wheter a room should try to hold its current sort position."""
|
|
|
|
|
2021-04-13 15:24:57 -04:00
|
|
|
room = self.models[self.user_id, "rooms"].get(room_id)
|
|
|
|
|
|
|
|
if not room:
|
|
|
|
return
|
2021-03-02 08:53:50 -04:00
|
|
|
|
|
|
|
if not lock:
|
|
|
|
room._sort_overrides = {}
|
|
|
|
return
|
|
|
|
|
|
|
|
for k in ("last_event_date", "unreads", "highlights", "local_unreads"):
|
|
|
|
room._sort_overrides[k] = getattr(room, k)
|
|
|
|
|
|
|
|
room.notify_change("_sort_overrides")
|
|
|
|
|
|
|
|
|
2019-12-18 13:16:15 -04:00
|
|
|
async def register_nio_room(
|
2020-05-17 15:29:23 -04:00
|
|
|
self,
|
|
|
|
room: nio.MatrixRoom,
|
|
|
|
left: bool = False,
|
|
|
|
force_register_members: bool = False,
|
2019-12-18 13:16:15 -04:00
|
|
|
) -> None:
|
2020-05-21 20:45:02 -04:00
|
|
|
"""Register/update a `nio.MatrixRoom` as a `models.items.Room`."""
|
2019-12-18 13:16:15 -04:00
|
|
|
|
2019-12-13 08:32:18 -04:00
|
|
|
inviter = getattr(room, "inviter", "") or ""
|
|
|
|
levels = room.power_levels
|
|
|
|
can_send_state = partial(levels.can_user_send_state, self.user_id)
|
|
|
|
can_send_msg = partial(levels.can_user_send_message, self.user_id)
|
2019-08-15 12:13:41 -04:00
|
|
|
|
2019-12-02 16:29:29 -04:00
|
|
|
try:
|
2020-05-17 15:29:23 -04:00
|
|
|
registered = self.models[self.user_id, "rooms"][room.room_id]
|
2019-12-02 16:29:29 -04:00
|
|
|
except KeyError:
|
2020-05-31 19:41:05 -04:00
|
|
|
registered = None
|
2021-03-02 08:53:50 -04:00
|
|
|
sort_overrides = {}
|
2020-05-31 19:41:05 -04:00
|
|
|
last_event_date = datetime.fromtimestamp(0)
|
|
|
|
typing_members = []
|
2020-06-26 05:43:49 -04:00
|
|
|
local_unreads = False
|
2020-05-31 19:41:05 -04:00
|
|
|
update_account_unread_counts = True
|
2020-06-27 11:11:14 -04:00
|
|
|
unverified_devices = (
|
|
|
|
False
|
|
|
|
if isinstance(room, nio.MatrixInvitedRoom) else
|
|
|
|
self.room_contains_unverified(room.room_id)
|
|
|
|
)
|
2020-05-17 15:29:23 -04:00
|
|
|
else:
|
2021-03-02 08:53:50 -04:00
|
|
|
sort_overrides = registered._sort_overrides
|
2020-05-31 19:41:05 -04:00
|
|
|
last_event_date = registered.last_event_date
|
|
|
|
typing_members = registered.typing_members
|
2020-06-26 05:43:49 -04:00
|
|
|
local_unreads = registered.local_unreads
|
2020-05-31 19:41:05 -04:00
|
|
|
update_account_unread_counts = (
|
|
|
|
registered.unreads != room.unread_notifications or
|
|
|
|
registered.highlights != room.unread_highlights
|
|
|
|
)
|
2020-06-27 11:11:14 -04:00
|
|
|
unverified_devices = registered.unverified_devices
|
2019-12-02 16:29:29 -04:00
|
|
|
|
2021-02-28 09:52:51 -04:00
|
|
|
notification_setting = RoomNotificationOverride.UseDefaultSettings
|
|
|
|
|
|
|
|
for rule in self.models[self.user_id, "pushrules"].values():
|
|
|
|
overrides = self._rule_overrides_room(rule) == room.room_id
|
|
|
|
is_room_kind = rule.kind is nio.PushRuleKind.room
|
|
|
|
room_kind_match = is_room_kind and rule.rule_id == room.room_id
|
|
|
|
|
|
|
|
if overrides and not rule.actions:
|
|
|
|
notification_setting = RoomNotificationOverride.IgnoreEvents
|
|
|
|
break
|
|
|
|
elif overrides:
|
|
|
|
notification_setting = RoomNotificationOverride.AllEvents
|
|
|
|
break
|
|
|
|
elif room_kind_match and not rule.actions:
|
|
|
|
notification_setting = RoomNotificationOverride.HighlightsOnly
|
|
|
|
break
|
|
|
|
elif room_kind_match:
|
|
|
|
notification_setting = RoomNotificationOverride.AllEvents
|
|
|
|
break
|
|
|
|
|
2021-02-25 05:51:48 -04:00
|
|
|
pinned = self.backend.settings.RoomList.Pinned
|
2021-02-28 09:52:51 -04:00
|
|
|
|
2020-04-29 14:00:02 -04:00
|
|
|
room_item = Room(
|
2019-12-02 16:29:29 -04:00
|
|
|
id = room.room_id,
|
2020-04-29 14:00:02 -04:00
|
|
|
for_account = self.user_id,
|
2019-12-13 17:18:36 -04:00
|
|
|
given_name = room.name or "",
|
2019-11-27 10:03:49 -04:00
|
|
|
display_name = room.display_name or "",
|
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 08:01:22 -04:00
|
|
|
avatar_url = room.gen_avatar_url or "",
|
2019-12-13 15:09:13 -04:00
|
|
|
plain_topic = room.topic or "",
|
2020-03-22 20:58:05 -04:00
|
|
|
topic = HTML.filter(
|
2020-06-02 22:03:22 -04:00
|
|
|
utils.plain2html(room.topic or ""),
|
2020-08-21 01:17:29 -04:00
|
|
|
inline = True,
|
2020-03-22 20:58:05 -04:00
|
|
|
),
|
2019-08-15 12:13:41 -04:00
|
|
|
inviter_id = inviter,
|
|
|
|
inviter_name = room.user_name(inviter) if inviter else "",
|
2019-08-17 18:17:14 -04:00
|
|
|
inviter_avatar =
|
|
|
|
(room.avatar_url(inviter) or "") if inviter else "",
|
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 08:01:22 -04:00
|
|
|
left = left,
|
2019-12-11 12:42:59 -04:00
|
|
|
|
2019-12-02 16:29:29 -04:00
|
|
|
typing_members = typing_members,
|
|
|
|
|
2020-06-27 11:11:14 -04:00
|
|
|
encrypted = room.encrypted,
|
|
|
|
unverified_devices = unverified_devices,
|
|
|
|
invite_required = room.join_rule == "invite",
|
|
|
|
guests_allowed = room.guest_access == "can_join",
|
2019-12-13 08:32:18 -04:00
|
|
|
|
2020-07-12 19:28:40 -04:00
|
|
|
default_power_level = levels.defaults.users_default,
|
2020-07-13 18:44:20 -04:00
|
|
|
own_power_level = levels.get_user_level(self.user_id),
|
2020-07-12 20:40:32 -04:00
|
|
|
can_invite = levels.can_user_invite(self.user_id),
|
|
|
|
can_kick = levels.can_user_kick(self.user_id),
|
|
|
|
can_redact_all = levels.can_user_redact(self.user_id),
|
2019-12-13 08:32:18 -04:00
|
|
|
can_send_messages = can_send_msg(),
|
|
|
|
can_set_name = can_send_state("m.room.name"),
|
|
|
|
can_set_topic = can_send_state("m.room.topic"),
|
|
|
|
can_set_avatar = can_send_state("m.room.avatar"),
|
|
|
|
can_set_encryption = can_send_state("m.room.encryption"),
|
|
|
|
can_set_join_rules = can_send_state("m.room.join_rules"),
|
|
|
|
can_set_guest_access = can_send_state("m.room.guest_access"),
|
2020-07-13 18:44:20 -04:00
|
|
|
can_set_power_levels = can_send_state("m.room.power_levels"),
|
2019-12-11 12:42:59 -04:00
|
|
|
|
2019-12-02 16:29:29 -04:00
|
|
|
last_event_date = last_event_date,
|
2020-05-31 19:06:25 -04:00
|
|
|
|
2021-02-28 09:52:51 -04:00
|
|
|
unreads = room.unread_notifications,
|
|
|
|
highlights = room.unread_highlights,
|
|
|
|
local_unreads = local_unreads,
|
|
|
|
notification_setting = notification_setting,
|
2020-09-01 14:42:08 -04:00
|
|
|
|
2020-10-07 20:12:32 -04:00
|
|
|
lexical_sorting = self.backend.settings.RoomList.lexical_sort,
|
2021-02-25 05:51:48 -04:00
|
|
|
pinned = room.room_id in pinned.get(self.user_id, []),
|
2021-03-02 08:53:50 -04:00
|
|
|
|
|
|
|
_sort_overrides = sort_overrides,
|
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 08:01:22 -04:00
|
|
|
)
|
|
|
|
|
2020-05-06 01:49:25 -04:00
|
|
|
self.models[self.user_id, "rooms"][room.room_id] = room_item
|
2020-04-29 14:00:02 -04:00
|
|
|
|
2020-05-17 15:29:23 -04:00
|
|
|
if not registered or force_register_members:
|
2020-08-23 17:55:31 -04:00
|
|
|
model = self.models[self.user_id, room.room_id, "members"]
|
|
|
|
|
|
|
|
# The members we initially get from lazy sync may be outdated
|
|
|
|
# and contain members that already left.
|
|
|
|
# tuple() used to avoid "dict changed size during iteration".
|
|
|
|
for member_id in tuple(model):
|
|
|
|
if member_id not in room.users:
|
|
|
|
await self.remove_member(room, member_id)
|
|
|
|
|
2020-05-17 15:29:23 -04:00
|
|
|
for user_id in room.users:
|
|
|
|
await self.add_member(room, user_id)
|
|
|
|
|
2020-05-31 19:41:05 -04:00
|
|
|
if update_account_unread_counts:
|
|
|
|
await self.update_account_unread_counts()
|
|
|
|
|
2020-05-17 15:29:23 -04:00
|
|
|
|
|
|
|
async def add_member(self, room: nio.MatrixRoom, user_id: str) -> None:
|
2020-05-21 20:45:02 -04:00
|
|
|
"""Register/update a room member into our models."""
|
2020-09-05 16:47:34 -04:00
|
|
|
|
|
|
|
room_id = room.room_id
|
|
|
|
member_model = self.models[self.user_id, room_id, "members"]
|
|
|
|
member = room.users[user_id]
|
|
|
|
presence = self.backend.presences.get(user_id, None)
|
|
|
|
|
|
|
|
try:
|
|
|
|
registered = member_model[user_id]
|
|
|
|
except KeyError:
|
|
|
|
last_read_event = self.unassigned_member_last_read_event\
|
|
|
|
.pop((room_id, user_id), "")
|
|
|
|
else:
|
|
|
|
last_read_event = registered.last_read_event
|
|
|
|
|
2020-07-02 11:44:54 -03:00
|
|
|
member_item = Member(
|
2020-09-05 16:47:34 -04:00
|
|
|
id = user_id,
|
|
|
|
display_name = room.user_name(user_id) # disambiguated
|
|
|
|
if member.display_name else "",
|
|
|
|
avatar_url = member.avatar_url or "",
|
|
|
|
typing = user_id in room.typing_users,
|
|
|
|
power_level = member.power_level,
|
|
|
|
invited = member.invited,
|
|
|
|
last_read_event = last_read_event,
|
2020-05-17 15:29:23 -04:00
|
|
|
)
|
|
|
|
|
2020-07-02 21:25:24 -03:00
|
|
|
# Associate presence with member, if it exists
|
2020-07-10 11:59:26 -03:00
|
|
|
if presence:
|
2020-09-05 16:47:34 -04:00
|
|
|
presence.members[room_id] = member_item
|
2020-07-02 11:44:54 -03:00
|
|
|
|
2020-07-02 21:25:24 -03:00
|
|
|
# And then update presence fields
|
|
|
|
presence.update_members()
|
2020-07-02 11:44:54 -03:00
|
|
|
|
2020-09-05 16:47:34 -04:00
|
|
|
member_model[user_id] = member_item
|
2020-07-02 11:44:54 -03: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 08:01:22 -04:00
|
|
|
|
2020-05-17 15:29:23 -04:00
|
|
|
async def remove_member(self, room: nio.MatrixRoom, user_id: str) -> None:
|
2020-05-21 20:45:02 -04:00
|
|
|
"""Remove a room member from our models."""
|
2020-07-11 17:23:36 -04:00
|
|
|
|
2020-05-17 15:29:23 -04:00
|
|
|
self.models[self.user_id, room.room_id, "members"].pop(user_id, None)
|
2020-03-22 20:58:05 -04:00
|
|
|
|
2020-07-11 18:47:28 -04:00
|
|
|
room_item = self.models[self.user_id, "rooms"].get(room.room_id)
|
|
|
|
|
|
|
|
if room_item:
|
|
|
|
room_item.unverified_devices = \
|
|
|
|
self.room_contains_unverified(room.room_id)
|
2020-07-11 17:23:36 -04: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 08:01:22 -04:00
|
|
|
|
2020-05-20 03:42:40 -04:00
|
|
|
async def get_event_profiles(self, room_id: str, event_id: str) -> None:
|
|
|
|
"""Fetch from network an event's sender, target and remover's profile.
|
2019-12-18 13:16:15 -04:00
|
|
|
|
2020-05-20 03:42:40 -04:00
|
|
|
This should be called from QML, see `MatrixClient.get_member_profile`'s
|
|
|
|
docstring.
|
|
|
|
"""
|
|
|
|
|
|
|
|
ev: Event = self.models[self.user_id, room_id, "events"][event_id]
|
|
|
|
|
|
|
|
if not ev.fetch_profile:
|
|
|
|
return
|
|
|
|
|
|
|
|
get_profile = partial(
|
|
|
|
self.get_member_profile, room_id, can_fetch_from_network=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
if not ev.sender_name and not ev.sender_avatar:
|
|
|
|
sender_name, sender_avatar, _ = await get_profile(ev.sender_id)
|
2020-07-01 12:00:50 -04:00
|
|
|
ev.set_fields(sender_name=sender_name, sender_avatar=sender_avatar)
|
2020-05-20 03:42:40 -04:00
|
|
|
|
|
|
|
if ev.target_id and not ev.target_name and not ev.target_avatar:
|
|
|
|
target_name, target_avatar, _ = await get_profile(ev.target_id)
|
2020-07-01 12:00:50 -04:00
|
|
|
ev.set_fields(target_name=target_name, target_avatar=target_avatar)
|
2020-05-20 03:42:40 -04:00
|
|
|
|
|
|
|
if ev.redacter_id and not ev.redacter_name:
|
|
|
|
redacter_name, _, _ = await get_profile(ev.target_id)
|
|
|
|
ev.redacter_name = redacter_name
|
|
|
|
|
|
|
|
ev.fetch_profile = False
|
|
|
|
|
|
|
|
|
|
|
|
async def get_member_profile(
|
|
|
|
self, room_id: str, user_id: str, can_fetch_from_network: bool = False,
|
|
|
|
) -> Tuple[str, str, bool]:
|
|
|
|
"""Return a room member's (display_name, avatar, should_lazy_fetch)
|
|
|
|
|
|
|
|
The returned tuple's last element tells whether
|
|
|
|
`MatrixClient.get_event_profiles()` should be called by QML
|
|
|
|
with `can_fetch_from_network = True` when appropriate,
|
|
|
|
e.g. when this message comes in the user's view.
|
|
|
|
|
|
|
|
If the member isn't found in the room (e.g. they left) and
|
|
|
|
`can_fetch_from_network` is `True`, their
|
2020-08-23 16:18:54 -04:00
|
|
|
profile is retrieved using `MatrixClient.get_profile()`.
|
2019-12-18 13:16:15 -04: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 08:01:22 -04:00
|
|
|
try:
|
2020-05-20 03:42:40 -04:00
|
|
|
member = self.models[self.user_id, room_id, "members"][user_id]
|
|
|
|
return (member.display_name, member.avatar_url, False)
|
|
|
|
|
|
|
|
except KeyError: # e.g. member is not in the room anymore
|
|
|
|
if not can_fetch_from_network:
|
|
|
|
return ("", "", True)
|
2019-12-18 13:16:15 -04:00
|
|
|
|
2019-11-12 08:47:03 -04:00
|
|
|
try:
|
2020-08-23 16:18:54 -04:00
|
|
|
info = await self.get_profile(user_id)
|
2020-05-20 03:42:40 -04:00
|
|
|
return (info.displayname or "", info.avatar_url or "", False)
|
2019-11-12 08:47:03 -04:00
|
|
|
except MatrixError:
|
2020-05-20 03:42:40 -04:00
|
|
|
return ("", "", False)
|
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 08:01:22 -04:00
|
|
|
|
|
|
|
|
2020-09-16 13:06:34 -04:00
|
|
|
async def get_notification_avatar(self, mxc: str, user_id: str) -> Path:
|
|
|
|
"""Get the path to an avatar for desktop notifications."""
|
|
|
|
# TODO: test this function on windows
|
|
|
|
|
|
|
|
if mxc in self.backend.notification_avatar_cache:
|
|
|
|
return self.backend.notification_avatar_cache[mxc]
|
|
|
|
|
|
|
|
avatar_size = (48, 48)
|
|
|
|
|
2021-01-20 15:50:04 -04:00
|
|
|
avatar_path = await Thumbnail(
|
|
|
|
cache = self.backend.media_cache,
|
2020-09-16 13:06:34 -04:00
|
|
|
client_user_id = self.user_id,
|
|
|
|
mxc = mxc,
|
|
|
|
title = f"user_{user_id}.notification",
|
2021-01-20 15:50:04 -04:00
|
|
|
wanted_size = avatar_size,
|
|
|
|
).get()
|
2020-09-16 13:06:34 -04:00
|
|
|
|
|
|
|
image_data = None
|
|
|
|
create = False
|
|
|
|
|
2020-11-15 14:57:00 -04:00
|
|
|
async with utils.aiopen(avatar_path, "rb") as file:
|
2020-09-16 13:06:34 -04:00
|
|
|
if await utils.is_svg(file):
|
|
|
|
await file.seek(0, 0)
|
|
|
|
|
|
|
|
create = True
|
|
|
|
image_data = cairosvg.svg2png(
|
|
|
|
bytestring = await file.read(),
|
|
|
|
parent_width = avatar_size[0],
|
|
|
|
parent_height = avatar_size[1],
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
await file.seek(0, 0)
|
|
|
|
image_data = await file.read()
|
|
|
|
|
|
|
|
pil_image = PILImage.open(io.BytesIO(image_data))
|
|
|
|
|
|
|
|
if pil_image.size != avatar_size:
|
|
|
|
create = True
|
|
|
|
pil_image.thumbnail(avatar_size)
|
|
|
|
|
|
|
|
if IS_WINDOWS and pil_image.format != "ICO":
|
|
|
|
create = True
|
|
|
|
|
|
|
|
if not create:
|
|
|
|
self.backend.notification_avatar_cache[mxc] = avatar_path
|
|
|
|
return avatar_path
|
|
|
|
|
|
|
|
out = io.BytesIO()
|
|
|
|
|
|
|
|
if IS_WINDOWS:
|
|
|
|
pil_image.save(out, "ICO", sizes=[avatar_size])
|
|
|
|
else:
|
|
|
|
pil_image.save(out, "PNG")
|
|
|
|
|
|
|
|
thumb = await Thumbnail.from_bytes(
|
|
|
|
cache = self.backend.media_cache,
|
|
|
|
client_user_id = self.user_id,
|
|
|
|
mxc = mxc,
|
|
|
|
filename = f"user_{user_id}.notification",
|
|
|
|
overwrite = True,
|
|
|
|
data = out.getvalue(),
|
|
|
|
wanted_size = avatar_size,
|
|
|
|
)
|
|
|
|
|
|
|
|
path = await thumb.get()
|
|
|
|
self.backend.notification_avatar_cache[mxc] = path
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
2019-08-16 01:51:42 -04:00
|
|
|
async def register_nio_event(
|
2020-04-01 14:33:19 -03:00
|
|
|
self,
|
2020-05-20 03:42:40 -04:00
|
|
|
room: nio.MatrixRoom,
|
2021-03-12 16:12:40 -04:00
|
|
|
ev: Union[nio.Event, nio.BadEvent],
|
2020-05-20 03:42:40 -04:00
|
|
|
event_id: str = "",
|
|
|
|
override_fetch_profile: Optional[bool] = None,
|
2020-03-29 19:06:13 -03:00
|
|
|
**fields,
|
2020-07-20 22:58:02 -04:00
|
|
|
) -> Event:
|
2020-05-21 20:45:02 -04:00
|
|
|
"""Register/update a `nio.Event` as a `models.items.Event` object."""
|
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 08:01:22 -04:00
|
|
|
|
|
|
|
await self.register_nio_room(room)
|
|
|
|
|
2020-05-20 03:42:40 -04:00
|
|
|
sender_name, sender_avatar, must_fetch_sender = \
|
|
|
|
await self.get_member_profile(room.room_id, ev.sender)
|
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 08:01:22 -04:00
|
|
|
|
|
|
|
target_id = getattr(ev, "state_key", "") or ""
|
|
|
|
|
2020-05-20 03:42:40 -04:00
|
|
|
target_name, target_avatar, must_fetch_target = \
|
|
|
|
await self.get_member_profile(room.room_id, target_id) \
|
|
|
|
if target_id else ("", "", False)
|
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 08:01:22 -04:00
|
|
|
|
2020-02-11 17:32:22 -04:00
|
|
|
content = fields.get("content", "").strip()
|
|
|
|
|
|
|
|
if content and "inline_content" not in fields:
|
2020-08-21 01:17:29 -04:00
|
|
|
fields["inline_content"] = HTML.filter(content, inline=True)
|
2020-02-11 17:32:22 -04:00
|
|
|
|
2020-09-05 16:47:34 -04:00
|
|
|
event_model = self.models[self.user_id, room.room_id, "events"]
|
|
|
|
|
|
|
|
try:
|
|
|
|
registered = event_model[event_id or ev.event_id]
|
|
|
|
except KeyError:
|
|
|
|
last_read_by = self.unassigned_event_last_read_by.pop(
|
|
|
|
event_id or ev.event_id, {},
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
last_read_by = registered.last_read_by
|
|
|
|
|
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 08:01:22 -04:00
|
|
|
# Create Event ModelItem
|
2020-03-07 12:35:12 -04: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 08:01:22 -04:00
|
|
|
item = Event(
|
2020-03-29 19:06:13 -03:00
|
|
|
id = event_id or ev.event_id,
|
2019-09-13 23:02:11 -04:00
|
|
|
event_id = ev.event_id,
|
2020-02-11 17:17:11 -04:00
|
|
|
event_type = type(ev),
|
2019-12-02 16:29:29 -04:00
|
|
|
source = ev,
|
2020-03-29 19:06:13 -03:00
|
|
|
date = datetime.fromtimestamp(ev.server_timestamp / 1000),
|
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 08:01:22 -04:00
|
|
|
sender_id = ev.sender,
|
|
|
|
sender_name = sender_name,
|
|
|
|
sender_avatar = sender_avatar,
|
|
|
|
target_id = target_id,
|
|
|
|
target_name = target_name,
|
|
|
|
target_avatar = target_avatar,
|
2020-02-11 17:32:22 -04:00
|
|
|
links = Event.parse_links(content),
|
2020-09-05 16:47:34 -04:00
|
|
|
last_read_by = last_read_by,
|
2020-09-14 11:33:16 -04:00
|
|
|
read_by_count = len(last_read_by),
|
2020-05-20 06:17:14 -04:00
|
|
|
|
2020-05-20 03:42:40 -04:00
|
|
|
fetch_profile =
|
|
|
|
(must_fetch_sender or must_fetch_target)
|
|
|
|
if override_fetch_profile is None else
|
|
|
|
override_fetch_profile,
|
2020-09-05 16:47:34 -04:00
|
|
|
|
2019-09-13 22:34:20 -04:00
|
|
|
**fields,
|
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 08:01:22 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
# Add the Event to model
|
2020-03-07 12:35:12 -04:00
|
|
|
|
2020-03-15 17:26:41 -04:00
|
|
|
model = self.models[self.user_id, room.room_id, "events"]
|
|
|
|
|
2019-12-16 10:36:59 -04:00
|
|
|
tx_id = ev.source.get("content", {}).get(
|
2020-09-22 21:22:38 -04:00
|
|
|
f"{__reverse_dns__}.transaction_id",
|
2019-12-16 10:36:59 -04:00
|
|
|
)
|
2020-04-16 14:08:24 -04:00
|
|
|
from_us = ev.sender in self.backend.clients
|
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 08:01:22 -04:00
|
|
|
|
2020-04-16 14:08:24 -04:00
|
|
|
if from_us and tx_id and f"echo-{tx_id}" in model:
|
2020-09-05 16:47:34 -04:00
|
|
|
item.id = f"echo-{tx_id}"
|
|
|
|
self.event_to_echo_ids[ev.event_id] = item.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 08:01:22 -04:00
|
|
|
|
2020-09-16 10:53:29 -04:00
|
|
|
model[item.id] = item
|
|
|
|
await self.set_room_last_event(room.room_id, item)
|
|
|
|
|
2020-09-16 10:57:29 -04:00
|
|
|
if from_us:
|
|
|
|
return item
|
|
|
|
|
2020-09-19 15:42:25 -04:00
|
|
|
if await self.event_is_past(ev):
|
2020-09-16 10:57:29 -04:00
|
|
|
await self.update_account_unread_counts()
|
2020-09-16 10:53:29 -04:00
|
|
|
return item
|
|
|
|
|
2021-01-21 11:16:03 -04:00
|
|
|
if self.backend.settings.RoomList.local_unread_markers:
|
|
|
|
room_item = self.models[self.user_id, "rooms"][room.room_id]
|
|
|
|
room_item.local_unreads = True
|
|
|
|
await self.update_account_unread_counts()
|
2020-09-16 10:53:29 -04:00
|
|
|
|
2020-11-03 07:29:32 -04:00
|
|
|
# Alerts & notifications
|
2020-09-16 10:53:29 -04:00
|
|
|
|
2020-11-03 07:29:32 -04:00
|
|
|
name = self.models["accounts"][self.user_id].display_name
|
|
|
|
nio_rule = self.push_rules.global_rules.matching_rule(ev, room, name)
|
2020-09-16 10:53:29 -04:00
|
|
|
|
2020-11-03 07:29:32 -04:00
|
|
|
if not nio_rule:
|
|
|
|
return item
|
2020-09-16 10:53:29 -04:00
|
|
|
|
2020-11-03 07:29:32 -04:00
|
|
|
model = self.models[self.user_id, "pushrules"]
|
|
|
|
rule = model[nio_rule.kind.value, nio_rule.id]
|
2020-03-30 08:02:33 -04:00
|
|
|
|
2020-11-03 07:29:32 -04:00
|
|
|
if not rule.notify and not rule.highlight:
|
|
|
|
return item
|
2020-09-16 20:58:02 -04:00
|
|
|
|
2021-02-28 12:07:00 -04:00
|
|
|
if must_fetch_sender:
|
|
|
|
sender_name, sender_avatar, _ = await self.get_member_profile(
|
|
|
|
room.room_id, ev.sender, can_fetch_from_network=True,
|
|
|
|
)
|
|
|
|
item.set_fields(
|
|
|
|
sender_name=sender_name, sender_avatar=sender_avatar,
|
|
|
|
)
|
|
|
|
|
2021-02-28 12:40:24 -04:00
|
|
|
sender = item.sender_name or item.sender_id
|
|
|
|
is_linux = platform.system() == "Linux"
|
|
|
|
use_html = is_linux and self.backend.settings.Notifications.use_html
|
|
|
|
content = item.inline_content if use_html else item.plain_content
|
|
|
|
|
|
|
|
if isinstance(ev, nio.RoomMessageEmote) and use_html:
|
|
|
|
body = f"<i>{sender} {content}</i>"
|
|
|
|
elif isinstance(ev, nio.RoomMessageEmote):
|
|
|
|
body = f"{sender} {content}"
|
2020-11-03 07:29:32 -04:00
|
|
|
elif not isinstance(ev, nio.RoomMessage):
|
2021-02-28 12:40:24 -04:00
|
|
|
body = content.replace(
|
2020-11-03 07:29:32 -04:00
|
|
|
"%1", item.sender_name or item.sender_id,
|
|
|
|
).replace(
|
|
|
|
"%2", item.target_name or item.target_id,
|
2020-03-30 08:02:33 -04:00
|
|
|
)
|
2020-11-03 07:29:32 -04:00
|
|
|
elif room.member_count == 2 and room.display_name == sender:
|
2021-02-28 12:40:24 -04:00
|
|
|
body = content
|
2020-11-03 07:29:32 -04:00
|
|
|
else:
|
2021-02-28 12:40:24 -04:00
|
|
|
body = f"{sender}: {content}"
|
2020-11-03 07:29:32 -04:00
|
|
|
|
|
|
|
NotificationRequested(
|
|
|
|
id = item.id,
|
|
|
|
critical = rule.highlight,
|
|
|
|
bubble = rule.bubble,
|
|
|
|
sound = rule.sound,
|
|
|
|
urgency_hint = rule.urgency_hint,
|
|
|
|
|
|
|
|
title = room.display_name,
|
|
|
|
body = body.replace(" ⏎ ", "<br>")
|
|
|
|
.replace(" ⏎⏎ ", f"<br>{'─' * 24}<br>"),
|
|
|
|
|
|
|
|
image = await self.get_notification_avatar(
|
|
|
|
mxc=item.sender_avatar, user_id=item.sender_id,
|
|
|
|
) if item.sender_avatar else "",
|
|
|
|
)
|
2020-03-30 08:02:33 -04:00
|
|
|
|
2020-07-20 22:58:02 -04:00
|
|
|
return item
|