Add ignoring invite sender & fix forgotten rooms

When declining an invite, the popup now offers a checkbox to also add
the sender to the account data m.ignored_user_list event.

This will hide any further invite or message from that user in syncs,
and also immediatly remove the ones we currently have.

As a side-effect, the long-time bug that caused forgotten rooms to
immediatly reappear due to received a "<user> left" event has been
fixed.
This commit is contained in:
miruka
2021-04-14 10:58:53 -04:00
parent 8ea951957d
commit 678a56400a
3 changed files with 100 additions and 10 deletions

View File

@@ -210,6 +210,7 @@ class MatrixClient(nio.AsyncClient):
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}
self.ignored_rooms: Set[str] = set() # {room_id}
self.event_to_echo_ids: Dict[str, str] = {}
@@ -220,7 +221,8 @@ class MatrixClient(nio.AsyncClient):
self.unassigned_event_last_read_by: DefaultDict[str, Dict[str, int]] =\
DefaultDict(dict)
self.push_rules: nio.PushRulesEvent = nio.PushRulesEvent()
self.push_rules: nio.PushRulesEvent = nio.PushRulesEvent()
self.ignored_user_ids: Set[str] = set()
# {room_id: event}
self.power_level_events: Dict[str, nio.PowerLevelsEvent] = {}
@@ -523,6 +525,51 @@ class MatrixClient(nio.AsyncClient):
await asyncio.sleep(0.2)
async def ignore_user(self, user_id: str, ignore: bool) -> None:
ignored = self.ignored_user_ids.copy()
if ignore:
ignored.add(user_id)
else:
ignored.discard(user_id)
path = ["user", self.user_id, "account_data", "m.ignored_user_list"]
params = {"access_token": self.access_token}
await self._send(
nio.responses.EmptyResponse,
"PUT",
nio.Api._build_path(path, params),
nio.Api.to_json({"ignored_users": {u: {} for u in ignored}}),
)
if not ignore:
return
# Invites and messages from ignored users won't be returned anymore on
# syncs, thus will be absent on client restart. Clean up immediatly:
room_model = self.models[self.user_id, "rooms"]
with room_model.batch_remove():
for room_id, room in room_model.copy().items():
if room.inviter_id == user_id:
self.ignored_rooms.add(room_id)
del room_model[room_id]
self.models.pop((self.user_id, room_id, "events"), None)
self.models.pop((self.user_id, room_id, "members"), None)
continue
event_model = self.models[self.user_id, room_id, "events"]
with event_model.batch_remove():
for event_id, event in event_model.copy().items():
if event.sender_id == user_id:
del event_model[event_id]
await self.update_account_unread_counts()
async def can_kick(self, room_id: str, target_user_id: str) -> bool:
"""Return whether we can kick a certain user in a room."""
@@ -1172,6 +1219,7 @@ class MatrixClient(nio.AsyncClient):
will be marked as suitable for destruction by the server.
"""
self.ignored_rooms.add(room_id)
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)
@@ -2057,6 +2105,8 @@ class MatrixClient(nio.AsyncClient):
) -> None:
"""Register/update a `nio.MatrixRoom` as a `models.items.Room`."""
self.ignored_rooms.discard(room.room_id)
inviter = getattr(room, "inviter", "") or ""
levels = room.power_levels
can_send_state = partial(levels.can_user_send_state, self.user_id)