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-05-11 04:26:26 +10:00
|
|
|
from .filters import FieldSubstringFilter, ModelFilter
|
2020-05-12 20:09:07 +10:00
|
|
|
from .items import Account, AccountOrRoom
|
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):
|
|
|
|
def __init__(self) -> None:
|
|
|
|
super().__init__(sync_id="all_rooms", fields=("display_name",))
|
2020-05-12 20:09:07 +10:00
|
|
|
self.items_changed_callbacks.append(self.refilter_accounts)
|
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:
|
|
|
|
return AccountOrRoom(**asdict(item), type=type(item)) # type: ignore
|
|
|
|
|
|
|
|
|
|
|
|
def accept_item(self, item: ModelItem) -> bool:
|
|
|
|
matches_filter = super().accept_item(item)
|
|
|
|
|
|
|
|
if item.type is not Account or not self.filter: # type: ignore
|
|
|
|
return matches_filter
|
|
|
|
|
|
|
|
return next(
|
|
|
|
(i for i in self.values() if i.for_account == item.id), False,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def refilter_accounts(self) -> None:
|
|
|
|
self.refilter(
|
|
|
|
lambda i: isinstance(i, AccountOrRoom) and i.type is Account,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-05-11 04:26:26 +10:00
|
|
|
class MatchingAccounts(ModelFilter):
|
|
|
|
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):
|
|
|
|
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")
|