2020-09-24 09:57:54 +10:00
|
|
|
# Copyright Mirage authors & contributors <https://github.com/mirukana/mirage>
|
2020-05-06 14:26:52 +10:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
2020-05-12 20:09:07 +10:00
|
|
|
from dataclasses import asdict
|
2020-08-21 02:21:47 +10:00
|
|
|
from typing import Dict, Set
|
2020-05-12 20:09:07 +10:00
|
|
|
|
2020-08-21 02:21:47 +10:00
|
|
|
from .filters import FieldStringFilter, FieldSubstringFilter, ModelFilter
|
2020-05-14 10:22:48 +10:00
|
|
|
from .items import Account, AccountOrRoom, Room
|
2020-05-06 14:26:52 +10:00
|
|
|
from .model import Model
|
2020-05-11 04:26:26 +10:00
|
|
|
from .model_item import ModelItem
|
2020-05-06 14:26:52 +10:00
|
|
|
|
|
|
|
|
|
|
|
class AllRooms(FieldSubstringFilter):
|
2020-05-22 10:45:02 +10:00
|
|
|
"""Flat filtered list of all accounts and their rooms."""
|
|
|
|
|
2020-05-14 17:33:34 +10:00
|
|
|
def __init__(self, accounts: Model) -> None:
|
|
|
|
self.accounts = accounts
|
2020-05-14 10:22:48 +10:00
|
|
|
self._collapsed: Set[str] = set()
|
|
|
|
|
2020-05-23 06:47:15 +10:00
|
|
|
super().__init__(sync_id="all_rooms", fields=("display_name",))
|
|
|
|
self.items_changed_callbacks.append(self.refilter_accounts)
|
|
|
|
|
2020-05-14 10:22:48 +10:00
|
|
|
|
|
|
|
def set_account_collapse(self, user_id: str, collapsed: bool) -> None:
|
2020-05-22 10:45:02 +10:00
|
|
|
"""Set whether the rooms for an account should be filtered out."""
|
|
|
|
|
2020-05-14 10:22:48 +10:00
|
|
|
def only_if(item):
|
|
|
|
return item.type is Room and item.for_account == user_id
|
|
|
|
|
|
|
|
if collapsed and user_id not in self._collapsed:
|
|
|
|
self._collapsed.add(user_id)
|
|
|
|
self.refilter(only_if)
|
|
|
|
|
|
|
|
if not collapsed and user_id in self._collapsed:
|
|
|
|
self._collapsed.remove(user_id)
|
|
|
|
self.refilter(only_if)
|
|
|
|
|
2020-05-06 14:26:52 +10:00
|
|
|
|
|
|
|
def accept_source(self, source: Model) -> bool:
|
2020-05-12 20:09:07 +10:00
|
|
|
return source.sync_id == "accounts" or (
|
2020-05-06 14:26:52 +10:00
|
|
|
isinstance(source.sync_id, tuple) and
|
|
|
|
len(source.sync_id) == 2 and
|
|
|
|
source.sync_id[1] == "rooms" # type: ignore
|
|
|
|
)
|
2020-05-11 04:26:26 +10:00
|
|
|
|
|
|
|
|
2020-05-12 20:09:07 +10:00
|
|
|
def convert_item(self, item: ModelItem) -> AccountOrRoom:
|
2020-05-14 17:33:34 +10:00
|
|
|
return AccountOrRoom(
|
|
|
|
**asdict(item),
|
|
|
|
type = type(item), # type: ignore
|
|
|
|
|
|
|
|
account_order =
|
|
|
|
item.order if isinstance(item, Account) else
|
|
|
|
self.accounts[item.for_account].order, # type: ignore
|
|
|
|
)
|
2020-05-12 20:09:07 +10:00
|
|
|
|
|
|
|
|
|
|
|
def accept_item(self, item: ModelItem) -> bool:
|
2020-05-14 10:22:48 +10:00
|
|
|
assert isinstance(item, AccountOrRoom) # nosec
|
|
|
|
|
|
|
|
if not self.filter and \
|
|
|
|
item.type is Room and \
|
|
|
|
item.for_account in self._collapsed:
|
|
|
|
return False
|
|
|
|
|
2020-05-12 20:09:07 +10:00
|
|
|
matches_filter = super().accept_item(item)
|
|
|
|
|
2020-05-14 10:22:48 +10:00
|
|
|
if item.type is not Account or not self.filter:
|
2020-05-12 20:09:07 +10:00
|
|
|
return matches_filter
|
|
|
|
|
|
|
|
return next(
|
|
|
|
(i for i in self.values() if i.for_account == item.id), False,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def refilter_accounts(self) -> None:
|
2020-05-14 10:22:48 +10:00
|
|
|
self.refilter(lambda i: i.type is Account) # type: ignore
|
2020-05-12 20:09:07 +10:00
|
|
|
|
|
|
|
|
2020-05-11 04:26:26 +10:00
|
|
|
class MatchingAccounts(ModelFilter):
|
2020-05-22 10:45:02 +10:00
|
|
|
"""List of our accounts in `AllRooms` with at least one matching room if
|
|
|
|
a `filter` is set, else list of all accounts.
|
|
|
|
"""
|
|
|
|
|
2020-05-11 04:26:26 +10:00
|
|
|
def __init__(self, all_rooms: AllRooms) -> None:
|
|
|
|
self.all_rooms = all_rooms
|
|
|
|
self.all_rooms.items_changed_callbacks.append(self.refilter)
|
|
|
|
|
2020-05-11 05:12:59 +10:00
|
|
|
super().__init__(sync_id="matching_accounts")
|
|
|
|
|
2020-05-11 04:26:26 +10:00
|
|
|
|
|
|
|
def accept_source(self, source: Model) -> bool:
|
|
|
|
return source.sync_id == "accounts"
|
|
|
|
|
|
|
|
|
|
|
|
def accept_item(self, item: ModelItem) -> bool:
|
|
|
|
if not self.all_rooms.filter:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return next(
|
2020-05-12 20:09:07 +10:00
|
|
|
(i for i in self.all_rooms.values() if i.id == item.id),
|
2020-05-11 04:26:26 +10:00
|
|
|
False,
|
|
|
|
)
|
2020-05-11 04:58:59 +10:00
|
|
|
|
|
|
|
|
|
|
|
class FilteredMembers(FieldSubstringFilter):
|
2020-05-22 10:45:02 +10:00
|
|
|
"""Filtered list of members for a room."""
|
|
|
|
|
2020-05-11 04:58:59 +10:00
|
|
|
def __init__(self, user_id: str, room_id: str) -> None:
|
|
|
|
self.user_id = user_id
|
|
|
|
self.room_id = room_id
|
|
|
|
sync_id = (user_id, room_id, "filtered_members")
|
|
|
|
|
|
|
|
super().__init__(sync_id=sync_id, fields=("display_name",))
|
|
|
|
|
|
|
|
|
|
|
|
def accept_source(self, source: Model) -> bool:
|
|
|
|
return source.sync_id == (self.user_id, self.room_id, "members")
|
2020-08-19 18:38:29 +10:00
|
|
|
|
|
|
|
|
2020-08-21 02:21:47 +10:00
|
|
|
class AutoCompletedMembers(FieldStringFilter):
|
|
|
|
"""Filtered list of mentionable members for tab-completion."""
|
|
|
|
|
|
|
|
def __init__(self, user_id: str, room_id: str) -> None:
|
|
|
|
self.user_id = user_id
|
|
|
|
self.room_id = room_id
|
|
|
|
sync_id = (user_id, room_id, "autocompleted_members")
|
|
|
|
|
2020-08-24 00:54:03 +10:00
|
|
|
super().__init__(
|
|
|
|
sync_id = sync_id,
|
|
|
|
fields = ("display_name", "id"),
|
|
|
|
no_filter_accept_all_items = False,
|
|
|
|
)
|
2020-08-21 02:21:47 +10:00
|
|
|
|
|
|
|
|
|
|
|
def accept_source(self, source: Model) -> bool:
|
|
|
|
return source.sync_id == (self.user_id, self.room_id, "members")
|
|
|
|
|
|
|
|
|
|
|
|
def match(self, fields: Dict[str, str], filtr: str) -> bool:
|
|
|
|
fields["id"] = fields["id"][1:] # remove leading @
|
|
|
|
return super().match(fields, filtr)
|
|
|
|
|
|
|
|
|
2020-08-19 18:38:29 +10:00
|
|
|
class FilteredHomeservers(FieldSubstringFilter):
|
|
|
|
"""Filtered list of public Matrix homeservers."""
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
2020-08-19 20:37:26 +10:00
|
|
|
super().__init__(sync_id="filtered_homeservers", fields=("id", "name"))
|
2020-08-19 18:38:29 +10:00
|
|
|
|
|
|
|
|
|
|
|
def accept_source(self, source: Model) -> bool:
|
|
|
|
return source.sync_id == "homeservers"
|