mirror of
https://github.com/element-hq/synapse.git
synced 2024-12-14 11:57:44 +00:00
remove dead filter_events_for_clients
This is only used by filter_events_for_client, so we can simplify the whole thing by just doing one user at a time, and removing a dead storage function to boot.
This commit is contained in:
parent
187a546bff
commit
bd348f0af6
3 changed files with 35 additions and 92 deletions
|
@ -20,7 +20,7 @@ from synapse.storage._base import SQLBaseStore
|
||||||
from synapse.storage.util.id_generators import StreamIdGenerator
|
from synapse.storage.util.id_generators import StreamIdGenerator
|
||||||
|
|
||||||
from synapse.util.caches.stream_change_cache import StreamChangeCache
|
from synapse.util.caches.stream_change_cache import StreamChangeCache
|
||||||
from synapse.util.caches.descriptors import cached, cachedList, cachedInlineCallbacks
|
from synapse.util.caches.descriptors import cached, cachedInlineCallbacks
|
||||||
|
|
||||||
import abc
|
import abc
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
|
@ -114,25 +114,6 @@ class AccountDataWorkerStore(SQLBaseStore):
|
||||||
else:
|
else:
|
||||||
defer.returnValue(None)
|
defer.returnValue(None)
|
||||||
|
|
||||||
@cachedList(cached_method_name="get_global_account_data_by_type_for_user",
|
|
||||||
num_args=2, list_name="user_ids", inlineCallbacks=True)
|
|
||||||
def get_global_account_data_by_type_for_users(self, data_type, user_ids):
|
|
||||||
rows = yield self._simple_select_many_batch(
|
|
||||||
table="account_data",
|
|
||||||
column="user_id",
|
|
||||||
iterable=user_ids,
|
|
||||||
keyvalues={
|
|
||||||
"account_data_type": data_type,
|
|
||||||
},
|
|
||||||
retcols=("user_id", "content",),
|
|
||||||
desc="get_global_account_data_by_type_for_users",
|
|
||||||
)
|
|
||||||
|
|
||||||
defer.returnValue({
|
|
||||||
row["user_id"]: json.loads(row["content"]) if row["content"] else None
|
|
||||||
for row in rows
|
|
||||||
})
|
|
||||||
|
|
||||||
@cached(num_args=2)
|
@cached(num_args=2)
|
||||||
def get_account_data_for_room(self, user_id, room_id):
|
def get_account_data_for_room(self, user_id, room_id):
|
||||||
"""Get all the client account_data for a user for a room.
|
"""Get all the client account_data for a user for a room.
|
||||||
|
|
|
@ -13,14 +13,14 @@
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
from twisted.internet import defer
|
from twisted.internet import defer
|
||||||
|
|
||||||
from synapse.api.constants import Membership, EventTypes
|
from synapse.api.constants import Membership, EventTypes
|
||||||
|
|
||||||
from synapse.util.logcontext import make_deferred_yieldable, preserve_fn
|
from synapse.util.logcontext import make_deferred_yieldable, preserve_fn
|
||||||
|
|
||||||
import logging
|
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -43,21 +43,35 @@ MEMBERSHIP_PRIORITY = (
|
||||||
|
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def filter_events_for_clients(store, user_tuples, events, event_id_to_state,
|
def filter_events_for_client(store, user_id, events, is_peeking=False,
|
||||||
always_include_ids=frozenset()):
|
always_include_ids=frozenset()):
|
||||||
""" Returns dict of user_id -> list of events that user is allowed to
|
"""
|
||||||
see.
|
Check which events a user is allowed to see
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
user_tuples (str, bool): (user id, is_peeking) for each user to be
|
store (synapse.storage.DataStore): our datastore (can also be a worker
|
||||||
checked. is_peeking should be true if:
|
store)
|
||||||
* the user is not currently a member of the room, and:
|
user_id(str): user id to be checked
|
||||||
* the user has not been a member of the room since the
|
events(list[synapse.events.EventBase]): sequence of events to be checked
|
||||||
given events
|
is_peeking(bool): should be True if:
|
||||||
events ([synapse.events.EventBase]): list of events to filter
|
* the user is not currently a member of the room, and:
|
||||||
|
* the user has not been a member of the room since the given
|
||||||
|
events
|
||||||
always_include_ids (set(event_id)): set of event ids to specifically
|
always_include_ids (set(event_id)): set of event ids to specifically
|
||||||
include (unless sender is ignored)
|
include (unless sender is ignored)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Deferred[list[synapse.events.EventBase]]
|
||||||
"""
|
"""
|
||||||
|
types = (
|
||||||
|
(EventTypes.RoomHistoryVisibility, ""),
|
||||||
|
(EventTypes.Member, user_id),
|
||||||
|
)
|
||||||
|
event_id_to_state = yield store.get_state_for_events(
|
||||||
|
frozenset(e.event_id for e in events),
|
||||||
|
types=types,
|
||||||
|
)
|
||||||
|
|
||||||
forgotten = yield make_deferred_yieldable(defer.gatherResults([
|
forgotten = yield make_deferred_yieldable(defer.gatherResults([
|
||||||
defer.maybeDeferred(
|
defer.maybeDeferred(
|
||||||
preserve_fn(store.who_forgot_in_room),
|
preserve_fn(store.who_forgot_in_room),
|
||||||
|
@ -71,25 +85,20 @@ def filter_events_for_clients(store, user_tuples, events, event_id_to_state,
|
||||||
row["event_id"] for rows in forgotten for row in rows
|
row["event_id"] for rows in forgotten for row in rows
|
||||||
)
|
)
|
||||||
|
|
||||||
ignore_dict_content = yield store.get_global_account_data_by_type_for_users(
|
ignore_dict_content = yield store.get_global_account_data_by_type_for_user(
|
||||||
"m.ignored_user_list", user_ids=[user_id for user_id, _ in user_tuples]
|
"m.ignored_user_list", user_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
# FIXME: This will explode if people upload something incorrect.
|
# FIXME: This will explode if people upload something incorrect.
|
||||||
ignore_dict = {
|
ignore_list = frozenset(
|
||||||
user_id: frozenset(
|
ignore_dict_content.get("ignored_users", {}).keys()
|
||||||
content.get("ignored_users", {}).keys() if content else []
|
if ignore_dict_content else []
|
||||||
)
|
)
|
||||||
for user_id, content in ignore_dict_content.items()
|
|
||||||
}
|
|
||||||
|
|
||||||
def allowed(event, user_id, is_peeking, ignore_list):
|
def allowed(event):
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
event (synapse.events.EventBase): event to check
|
event (synapse.events.EventBase): event to check
|
||||||
user_id (str)
|
|
||||||
is_peeking (bool)
|
|
||||||
ignore_list (list): list of users to ignore
|
|
||||||
"""
|
"""
|
||||||
if not event.is_state() and event.sender in ignore_list:
|
if not event.is_state() and event.sender in ignore_list:
|
||||||
return False
|
return False
|
||||||
|
@ -184,43 +193,4 @@ def filter_events_for_clients(store, user_tuples, events, event_id_to_state,
|
||||||
# we don't know when they left.
|
# we don't know when they left.
|
||||||
return not is_peeking
|
return not is_peeking
|
||||||
|
|
||||||
defer.returnValue({
|
defer.returnValue(list(filter(allowed, events)))
|
||||||
user_id: [
|
|
||||||
event
|
|
||||||
for event in events
|
|
||||||
if allowed(event, user_id, is_peeking, ignore_dict.get(user_id, []))
|
|
||||||
]
|
|
||||||
for user_id, is_peeking in user_tuples
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
|
||||||
def filter_events_for_client(store, user_id, events, is_peeking=False,
|
|
||||||
always_include_ids=frozenset()):
|
|
||||||
"""
|
|
||||||
Check which events a user is allowed to see
|
|
||||||
|
|
||||||
Args:
|
|
||||||
user_id(str): user id to be checked
|
|
||||||
events([synapse.events.EventBase]): list of events to be checked
|
|
||||||
is_peeking(bool): should be True if:
|
|
||||||
* the user is not currently a member of the room, and:
|
|
||||||
* the user has not been a member of the room since the given
|
|
||||||
events
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
[synapse.events.EventBase]
|
|
||||||
"""
|
|
||||||
types = (
|
|
||||||
(EventTypes.RoomHistoryVisibility, ""),
|
|
||||||
(EventTypes.Member, user_id),
|
|
||||||
)
|
|
||||||
event_id_to_state = yield store.get_state_for_events(
|
|
||||||
frozenset(e.event_id for e in events),
|
|
||||||
types=types
|
|
||||||
)
|
|
||||||
res = yield filter_events_for_clients(
|
|
||||||
store, [(user_id, is_peeking)], events, event_id_to_state,
|
|
||||||
always_include_ids=always_include_ids,
|
|
||||||
)
|
|
||||||
defer.returnValue(res.get(user_id, []))
|
|
||||||
|
|
|
@ -37,10 +37,6 @@ class SlavedAccountDataStoreTestCase(BaseSlavedStoreTestCase):
|
||||||
"get_global_account_data_by_type_for_user",
|
"get_global_account_data_by_type_for_user",
|
||||||
[TYPE, USER_ID], {"a": 1}
|
[TYPE, USER_ID], {"a": 1}
|
||||||
)
|
)
|
||||||
yield self.check(
|
|
||||||
"get_global_account_data_by_type_for_users",
|
|
||||||
[TYPE, [USER_ID]], {USER_ID: {"a": 1}}
|
|
||||||
)
|
|
||||||
|
|
||||||
yield self.master_store.add_account_data_for_user(
|
yield self.master_store.add_account_data_for_user(
|
||||||
USER_ID, TYPE, {"a": 2}
|
USER_ID, TYPE, {"a": 2}
|
||||||
|
@ -50,7 +46,3 @@ class SlavedAccountDataStoreTestCase(BaseSlavedStoreTestCase):
|
||||||
"get_global_account_data_by_type_for_user",
|
"get_global_account_data_by_type_for_user",
|
||||||
[TYPE, USER_ID], {"a": 2}
|
[TYPE, USER_ID], {"a": 2}
|
||||||
)
|
)
|
||||||
yield self.check(
|
|
||||||
"get_global_account_data_by_type_for_users",
|
|
||||||
[TYPE, [USER_ID]], {USER_ID: {"a": 2}}
|
|
||||||
)
|
|
||||||
|
|
Loading…
Reference in a new issue