moment/src/backend/models/special_models.py

40 lines
1.1 KiB
Python
Raw Normal View History

2020-05-06 14:26:52 +10:00
# SPDX-License-Identifier: LGPL-3.0-or-later
2020-05-11 04:26:26 +10:00
from .filters import FieldSubstringFilter, ModelFilter
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",))
def accept_source(self, source: Model) -> bool:
return (
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
class MatchingAccounts(ModelFilter):
def __init__(self, all_rooms: AllRooms) -> None:
super().__init__(sync_id="matching_accounts")
self.all_rooms = all_rooms
self.all_rooms.items_changed_callbacks.append(self.refilter)
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(
(r for r in self.all_rooms.values() if r.for_account == item.id),
False,
)