From a4c33f8edb7bd8e3716bae83e712929e8698196a Mon Sep 17 00:00:00 2001 From: miruka Date: Tue, 7 Apr 2020 09:54:32 -0400 Subject: [PATCH] Ensure first event for each rooms is a visible one Dependening on the user's config, request for the first sync that profile changes, membership changes or unknown events be filtered out. This ensures we have something to show as the room subtitle (last message) in the UI. --- src/backend/matrix_client.py | 37 ++++++++++++++++++++++++++++++++---- src/backend/nio_callbacks.py | 3 --- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index 04b91da6..62f31df4 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -16,8 +16,8 @@ from datetime import datetime, timedelta from functools import partial from pathlib import Path from typing import ( - TYPE_CHECKING, Any, ClassVar, DefaultDict, Dict, List, NamedTuple, - Optional, Set, Tuple, Type, Union, + TYPE_CHECKING, Any, ClassVar, Dict, List, NamedTuple, Optional, Set, Tuple, + Type, Union, ) from urllib.parse import urlparse from uuid import UUID, uuid4 @@ -109,6 +109,27 @@ class MatrixClient(nio.AsyncClient): }, } + no_profile_events_filter: ClassVar[Dict[str, Any]] = { + "room": { + "timeline": {"not_types": ["m.room.member"]}, + }, + } + + no_unknown_events_filter: ClassVar[Dict[str, Any]] = { + "room": { + "timeline": { + "not_types": [ + "m.room.message.feedback", + "m.room.pinned_events", + "m.call.*", + "m.room.third_part_invite", + "m.room.tombstone", + "m.reaction", + ], + }, + }, + } + def __init__(self, backend, @@ -155,8 +176,6 @@ class MatrixClient(nio.AsyncClient): self.loaded_once_rooms: Set[str] = set() # {room_id} self.cleared_events_rooms: Set[str] = set() # {room_id} - self.skipped_events: DefaultDict[str, int] = DefaultDict(lambda: 0) - self.nio_callbacks = NioCallbacks(self) @@ -299,6 +318,16 @@ class MatrixClient(nio.AsyncClient): filter1 = deepcopy(self.lazy_load_filter) utils.dict_update_recursive(filter1, self.limit_1_filter) + + cfg = self.backend.ui_settings + if cfg["hideProfileChangeEvents"] or cfg["hideMembershipEvents"]: + utils.dict_update_recursive(filter1, self.no_profile_events_filter) + + if cfg["hideUnknownEvents"]: + filter1["room"]["timeline"]["not_types"].extend( + self.no_unknown_events_filter["room"]["timeline"]["not_types"], + ) + while True: try: self.sync_task = asyncio.ensure_future(self.sync_forever( diff --git a/src/backend/nio_callbacks.py b/src/backend/nio_callbacks.py index 580a6d0b..55973805 100644 --- a/src/backend/nio_callbacks.py +++ b/src/backend/nio_callbacks.py @@ -264,7 +264,6 @@ class NioCallbacks: # Membership changes if not prev or membership != prev_membership: if self.client.backend.ui_settings["hideMembershipEvents"]: - self.client.skipped_events[room.room_id] += 1 return None reason = f", reason: {now['reason']}" if now.get("reason") else "" @@ -327,7 +326,6 @@ class NioCallbacks: account.avatar_url = now.get("avatar_url") or "" if self.client.backend.ui_settings["hideProfileChangeEvents"]: - self.client.skipped_events[room.room_id] += 1 return None return ( @@ -419,7 +417,6 @@ class NioCallbacks: async def onUnknownEvent(self, room, ev) -> None: if self.client.backend.ui_settings["hideUnknownEvents"]: - self.client.skipped_events[room.room_id] += 1 return co = f"%1 sent an unsupported {ev.type} event"