Python filtered models for room members
This commit is contained in:
@@ -2,10 +2,11 @@
|
||||
|
||||
from collections import UserDict
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Dict
|
||||
from typing import Dict, Type
|
||||
|
||||
from . import SyncId
|
||||
from .model import Model
|
||||
from .special_models import FilteredMembers
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -23,7 +24,16 @@ class ModelStore(UserDict):
|
||||
def __missing__(self, key: SyncId) -> Model:
|
||||
"""When accessing a non-existent model, create and return it."""
|
||||
|
||||
model = Model(sync_id=key)
|
||||
is_tuple = isinstance(key, tuple)
|
||||
|
||||
model: Model
|
||||
|
||||
if is_tuple and len(key) == 3 and key[2] == "filtered_members":
|
||||
model = FilteredMembers(user_id=key[0], room_id=key[1])
|
||||
else:
|
||||
model = Model(sync_id=key)
|
||||
print( key, model)
|
||||
|
||||
self.data[key] = model
|
||||
return model
|
||||
|
||||
@@ -35,3 +45,10 @@ class ModelStore(UserDict):
|
||||
type(self).__name__,
|
||||
"\n ".join(sorted(str(v) for v in self.values())),
|
||||
)
|
||||
|
||||
|
||||
async def ensure_exists_from_qml(self, sync_id: SyncId) -> None:
|
||||
if isinstance(sync_id, list): # QML can't pass tuples
|
||||
sync_id = tuple(sync_id)
|
||||
|
||||
self[sync_id] # will call __missing__ if needed
|
||||
|
@@ -37,3 +37,16 @@ class MatchingAccounts(ModelFilter):
|
||||
(r for r in self.all_rooms.values() if r.for_account == item.id),
|
||||
False,
|
||||
)
|
||||
|
||||
|
||||
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")
|
||||
|
Reference in New Issue
Block a user